blob: e88ccc8a0de27a0c116fde9b0f6ef24df2e3b61b [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_Troy8170a922012-09-18 15:40:25 -0400336 Storage_Path = "/emmc";
Dees_Troya13d74f2013-03-24 08:54:55 -0500337 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400338#ifndef RECOVERY_SDCARD_ON_DATA
339 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400340 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400341#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400342 }
343#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400344 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400345 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400346 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500347 if (Mount_Point == "/boot") {
348 Display_Name = "Boot";
349 Backup_Display_Name = Display_Name;
350 Can_Be_Backed_Up = true;
351 } else if (Mount_Point == "/recovery") {
352 Display_Name = "Recovery";
353 Backup_Display_Name = Display_Name;
354 Can_Be_Backed_Up = true;
355 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400356 }
357
Dees_Troy51127312012-09-08 13:08:49 -0400358 // Process any custom flags
359 if (Flags.size() > 0)
360 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400361 return true;
362}
363
364bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
365 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500366 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400367 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500368 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400369
370 strcpy(flags, Flags.c_str());
371 flags_len = Flags.size();
372 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500373 if (flags[index] == 34)
374 skip = !skip;
375 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400376 flags[index] = '\0';
377 }
378
379 index = 0;
380 while (index < flags_len) {
381 while (index < flags_len && flags[index] == '\0')
382 index++;
383 if (index >= flags_len)
384 continue;
385 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500386 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400387 if (strcmp(ptr, "removable") == 0) {
388 Removable = true;
389 } else if (strcmp(ptr, "storage") == 0) {
390 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500391 } else if (strcmp(ptr, "settingsstorage") == 0) {
392 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400393 } else if (strcmp(ptr, "canbewiped") == 0) {
394 Can_Be_Wiped = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500395 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
396 ptr += 7;
397 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
398 Can_Be_Backed_Up = true;
399 else
400 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400401 } else if (strcmp(ptr, "wipeingui") == 0) {
402 Can_Be_Wiped = true;
403 Wipe_Available_in_GUI = true;
404 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
405 Can_Be_Wiped = true;
406 Wipe_Available_in_GUI = true;
407 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500408 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000409 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400410 Is_SubPartition = true;
411 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000412 } else if (strcmp(ptr, "ignoreblkid") == 0) {
413 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000414 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
415 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500416 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400417 ptr += 8;
418 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500419 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
420 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400421 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500422 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400423 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500424 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
425 Display_Name.resize(Display_Name.size() - 1);
426 }
427 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
428 has_storage_name = true;
429 ptr += 11;
430 if (*ptr == '\"') ptr++;
431 Storage_Name = ptr;
432 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
433 Storage_Name.resize(Storage_Name.size() - 1);
434 }
435 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
436 has_backup_name = true;
437 ptr += 10;
438 if (*ptr == '\"') ptr++;
439 Backup_Display_Name = ptr;
440 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
441 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
442 }
443 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400444 ptr += 10;
445 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500446 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400447 ptr += 7;
448 Length = atoi(ptr);
449 } else {
450 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000451 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400452 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000453 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400454 }
455 while (index < flags_len && flags[index] != '\0')
456 index++;
457 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500458 if (has_display_name && !has_storage_name)
459 Storage_Name = Display_Name;
460 if (!has_display_name && has_storage_name)
461 Display_Name = Storage_Name;
462 if (has_display_name && !has_backup_name)
463 Backup_Display_Name = Display_Name;
464 if (!has_display_name && has_backup_name)
465 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400466 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400467}
468
Dees_Troy5bf43922012-09-07 16:07:55 -0400469bool TWPartition::Is_File_System(string File_System) {
470 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400471 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400472 File_System == "ext4" ||
473 File_System == "vfat" ||
474 File_System == "ntfs" ||
475 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500476 File_System == "exfat" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400477 File_System == "auto")
478 return true;
479 else
480 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400481}
482
Dees_Troy5bf43922012-09-07 16:07:55 -0400483bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400484 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400485 return true;
486 else
487 return false;
488}
489
Dees_Troy51127312012-09-08 13:08:49 -0400490bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400491 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400492 if (mkdir(Path.c_str(), 0777) == -1) {
493 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000494 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400495 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000496 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400497 return false;
498 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000499 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400500 return true;
501 }
502 }
503 return true;
504}
505
Dees_Troy5bf43922012-09-07 16:07:55 -0400506void TWPartition::Setup_File_System(bool Display_Error) {
507 struct statfs st;
508
509 Can_Be_Mounted = true;
510 Can_Be_Wiped = true;
511
Dees_Troy5bf43922012-09-07 16:07:55 -0400512 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400513 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400514 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
515 Backup_Name = Display_Name;
516 Backup_Method = FILES;
517}
518
519void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400520 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
521 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800522 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400523 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800524 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400525 Backup_Method = FLASH_UTILS;
526 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000527 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 -0400528 if (Find_Partition_Size()) {
529 Used = Size;
530 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400531 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400532 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000533 LOGERR("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400534 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000535 LOGINFO("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400536 }
537}
538
Dees_Troye58d5262012-09-21 12:27:57 -0400539void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500540 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400541 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500542 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400543 Has_Android_Secure = true;
544 Symlink_Path = Mount_Point + "/.android_secure";
545 Symlink_Mount_Point = "/and-sec";
546 Backup_Path = Symlink_Mount_Point;
547 Make_Dir("/and-sec", true);
548 Recreate_AndSec_Folder();
549}
550
Dees_Troy5bf43922012-09-07 16:07:55 -0400551void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
552 char device[512], realDevice[512];
553
554 strcpy(device, Block.c_str());
555 memset(realDevice, 0, sizeof(realDevice));
556 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
557 {
558 strcpy(device, realDevice);
559 memset(realDevice, 0, sizeof(realDevice));
560 }
561
562 if (device[0] != '/') {
563 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000564 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400565 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000566 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400567 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400568 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400569 Block = device;
570 return;
571 }
572}
573
Dees_Troy8e337f32012-10-13 22:07:49 -0400574void TWPartition::Mount_Storage_Retry(void) {
575 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500576 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400577 int retry_count = 5;
578 while (retry_count > 0 && !Mount(false)) {
579 usleep(500000);
580 retry_count--;
581 }
582 Mount(true);
583 }
584}
585
Dees_Troy38bd7602012-09-14 13:33:53 -0400586bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
587 FILE *fp = NULL;
588 char line[255];
589
590 fp = fopen("/proc/mtd", "rt");
591 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000592 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400593 return false;
594 }
595
596 while (fgets(line, sizeof(line), fp) != NULL)
597 {
598 char device[32], label[32];
599 unsigned long size = 0;
600 char* fstype = NULL;
601 int deviceId;
602
603 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
604
605 // Skip header and blank lines
606 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
607 continue;
608
609 // Strip off the trailing " from the label
610 label[strlen(label)-1] = '\0';
611
612 if (strcmp(label, MTD_Name.c_str()) == 0) {
613 // We found our device
614 // Strip off the trailing : from the device
615 device[strlen(device)-1] = '\0';
616 if (sscanf(device,"mtd%d", &deviceId) == 1) {
617 sprintf(device, "/dev/block/mtdblock%d", deviceId);
618 Primary_Block_Device = device;
619 }
620 }
621 }
622 fclose(fp);
623
624 return false;
625}
626
Dees_Troy51127312012-09-08 13:08:49 -0400627bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
628 struct statfs st;
629 string Local_Path = Mount_Point + "/.";
630
631 if (!Mount(Display_Error))
632 return false;
633
634 if (statfs(Local_Path.c_str(), &st) != 0) {
635 if (!Removable) {
636 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000637 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400638 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000639 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400640 }
641 return false;
642 }
643 Size = (st.f_blocks * st.f_bsize);
644 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
645 Free = (st.f_bfree * st.f_bsize);
646 Backup_Size = Used;
647 return true;
648}
649
650bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400651 FILE* fp;
652 char command[255], line[512];
653 int include_block = 1;
654 unsigned int min_len;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500655 string result;
Dees_Troy5bf43922012-09-07 16:07:55 -0400656
657 if (!Mount(Display_Error))
658 return false;
659
Dees_Troy38bd7602012-09-14 13:33:53 -0400660 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400661 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500662 TWFunc::Exec_Cmd(command, result);
Dees_Troy51127312012-09-08 13:08:49 -0400663 fp = fopen("/tmp/dfoutput.txt", "rt");
664 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000665 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400666 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400667 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400668
669 while (fgets(line, sizeof(line), fp) != NULL)
670 {
671 unsigned long blocks, used, available;
672 char device[64];
673 char tmpString[64];
674
675 if (strncmp(line, "Filesystem", 10) == 0)
676 continue;
677 if (strlen(line) < min_len) {
678 include_block = 0;
679 continue;
680 }
681 if (include_block) {
682 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
683 } else {
684 // The device block string is so long that the df information is on the next line
685 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400686 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400687 while (tmpString[space_count] == 32)
688 space_count++;
689 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
690 }
691
692 // Adjust block size to byte size
693 Size = blocks * 1024ULL;
694 Used = used * 1024ULL;
695 Free = available * 1024ULL;
696 Backup_Size = Used;
697 }
698 fclose(fp);
699 return true;
700}
701
Dees_Troy5bf43922012-09-07 16:07:55 -0400702bool TWPartition::Find_Partition_Size(void) {
703 FILE* fp;
704 char line[512];
705 string tmpdevice;
706
igoriok87e3d932013-01-31 21:03:53 +0200707 fp = fopen("/proc/dumchar_info", "rt");
708 if (fp != NULL) {
709 while (fgets(line, sizeof(line), fp) != NULL)
710 {
711 char label[32], device[32];
712 unsigned long size = 0;
713
714 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
715
716 // Skip header, annotation and blank lines
717 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
718 continue;
719
720 tmpdevice = "/dev/";
721 tmpdevice += label;
722 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
723 Size = size;
724 fclose(fp);
725 return true;
726 }
727 }
728 }
729
Dees_Troy5bf43922012-09-07 16:07:55 -0400730 // In this case, we'll first get the partitions we care about (with labels)
731 fp = fopen("/proc/partitions", "rt");
732 if (fp == NULL)
733 return false;
734
735 while (fgets(line, sizeof(line), fp) != NULL)
736 {
737 unsigned long major, minor, blocks;
738 char device[512];
739 char tmpString[64];
740
Dees_Troy63c8df72012-09-10 14:02:05 -0400741 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400742 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
743
744 tmpdevice = "/dev/block/";
745 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400746 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400747 // Adjust block size to byte size
748 Size = blocks * 1024ULL;
749 fclose(fp);
750 return true;
751 }
752 }
753 fclose(fp);
754 return false;
755}
756
Dees_Troy5bf43922012-09-07 16:07:55 -0400757bool TWPartition::Is_Mounted(void) {
758 if (!Can_Be_Mounted)
759 return false;
760
761 struct stat st1, st2;
762 string test_path;
763
764 // Check to see if the mount point directory exists
765 test_path = Mount_Point + "/.";
766 if (stat(test_path.c_str(), &st1) != 0) return false;
767
768 // Check to see if the directory above the mount point exists
769 test_path = Mount_Point + "/../.";
770 if (stat(test_path.c_str(), &st2) != 0) return false;
771
772 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
773 int ret = (st1.st_dev != st2.st_dev) ? true : false;
774
775 return ret;
776}
777
778bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000779 int exfat_mounted = 0;
780
Dees_Troy5bf43922012-09-07 16:07:55 -0400781 if (Is_Mounted()) {
782 return true;
783 } else if (!Can_Be_Mounted) {
784 return false;
785 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400786
787 Find_Actual_Block_Device();
788
789 // Check the current file system before mounting
790 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000791 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000792 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 +0000793 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000794 string result;
795 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000796 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000797 Current_File_System = "vfat";
798 } else {
799#ifdef TW_NO_EXFAT_FUSE
800 UnMount(false);
801 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
802 // Some kernels let us mount vfat as exfat which doesn't work out too well
803#else
804 exfat_mounted = 1;
805#endif
806 }
807 }
Dees_Troy22042032012-12-18 21:23:08 +0000808 if (Fstab_File_System == "yaffs2") {
809 // mount an MTD partition as a YAFFS2 filesystem.
810 mtd_scan_partitions();
811 const MtdPartition* partition;
812 partition = mtd_find_partition_by_name(MTD_Name.c_str());
813 if (partition == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000814 LOGERR("Failed to find '%s' partition to mount at '%s'\n",
Dees_Troy22042032012-12-18 21:23:08 +0000815 MTD_Name.c_str(), Mount_Point.c_str());
816 return false;
817 }
818 if (mtd_mount_partition(partition, Mount_Point.c_str(), Fstab_File_System.c_str(), 0)) {
819 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000820 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
Dees_Troy22042032012-12-18 21:23:08 +0000821 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000822 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
Dees_Troy22042032012-12-18 21:23:08 +0000823 return false;
824 } else
825 return true;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000826 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
827#ifdef TW_NO_EXFAT_FUSE
828 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000829 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000830 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000831 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000832 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000833 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000834 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
835 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 +0000836 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000837 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000838 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000839#endif
840 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000841 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000842 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000843 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
844 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 +0000845 return false;
846#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000847 }
848#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000849 }
850#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
851 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
852 MetaEcfsFile += "/.MetaEcfsFile";
853 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
854 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
855 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000856 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000857 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000858 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000859 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000860 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000861 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400862 }
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000863 } else {
864 Is_Decrypted = false;
865 }
866#endif
867 if (Removable)
868 Update_Size(Display_Error);
869
870 if (!Symlink_Mount_Point.empty()) {
871 string Command, Result;
872 Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
873 TWFunc::Exec_Cmd(Command, Result);
Dees_Troy5bf43922012-09-07 16:07:55 -0400874 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400875 return true;
876}
877
878bool TWPartition::UnMount(bool Display_Error) {
879 if (Is_Mounted()) {
880 int never_unmount_system;
881
882 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
883 if (never_unmount_system == 1 && Mount_Point == "/system")
884 return true; // Never unmount system if you're not supposed to unmount it
885
Dees_Troyc8bafa12013-01-10 15:43:00 +0000886#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
887 if (EcryptFS_Password.size() > 0) {
888 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
889 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000890 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000891 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000892 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000893 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000894 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000895 }
896 }
897#endif
898
Dees_Troy38bd7602012-09-14 13:33:53 -0400899 if (!Symlink_Mount_Point.empty())
900 umount(Symlink_Mount_Point.c_str());
901
Dees_Troyb05ddee2013-01-28 20:24:50 +0000902 umount(Mount_Point.c_str());
903 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400904 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000905 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400906 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000907 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400908 return false;
909 } else
910 return true;
911 } else {
912 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400913 }
914}
915
Gary Peck43acadf2012-11-21 21:19:01 -0800916bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +0000917 bool wiped = false, update_crypt = false;
918 int check;
919 string Layout_Filename = Mount_Point + "/.layout_version";
920
Dees_Troy38bd7602012-09-14 13:33:53 -0400921 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000922 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400923 return false;
924 }
925
Dees_Troyc51f1f92012-09-20 15:32:13 -0400926 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +0000927 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400928
Dees_Troyce675462013-01-09 19:48:21 +0000929#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
930 if (Mount_Point == "/data" && Mount(false)) {
931 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
932 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
933 }
934#endif
935
Dees_Troy16c2b312013-01-15 16:51:18 +0000936 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
937 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
938 else
939 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -0400940
Dees_Troy16c2b312013-01-15 16:51:18 +0000941 if (Has_Data_Media) {
942 wiped = Wipe_Data_Without_Wiping_Media();
943 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -0800944
Dees_Troy16c2b312013-01-15 16:51:18 +0000945 DataManager::GetValue(TW_RM_RF_VAR, check);
946
947 if (check)
948 wiped = Wipe_RMRF();
949 else if (New_File_System == "ext4")
950 wiped = Wipe_EXT4();
951 else if (New_File_System == "ext2" || New_File_System == "ext3")
952 wiped = Wipe_EXT23(New_File_System);
953 else if (New_File_System == "vfat")
954 wiped = Wipe_FAT();
955 else if (New_File_System == "exfat")
956 wiped = Wipe_EXFAT();
957 else if (New_File_System == "yaffs2")
958 wiped = Wipe_MTD();
959 else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000960 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 +0000961 unlink("/.layout_version");
962 return false;
963 }
964 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800965 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400966
Gary Pecke8bc5d72012-12-21 06:45:25 -0800967 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +0000968#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
969 if (Mount_Point == "/data" && Mount(false)) {
970 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
971 Make_Dir("/data/system", true);
972 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
973 }
974 }
975#endif
Dees_Troy16c2b312013-01-15 16:51:18 +0000976
Dees_Troy1c1ac442013-01-17 21:42:14 +0000977 if (Mount_Point == "/cache")
978 DataManager::Output_Version();
979
Dees_Troy16c2b312013-01-15 16:51:18 +0000980 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
981 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
982
983 if (update_crypt) {
984 Setup_File_System(false);
985 if (Is_Encrypted && !Is_Decrypted) {
986 // just wiped an encrypted partition back to its unencrypted state
987 Is_Encrypted = false;
988 Is_Decrypted = false;
989 Decrypted_Block_Device = "";
990 if (Mount_Point == "/data") {
991 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
992 DataManager::SetValue(TW_IS_DECRYPTED, 0);
993 }
Gary Pecke8bc5d72012-12-21 06:45:25 -0800994 }
995 }
996 }
997 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -0400998}
999
Gary Peck43acadf2012-11-21 21:19:01 -08001000bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001001 if (Is_File_System(Current_File_System))
1002 return Wipe(Current_File_System);
1003 else
1004 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001005}
1006
Dees_Troye58d5262012-09-21 12:27:57 -04001007bool TWPartition::Wipe_AndSec(void) {
1008 if (!Has_Android_Secure)
1009 return false;
1010
Dees_Troye58d5262012-09-21 12:27:57 -04001011 if (!Mount(true))
1012 return false;
1013
Dees_Troy2673cec2013-04-02 20:22:16 +00001014 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001015 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Dees_Troye58d5262012-09-21 12:27:57 -04001016 return true;
1017}
1018
Dees_Troy51a0e822012-09-05 15:24:24 -04001019bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001020 if (Backup_Method == FILES)
1021 return Backup_Tar(backup_folder);
1022 else if (Backup_Method == DD)
1023 return Backup_DD(backup_folder);
1024 else if (Backup_Method == FLASH_UTILS)
1025 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001026 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001027 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001028}
1029
Dees_Troy43d8b002012-09-17 16:00:01 -04001030bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001031 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001032 char split_filename[512];
1033 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001034 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001035
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001036 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001037 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001038 if (!TWFunc::Path_Exists(Full_Filename)) {
1039 // This is a split archive, we presume
1040 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001041 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001042 md5file = split_filename;
1043 md5file += ".md5";
1044 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001045 LOGERR("No md5 file found for '%s'.\n", split_filename);
1046 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001047 return false;
1048 }
1049 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001050 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001051 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001052 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001053 return false;
1054 }
1055 index++;
1056 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001057 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001058 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001059 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001060 } else {
1061 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001062 md5file = Full_Filename + ".md5";
1063 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001064 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1065 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001066 return false;
1067 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001068 md5sum.setfn(Full_Filename);
1069 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001070 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001071 return false;
1072 } else
1073 return true;
1074 }
1075 return false;
1076}
1077
Dees_Troy51a0e822012-09-05 15:24:24 -04001078bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001079 size_t first_period, second_period;
1080 string Restore_File_System;
1081
1082 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001083 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001084
1085 // Parse backup filename to extract the file system before wiping
1086 first_period = Backup_FileName.find(".");
1087 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001088 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001089 return false;
1090 }
1091 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1092 second_period = Restore_File_System.find(".");
1093 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001094 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001095 return false;
1096 }
1097 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001098 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001099
1100 if (Is_File_System(Restore_File_System))
1101 return Restore_Tar(restore_folder, Restore_File_System);
1102 else if (Is_Image(Restore_File_System)) {
1103 if (Restore_File_System == "emmc")
1104 return Restore_DD(restore_folder);
1105 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1106 return Restore_Flash_Image(restore_folder);
1107 }
1108
Dees_Troy2673cec2013-04-02 20:22:16 +00001109 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001110 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001111}
1112
1113string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001114 if (Backup_Method == NONE)
1115 return "none";
1116 else if (Backup_Method == FILES)
1117 return "files";
1118 else if (Backup_Method == DD)
1119 return "dd";
1120 else if (Backup_Method == FLASH_UTILS)
1121 return "flash_utils";
1122 else
1123 return "undefined";
1124 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001125}
1126
1127bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001128 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001129 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001130 return 1;
1131}
1132
1133bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001134 bool Save_Data_Media = Has_Data_Media;
1135
1136 if (!UnMount(true))
1137 return false;
1138
Dees_Troy38bd7602012-09-14 13:33:53 -04001139 Has_Data_Media = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001140 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001141 Has_Data_Media = Save_Data_Media;
1142 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1143 Recreate_Media_Folder();
1144 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001145 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001146 return true;
1147 } else {
1148 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001149 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001150 return false;
1151 }
1152 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001153}
1154
1155void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001156 const char* type;
1157 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001158
Dees_Troy68cab492012-12-12 19:29:35 +00001159 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1160 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001161
Dees_Troy38bd7602012-09-14 13:33:53 -04001162 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001163 if (!Is_Present)
1164 return;
Dees_Troy51127312012-09-08 13:08:49 -04001165
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001166 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1167 if (blkid_do_fullprobe(pr)) {
1168 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001169 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001170 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001171 }
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001172 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1173 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001174 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001175 return;
1176 }
1177 Current_File_System = type;
Dees_Troy51a0e822012-09-05 15:24:24 -04001178 return;
1179}
1180
Gary Peck43acadf2012-11-21 21:19:01 -08001181bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001182 if (!UnMount(true))
1183 return false;
1184
Dees_Troy43d8b002012-09-17 16:00:01 -04001185 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001186 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001187
Dees_Troy2673cec2013-04-02 20:22:16 +00001188 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001189 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001190 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001191 LOGINFO("mke2fs command: %s\n", command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001192 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001193 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001194 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001195 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001196 return true;
1197 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001198 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001199 return false;
1200 }
1201 } else
1202 return Wipe_RMRF();
1203
1204 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001205}
1206
1207bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001208 if (!UnMount(true))
1209 return false;
1210
Dees_Troy43d8b002012-09-17 16:00:01 -04001211 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001212 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001213
Dees_Troy2673cec2013-04-02 20:22:16 +00001214 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001215 Find_Actual_Block_Device();
1216 Command = "make_ext4fs";
1217 if (!Is_Decrypted && Length != 0) {
1218 // Only use length if we're not decrypted
1219 char len[32];
1220 sprintf(len, "%i", Length);
1221 Command += " -l ";
1222 Command += len;
1223 }
1224 Command += " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001225 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001226 if (TWFunc::Exec_Cmd(Command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001227 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001228 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001229 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001230 return true;
1231 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001232 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001233 return false;
1234 }
1235 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001236 return Wipe_EXT23("ext4");
Dees_Troy38bd7602012-09-14 13:33:53 -04001237
1238 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001239}
1240
1241bool TWPartition::Wipe_FAT() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001242 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001243
Dees_Troy43d8b002012-09-17 16:00:01 -04001244 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001245 if (!UnMount(true))
1246 return false;
1247
Dees_Troy2673cec2013-04-02 20:22:16 +00001248 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001249 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001250 command = "mkdosfs " + Actual_Block_Device;
1251 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001252 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001253 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001254 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001255 return true;
1256 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001257 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001258 return false;
1259 }
1260 return true;
1261 }
1262 else
1263 return Wipe_RMRF();
1264
1265 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001266}
1267
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001268bool TWPartition::Wipe_EXFAT() {
1269 string command, result;
1270
1271 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1272 if (!UnMount(true))
1273 return false;
1274
Dees_Troy2673cec2013-04-02 20:22:16 +00001275 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001276 Find_Actual_Block_Device();
1277 command = "mkexfatfs " + Actual_Block_Device;
1278 if (TWFunc::Exec_Cmd(command, result) == 0) {
1279 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001280 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001281 return true;
1282 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001283 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001284 return false;
1285 }
1286 return true;
1287 }
1288 return false;
1289}
1290
Dees_Troy38bd7602012-09-14 13:33:53 -04001291bool TWPartition::Wipe_MTD() {
1292 if (!UnMount(true))
1293 return false;
1294
Dees_Troy2673cec2013-04-02 20:22:16 +00001295 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001296
1297 mtd_scan_partitions();
1298 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1299 if (mtd == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001300 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001301 return false;
1302 }
1303
1304 MtdWriteContext* ctx = mtd_write_partition(mtd);
1305 if (ctx == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001306 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001307 return false;
1308 }
1309 if (mtd_erase_blocks(ctx, -1) == -1) {
1310 mtd_write_close(ctx);
Dees_Troy2673cec2013-04-02 20:22:16 +00001311 LOGERR("Failed to format '%s'", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001312 return false;
1313 }
1314 if (mtd_write_close(ctx) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001315 LOGERR("Failed to close '%s'", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001316 return false;
1317 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001318 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001319 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001320 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001321 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001322}
1323
1324bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001325 if (!Mount(true))
1326 return false;
1327
Dees_Troy2673cec2013-04-02 20:22:16 +00001328 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001329 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001330 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001331 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001332}
1333
1334bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001335 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001336
1337 // This handles wiping data on devices with "sdcard" in /data/media
1338 if (!Mount(true))
1339 return false;
1340
Dees_Troy2673cec2013-04-02 20:22:16 +00001341 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001342
1343 DIR* d;
1344 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001345 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001346 struct dirent* de;
1347 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001348 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1349 // The media folder is the "internal sdcard"
1350 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
1351 // the media folder for multi-user.
1352 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001353
1354 dir = "/data/";
1355 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001356 if (de->d_type == DT_DIR) {
1357 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001358 } 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 +00001359 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001360 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001361 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001362 }
1363 closedir(d);
Dees_Troy2673cec2013-04-02 20:22:16 +00001364 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001365 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001366 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001367 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001368 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001369}
1370
1371bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001372 char back_name[255], split_index[5];
1373 string Full_FileName, Split_FileName, Tar_Args, Command;
1374 int use_compression, index, backup_count;
1375 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001376 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001377 twrpTar tar;
1378 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001379
Dees_Troy43d8b002012-09-17 16:00:01 -04001380 if (!Mount(true))
1381 return false;
1382
Dees_Troya13d74f2013-03-24 08:54:55 -05001383 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001384 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001385
1386 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy43d8b002012-09-17 16:00:01 -04001387
1388 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1389 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001390 Full_FileName = backup_folder + "/" + Backup_FileName;
1391 if (Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001392 // This backup needs to be split into multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001393 gui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001394 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001395 tar.setdir(back_name);
1396 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001397 backup_count = tar.splitArchiveFork();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001398 if (backup_count == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001399 LOGERR("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001400 return false;
1401 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001402 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001403 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001404 Full_FileName = backup_folder + "/" + Backup_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001405 if (use_compression) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001406 tar.setdir(Backup_Path);
1407 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001408 if (tar.createTarGZFork() != 0)
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001409 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001410 string gzname = Full_FileName + ".gz";
1411 rename(gzname.c_str(), Full_FileName.c_str());
1412 }
1413 else {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001414 tar.setdir(Backup_Path);
1415 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001416 if (tar.createTarFork() != 0)
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001417 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001418 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001419 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001420 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001421 return false;
1422 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001423 }
1424 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001425}
1426
1427bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001428 char back_name[255], backup_size[32];
1429 string Full_FileName, Command, result, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001430 int use_compression;
1431
igoriok87e3d932013-01-31 21:03:53 +02001432 sprintf(backup_size, "%llu", Backup_Size);
1433 DD_BS = backup_size;
1434
Dees_Troyb46a6842012-09-25 11:06:46 -04001435 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001436 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001437
1438 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1439 Backup_FileName = back_name;
1440
1441 Full_FileName = backup_folder + "/" + Backup_FileName;
1442
igoriok87e3d932013-01-31 21:03:53 +02001443 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001444 LOGINFO("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001445 TWFunc::Exec_Cmd(Command, result);
Dees_Troyc154ac22012-10-12 15:36:47 -04001446 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001447 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001448 return false;
1449 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001450 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001451}
1452
1453bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001454 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001455 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001456 int use_compression;
1457
Dees_Troyb46a6842012-09-25 11:06:46 -04001458 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001459 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001460
1461 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1462 Backup_FileName = back_name;
1463
1464 Full_FileName = backup_folder + "/" + Backup_FileName;
1465
1466 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001467 LOGINFO("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001468 TWFunc::Exec_Cmd(Command, result);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001469 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1470 // 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 +00001471 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001472 return false;
1473 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001474 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001475}
1476
Gary Peck43acadf2012-11-21 21:19:01 -08001477bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1478 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001479 int index = 0;
1480 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001481
Dees_Troye58d5262012-09-21 12:27:57 -04001482 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001483 if (!Wipe_AndSec())
1484 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001485 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001486 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001487 if (!Wipe(Restore_File_System))
1488 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001489 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001490 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001491 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001492
1493 if (!Mount(true))
1494 return false;
1495
Dees_Troy4a2a1262012-09-18 09:33:47 -04001496 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001497 if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001498 if (!TWFunc::Path_Exists(Full_FileName)) {
1499 // Backup is multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001500 LOGINFO("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001501 sprintf(split_index, "%03i", index);
1502 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001503 while (TWFunc::Path_Exists(Full_FileName)) {
1504 index++;
Dees_Troy2673cec2013-04-02 20:22:16 +00001505 gui_print("Restoring archive %i...\n", index);
1506 LOGINFO("Restoring '%s'...\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001507 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001508 tar.setdir("/");
1509 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001510 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001511 return false;
1512 sprintf(split_index, "%03i", index);
1513 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1514 }
1515 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001516 LOGERR("Error locating restore file: '%s'\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001517 return false;
1518 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001519 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001520 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001521 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001522 tar.setdir(Backup_Path);
1523 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001524 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001525 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001526 }
1527 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001528}
1529
1530bool TWPartition::Restore_DD(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001531 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001532
Dees_Troyda8b55a2012-12-12 19:18:30 +00001533 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001534 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001535
1536 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001537 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001538 return false;
1539 }
1540 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1541 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001542 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001543 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1544 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1545 return false;
1546 }
1547
Dees_Troy2673cec2013-04-02 20:22:16 +00001548 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001549 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001550 LOGINFO("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001551 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001552 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001553}
1554
1555bool TWPartition::Restore_Flash_Image(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001556 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001557
Dees_Troy2673cec2013-04-02 20:22:16 +00001558 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001559 Full_FileName = restore_folder + "/" + Backup_FileName;
1560 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1561 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001562 LOGINFO("Erase command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001563 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001564 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001565 LOGINFO("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001566 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001567 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001568}
Dees_Troy5bf43922012-09-07 16:07:55 -04001569
1570bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001571 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001572
Dees_Troyab10ee22012-09-21 14:27:30 -04001573 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001574 return false;
1575
Dees_Troy0550cfb2012-10-13 11:56:13 -04001576 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001577 if (Removable || Is_Encrypted) {
1578 if (!Mount(false))
1579 return true;
1580 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001581 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001582
1583 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001584 if (!ret || Size == 0) {
1585 if (!Get_Size_Via_df(Display_Error)) {
1586 if (!Was_Already_Mounted)
1587 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001588 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001589 }
1590 }
Dees_Troy51127312012-09-08 13:08:49 -04001591
Dees_Troy5bf43922012-09-07 16:07:55 -04001592 if (Has_Data_Media) {
1593 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001594 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001595 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1596 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001597 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001598 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001599 int bak = (int)(Backup_Size / 1048576LLU);
1600 int total = (int)(Size / 1048576LLU);
1601 int us = (int)(Used / 1048576LLU);
1602 int fre = (int)(Free / 1048576LLU);
1603 int datmed = (int)(data_media_used / 1048576LLU);
Dees_Troy2673cec2013-04-02 20:22:16 +00001604 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 -04001605 } else {
1606 if (!Was_Already_Mounted)
1607 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001608 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001609 }
Dees_Troye58d5262012-09-21 12:27:57 -04001610 } else if (Has_Android_Secure) {
1611 if (Mount(Display_Error))
1612 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001613 else {
1614 if (!Was_Already_Mounted)
1615 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001616 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001617 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001618 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001619 if (!Was_Already_Mounted)
1620 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001621 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001622}
Dees_Troy38bd7602012-09-14 13:33:53 -04001623
1624void TWPartition::Find_Actual_Block_Device(void) {
1625 if (Is_Decrypted) {
1626 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001627 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001628 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001629 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001630 Is_Present = true;
1631 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001632 return;
1633 }
1634 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001635 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001636 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001637 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001638 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001639 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001640 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001641}
1642
1643void TWPartition::Recreate_Media_Folder(void) {
1644 string Command;
1645
1646 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001647 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001648 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001649 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00001650 LOGINFO("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001651 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1652 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001653 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001654}
Dees_Troye58d5262012-09-21 12:27:57 -04001655
1656void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001657 if (!Has_Android_Secure)
1658 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00001659 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001660 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001661 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001662 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001663 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001664 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1665 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1666 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001667 }
1668}