blob: f92d62f211fe87e127df3796216e0230d4eedecc [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_Troy5bf43922012-09-07 16:07:55 -040052}
Dees_Troy51a0e822012-09-05 15:24:24 -040053
bigbiff bigbiff9c754052013-01-09 09:09:08 -050054using namespace std;
55
Dees_Troy51a0e822012-09-05 15:24:24 -040056TWPartition::TWPartition(void) {
57 Can_Be_Mounted = false;
58 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050059 Can_Be_Backed_Up = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040060 Wipe_During_Factory_Reset = false;
61 Wipe_Available_in_GUI = false;
62 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040063 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040064 SubPartition_Of = "";
65 Symlink_Path = "";
66 Symlink_Mount_Point = "";
67 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040068 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040069 Actual_Block_Device = "";
70 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040071 Alternate_Block_Device = "";
72 Removable = false;
73 Is_Present = false;
74 Length = 0;
75 Size = 0;
76 Used = 0;
77 Free = 0;
78 Backup_Size = 0;
79 Can_Be_Encrypted = false;
80 Is_Encrypted = false;
81 Is_Decrypted = false;
82 Decrypted_Block_Device = "";
83 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -050084 Backup_Display_Name = "";
85 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040086 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040087 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040088 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040089 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +000090 Can_Encrypt_Backup = false;
91 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040092 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -040093 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040094 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050095 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040096 Storage_Path = "";
97 Current_File_System = "";
98 Fstab_File_System = "";
99 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000100 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000101 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000102#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
103 EcryptFS_Password = "";
104#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400105}
106
107TWPartition::~TWPartition(void) {
108 // Do nothing
109}
110
Dees_Troy5bf43922012-09-07 16:07:55 -0400111bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
112 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
113 int line_len = Line.size(), index = 0, item_index = 0;
114 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400115 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400116 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500117 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400118
Dees_Troy51127312012-09-08 13:08:49 -0400119 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500120 if (full_line[index] == 34)
121 skip = !skip;
122 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400123 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400124 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400125 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000126 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400127 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500128 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000129 Display_Name = full_line + 1;
130 Backup_Display_Name = Display_Name;
131 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400132 index = Mount_Point.size();
133 while (index < line_len) {
134 while (index < line_len && full_line[index] == '\0')
135 index++;
136 if (index >= line_len)
137 continue;
138 ptr = full_line + index;
139 if (item_index == 0) {
140 // File System
141 Fstab_File_System = ptr;
142 Current_File_System = ptr;
143 item_index++;
144 } else if (item_index == 1) {
145 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400146 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400147 MTD_Name = ptr;
148 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400149 } else if (Fstab_File_System == "bml") {
150 if (Mount_Point == "/boot")
151 MTD_Name = "boot";
152 else if (Mount_Point == "/recovery")
153 MTD_Name = "recovery";
154 Primary_Block_Device = ptr;
155 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000156 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 -0400157 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400158 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000159 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400160 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000161 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400162 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400163 } else {
164 Primary_Block_Device = ptr;
165 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400166 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400167 item_index++;
168 } else if (item_index > 1) {
169 if (*ptr == '/') {
170 // Alternate Block Device
171 Alternate_Block_Device = ptr;
172 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
173 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
174 // Partition length
175 ptr += 7;
176 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400177 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
178 // Custom flags, save for later so that new values aren't overwritten by defaults
179 ptr += 6;
180 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000181 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400182 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
183 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400184 } else {
185 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000186 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400187 }
188 }
189 while (index < line_len && full_line[index] != '\0')
190 index++;
191 }
192
193 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
194 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000195 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400196 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000197 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400198 return 0;
199 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400200 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400201 Setup_File_System(Display_Error);
202 if (Mount_Point == "/system") {
203 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500204 Backup_Display_Name = Display_Name;
205 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400206 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500207 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400208 } else if (Mount_Point == "/data") {
209 Display_Name = "Data";
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_Troy51127312012-09-08 13:08:49 -0400213 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500214 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000215 Can_Encrypt_Backup = true;
216 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400217#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troya13d74f2013-03-24 08:54:55 -0500218 Storage_Name = "Internal Storage";
Dees_Troy5bf43922012-09-07 16:07:55 -0400219 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400220 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500221 Is_Settings_Storage = true;
Dees_Troy51127312012-09-08 13:08:49 -0400222 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000223 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400224 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
225 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400226 Symlink_Mount_Point = "/emmc";
227 } else {
228 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400229 Symlink_Mount_Point = "/sdcard";
230 }
Dees_Troy16b74352012-11-14 22:27:31 +0000231 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
232 Storage_Path = "/data/media/0";
233 Symlink_Path = Storage_Path;
234 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
235 UnMount(true);
236 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400237#endif
238#ifdef TW_INCLUDE_CRYPTO
239 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400240 char crypto_blkdev[255];
241 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
242 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400243 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400244 DataManager::SetValue(TW_IS_DECRYPTED, 1);
245 Is_Encrypted = true;
246 Is_Decrypted = true;
247 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000248 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400249 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400250 Is_Encrypted = true;
251 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800252 Can_Be_Mounted = false;
253 Current_File_System = "emmc";
254 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400255 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
256 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
257 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800258 } else {
259 // Filesystem is not encrypted and the mount
260 // succeeded, so get it back to the original
261 // unmounted state
262 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400263 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400264 #ifdef RECOVERY_SDCARD_ON_DATA
265 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
266 Recreate_Media_Folder();
267 #endif
268#else
269 #ifdef RECOVERY_SDCARD_ON_DATA
270 Recreate_Media_Folder();
271 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400272#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400273 } else if (Mount_Point == "/cache") {
274 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500275 Backup_Display_Name = Display_Name;
276 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400277 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400278 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500279 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400280 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000281 LOGINFO("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500282 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
283 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400284 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400285 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400286 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400287 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500288 Backup_Display_Name = Display_Name;
289 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400290 Is_SubPartition = true;
291 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400292 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500293 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000294 Can_Encrypt_Backup = true;
295 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400296 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400297 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400298 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500299 Backup_Display_Name = Display_Name;
300 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400301 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400302 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500303 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000304 Can_Encrypt_Backup = true;
305 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400306 } else if (Mount_Point == "/boot") {
307 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500308 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400309 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500310 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400311 }
312#ifdef TW_EXTERNAL_STORAGE_PATH
313 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
314 Is_Storage = true;
315 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400316 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500317 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400318#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000319 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400320 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400321 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500322 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400323#ifndef RECOVERY_SDCARD_ON_DATA
324 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400325 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400326#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400327#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000328 }
Dees_Troy8170a922012-09-18 15:40:25 -0400329#ifdef TW_INTERNAL_STORAGE_PATH
330 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
331 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500332 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400333 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500334 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400335#ifndef RECOVERY_SDCARD_ON_DATA
336 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400337 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400338#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400339 }
340#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000341 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400342 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500343 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500344 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400345#ifndef RECOVERY_SDCARD_ON_DATA
346 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400347 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400348#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400349 }
350#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400351 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400352 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400353 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500354 if (Mount_Point == "/boot") {
355 Display_Name = "Boot";
356 Backup_Display_Name = Display_Name;
357 Can_Be_Backed_Up = true;
358 } else if (Mount_Point == "/recovery") {
359 Display_Name = "Recovery";
360 Backup_Display_Name = Display_Name;
361 Can_Be_Backed_Up = true;
362 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400363 }
364
Dees_Troy51127312012-09-08 13:08:49 -0400365 // Process any custom flags
366 if (Flags.size() > 0)
367 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400368 return true;
369}
370
371bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
372 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500373 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400374 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500375 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400376
377 strcpy(flags, Flags.c_str());
378 flags_len = Flags.size();
379 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500380 if (flags[index] == 34)
381 skip = !skip;
382 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400383 flags[index] = '\0';
384 }
385
386 index = 0;
387 while (index < flags_len) {
388 while (index < flags_len && flags[index] == '\0')
389 index++;
390 if (index >= flags_len)
391 continue;
392 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500393 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400394 if (strcmp(ptr, "removable") == 0) {
395 Removable = true;
396 } else if (strcmp(ptr, "storage") == 0) {
397 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500398 } else if (strcmp(ptr, "settingsstorage") == 0) {
399 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400400 } else if (strcmp(ptr, "canbewiped") == 0) {
401 Can_Be_Wiped = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500402 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
403 ptr += 7;
404 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
405 Can_Be_Backed_Up = true;
406 else
407 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400408 } else if (strcmp(ptr, "wipeingui") == 0) {
409 Can_Be_Wiped = true;
410 Wipe_Available_in_GUI = true;
411 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
412 Can_Be_Wiped = true;
413 Wipe_Available_in_GUI = true;
414 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500415 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000416 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400417 Is_SubPartition = true;
418 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000419 } else if (strcmp(ptr, "ignoreblkid") == 0) {
420 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000421 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
422 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500423 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400424 ptr += 8;
425 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500426 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
427 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400428 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500429 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400430 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500431 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
432 Display_Name.resize(Display_Name.size() - 1);
433 }
434 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
435 has_storage_name = true;
436 ptr += 11;
437 if (*ptr == '\"') ptr++;
438 Storage_Name = ptr;
439 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
440 Storage_Name.resize(Storage_Name.size() - 1);
441 }
442 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
443 has_backup_name = true;
444 ptr += 10;
445 if (*ptr == '\"') ptr++;
446 Backup_Display_Name = ptr;
447 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
448 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
449 }
450 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400451 ptr += 10;
452 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500453 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400454 ptr += 7;
455 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000456 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
457 ptr += 17;
458 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
459 Can_Encrypt_Backup = true;
460 else
461 Can_Encrypt_Backup = false;
462 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
463 ptr += 21;
464 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
465 Can_Encrypt_Backup = true;
466 Use_Userdata_Encryption = true;
467 } else {
468 Use_Userdata_Encryption = false;
469 }
Dees_Troy51127312012-09-08 13:08:49 -0400470 } else {
471 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000472 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400473 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000474 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400475 }
476 while (index < flags_len && flags[index] != '\0')
477 index++;
478 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500479 if (has_display_name && !has_storage_name)
480 Storage_Name = Display_Name;
481 if (!has_display_name && has_storage_name)
482 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000483 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500484 Backup_Display_Name = Display_Name;
485 if (!has_display_name && has_backup_name)
486 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400487 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400488}
489
Dees_Troy5bf43922012-09-07 16:07:55 -0400490bool TWPartition::Is_File_System(string File_System) {
491 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400492 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400493 File_System == "ext4" ||
494 File_System == "vfat" ||
495 File_System == "ntfs" ||
496 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500497 File_System == "exfat" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400498 File_System == "auto")
499 return true;
500 else
501 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400502}
503
Dees_Troy5bf43922012-09-07 16:07:55 -0400504bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400505 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400506 return true;
507 else
508 return false;
509}
510
Dees_Troy51127312012-09-08 13:08:49 -0400511bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400512 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400513 if (mkdir(Path.c_str(), 0777) == -1) {
514 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000515 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400516 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000517 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400518 return false;
519 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000520 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400521 return true;
522 }
523 }
524 return true;
525}
526
Dees_Troy5bf43922012-09-07 16:07:55 -0400527void TWPartition::Setup_File_System(bool Display_Error) {
528 struct statfs st;
529
530 Can_Be_Mounted = true;
531 Can_Be_Wiped = true;
532
Dees_Troy5bf43922012-09-07 16:07:55 -0400533 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400534 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400535 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
536 Backup_Name = Display_Name;
537 Backup_Method = FILES;
538}
539
540void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400541 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
542 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800543 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400544 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800545 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400546 Backup_Method = FLASH_UTILS;
547 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000548 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 -0400549 if (Find_Partition_Size()) {
550 Used = Size;
551 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400552 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400553 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000554 LOGERR("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400555 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000556 LOGINFO("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400557 }
558}
559
Dees_Troye58d5262012-09-21 12:27:57 -0400560void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500561 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400562 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500563 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400564 Has_Android_Secure = true;
565 Symlink_Path = Mount_Point + "/.android_secure";
566 Symlink_Mount_Point = "/and-sec";
567 Backup_Path = Symlink_Mount_Point;
568 Make_Dir("/and-sec", true);
569 Recreate_AndSec_Folder();
570}
571
Dees_Troy5bf43922012-09-07 16:07:55 -0400572void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
573 char device[512], realDevice[512];
574
575 strcpy(device, Block.c_str());
576 memset(realDevice, 0, sizeof(realDevice));
577 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
578 {
579 strcpy(device, realDevice);
580 memset(realDevice, 0, sizeof(realDevice));
581 }
582
583 if (device[0] != '/') {
584 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000585 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400586 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000587 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400588 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400589 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400590 Block = device;
591 return;
592 }
593}
594
Dees_Troy8e337f32012-10-13 22:07:49 -0400595void TWPartition::Mount_Storage_Retry(void) {
596 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500597 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400598 int retry_count = 5;
599 while (retry_count > 0 && !Mount(false)) {
600 usleep(500000);
601 retry_count--;
602 }
603 Mount(true);
604 }
605}
606
Dees_Troy38bd7602012-09-14 13:33:53 -0400607bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
608 FILE *fp = NULL;
609 char line[255];
610
611 fp = fopen("/proc/mtd", "rt");
612 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000613 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400614 return false;
615 }
616
617 while (fgets(line, sizeof(line), fp) != NULL)
618 {
619 char device[32], label[32];
620 unsigned long size = 0;
621 char* fstype = NULL;
622 int deviceId;
623
624 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
625
626 // Skip header and blank lines
627 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
628 continue;
629
630 // Strip off the trailing " from the label
631 label[strlen(label)-1] = '\0';
632
633 if (strcmp(label, MTD_Name.c_str()) == 0) {
634 // We found our device
635 // Strip off the trailing : from the device
636 device[strlen(device)-1] = '\0';
637 if (sscanf(device,"mtd%d", &deviceId) == 1) {
638 sprintf(device, "/dev/block/mtdblock%d", deviceId);
639 Primary_Block_Device = device;
640 }
641 }
642 }
643 fclose(fp);
644
645 return false;
646}
647
Dees_Troy51127312012-09-08 13:08:49 -0400648bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
649 struct statfs st;
650 string Local_Path = Mount_Point + "/.";
651
652 if (!Mount(Display_Error))
653 return false;
654
655 if (statfs(Local_Path.c_str(), &st) != 0) {
656 if (!Removable) {
657 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000658 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400659 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000660 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400661 }
662 return false;
663 }
664 Size = (st.f_blocks * st.f_bsize);
665 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
666 Free = (st.f_bfree * st.f_bsize);
667 Backup_Size = Used;
668 return true;
669}
670
671bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400672 FILE* fp;
673 char command[255], line[512];
674 int include_block = 1;
675 unsigned int min_len;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500676 string result;
Dees_Troy5bf43922012-09-07 16:07:55 -0400677
678 if (!Mount(Display_Error))
679 return false;
680
Dees_Troy38bd7602012-09-14 13:33:53 -0400681 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400682 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500683 TWFunc::Exec_Cmd(command, result);
Dees_Troy51127312012-09-08 13:08:49 -0400684 fp = fopen("/tmp/dfoutput.txt", "rt");
685 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000686 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400687 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400688 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400689
690 while (fgets(line, sizeof(line), fp) != NULL)
691 {
692 unsigned long blocks, used, available;
693 char device[64];
694 char tmpString[64];
695
696 if (strncmp(line, "Filesystem", 10) == 0)
697 continue;
698 if (strlen(line) < min_len) {
699 include_block = 0;
700 continue;
701 }
702 if (include_block) {
703 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
704 } else {
705 // The device block string is so long that the df information is on the next line
706 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400707 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400708 while (tmpString[space_count] == 32)
709 space_count++;
710 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
711 }
712
713 // Adjust block size to byte size
714 Size = blocks * 1024ULL;
715 Used = used * 1024ULL;
716 Free = available * 1024ULL;
717 Backup_Size = Used;
718 }
719 fclose(fp);
720 return true;
721}
722
Dees_Troy5bf43922012-09-07 16:07:55 -0400723bool TWPartition::Find_Partition_Size(void) {
724 FILE* fp;
725 char line[512];
726 string tmpdevice;
727
igoriok87e3d932013-01-31 21:03:53 +0200728 fp = fopen("/proc/dumchar_info", "rt");
729 if (fp != NULL) {
730 while (fgets(line, sizeof(line), fp) != NULL)
731 {
732 char label[32], device[32];
733 unsigned long size = 0;
734
735 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
736
737 // Skip header, annotation and blank lines
738 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
739 continue;
740
741 tmpdevice = "/dev/";
742 tmpdevice += label;
743 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
744 Size = size;
745 fclose(fp);
746 return true;
747 }
748 }
749 }
750
Dees_Troy5bf43922012-09-07 16:07:55 -0400751 // In this case, we'll first get the partitions we care about (with labels)
752 fp = fopen("/proc/partitions", "rt");
753 if (fp == NULL)
754 return false;
755
756 while (fgets(line, sizeof(line), fp) != NULL)
757 {
758 unsigned long major, minor, blocks;
759 char device[512];
760 char tmpString[64];
761
Dees_Troy63c8df72012-09-10 14:02:05 -0400762 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400763 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
764
765 tmpdevice = "/dev/block/";
766 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400767 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400768 // Adjust block size to byte size
769 Size = blocks * 1024ULL;
770 fclose(fp);
771 return true;
772 }
773 }
774 fclose(fp);
775 return false;
776}
777
Dees_Troy5bf43922012-09-07 16:07:55 -0400778bool TWPartition::Is_Mounted(void) {
779 if (!Can_Be_Mounted)
780 return false;
781
782 struct stat st1, st2;
783 string test_path;
784
785 // Check to see if the mount point directory exists
786 test_path = Mount_Point + "/.";
787 if (stat(test_path.c_str(), &st1) != 0) return false;
788
789 // Check to see if the directory above the mount point exists
790 test_path = Mount_Point + "/../.";
791 if (stat(test_path.c_str(), &st2) != 0) return false;
792
793 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
794 int ret = (st1.st_dev != st2.st_dev) ? true : false;
795
796 return ret;
797}
798
799bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000800 int exfat_mounted = 0;
801
Dees_Troy5bf43922012-09-07 16:07:55 -0400802 if (Is_Mounted()) {
803 return true;
804 } else if (!Can_Be_Mounted) {
805 return false;
806 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400807
808 Find_Actual_Block_Device();
809
810 // Check the current file system before mounting
811 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000812 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000813 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 +0000814 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000815 string result;
816 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000817 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000818 Current_File_System = "vfat";
819 } else {
820#ifdef TW_NO_EXFAT_FUSE
821 UnMount(false);
822 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
823 // Some kernels let us mount vfat as exfat which doesn't work out too well
824#else
825 exfat_mounted = 1;
826#endif
827 }
828 }
Dees_Troy22042032012-12-18 21:23:08 +0000829 if (Fstab_File_System == "yaffs2") {
830 // mount an MTD partition as a YAFFS2 filesystem.
831 mtd_scan_partitions();
832 const MtdPartition* partition;
833 partition = mtd_find_partition_by_name(MTD_Name.c_str());
834 if (partition == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000835 LOGERR("Failed to find '%s' partition to mount at '%s'\n",
Dees_Troy22042032012-12-18 21:23:08 +0000836 MTD_Name.c_str(), Mount_Point.c_str());
837 return false;
838 }
839 if (mtd_mount_partition(partition, Mount_Point.c_str(), Fstab_File_System.c_str(), 0)) {
840 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000841 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
Dees_Troy22042032012-12-18 21:23:08 +0000842 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000843 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
Dees_Troy22042032012-12-18 21:23:08 +0000844 return false;
845 } else
846 return true;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000847 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
848#ifdef TW_NO_EXFAT_FUSE
849 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000850 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000851 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000852 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000853 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000854 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000855 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
856 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 +0000857 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000858 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000859 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000860#endif
861 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000862 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000863 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000864 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
865 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 +0000866 return false;
867#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000868 }
869#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000870 }
871#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
872 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
873 MetaEcfsFile += "/.MetaEcfsFile";
874 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
875 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
876 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000877 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000878 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000879 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000880 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000881 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000882 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400883 }
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000884 } else {
885 Is_Decrypted = false;
886 }
887#endif
888 if (Removable)
889 Update_Size(Display_Error);
890
891 if (!Symlink_Mount_Point.empty()) {
892 string Command, Result;
893 Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
894 TWFunc::Exec_Cmd(Command, Result);
Dees_Troy5bf43922012-09-07 16:07:55 -0400895 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400896 return true;
897}
898
899bool TWPartition::UnMount(bool Display_Error) {
900 if (Is_Mounted()) {
901 int never_unmount_system;
902
903 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
904 if (never_unmount_system == 1 && Mount_Point == "/system")
905 return true; // Never unmount system if you're not supposed to unmount it
906
Dees_Troyc8bafa12013-01-10 15:43:00 +0000907#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
908 if (EcryptFS_Password.size() > 0) {
909 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
910 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000911 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000912 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000913 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000914 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000915 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000916 }
917 }
918#endif
919
Dees_Troy38bd7602012-09-14 13:33:53 -0400920 if (!Symlink_Mount_Point.empty())
921 umount(Symlink_Mount_Point.c_str());
922
Dees_Troyb05ddee2013-01-28 20:24:50 +0000923 umount(Mount_Point.c_str());
924 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400925 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000926 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400927 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000928 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400929 return false;
930 } else
931 return true;
932 } else {
933 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400934 }
935}
936
Gary Peck43acadf2012-11-21 21:19:01 -0800937bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +0000938 bool wiped = false, update_crypt = false;
939 int check;
940 string Layout_Filename = Mount_Point + "/.layout_version";
941
Dees_Troy38bd7602012-09-14 13:33:53 -0400942 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000943 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400944 return false;
945 }
946
Dees_Troyc51f1f92012-09-20 15:32:13 -0400947 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +0000948 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400949
Dees_Troyce675462013-01-09 19:48:21 +0000950#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
951 if (Mount_Point == "/data" && Mount(false)) {
952 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
953 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
954 }
955#endif
956
Dees_Troy16c2b312013-01-15 16:51:18 +0000957 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
958 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
959 else
960 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -0400961
Dees_Troy16c2b312013-01-15 16:51:18 +0000962 if (Has_Data_Media) {
963 wiped = Wipe_Data_Without_Wiping_Media();
964 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -0800965
Dees_Troy16c2b312013-01-15 16:51:18 +0000966 DataManager::GetValue(TW_RM_RF_VAR, check);
967
968 if (check)
969 wiped = Wipe_RMRF();
970 else if (New_File_System == "ext4")
971 wiped = Wipe_EXT4();
972 else if (New_File_System == "ext2" || New_File_System == "ext3")
973 wiped = Wipe_EXT23(New_File_System);
974 else if (New_File_System == "vfat")
975 wiped = Wipe_FAT();
976 else if (New_File_System == "exfat")
977 wiped = Wipe_EXFAT();
978 else if (New_File_System == "yaffs2")
979 wiped = Wipe_MTD();
980 else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000981 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 +0000982 unlink("/.layout_version");
983 return false;
984 }
985 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800986 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400987
Gary Pecke8bc5d72012-12-21 06:45:25 -0800988 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +0000989#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
990 if (Mount_Point == "/data" && Mount(false)) {
991 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
992 Make_Dir("/data/system", true);
993 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
994 }
995 }
996#endif
Dees_Troy16c2b312013-01-15 16:51:18 +0000997
Dees_Troy1c1ac442013-01-17 21:42:14 +0000998 if (Mount_Point == "/cache")
999 DataManager::Output_Version();
1000
Dees_Troy16c2b312013-01-15 16:51:18 +00001001 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1002 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1003
1004 if (update_crypt) {
1005 Setup_File_System(false);
1006 if (Is_Encrypted && !Is_Decrypted) {
1007 // just wiped an encrypted partition back to its unencrypted state
1008 Is_Encrypted = false;
1009 Is_Decrypted = false;
1010 Decrypted_Block_Device = "";
1011 if (Mount_Point == "/data") {
1012 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1013 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1014 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001015 }
1016 }
1017 }
1018 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001019}
1020
Gary Peck43acadf2012-11-21 21:19:01 -08001021bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001022 if (Is_File_System(Current_File_System))
1023 return Wipe(Current_File_System);
1024 else
1025 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001026}
1027
Dees_Troye58d5262012-09-21 12:27:57 -04001028bool TWPartition::Wipe_AndSec(void) {
1029 if (!Has_Android_Secure)
1030 return false;
1031
Dees_Troye58d5262012-09-21 12:27:57 -04001032 if (!Mount(true))
1033 return false;
1034
Dees_Troy2673cec2013-04-02 20:22:16 +00001035 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001036 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Dees_Troye58d5262012-09-21 12:27:57 -04001037 return true;
1038}
1039
Dees_Troy51a0e822012-09-05 15:24:24 -04001040bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001041 if (Backup_Method == FILES)
1042 return Backup_Tar(backup_folder);
1043 else if (Backup_Method == DD)
1044 return Backup_DD(backup_folder);
1045 else if (Backup_Method == FLASH_UTILS)
1046 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001047 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001048 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001049}
1050
Dees_Troy43d8b002012-09-17 16:00:01 -04001051bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001052 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001053 char split_filename[512];
1054 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001055 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001056
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001057 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001058 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001059 if (!TWFunc::Path_Exists(Full_Filename)) {
1060 // This is a split archive, we presume
1061 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001062 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001063 md5file = split_filename;
1064 md5file += ".md5";
1065 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001066 LOGERR("No md5 file found for '%s'.\n", split_filename);
1067 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001068 return false;
1069 }
1070 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001071 while (index < 1000) {
1072 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001073 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001074 return false;
1075 }
1076 index++;
1077 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001078 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001079 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001080 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001081 } else {
1082 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001083 md5file = Full_Filename + ".md5";
1084 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001085 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1086 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001087 return false;
1088 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001089 md5sum.setfn(Full_Filename);
1090 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001091 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001092 return false;
1093 } else
1094 return true;
1095 }
1096 return false;
1097}
1098
Dees_Troy51a0e822012-09-05 15:24:24 -04001099bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001100 size_t first_period, second_period;
1101 string Restore_File_System;
1102
1103 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001104 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001105
1106 // Parse backup filename to extract the file system before wiping
1107 first_period = Backup_FileName.find(".");
1108 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001109 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001110 return false;
1111 }
1112 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1113 second_period = Restore_File_System.find(".");
1114 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001115 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001116 return false;
1117 }
1118 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001119 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001120
1121 if (Is_File_System(Restore_File_System))
1122 return Restore_Tar(restore_folder, Restore_File_System);
1123 else if (Is_Image(Restore_File_System)) {
1124 if (Restore_File_System == "emmc")
1125 return Restore_DD(restore_folder);
1126 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1127 return Restore_Flash_Image(restore_folder);
1128 }
1129
Dees_Troy2673cec2013-04-02 20:22:16 +00001130 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001131 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001132}
1133
1134string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001135 if (Backup_Method == NONE)
1136 return "none";
1137 else if (Backup_Method == FILES)
1138 return "files";
1139 else if (Backup_Method == DD)
1140 return "dd";
1141 else if (Backup_Method == FLASH_UTILS)
1142 return "flash_utils";
1143 else
1144 return "undefined";
1145 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001146}
1147
1148bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001149 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001150 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001151 return 1;
1152}
1153
1154bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001155 bool Save_Data_Media = Has_Data_Media;
1156
1157 if (!UnMount(true))
1158 return false;
1159
Dees_Troy38bd7602012-09-14 13:33:53 -04001160 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001161 Decrypted_Block_Device = "";
1162 Is_Decrypted = false;
1163 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001164 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001165 Has_Data_Media = Save_Data_Media;
1166 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1167 Recreate_Media_Folder();
1168 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001169 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001170 return true;
1171 } else {
1172 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001173 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001174 return false;
1175 }
1176 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001177}
1178
1179void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001180 const char* type;
1181 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001182
Dees_Troy68cab492012-12-12 19:29:35 +00001183 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1184 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001185
Dees_Troy38bd7602012-09-14 13:33:53 -04001186 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001187 if (!Is_Present)
1188 return;
Dees_Troy51127312012-09-08 13:08:49 -04001189
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001190 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1191 if (blkid_do_fullprobe(pr)) {
1192 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001193 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001194 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001195 }
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001196 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1197 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001198 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001199 return;
1200 }
1201 Current_File_System = type;
Dees_Troy51a0e822012-09-05 15:24:24 -04001202 return;
1203}
1204
Gary Peck43acadf2012-11-21 21:19:01 -08001205bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001206 if (!UnMount(true))
1207 return false;
1208
Dees_Troy43d8b002012-09-17 16:00:01 -04001209 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001210 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001211
Dees_Troy2673cec2013-04-02 20:22:16 +00001212 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001213 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001214 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001215 LOGINFO("mke2fs command: %s\n", command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001216 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001217 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001218 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001219 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001220 return true;
1221 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001222 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001223 return false;
1224 }
1225 } else
1226 return Wipe_RMRF();
1227
1228 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001229}
1230
1231bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001232 if (!UnMount(true))
1233 return false;
1234
Dees_Troy43d8b002012-09-17 16:00:01 -04001235 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001236 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001237
Dees_Troy2673cec2013-04-02 20:22:16 +00001238 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001239 Find_Actual_Block_Device();
1240 Command = "make_ext4fs";
1241 if (!Is_Decrypted && Length != 0) {
1242 // Only use length if we're not decrypted
1243 char len[32];
1244 sprintf(len, "%i", Length);
1245 Command += " -l ";
1246 Command += len;
1247 }
1248 Command += " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001249 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001250 if (TWFunc::Exec_Cmd(Command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001251 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001252 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001253 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001254 return true;
1255 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001256 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001257 return false;
1258 }
1259 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001260 return Wipe_EXT23("ext4");
Dees_Troy38bd7602012-09-14 13:33:53 -04001261
1262 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001263}
1264
1265bool TWPartition::Wipe_FAT() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001266 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001267
Dees_Troy43d8b002012-09-17 16:00:01 -04001268 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001269 if (!UnMount(true))
1270 return false;
1271
Dees_Troy2673cec2013-04-02 20:22:16 +00001272 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001273 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001274 command = "mkdosfs " + Actual_Block_Device;
1275 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001276 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001277 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001278 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001279 return true;
1280 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001281 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001282 return false;
1283 }
1284 return true;
1285 }
1286 else
1287 return Wipe_RMRF();
1288
1289 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001290}
1291
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001292bool TWPartition::Wipe_EXFAT() {
1293 string command, result;
1294
1295 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1296 if (!UnMount(true))
1297 return false;
1298
Dees_Troy2673cec2013-04-02 20:22:16 +00001299 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001300 Find_Actual_Block_Device();
1301 command = "mkexfatfs " + Actual_Block_Device;
1302 if (TWFunc::Exec_Cmd(command, result) == 0) {
1303 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001304 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001305 return true;
1306 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001307 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001308 return false;
1309 }
1310 return true;
1311 }
1312 return false;
1313}
1314
Dees_Troy38bd7602012-09-14 13:33:53 -04001315bool TWPartition::Wipe_MTD() {
1316 if (!UnMount(true))
1317 return false;
1318
Dees_Troy2673cec2013-04-02 20:22:16 +00001319 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001320
1321 mtd_scan_partitions();
1322 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1323 if (mtd == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001324 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001325 return false;
1326 }
1327
1328 MtdWriteContext* ctx = mtd_write_partition(mtd);
1329 if (ctx == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001330 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001331 return false;
1332 }
1333 if (mtd_erase_blocks(ctx, -1) == -1) {
1334 mtd_write_close(ctx);
Dees_Troy2673cec2013-04-02 20:22:16 +00001335 LOGERR("Failed to format '%s'", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001336 return false;
1337 }
1338 if (mtd_write_close(ctx) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001339 LOGERR("Failed to close '%s'", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001340 return false;
1341 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001342 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001343 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001344 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001345 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001346}
1347
1348bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001349 if (!Mount(true))
1350 return false;
1351
Dees_Troy2673cec2013-04-02 20:22:16 +00001352 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001353 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001354 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001355 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001356}
1357
1358bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001359 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001360
1361 // This handles wiping data on devices with "sdcard" in /data/media
1362 if (!Mount(true))
1363 return false;
1364
Dees_Troy2673cec2013-04-02 20:22:16 +00001365 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001366
1367 DIR* d;
1368 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001369 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001370 struct dirent* de;
1371 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001372 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1373 // The media folder is the "internal sdcard"
1374 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
1375 // the media folder for multi-user.
1376 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001377
1378 dir = "/data/";
1379 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001380 if (de->d_type == DT_DIR) {
1381 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001382 } 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 +00001383 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001384 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001385 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001386 }
1387 closedir(d);
Dees_Troy2673cec2013-04-02 20:22:16 +00001388 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001389 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001390 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001391 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001392 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001393}
1394
1395bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001396 char back_name[255], split_index[5];
1397 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001398 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001399 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001400 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001401 twrpTar tar;
1402 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001403
Dees_Troy43d8b002012-09-17 16:00:01 -04001404 if (!Mount(true))
1405 return false;
1406
Dees_Troya13d74f2013-03-24 08:54:55 -05001407 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001408 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001409
1410 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001411 tar.use_compression = use_compression;
1412#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1413 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1414 if (use_encryption && Can_Encrypt_Backup) {
1415 tar.use_encryption = use_encryption;
1416 if (Use_Userdata_Encryption)
1417 tar.userdata_encryption = use_encryption;
1418 } else {
1419 use_encryption = false;
1420 }
1421#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001422
1423 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1424 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001425 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001426 tar.has_data_media = Has_Data_Media;
1427 if (!use_encryption && Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001428 // This backup needs to be split into multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001429 gui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001430 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001431 tar.setdir(back_name);
1432 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001433 backup_count = tar.splitArchiveFork();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001434 if (backup_count == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001435 LOGERR("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001436 return false;
1437 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001438 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001439 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001440 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001441 tar.setdir(Backup_Path);
1442 tar.setfn(Full_FileName);
1443 if (tar.createTarFork() != 0)
1444 return false;
1445 if (use_compression && !use_encryption) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001446 string gzname = Full_FileName + ".gz";
1447 rename(gzname.c_str(), Full_FileName.c_str());
1448 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001449 if (use_encryption)
1450 Full_FileName += "000";
Dees_Troy7c2dec82012-09-26 09:49:14 -04001451 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001452 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001453 return false;
1454 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001455 }
1456 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001457}
1458
1459bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001460 char back_name[255], backup_size[32];
1461 string Full_FileName, Command, result, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001462 int use_compression;
1463
igoriok87e3d932013-01-31 21:03:53 +02001464 sprintf(backup_size, "%llu", Backup_Size);
1465 DD_BS = backup_size;
1466
Dees_Troyb46a6842012-09-25 11:06:46 -04001467 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001468 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001469
1470 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1471 Backup_FileName = back_name;
1472
1473 Full_FileName = backup_folder + "/" + Backup_FileName;
1474
igoriok87e3d932013-01-31 21:03:53 +02001475 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001476 LOGINFO("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001477 TWFunc::Exec_Cmd(Command, result);
Dees_Troyc154ac22012-10-12 15:36:47 -04001478 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001479 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001480 return false;
1481 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001482 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001483}
1484
1485bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001486 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001487 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001488 int use_compression;
1489
Dees_Troyb46a6842012-09-25 11:06:46 -04001490 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001491 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001492
1493 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1494 Backup_FileName = back_name;
1495
1496 Full_FileName = backup_folder + "/" + Backup_FileName;
1497
1498 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001499 LOGINFO("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001500 TWFunc::Exec_Cmd(Command, result);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001501 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1502 // 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 +00001503 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001504 return false;
1505 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001506 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001507}
1508
Gary Peck43acadf2012-11-21 21:19:01 -08001509bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1510 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001511 int index = 0;
1512 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001513
Dees_Troye58d5262012-09-21 12:27:57 -04001514 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001515 if (!Wipe_AndSec())
1516 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001517 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001518 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001519 if (!Wipe(Restore_File_System))
1520 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001521 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001522 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001523 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001524
1525 if (!Mount(true))
1526 return false;
1527
Dees_Troy4a2a1262012-09-18 09:33:47 -04001528 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001529 /*if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001530 if (!TWFunc::Path_Exists(Full_FileName)) {
1531 // Backup is multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001532 LOGINFO("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001533 sprintf(split_index, "%03i", index);
1534 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001535 while (TWFunc::Path_Exists(Full_FileName)) {
1536 index++;
Dees_Troy2673cec2013-04-02 20:22:16 +00001537 gui_print("Restoring archive %i...\n", index);
1538 LOGINFO("Restoring '%s'...\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001539 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001540 tar.setdir("/");
1541 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001542 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001543 return false;
1544 sprintf(split_index, "%03i", index);
1545 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1546 }
1547 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001548 LOGERR("Error locating restore file: '%s'\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001549 return false;
1550 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001551 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001552 } else {*/
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001553 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001554 tar.setdir(Backup_Path);
1555 tar.setfn(Full_FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +00001556 tar.backup_name = Backup_Name;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001557 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001558 return false;
Dees_Troy83bd4832013-05-04 12:39:56 +00001559 //}
Dees_Troy43d8b002012-09-17 16:00:01 -04001560 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001561}
1562
1563bool TWPartition::Restore_DD(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001564 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001565
Dees_Troyda8b55a2012-12-12 19:18:30 +00001566 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001567 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001568
1569 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001570 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001571 return false;
1572 }
1573 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1574 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001575 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001576 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1577 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1578 return false;
1579 }
1580
Dees_Troy2673cec2013-04-02 20:22:16 +00001581 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001582 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001583 LOGINFO("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001584 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001585 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001586}
1587
1588bool TWPartition::Restore_Flash_Image(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001589 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001590
Dees_Troy2673cec2013-04-02 20:22:16 +00001591 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001592 Full_FileName = restore_folder + "/" + Backup_FileName;
1593 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1594 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001595 LOGINFO("Erase command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001596 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001597 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001598 LOGINFO("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001599 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001600 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001601}
Dees_Troy5bf43922012-09-07 16:07:55 -04001602
1603bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001604 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001605
Dees_Troyab10ee22012-09-21 14:27:30 -04001606 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001607 return false;
1608
Dees_Troy0550cfb2012-10-13 11:56:13 -04001609 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001610 if (Removable || Is_Encrypted) {
1611 if (!Mount(false))
1612 return true;
1613 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001614 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001615
1616 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001617 if (!ret || Size == 0) {
1618 if (!Get_Size_Via_df(Display_Error)) {
1619 if (!Was_Already_Mounted)
1620 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001621 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001622 }
1623 }
Dees_Troy51127312012-09-08 13:08:49 -04001624
Dees_Troy5bf43922012-09-07 16:07:55 -04001625 if (Has_Data_Media) {
1626 if (Mount(Display_Error)) {