blob: 4405536477b0bb7f7ec0256b63812081fd154554 [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;
90 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -040091 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040092 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050093 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040094 Storage_Path = "";
95 Current_File_System = "";
96 Fstab_File_System = "";
97 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +000098 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +000099 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000100#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
101 EcryptFS_Password = "";
102#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400103}
104
105TWPartition::~TWPartition(void) {
106 // Do nothing
107}
108
Dees_Troy5bf43922012-09-07 16:07:55 -0400109bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
110 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
111 int line_len = Line.size(), index = 0, item_index = 0;
112 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400113 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400114 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500115 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400116
Dees_Troy51127312012-09-08 13:08:49 -0400117 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500118 if (full_line[index] == 34)
119 skip = !skip;
120 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400121 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400122 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400123 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000124 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400125 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500126 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000127 Display_Name = full_line + 1;
128 Backup_Display_Name = Display_Name;
129 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400130 index = Mount_Point.size();
131 while (index < line_len) {
132 while (index < line_len && full_line[index] == '\0')
133 index++;
134 if (index >= line_len)
135 continue;
136 ptr = full_line + index;
137 if (item_index == 0) {
138 // File System
139 Fstab_File_System = ptr;
140 Current_File_System = ptr;
141 item_index++;
142 } else if (item_index == 1) {
143 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400144 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400145 MTD_Name = ptr;
146 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400147 } else if (Fstab_File_System == "bml") {
148 if (Mount_Point == "/boot")
149 MTD_Name = "boot";
150 else if (Mount_Point == "/recovery")
151 MTD_Name = "recovery";
152 Primary_Block_Device = ptr;
153 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000154 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 -0400155 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400156 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000157 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400158 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000159 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400160 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400161 } else {
162 Primary_Block_Device = ptr;
163 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400164 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400165 item_index++;
166 } else if (item_index > 1) {
167 if (*ptr == '/') {
168 // Alternate Block Device
169 Alternate_Block_Device = ptr;
170 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
171 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
172 // Partition length
173 ptr += 7;
174 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400175 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
176 // Custom flags, save for later so that new values aren't overwritten by defaults
177 ptr += 6;
178 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000179 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400180 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
181 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400182 } else {
183 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000184 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400185 }
186 }
187 while (index < line_len && full_line[index] != '\0')
188 index++;
189 }
190
191 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
192 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000193 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400194 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000195 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400196 return 0;
197 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400198 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400199 Setup_File_System(Display_Error);
200 if (Mount_Point == "/system") {
201 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500202 Backup_Display_Name = Display_Name;
203 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400204 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500205 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400206 } else if (Mount_Point == "/data") {
207 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500208 Backup_Display_Name = Display_Name;
209 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400210 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400211 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500212 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400213#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troya13d74f2013-03-24 08:54:55 -0500214 Storage_Name = "Internal Storage";
Dees_Troy5bf43922012-09-07 16:07:55 -0400215 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400216 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500217 Is_Settings_Storage = true;
Dees_Troy51127312012-09-08 13:08:49 -0400218 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000219 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400220 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
221 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400222 Symlink_Mount_Point = "/emmc";
223 } else {
224 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400225 Symlink_Mount_Point = "/sdcard";
226 }
Dees_Troy16b74352012-11-14 22:27:31 +0000227 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
228 Storage_Path = "/data/media/0";
229 Symlink_Path = Storage_Path;
230 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
231 UnMount(true);
232 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400233#endif
234#ifdef TW_INCLUDE_CRYPTO
235 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400236 char crypto_blkdev[255];
237 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
238 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400239 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400240 DataManager::SetValue(TW_IS_DECRYPTED, 1);
241 Is_Encrypted = true;
242 Is_Decrypted = true;
243 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000244 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400245 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400246 Is_Encrypted = true;
247 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800248 Can_Be_Mounted = false;
249 Current_File_System = "emmc";
250 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400251 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
252 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
253 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800254 } else {
255 // Filesystem is not encrypted and the mount
256 // succeeded, so get it back to the original
257 // unmounted state
258 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400259 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400260 #ifdef RECOVERY_SDCARD_ON_DATA
261 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
262 Recreate_Media_Folder();
263 #endif
264#else
265 #ifdef RECOVERY_SDCARD_ON_DATA
266 Recreate_Media_Folder();
267 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400268#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400269 } else if (Mount_Point == "/cache") {
270 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500271 Backup_Display_Name = Display_Name;
272 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400273 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400274 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500275 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400276 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000277 LOGINFO("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500278 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
279 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400280 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400281 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400282 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400283 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500284 Backup_Display_Name = Display_Name;
285 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400286 Is_SubPartition = true;
287 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400288 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500289 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400290 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400291 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400292 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500293 Backup_Display_Name = Display_Name;
294 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400295 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400296 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500297 Can_Be_Backed_Up = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400298 } else if (Mount_Point == "/boot") {
299 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500300 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400301 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500302 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400303 }
304#ifdef TW_EXTERNAL_STORAGE_PATH
305 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
306 Is_Storage = true;
307 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400308 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500309 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400310#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000311 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400312 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400313 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500314 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400315#ifndef RECOVERY_SDCARD_ON_DATA
316 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400317 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400318#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400319#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000320 }
Dees_Troy8170a922012-09-18 15:40:25 -0400321#ifdef TW_INTERNAL_STORAGE_PATH
322 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
323 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500324 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400325 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500326 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400327#ifndef RECOVERY_SDCARD_ON_DATA
328 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400329 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400330#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400331 }
332#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000333 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400334 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500335 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500336 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400337#ifndef RECOVERY_SDCARD_ON_DATA
338 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400339 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400340#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400341 }
342#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400343 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400344 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400345 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500346 if (Mount_Point == "/boot") {
347 Display_Name = "Boot";
348 Backup_Display_Name = Display_Name;
349 Can_Be_Backed_Up = true;
350 } else if (Mount_Point == "/recovery") {
351 Display_Name = "Recovery";
352 Backup_Display_Name = Display_Name;
353 Can_Be_Backed_Up = true;
354 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400355 }
356
Dees_Troy51127312012-09-08 13:08:49 -0400357 // Process any custom flags
358 if (Flags.size() > 0)
359 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400360 return true;
361}
362
363bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
364 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500365 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400366 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500367 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400368
369 strcpy(flags, Flags.c_str());
370 flags_len = Flags.size();
371 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500372 if (flags[index] == 34)
373 skip = !skip;
374 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400375 flags[index] = '\0';
376 }
377
378 index = 0;
379 while (index < flags_len) {
380 while (index < flags_len && flags[index] == '\0')
381 index++;
382 if (index >= flags_len)
383 continue;
384 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500385 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400386 if (strcmp(ptr, "removable") == 0) {
387 Removable = true;
388 } else if (strcmp(ptr, "storage") == 0) {
389 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500390 } else if (strcmp(ptr, "settingsstorage") == 0) {
391 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400392 } else if (strcmp(ptr, "canbewiped") == 0) {
393 Can_Be_Wiped = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500394 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
395 ptr += 7;
396 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
397 Can_Be_Backed_Up = true;
398 else
399 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400400 } else if (strcmp(ptr, "wipeingui") == 0) {
401 Can_Be_Wiped = true;
402 Wipe_Available_in_GUI = true;
403 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
404 Can_Be_Wiped = true;
405 Wipe_Available_in_GUI = true;
406 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500407 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000408 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400409 Is_SubPartition = true;
410 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000411 } else if (strcmp(ptr, "ignoreblkid") == 0) {
412 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000413 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
414 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500415 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400416 ptr += 8;
417 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500418 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
419 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400420 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500421 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400422 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500423 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
424 Display_Name.resize(Display_Name.size() - 1);
425 }
426 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
427 has_storage_name = true;
428 ptr += 11;
429 if (*ptr == '\"') ptr++;
430 Storage_Name = ptr;
431 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
432 Storage_Name.resize(Storage_Name.size() - 1);
433 }
434 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
435 has_backup_name = true;
436 ptr += 10;
437 if (*ptr == '\"') ptr++;
438 Backup_Display_Name = ptr;
439 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
440 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
441 }
442 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400443 ptr += 10;
444 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500445 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400446 ptr += 7;
447 Length = atoi(ptr);
448 } else {
449 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000450 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400451 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000452 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400453 }
454 while (index < flags_len && flags[index] != '\0')
455 index++;
456 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500457 if (has_display_name && !has_storage_name)
458 Storage_Name = Display_Name;
459 if (!has_display_name && has_storage_name)
460 Display_Name = Storage_Name;
461 if (has_display_name && !has_backup_name)
462 Backup_Display_Name = Display_Name;
463 if (!has_display_name && has_backup_name)
464 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400465 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400466}
467
Dees_Troy5bf43922012-09-07 16:07:55 -0400468bool TWPartition::Is_File_System(string File_System) {
469 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400470 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400471 File_System == "ext4" ||
472 File_System == "vfat" ||
473 File_System == "ntfs" ||
474 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500475 File_System == "exfat" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400476 File_System == "auto")
477 return true;
478 else
479 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400480}
481
Dees_Troy5bf43922012-09-07 16:07:55 -0400482bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400483 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400484 return true;
485 else
486 return false;
487}
488
Dees_Troy51127312012-09-08 13:08:49 -0400489bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400490 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400491 if (mkdir(Path.c_str(), 0777) == -1) {
492 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000493 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400494 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000495 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400496 return false;
497 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000498 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400499 return true;
500 }
501 }
502 return true;
503}
504
Dees_Troy5bf43922012-09-07 16:07:55 -0400505void TWPartition::Setup_File_System(bool Display_Error) {
506 struct statfs st;
507
508 Can_Be_Mounted = true;
509 Can_Be_Wiped = true;
510
Dees_Troy5bf43922012-09-07 16:07:55 -0400511 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400512 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400513 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
514 Backup_Name = Display_Name;
515 Backup_Method = FILES;
516}
517
518void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400519 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
520 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800521 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400522 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800523 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400524 Backup_Method = FLASH_UTILS;
525 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000526 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 -0400527 if (Find_Partition_Size()) {
528 Used = Size;
529 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400530 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400531 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000532 LOGERR("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400533 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000534 LOGINFO("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400535 }
536}
537
Dees_Troye58d5262012-09-21 12:27:57 -0400538void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500539 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400540 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500541 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400542 Has_Android_Secure = true;
543 Symlink_Path = Mount_Point + "/.android_secure";
544 Symlink_Mount_Point = "/and-sec";
545 Backup_Path = Symlink_Mount_Point;
546 Make_Dir("/and-sec", true);
547 Recreate_AndSec_Folder();
548}
549
Dees_Troy5bf43922012-09-07 16:07:55 -0400550void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
551 char device[512], realDevice[512];
552
553 strcpy(device, Block.c_str());
554 memset(realDevice, 0, sizeof(realDevice));
555 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
556 {
557 strcpy(device, realDevice);
558 memset(realDevice, 0, sizeof(realDevice));
559 }
560
561 if (device[0] != '/') {
562 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000563 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400564 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000565 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400566 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400567 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400568 Block = device;
569 return;
570 }
571}
572
Dees_Troy8e337f32012-10-13 22:07:49 -0400573void TWPartition::Mount_Storage_Retry(void) {
574 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500575 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400576 int retry_count = 5;
577 while (retry_count > 0 && !Mount(false)) {
578 usleep(500000);
579 retry_count--;
580 }
581 Mount(true);
582 }
583}
584
Dees_Troy38bd7602012-09-14 13:33:53 -0400585bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
586 FILE *fp = NULL;
587 char line[255];
588
589 fp = fopen("/proc/mtd", "rt");
590 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000591 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400592 return false;
593 }
594
595 while (fgets(line, sizeof(line), fp) != NULL)
596 {
597 char device[32], label[32];
598 unsigned long size = 0;
599 char* fstype = NULL;
600 int deviceId;
601
602 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
603
604 // Skip header and blank lines
605 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
606 continue;
607
608 // Strip off the trailing " from the label
609 label[strlen(label)-1] = '\0';
610
611 if (strcmp(label, MTD_Name.c_str()) == 0) {
612 // We found our device
613 // Strip off the trailing : from the device
614 device[strlen(device)-1] = '\0';
615 if (sscanf(device,"mtd%d", &deviceId) == 1) {
616 sprintf(device, "/dev/block/mtdblock%d", deviceId);
617 Primary_Block_Device = device;
618 }
619 }
620 }
621 fclose(fp);
622
623 return false;
624}
625
Dees_Troy51127312012-09-08 13:08:49 -0400626bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
627 struct statfs st;
628 string Local_Path = Mount_Point + "/.";
629
630 if (!Mount(Display_Error))
631 return false;
632
633 if (statfs(Local_Path.c_str(), &st) != 0) {
634 if (!Removable) {
635 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000636 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400637 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000638 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400639 }
640 return false;
641 }
642 Size = (st.f_blocks * st.f_bsize);
643 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
644 Free = (st.f_bfree * st.f_bsize);
645 Backup_Size = Used;
646 return true;
647}
648
649bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400650 FILE* fp;
651 char command[255], line[512];
652 int include_block = 1;
653 unsigned int min_len;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500654 string result;
Dees_Troy5bf43922012-09-07 16:07:55 -0400655
656 if (!Mount(Display_Error))
657 return false;
658
Dees_Troy38bd7602012-09-14 13:33:53 -0400659 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400660 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500661 TWFunc::Exec_Cmd(command, result);
Dees_Troy51127312012-09-08 13:08:49 -0400662 fp = fopen("/tmp/dfoutput.txt", "rt");
663 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000664 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400665 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400666 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400667
668 while (fgets(line, sizeof(line), fp) != NULL)
669 {
670 unsigned long blocks, used, available;
671 char device[64];
672 char tmpString[64];
673
674 if (strncmp(line, "Filesystem", 10) == 0)
675 continue;
676 if (strlen(line) < min_len) {
677 include_block = 0;
678 continue;
679 }
680 if (include_block) {
681 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
682 } else {
683 // The device block string is so long that the df information is on the next line
684 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400685 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400686 while (tmpString[space_count] == 32)
687 space_count++;
688 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
689 }
690
691 // Adjust block size to byte size
692 Size = blocks * 1024ULL;
693 Used = used * 1024ULL;
694 Free = available * 1024ULL;
695 Backup_Size = Used;
696 }
697 fclose(fp);
698 return true;
699}
700
Dees_Troy5bf43922012-09-07 16:07:55 -0400701bool TWPartition::Find_Partition_Size(void) {
702 FILE* fp;
703 char line[512];
704 string tmpdevice;
705
igoriok87e3d932013-01-31 21:03:53 +0200706 fp = fopen("/proc/dumchar_info", "rt");
707 if (fp != NULL) {
708 while (fgets(line, sizeof(line), fp) != NULL)
709 {
710 char label[32], device[32];
711 unsigned long size = 0;
712
713 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
714
715 // Skip header, annotation and blank lines
716 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
717 continue;
718
719 tmpdevice = "/dev/";
720 tmpdevice += label;
721 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
722 Size = size;
723 fclose(fp);
724 return true;
725 }
726 }
727 }
728
Dees_Troy5bf43922012-09-07 16:07:55 -0400729 // In this case, we'll first get the partitions we care about (with labels)
730 fp = fopen("/proc/partitions", "rt");
731 if (fp == NULL)
732 return false;
733
734 while (fgets(line, sizeof(line), fp) != NULL)
735 {
736 unsigned long major, minor, blocks;
737 char device[512];
738 char tmpString[64];
739
Dees_Troy63c8df72012-09-10 14:02:05 -0400740 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400741 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
742
743 tmpdevice = "/dev/block/";
744 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400745 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400746 // Adjust block size to byte size
747 Size = blocks * 1024ULL;
748 fclose(fp);
749 return true;
750 }
751 }
752 fclose(fp);
753 return false;
754}
755
Dees_Troy5bf43922012-09-07 16:07:55 -0400756bool TWPartition::Is_Mounted(void) {
757 if (!Can_Be_Mounted)
758 return false;
759
760 struct stat st1, st2;
761 string test_path;
762
763 // Check to see if the mount point directory exists
764 test_path = Mount_Point + "/.";
765 if (stat(test_path.c_str(), &st1) != 0) return false;
766
767 // Check to see if the directory above the mount point exists
768 test_path = Mount_Point + "/../.";
769 if (stat(test_path.c_str(), &st2) != 0) return false;
770
771 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
772 int ret = (st1.st_dev != st2.st_dev) ? true : false;
773
774 return ret;
775}
776
777bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000778 int exfat_mounted = 0;
779
Dees_Troy5bf43922012-09-07 16:07:55 -0400780 if (Is_Mounted()) {
781 return true;
782 } else if (!Can_Be_Mounted) {
783 return false;
784 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400785
786 Find_Actual_Block_Device();
787
788 // Check the current file system before mounting
789 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000790 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000791 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 +0000792 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000793 string result;
794 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000795 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000796 Current_File_System = "vfat";
797 } else {
798#ifdef TW_NO_EXFAT_FUSE
799 UnMount(false);
800 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
801 // Some kernels let us mount vfat as exfat which doesn't work out too well
802#else
803 exfat_mounted = 1;
804#endif
805 }
806 }
Dees_Troy22042032012-12-18 21:23:08 +0000807 if (Fstab_File_System == "yaffs2") {
808 // mount an MTD partition as a YAFFS2 filesystem.
809 mtd_scan_partitions();
810 const MtdPartition* partition;
811 partition = mtd_find_partition_by_name(MTD_Name.c_str());
812 if (partition == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000813 LOGERR("Failed to find '%s' partition to mount at '%s'\n",
Dees_Troy22042032012-12-18 21:23:08 +0000814 MTD_Name.c_str(), Mount_Point.c_str());
815 return false;
816 }
817 if (mtd_mount_partition(partition, Mount_Point.c_str(), Fstab_File_System.c_str(), 0)) {
818 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000819 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
Dees_Troy22042032012-12-18 21:23:08 +0000820 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000821 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
Dees_Troy22042032012-12-18 21:23:08 +0000822 return false;
823 } else
824 return true;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000825 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
826#ifdef TW_NO_EXFAT_FUSE
827 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000828 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000829 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000830 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000831 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000832 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000833 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
834 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 +0000835 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000836 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000837 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000838#endif
839 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000840 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000841 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000842 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
843 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 +0000844 return false;
845#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000846 }
847#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000848 }
849#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
850 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
851 MetaEcfsFile += "/.MetaEcfsFile";
852 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
853 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
854 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000855 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000856 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000857 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000858 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000859 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000860 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400861 }
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000862 } else {
863 Is_Decrypted = false;
864 }
865#endif
866 if (Removable)
867 Update_Size(Display_Error);
868
869 if (!Symlink_Mount_Point.empty()) {
870 string Command, Result;
871 Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
872 TWFunc::Exec_Cmd(Command, Result);
Dees_Troy5bf43922012-09-07 16:07:55 -0400873 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400874 return true;
875}
876
877bool TWPartition::UnMount(bool Display_Error) {
878 if (Is_Mounted()) {
879 int never_unmount_system;
880
881 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
882 if (never_unmount_system == 1 && Mount_Point == "/system")
883 return true; // Never unmount system if you're not supposed to unmount it
884
Dees_Troyc8bafa12013-01-10 15:43:00 +0000885#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
886 if (EcryptFS_Password.size() > 0) {
887 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
888 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000889 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000890 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000891 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000892 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000893 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000894 }
895 }
896#endif
897
Dees_Troy38bd7602012-09-14 13:33:53 -0400898 if (!Symlink_Mount_Point.empty())
899 umount(Symlink_Mount_Point.c_str());
900
Dees_Troyb05ddee2013-01-28 20:24:50 +0000901 umount(Mount_Point.c_str());
902 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400903 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000904 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400905 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000906 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400907 return false;
908 } else
909 return true;
910 } else {
911 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400912 }
913}
914
Gary Peck43acadf2012-11-21 21:19:01 -0800915bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +0000916 bool wiped = false, update_crypt = false;
917 int check;
918 string Layout_Filename = Mount_Point + "/.layout_version";
919
Dees_Troy38bd7602012-09-14 13:33:53 -0400920 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000921 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400922 return false;
923 }
924
Dees_Troyc51f1f92012-09-20 15:32:13 -0400925 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +0000926 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400927
Dees_Troyce675462013-01-09 19:48:21 +0000928#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
929 if (Mount_Point == "/data" && Mount(false)) {
930 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
931 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
932 }
933#endif
934
Dees_Troy16c2b312013-01-15 16:51:18 +0000935 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
936 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
937 else
938 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -0400939
Dees_Troy16c2b312013-01-15 16:51:18 +0000940 if (Has_Data_Media) {
941 wiped = Wipe_Data_Without_Wiping_Media();
942 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -0800943
Dees_Troy16c2b312013-01-15 16:51:18 +0000944 DataManager::GetValue(TW_RM_RF_VAR, check);
945
946 if (check)
947 wiped = Wipe_RMRF();
948 else if (New_File_System == "ext4")
949 wiped = Wipe_EXT4();
950 else if (New_File_System == "ext2" || New_File_System == "ext3")
951 wiped = Wipe_EXT23(New_File_System);
952 else if (New_File_System == "vfat")
953 wiped = Wipe_FAT();
954 else if (New_File_System == "exfat")
955 wiped = Wipe_EXFAT();
956 else if (New_File_System == "yaffs2")
957 wiped = Wipe_MTD();
958 else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000959 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 +0000960 unlink("/.layout_version");
961 return false;
962 }
963 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800964 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400965
Gary Pecke8bc5d72012-12-21 06:45:25 -0800966 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +0000967#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
968 if (Mount_Point == "/data" && Mount(false)) {
969 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
970 Make_Dir("/data/system", true);
971 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
972 }
973 }
974#endif
Dees_Troy16c2b312013-01-15 16:51:18 +0000975
Dees_Troy1c1ac442013-01-17 21:42:14 +0000976 if (Mount_Point == "/cache")
977 DataManager::Output_Version();
978
Dees_Troy16c2b312013-01-15 16:51:18 +0000979 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
980 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
981
982 if (update_crypt) {
983 Setup_File_System(false);
984 if (Is_Encrypted && !Is_Decrypted) {
985 // just wiped an encrypted partition back to its unencrypted state
986 Is_Encrypted = false;
987 Is_Decrypted = false;
988 Decrypted_Block_Device = "";
989 if (Mount_Point == "/data") {
990 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
991 DataManager::SetValue(TW_IS_DECRYPTED, 0);
992 }
Gary Pecke8bc5d72012-12-21 06:45:25 -0800993 }
994 }
995 }
996 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -0400997}
998
Gary Peck43acadf2012-11-21 21:19:01 -0800999bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001000 if (Is_File_System(Current_File_System))
1001 return Wipe(Current_File_System);
1002 else
1003 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001004}
1005
Dees_Troye58d5262012-09-21 12:27:57 -04001006bool TWPartition::Wipe_AndSec(void) {
1007 if (!Has_Android_Secure)
1008 return false;
1009
Dees_Troye58d5262012-09-21 12:27:57 -04001010 if (!Mount(true))
1011 return false;
1012
Dees_Troy2673cec2013-04-02 20:22:16 +00001013 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001014 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Dees_Troye58d5262012-09-21 12:27:57 -04001015 return true;
1016}
1017
Dees_Troy51a0e822012-09-05 15:24:24 -04001018bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001019 if (Backup_Method == FILES)
1020 return Backup_Tar(backup_folder);
1021 else if (Backup_Method == DD)
1022 return Backup_DD(backup_folder);
1023 else if (Backup_Method == FLASH_UTILS)
1024 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001025 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001026 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001027}
1028
Dees_Troy43d8b002012-09-17 16:00:01 -04001029bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001030 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001031 char split_filename[512];
1032 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001033 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001034
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001035 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001036 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001037 if (!TWFunc::Path_Exists(Full_Filename)) {
1038 // This is a split archive, we presume
1039 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001040 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001041 md5file = split_filename;
1042 md5file += ".md5";
1043 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001044 LOGERR("No md5 file found for '%s'.\n", split_filename);
1045 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001046 return false;
1047 }
1048 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001049 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001050 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001051 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001052 return false;
1053 }
1054 index++;
1055 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001056 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001057 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001058 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001059 } else {
1060 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001061 md5file = Full_Filename + ".md5";
1062 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001063 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1064 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001065 return false;
1066 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001067 md5sum.setfn(Full_Filename);
1068 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001069 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001070 return false;
1071 } else
1072 return true;
1073 }
1074 return false;
1075}
1076
Dees_Troy51a0e822012-09-05 15:24:24 -04001077bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001078 size_t first_period, second_period;
1079 string Restore_File_System;
1080
1081 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001082 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001083
1084 // Parse backup filename to extract the file system before wiping
1085 first_period = Backup_FileName.find(".");
1086 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001087 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001088 return false;
1089 }
1090 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1091 second_period = Restore_File_System.find(".");
1092 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001093 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001094 return false;
1095 }
1096 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001097 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001098
1099 if (Is_File_System(Restore_File_System))
1100 return Restore_Tar(restore_folder, Restore_File_System);
1101 else if (Is_Image(Restore_File_System)) {
1102 if (Restore_File_System == "emmc")
1103 return Restore_DD(restore_folder);
1104 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1105 return Restore_Flash_Image(restore_folder);
1106 }
1107
Dees_Troy2673cec2013-04-02 20:22:16 +00001108 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001109 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001110}
1111
1112string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001113 if (Backup_Method == NONE)
1114 return "none";
1115 else if (Backup_Method == FILES)
1116 return "files";
1117 else if (Backup_Method == DD)
1118 return "dd";
1119 else if (Backup_Method == FLASH_UTILS)
1120 return "flash_utils";
1121 else
1122 return "undefined";
1123 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001124}
1125
1126bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001127 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001128 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001129 return 1;
1130}
1131
1132bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001133 bool Save_Data_Media = Has_Data_Media;
1134
1135 if (!UnMount(true))
1136 return false;
1137
Dees_Troy38bd7602012-09-14 13:33:53 -04001138 Has_Data_Media = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001139 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001140 Has_Data_Media = Save_Data_Media;
1141 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1142 Recreate_Media_Folder();
1143 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001144 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001145 return true;
1146 } else {
1147 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001148 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001149 return false;
1150 }
1151 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001152}
1153
1154void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001155 const char* type;
1156 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001157
Dees_Troy68cab492012-12-12 19:29:35 +00001158 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1159 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001160
Dees_Troy38bd7602012-09-14 13:33:53 -04001161 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001162 if (!Is_Present)
1163 return;
Dees_Troy51127312012-09-08 13:08:49 -04001164
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001165 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1166 if (blkid_do_fullprobe(pr)) {
1167 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001168 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001169 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001170 }
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001171 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1172 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001173 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001174 return;
1175 }
1176 Current_File_System = type;
Dees_Troy51a0e822012-09-05 15:24:24 -04001177 return;
1178}
1179
Gary Peck43acadf2012-11-21 21:19:01 -08001180bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001181 if (!UnMount(true))
1182 return false;
1183
Dees_Troy43d8b002012-09-17 16:00:01 -04001184 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001185 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001186
Dees_Troy2673cec2013-04-02 20:22:16 +00001187 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001188 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001189 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001190 LOGINFO("mke2fs command: %s\n", command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001191 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001192 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001193 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001194 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001195 return true;
1196 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001197 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001198 return false;
1199 }
1200 } else
1201 return Wipe_RMRF();
1202
1203 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001204}
1205
1206bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001207 if (!UnMount(true))
1208 return false;
1209
Dees_Troy43d8b002012-09-17 16:00:01 -04001210 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001211 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001212
Dees_Troy2673cec2013-04-02 20:22:16 +00001213 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001214 Find_Actual_Block_Device();
1215 Command = "make_ext4fs";
1216 if (!Is_Decrypted && Length != 0) {
1217 // Only use length if we're not decrypted
1218 char len[32];
1219 sprintf(len, "%i", Length);
1220 Command += " -l ";
1221 Command += len;
1222 }
1223 Command += " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001224 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001225 if (TWFunc::Exec_Cmd(Command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001226 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001227 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001228 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001229 return true;
1230 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001231 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001232 return false;
1233 }
1234 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001235 return Wipe_EXT23("ext4");
Dees_Troy38bd7602012-09-14 13:33:53 -04001236
1237 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001238}
1239
1240bool TWPartition::Wipe_FAT() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001241 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001242
Dees_Troy43d8b002012-09-17 16:00:01 -04001243 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001244 if (!UnMount(true))
1245 return false;
1246
Dees_Troy2673cec2013-04-02 20:22:16 +00001247 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001248 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001249 command = "mkdosfs " + Actual_Block_Device;
1250 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001251 Current_File_System = "vfat";
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 return true;
1260 }
1261 else
1262 return Wipe_RMRF();
1263
1264 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001265}
1266
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001267bool TWPartition::Wipe_EXFAT() {
1268 string command, result;
1269
1270 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1271 if (!UnMount(true))
1272 return false;
1273
Dees_Troy2673cec2013-04-02 20:22:16 +00001274 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001275 Find_Actual_Block_Device();
1276 command = "mkexfatfs " + Actual_Block_Device;
1277 if (TWFunc::Exec_Cmd(command, result) == 0) {
1278 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001279 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001280 return true;
1281 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001282 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001283 return false;
1284 }
1285 return true;
1286 }
1287 return false;
1288}
1289
Dees_Troy38bd7602012-09-14 13:33:53 -04001290bool TWPartition::Wipe_MTD() {
1291 if (!UnMount(true))
1292 return false;
1293
Dees_Troy2673cec2013-04-02 20:22:16 +00001294 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001295
1296 mtd_scan_partitions();
1297 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1298 if (mtd == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001299 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001300 return false;
1301 }
1302
1303 MtdWriteContext* ctx = mtd_write_partition(mtd);
1304 if (ctx == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001305 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001306 return false;
1307 }
1308 if (mtd_erase_blocks(ctx, -1) == -1) {
1309 mtd_write_close(ctx);
Dees_Troy2673cec2013-04-02 20:22:16 +00001310 LOGERR("Failed to format '%s'", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001311 return false;
1312 }
1313 if (mtd_write_close(ctx) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001314 LOGERR("Failed to close '%s'", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001315 return false;
1316 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001317 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001318 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001319 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001320 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001321}
1322
1323bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001324 if (!Mount(true))
1325 return false;
1326
Dees_Troy2673cec2013-04-02 20:22:16 +00001327 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001328 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001329 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001330 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001331}
1332
1333bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001334 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001335
1336 // This handles wiping data on devices with "sdcard" in /data/media
1337 if (!Mount(true))
1338 return false;
1339
Dees_Troy2673cec2013-04-02 20:22:16 +00001340 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001341
1342 DIR* d;
1343 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001344 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001345 struct dirent* de;
1346 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001347 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1348 // The media folder is the "internal sdcard"
1349 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
1350 // the media folder for multi-user.
1351 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001352
1353 dir = "/data/";
1354 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001355 if (de->d_type == DT_DIR) {
1356 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001357 } 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 +00001358 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001359 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001360 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001361 }
1362 closedir(d);
Dees_Troy2673cec2013-04-02 20:22:16 +00001363 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001364 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001365 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001366 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001367 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001368}
1369
1370bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001371 char back_name[255], split_index[5];
1372 string Full_FileName, Split_FileName, Tar_Args, Command;
1373 int use_compression, index, backup_count;
1374 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001375 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001376 twrpTar tar;
1377 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001378
Dees_Troy43d8b002012-09-17 16:00:01 -04001379 if (!Mount(true))
1380 return false;
1381
Dees_Troya13d74f2013-03-24 08:54:55 -05001382 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001383 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001384
1385 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy43d8b002012-09-17 16:00:01 -04001386
1387 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1388 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001389 Full_FileName = backup_folder + "/" + Backup_FileName;
1390 if (Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001391 // This backup needs to be split into multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001392 gui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001393 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001394 tar.setdir(back_name);
1395 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001396 backup_count = tar.splitArchiveFork();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001397 if (backup_count == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001398 LOGERR("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001399 return false;
1400 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001401 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001402 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001403 Full_FileName = backup_folder + "/" + Backup_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001404 if (use_compression) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001405 tar.setdir(Backup_Path);
1406 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001407 if (tar.createTarGZFork() != 0)
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001408 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001409 string gzname = Full_FileName + ".gz";
1410 rename(gzname.c_str(), Full_FileName.c_str());
1411 }
1412 else {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001413 tar.setdir(Backup_Path);
1414 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001415 if (tar.createTarFork() != 0)
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001416 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001417 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001418 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001419 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001420 return false;
1421 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001422 }
1423 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001424}
1425
1426bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001427 char back_name[255], backup_size[32];
1428 string Full_FileName, Command, result, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001429 int use_compression;
1430
igoriok87e3d932013-01-31 21:03:53 +02001431 sprintf(backup_size, "%llu", Backup_Size);
1432 DD_BS = backup_size;
1433
Dees_Troyb46a6842012-09-25 11:06:46 -04001434 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001435 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001436
1437 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1438 Backup_FileName = back_name;
1439
1440 Full_FileName = backup_folder + "/" + Backup_FileName;
1441
igoriok87e3d932013-01-31 21:03:53 +02001442 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001443 LOGINFO("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001444 TWFunc::Exec_Cmd(Command, result);
Dees_Troyc154ac22012-10-12 15:36:47 -04001445 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001446 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001447 return false;
1448 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001449 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001450}
1451
1452bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001453 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001454 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001455 int use_compression;
1456
Dees_Troyb46a6842012-09-25 11:06:46 -04001457 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001458 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001459
1460 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1461 Backup_FileName = back_name;
1462
1463 Full_FileName = backup_folder + "/" + Backup_FileName;
1464
1465 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001466 LOGINFO("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001467 TWFunc::Exec_Cmd(Command, result);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001468 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1469 // 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 +00001470 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001471 return false;
1472 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001473 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001474}
1475
Gary Peck43acadf2012-11-21 21:19:01 -08001476bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1477 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001478 int index = 0;
1479 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001480
Dees_Troye58d5262012-09-21 12:27:57 -04001481 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001482 if (!Wipe_AndSec())
1483 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001484 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001485 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001486 if (!Wipe(Restore_File_System))
1487 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001488 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001489 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001490 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001491
1492 if (!Mount(true))
1493 return false;
1494
Dees_Troy4a2a1262012-09-18 09:33:47 -04001495 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001496 if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001497 if (!TWFunc::Path_Exists(Full_FileName)) {
1498 // Backup is multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001499 LOGINFO("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001500 sprintf(split_index, "%03i", index);
1501 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001502 while (TWFunc::Path_Exists(Full_FileName)) {
1503 index++;
Dees_Troy2673cec2013-04-02 20:22:16 +00001504 gui_print("Restoring archive %i...\n", index);
1505 LOGINFO("Restoring '%s'...\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001506 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001507 tar.setdir("/");
1508 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001509 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001510 return false;
1511 sprintf(split_index, "%03i", index);
1512 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1513 }
1514 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001515 LOGERR("Error locating restore file: '%s'\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001516 return false;
1517 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001518 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001519 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001520 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001521 tar.setdir(Backup_Path);
1522 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001523 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001524 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001525 }
1526 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001527}
1528
1529bool TWPartition::Restore_DD(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001530 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001531
Dees_Troyda8b55a2012-12-12 19:18:30 +00001532 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001533 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001534
1535 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001536 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001537 return false;
1538 }
1539 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1540 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001541 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001542 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1543 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1544 return false;
1545 }
1546
Dees_Troy2673cec2013-04-02 20:22:16 +00001547 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001548 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001549 LOGINFO("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001550 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001551 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001552}
1553
1554bool TWPartition::Restore_Flash_Image(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001555 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001556
Dees_Troy2673cec2013-04-02 20:22:16 +00001557 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001558 Full_FileName = restore_folder + "/" + Backup_FileName;
1559 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1560 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001561 LOGINFO("Erase command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001562 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001563 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001564 LOGINFO("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001565 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001566 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001567}
Dees_Troy5bf43922012-09-07 16:07:55 -04001568
1569bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001570 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001571
Dees_Troyab10ee22012-09-21 14:27:30 -04001572 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001573 return false;
1574
Dees_Troy0550cfb2012-10-13 11:56:13 -04001575 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001576 if (Removable || Is_Encrypted) {
1577 if (!Mount(false))
1578 return true;
1579 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001580 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001581
1582 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001583 if (!ret || Size == 0) {
1584 if (!Get_Size_Via_df(Display_Error)) {
1585 if (!Was_Already_Mounted)
1586 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001587 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001588 }
1589 }
Dees_Troy51127312012-09-08 13:08:49 -04001590
Dees_Troy5bf43922012-09-07 16:07:55 -04001591 if (Has_Data_Media) {
1592 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001593 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001594 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1595 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001596 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001597 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001598 int bak = (int)(Backup_Size / 1048576LLU);
1599 int total = (int)(Size / 1048576LLU);
1600 int us = (int)(Used / 1048576LLU);
1601 int fre = (int)(Free / 1048576LLU);
1602 int datmed = (int)(data_media_used / 1048576LLU);
Dees_Troy2673cec2013-04-02 20:22:16 +00001603 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 -04001604 } else {
1605 if (!Was_Already_Mounted)
1606 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001607 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001608 }
Dees_Troye58d5262012-09-21 12:27:57 -04001609 } else if (Has_Android_Secure) {
1610 if (Mount(Display_Error))
1611 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001612 else {
1613 if (!Was_Already_Mounted)
1614 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001615 return false;
Dees_Troy