blob: 7eb2ae5fbfdfb127e81f42214c01290fd13719a5 [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_Troy5bf43922012-09-07 16:07:55 -0400127 index = Mount_Point.size();
128 while (index < line_len) {
129 while (index < line_len && full_line[index] == '\0')
130 index++;
131 if (index >= line_len)
132 continue;
133 ptr = full_line + index;
134 if (item_index == 0) {
135 // File System
136 Fstab_File_System = ptr;
137 Current_File_System = ptr;
138 item_index++;
139 } else if (item_index == 1) {
140 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400141 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400142 MTD_Name = ptr;
143 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400144 } else if (Fstab_File_System == "bml") {
145 if (Mount_Point == "/boot")
146 MTD_Name = "boot";
147 else if (Mount_Point == "/recovery")
148 MTD_Name = "recovery";
149 Primary_Block_Device = ptr;
150 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000151 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 -0400152 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400153 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000154 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400155 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000156 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400157 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400158 } else {
159 Primary_Block_Device = ptr;
160 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400161 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400162 item_index++;
163 } else if (item_index > 1) {
164 if (*ptr == '/') {
165 // Alternate Block Device
166 Alternate_Block_Device = ptr;
167 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
168 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
169 // Partition length
170 ptr += 7;
171 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400172 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
173 // Custom flags, save for later so that new values aren't overwritten by defaults
174 ptr += 6;
175 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000176 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400177 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
178 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400179 } else {
180 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000181 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400182 }
183 }
184 while (index < line_len && full_line[index] != '\0')
185 index++;
186 }
187
188 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
189 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000190 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400191 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000192 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400193 return 0;
194 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400195 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400196 Setup_File_System(Display_Error);
197 if (Mount_Point == "/system") {
198 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500199 Backup_Display_Name = Display_Name;
200 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400201 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500202 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400203 } else if (Mount_Point == "/data") {
204 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500205 Backup_Display_Name = Display_Name;
206 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400207 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400208 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500209 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400210#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troya13d74f2013-03-24 08:54:55 -0500211 Storage_Name = "Internal Storage";
Dees_Troy5bf43922012-09-07 16:07:55 -0400212 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400213 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500214 Is_Settings_Storage = true;
Dees_Troy51127312012-09-08 13:08:49 -0400215 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000216 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400217 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
218 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400219 Symlink_Mount_Point = "/emmc";
220 } else {
221 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400222 Symlink_Mount_Point = "/sdcard";
223 }
Dees_Troy16b74352012-11-14 22:27:31 +0000224 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
225 Storage_Path = "/data/media/0";
226 Symlink_Path = Storage_Path;
227 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
228 UnMount(true);
229 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400230#endif
231#ifdef TW_INCLUDE_CRYPTO
232 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400233 char crypto_blkdev[255];
234 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
235 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400236 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400237 DataManager::SetValue(TW_IS_DECRYPTED, 1);
238 Is_Encrypted = true;
239 Is_Decrypted = true;
240 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000241 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400242 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400243 Is_Encrypted = true;
244 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800245 Can_Be_Mounted = false;
246 Current_File_System = "emmc";
247 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400248 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
249 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
250 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800251 } else {
252 // Filesystem is not encrypted and the mount
253 // succeeded, so get it back to the original
254 // unmounted state
255 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400256 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400257 #ifdef RECOVERY_SDCARD_ON_DATA
258 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
259 Recreate_Media_Folder();
260 #endif
261#else
262 #ifdef RECOVERY_SDCARD_ON_DATA
263 Recreate_Media_Folder();
264 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400265#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400266 } else if (Mount_Point == "/cache") {
267 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500268 Backup_Display_Name = Display_Name;
269 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400270 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400271 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500272 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400273 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000274 LOGINFO("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500275 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
276 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400277 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400278 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400279 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400280 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500281 Backup_Display_Name = Display_Name;
282 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400283 Is_SubPartition = true;
284 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400285 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500286 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400287 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400288 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400289 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500290 Backup_Display_Name = Display_Name;
291 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400292 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400293 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500294 Can_Be_Backed_Up = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400295 } else if (Mount_Point == "/boot") {
296 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500297 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400298 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500299 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400300 }
301#ifdef TW_EXTERNAL_STORAGE_PATH
302 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
303 Is_Storage = true;
304 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400305 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500306 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400307#else
308 if (Mount_Point == "/sdcard") {
309 Is_Storage = true;
310 Storage_Path = "/sdcard";
Dees_Troyc51f1f92012-09-20 15:32:13 -0400311 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500312 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400313#ifndef RECOVERY_SDCARD_ON_DATA
314 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400315 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400316#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400317#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000318 }
Dees_Troy8170a922012-09-18 15:40:25 -0400319#ifdef TW_INTERNAL_STORAGE_PATH
320 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
321 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500322 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400323 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500324 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400325#ifndef RECOVERY_SDCARD_ON_DATA
326 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400327 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400328#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400329 }
330#else
331 if (Mount_Point == "/emmc") {
332 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500333 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400334 Storage_Path = "/emmc";
Dees_Troya13d74f2013-03-24 08:54:55 -0500335 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400336#ifndef RECOVERY_SDCARD_ON_DATA
337 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400338 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400339#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400340 }
341#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400342 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400343 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400344 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500345 if (Mount_Point == "/boot") {
346 Display_Name = "Boot";
347 Backup_Display_Name = Display_Name;
348 Can_Be_Backed_Up = true;
349 } else if (Mount_Point == "/recovery") {
350 Display_Name = "Recovery";
351 Backup_Display_Name = Display_Name;
352 Can_Be_Backed_Up = true;
353 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400354 }
355
Dees_Troy51127312012-09-08 13:08:49 -0400356 // Process any custom flags
357 if (Flags.size() > 0)
358 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400359 return true;
360}
361
362bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
363 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500364 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400365 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500366 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400367
368 strcpy(flags, Flags.c_str());
369 flags_len = Flags.size();
370 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500371 if (flags[index] == 34)
372 skip = !skip;
373 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400374 flags[index] = '\0';
375 }
376
377 index = 0;
378 while (index < flags_len) {
379 while (index < flags_len && flags[index] == '\0')
380 index++;
381 if (index >= flags_len)
382 continue;
383 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500384 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400385 if (strcmp(ptr, "removable") == 0) {
386 Removable = true;
387 } else if (strcmp(ptr, "storage") == 0) {
388 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500389 } else if (strcmp(ptr, "settingsstorage") == 0) {
390 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400391 } else if (strcmp(ptr, "canbewiped") == 0) {
392 Can_Be_Wiped = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500393 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
394 ptr += 7;
395 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
396 Can_Be_Backed_Up = true;
397 else
398 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400399 } else if (strcmp(ptr, "wipeingui") == 0) {
400 Can_Be_Wiped = true;
401 Wipe_Available_in_GUI = true;
402 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
403 Can_Be_Wiped = true;
404 Wipe_Available_in_GUI = true;
405 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500406 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000407 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400408 Is_SubPartition = true;
409 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000410 } else if (strcmp(ptr, "ignoreblkid") == 0) {
411 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000412 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
413 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500414 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400415 ptr += 8;
416 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500417 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
418 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400419 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500420 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400421 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500422 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
423 Display_Name.resize(Display_Name.size() - 1);
424 }
425 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
426 has_storage_name = true;
427 ptr += 11;
428 if (*ptr == '\"') ptr++;
429 Storage_Name = ptr;
430 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
431 Storage_Name.resize(Storage_Name.size() - 1);
432 }
433 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
434 has_backup_name = true;
435 ptr += 10;
436 if (*ptr == '\"') ptr++;
437 Backup_Display_Name = ptr;
438 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
439 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
440 }
441 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400442 ptr += 10;
443 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500444 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400445 ptr += 7;
446 Length = atoi(ptr);
447 } else {
448 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000449 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400450 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000451 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400452 }
453 while (index < flags_len && flags[index] != '\0')
454 index++;
455 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500456 if (has_display_name && !has_storage_name)
457 Storage_Name = Display_Name;
458 if (!has_display_name && has_storage_name)
459 Display_Name = Storage_Name;
460 if (has_display_name && !has_backup_name)
461 Backup_Display_Name = Display_Name;
462 if (!has_display_name && has_backup_name)
463 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400464 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400465}
466
Dees_Troy5bf43922012-09-07 16:07:55 -0400467bool TWPartition::Is_File_System(string File_System) {
468 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400469 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400470 File_System == "ext4" ||
471 File_System == "vfat" ||
472 File_System == "ntfs" ||
473 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500474 File_System == "exfat" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400475 File_System == "auto")
476 return true;
477 else
478 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400479}
480
Dees_Troy5bf43922012-09-07 16:07:55 -0400481bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400482 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400483 return true;
484 else
485 return false;
486}
487
Dees_Troy51127312012-09-08 13:08:49 -0400488bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400489 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400490 if (mkdir(Path.c_str(), 0777) == -1) {
491 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000492 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400493 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000494 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400495 return false;
496 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000497 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400498 return true;
499 }
500 }
501 return true;
502}
503
Dees_Troy5bf43922012-09-07 16:07:55 -0400504void TWPartition::Setup_File_System(bool Display_Error) {
505 struct statfs st;
506
507 Can_Be_Mounted = true;
508 Can_Be_Wiped = true;
509
Dees_Troy5bf43922012-09-07 16:07:55 -0400510 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400511 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400512 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
513 Backup_Name = Display_Name;
514 Backup_Method = FILES;
515}
516
517void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400518 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
519 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800520 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400521 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800522 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400523 Backup_Method = FLASH_UTILS;
524 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000525 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 -0400526 if (Find_Partition_Size()) {
527 Used = Size;
528 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400529 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400530 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000531 LOGERR("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400532 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000533 LOGINFO("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400534 }
535}
536
Dees_Troye58d5262012-09-21 12:27:57 -0400537void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500538 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400539 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500540 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400541 Has_Android_Secure = true;
542 Symlink_Path = Mount_Point + "/.android_secure";
543 Symlink_Mount_Point = "/and-sec";
544 Backup_Path = Symlink_Mount_Point;
545 Make_Dir("/and-sec", true);
546 Recreate_AndSec_Folder();
547}
548
Dees_Troy5bf43922012-09-07 16:07:55 -0400549void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
550 char device[512], realDevice[512];
551
552 strcpy(device, Block.c_str());
553 memset(realDevice, 0, sizeof(realDevice));
554 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
555 {
556 strcpy(device, realDevice);
557 memset(realDevice, 0, sizeof(realDevice));
558 }
559
560 if (device[0] != '/') {
561 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000562 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400563 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000564 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400565 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400566 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400567 Block = device;
568 return;
569 }
570}
571
Dees_Troy8e337f32012-10-13 22:07:49 -0400572void TWPartition::Mount_Storage_Retry(void) {
573 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500574 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400575 int retry_count = 5;
576 while (retry_count > 0 && !Mount(false)) {
577 usleep(500000);
578 retry_count--;
579 }
580 Mount(true);
581 }
582}
583
Dees_Troy38bd7602012-09-14 13:33:53 -0400584bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
585 FILE *fp = NULL;
586 char line[255];
587
588 fp = fopen("/proc/mtd", "rt");
589 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000590 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400591 return false;
592 }
593
594 while (fgets(line, sizeof(line), fp) != NULL)
595 {
596 char device[32], label[32];
597 unsigned long size = 0;
598 char* fstype = NULL;
599 int deviceId;
600
601 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
602
603 // Skip header and blank lines
604 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
605 continue;
606
607 // Strip off the trailing " from the label
608 label[strlen(label)-1] = '\0';
609
610 if (strcmp(label, MTD_Name.c_str()) == 0) {
611 // We found our device
612 // Strip off the trailing : from the device
613 device[strlen(device)-1] = '\0';
614 if (sscanf(device,"mtd%d", &deviceId) == 1) {
615 sprintf(device, "/dev/block/mtdblock%d", deviceId);
616 Primary_Block_Device = device;
617 }
618 }
619 }
620 fclose(fp);
621
622 return false;
623}
624
Dees_Troy51127312012-09-08 13:08:49 -0400625bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
626 struct statfs st;
627 string Local_Path = Mount_Point + "/.";
628
629 if (!Mount(Display_Error))
630 return false;
631
632 if (statfs(Local_Path.c_str(), &st) != 0) {
633 if (!Removable) {
634 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000635 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400636 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000637 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400638 }
639 return false;
640 }
641 Size = (st.f_blocks * st.f_bsize);
642 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
643 Free = (st.f_bfree * st.f_bsize);
644 Backup_Size = Used;
645 return true;
646}
647
648bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400649 FILE* fp;
650 char command[255], line[512];
651 int include_block = 1;
652 unsigned int min_len;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500653 string result;
Dees_Troy5bf43922012-09-07 16:07:55 -0400654
655 if (!Mount(Display_Error))
656 return false;
657
Dees_Troy38bd7602012-09-14 13:33:53 -0400658 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400659 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500660 TWFunc::Exec_Cmd(command, result);
Dees_Troy51127312012-09-08 13:08:49 -0400661 fp = fopen("/tmp/dfoutput.txt", "rt");
662 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000663 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400664 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400665 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400666
667 while (fgets(line, sizeof(line), fp) != NULL)
668 {
669 unsigned long blocks, used, available;
670 char device[64];
671 char tmpString[64];
672
673 if (strncmp(line, "Filesystem", 10) == 0)
674 continue;
675 if (strlen(line) < min_len) {
676 include_block = 0;
677 continue;
678 }
679 if (include_block) {
680 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
681 } else {
682 // The device block string is so long that the df information is on the next line
683 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400684 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400685 while (tmpString[space_count] == 32)
686 space_count++;
687 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
688 }
689
690 // Adjust block size to byte size
691 Size = blocks * 1024ULL;
692 Used = used * 1024ULL;
693 Free = available * 1024ULL;
694 Backup_Size = Used;
695 }
696 fclose(fp);
697 return true;
698}
699
Dees_Troy5bf43922012-09-07 16:07:55 -0400700bool TWPartition::Find_Partition_Size(void) {
701 FILE* fp;
702 char line[512];
703 string tmpdevice;
704
igoriok87e3d932013-01-31 21:03:53 +0200705 fp = fopen("/proc/dumchar_info", "rt");
706 if (fp != NULL) {
707 while (fgets(line, sizeof(line), fp) != NULL)
708 {
709 char label[32], device[32];
710 unsigned long size = 0;
711
712 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
713
714 // Skip header, annotation and blank lines
715 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
716 continue;
717
718 tmpdevice = "/dev/";
719 tmpdevice += label;
720 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
721 Size = size;
722 fclose(fp);
723 return true;
724 }
725 }
726 }
727
Dees_Troy5bf43922012-09-07 16:07:55 -0400728 // In this case, we'll first get the partitions we care about (with labels)
729 fp = fopen("/proc/partitions", "rt");
730 if (fp == NULL)
731 return false;
732
733 while (fgets(line, sizeof(line), fp) != NULL)
734 {
735 unsigned long major, minor, blocks;
736 char device[512];
737 char tmpString[64];
738
Dees_Troy63c8df72012-09-10 14:02:05 -0400739 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400740 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
741
742 tmpdevice = "/dev/block/";
743 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400744 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400745 // Adjust block size to byte size
746 Size = blocks * 1024ULL;
747 fclose(fp);
748 return true;
749 }
750 }
751 fclose(fp);
752 return false;
753}
754
Dees_Troy5bf43922012-09-07 16:07:55 -0400755bool TWPartition::Is_Mounted(void) {
756 if (!Can_Be_Mounted)
757 return false;
758
759 struct stat st1, st2;
760 string test_path;
761
762 // Check to see if the mount point directory exists
763 test_path = Mount_Point + "/.";
764 if (stat(test_path.c_str(), &st1) != 0) return false;
765
766 // Check to see if the directory above the mount point exists
767 test_path = Mount_Point + "/../.";
768 if (stat(test_path.c_str(), &st2) != 0) return false;
769
770 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
771 int ret = (st1.st_dev != st2.st_dev) ? true : false;
772
773 return ret;
774}
775
776bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000777 int exfat_mounted = 0;
778
Dees_Troy5bf43922012-09-07 16:07:55 -0400779 if (Is_Mounted()) {
780 return true;
781 } else if (!Can_Be_Mounted) {
782 return false;
783 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400784
785 Find_Actual_Block_Device();
786
787 // Check the current file system before mounting
788 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000789 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000790 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 +0000791 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000792 string result;
793 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000794 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000795 Current_File_System = "vfat";
796 } else {
797#ifdef TW_NO_EXFAT_FUSE
798 UnMount(false);
799 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
800 // Some kernels let us mount vfat as exfat which doesn't work out too well
801#else
802 exfat_mounted = 1;
803#endif
804 }
805 }
Dees_Troy22042032012-12-18 21:23:08 +0000806 if (Fstab_File_System == "yaffs2") {
807 // mount an MTD partition as a YAFFS2 filesystem.
808 mtd_scan_partitions();
809 const MtdPartition* partition;
810 partition = mtd_find_partition_by_name(MTD_Name.c_str());
811 if (partition == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000812 LOGERR("Failed to find '%s' partition to mount at '%s'\n",
Dees_Troy22042032012-12-18 21:23:08 +0000813 MTD_Name.c_str(), Mount_Point.c_str());
814 return false;
815 }
816 if (mtd_mount_partition(partition, Mount_Point.c_str(), Fstab_File_System.c_str(), 0)) {
817 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000818 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
Dees_Troy22042032012-12-18 21:23:08 +0000819 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000820 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
Dees_Troy22042032012-12-18 21:23:08 +0000821 return false;
822 } else
823 return true;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000824 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
825#ifdef TW_NO_EXFAT_FUSE
826 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000827 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000828 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000829 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000830 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000831 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000832 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
833 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 +0000834 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000835 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000836 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000837#endif
838 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000839 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000840 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000841 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
842 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 +0000843 return false;
844#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000845 }
846#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000847 }
848#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
849 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
850 MetaEcfsFile += "/.MetaEcfsFile";
851 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
852 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
853 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000854 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000855 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000856 LOGINFO("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("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000859 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400860 }
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000861 } else {
862 Is_Decrypted = false;
863 }
864#endif
865 if (Removable)
866 Update_Size(Display_Error);
867
868 if (!Symlink_Mount_Point.empty()) {
869 string Command, Result;
870 Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
871 TWFunc::Exec_Cmd(Command, Result);
Dees_Troy5bf43922012-09-07 16:07:55 -0400872 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400873 return true;
874}
875
876bool TWPartition::UnMount(bool Display_Error) {
877 if (Is_Mounted()) {
878 int never_unmount_system;
879
880 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
881 if (never_unmount_system == 1 && Mount_Point == "/system")
882 return true; // Never unmount system if you're not supposed to unmount it
883
Dees_Troyc8bafa12013-01-10 15:43:00 +0000884#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
885 if (EcryptFS_Password.size() > 0) {
886 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
887 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000888 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000889 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000890 LOGINFO("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("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000893 }
894 }
895#endif
896
Dees_Troy38bd7602012-09-14 13:33:53 -0400897 if (!Symlink_Mount_Point.empty())
898 umount(Symlink_Mount_Point.c_str());
899
Dees_Troyb05ddee2013-01-28 20:24:50 +0000900 umount(Mount_Point.c_str());
901 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400902 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000903 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400904 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000905 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400906 return false;
907 } else
908 return true;
909 } else {
910 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400911 }
912}
913
Gary Peck43acadf2012-11-21 21:19:01 -0800914bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +0000915 bool wiped = false, update_crypt = false;
916 int check;
917 string Layout_Filename = Mount_Point + "/.layout_version";
918
Dees_Troy38bd7602012-09-14 13:33:53 -0400919 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000920 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400921 return false;
922 }
923
Dees_Troyc51f1f92012-09-20 15:32:13 -0400924 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +0000925 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400926
Dees_Troyce675462013-01-09 19:48:21 +0000927#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
928 if (Mount_Point == "/data" && Mount(false)) {
929 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
930 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
931 }
932#endif
933
Dees_Troy16c2b312013-01-15 16:51:18 +0000934 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
935 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
936 else
937 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -0400938
Dees_Troy16c2b312013-01-15 16:51:18 +0000939 if (Has_Data_Media) {
940 wiped = Wipe_Data_Without_Wiping_Media();
941 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -0800942
Dees_Troy16c2b312013-01-15 16:51:18 +0000943 DataManager::GetValue(TW_RM_RF_VAR, check);
944
945 if (check)
946 wiped = Wipe_RMRF();
947 else if (New_File_System == "ext4")
948 wiped = Wipe_EXT4();
949 else if (New_File_System == "ext2" || New_File_System == "ext3")
950 wiped = Wipe_EXT23(New_File_System);
951 else if (New_File_System == "vfat")
952 wiped = Wipe_FAT();
953 else if (New_File_System == "exfat")
954 wiped = Wipe_EXFAT();
955 else if (New_File_System == "yaffs2")
956 wiped = Wipe_MTD();
957 else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000958 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 +0000959 unlink("/.layout_version");
960 return false;
961 }
962 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800963 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400964
Gary Pecke8bc5d72012-12-21 06:45:25 -0800965 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +0000966#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
967 if (Mount_Point == "/data" && Mount(false)) {
968 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
969 Make_Dir("/data/system", true);
970 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
971 }
972 }
973#endif
Dees_Troy16c2b312013-01-15 16:51:18 +0000974
Dees_Troy1c1ac442013-01-17 21:42:14 +0000975 if (Mount_Point == "/cache")
976 DataManager::Output_Version();
977
Dees_Troy16c2b312013-01-15 16:51:18 +0000978 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
979 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
980
981 if (update_crypt) {
982 Setup_File_System(false);
983 if (Is_Encrypted && !Is_Decrypted) {
984 // just wiped an encrypted partition back to its unencrypted state
985 Is_Encrypted = false;
986 Is_Decrypted = false;
987 Decrypted_Block_Device = "";
988 if (Mount_Point == "/data") {
989 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
990 DataManager::SetValue(TW_IS_DECRYPTED, 0);
991 }
Gary Pecke8bc5d72012-12-21 06:45:25 -0800992 }
993 }
994 }
995 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -0400996}
997
Gary Peck43acadf2012-11-21 21:19:01 -0800998bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -0800999 if (Is_File_System(Current_File_System))
1000 return Wipe(Current_File_System);
1001 else
1002 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001003}
1004
Dees_Troye58d5262012-09-21 12:27:57 -04001005bool TWPartition::Wipe_AndSec(void) {
1006 if (!Has_Android_Secure)
1007 return false;
1008
Dees_Troye58d5262012-09-21 12:27:57 -04001009 if (!Mount(true))
1010 return false;
1011
Dees_Troy2673cec2013-04-02 20:22:16 +00001012 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001013 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Dees_Troye58d5262012-09-21 12:27:57 -04001014 return true;
1015}
1016
Dees_Troy51a0e822012-09-05 15:24:24 -04001017bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001018 if (Backup_Method == FILES)
1019 return Backup_Tar(backup_folder);
1020 else if (Backup_Method == DD)
1021 return Backup_DD(backup_folder);
1022 else if (Backup_Method == FLASH_UTILS)
1023 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001024 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001025 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001026}
1027
Dees_Troy43d8b002012-09-17 16:00:01 -04001028bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001029 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001030 char split_filename[512];
1031 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001032 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001033
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001034 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001035 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001036 if (!TWFunc::Path_Exists(Full_Filename)) {
1037 // This is a split archive, we presume
1038 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001039 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001040 md5file = split_filename;
1041 md5file += ".md5";
1042 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001043 LOGERR("No md5 file found for '%s'.\n", split_filename);
1044 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001045 return false;
1046 }
1047 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001048 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001049 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001050 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001051 return false;
1052 }
1053 index++;
1054 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001055 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001056 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001057 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001058 } else {
1059 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001060 md5file = Full_Filename + ".md5";
1061 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001062 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1063 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001064 return false;
1065 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001066 md5sum.setfn(Full_Filename);
1067 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001068 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001069 return false;
1070 } else
1071 return true;
1072 }
1073 return false;
1074}
1075
Dees_Troy51a0e822012-09-05 15:24:24 -04001076bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001077 size_t first_period, second_period;
1078 string Restore_File_System;
1079
1080 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001081 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001082
1083 // Parse backup filename to extract the file system before wiping
1084 first_period = Backup_FileName.find(".");
1085 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001086 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001087 return false;
1088 }
1089 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1090 second_period = Restore_File_System.find(".");
1091 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001092 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001093 return false;
1094 }
1095 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001096 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001097
1098 if (Is_File_System(Restore_File_System))
1099 return Restore_Tar(restore_folder, Restore_File_System);
1100 else if (Is_Image(Restore_File_System)) {
1101 if (Restore_File_System == "emmc")
1102 return Restore_DD(restore_folder);
1103 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1104 return Restore_Flash_Image(restore_folder);
1105 }
1106
Dees_Troy2673cec2013-04-02 20:22:16 +00001107 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001108 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001109}
1110
1111string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001112 if (Backup_Method == NONE)
1113 return "none";
1114 else if (Backup_Method == FILES)
1115 return "files";
1116 else if (Backup_Method == DD)
1117 return "dd";
1118 else if (Backup_Method == FLASH_UTILS)
1119 return "flash_utils";
1120 else
1121 return "undefined";
1122 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001123}
1124
1125bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001126 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001127 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001128 return 1;
1129}
1130
1131bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001132 bool Save_Data_Media = Has_Data_Media;
1133
1134 if (!UnMount(true))
1135 return false;
1136
Dees_Troy38bd7602012-09-14 13:33:53 -04001137 Has_Data_Media = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001138 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001139 Has_Data_Media = Save_Data_Media;
1140 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1141 Recreate_Media_Folder();
1142 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001143 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001144 return true;
1145 } else {
1146 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001147 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001148 return false;
1149 }
1150 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001151}
1152
1153void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001154 const char* type;
1155 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001156
Dees_Troy68cab492012-12-12 19:29:35 +00001157 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1158 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001159
Dees_Troy38bd7602012-09-14 13:33:53 -04001160 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001161 if (!Is_Present)
1162 return;
Dees_Troy51127312012-09-08 13:08:49 -04001163
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001164 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1165 if (blkid_do_fullprobe(pr)) {
1166 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001167 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001168 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001169 }
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001170 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1171 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001172 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001173 return;
1174 }
1175 Current_File_System = type;
Dees_Troy51a0e822012-09-05 15:24:24 -04001176 return;
1177}
1178
Gary Peck43acadf2012-11-21 21:19:01 -08001179bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001180 if (!UnMount(true))
1181 return false;
1182
Dees_Troy43d8b002012-09-17 16:00:01 -04001183 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001184 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001185
Dees_Troy2673cec2013-04-02 20:22:16 +00001186 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001187 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001188 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001189 LOGINFO("mke2fs command: %s\n", command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001190 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001191 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001192 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001193 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001194 return true;
1195 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001196 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001197 return false;
1198 }
1199 } else
1200 return Wipe_RMRF();
1201
1202 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001203}
1204
1205bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001206 if (!UnMount(true))
1207 return false;
1208
Dees_Troy43d8b002012-09-17 16:00:01 -04001209 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001210 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001211
Dees_Troy2673cec2013-04-02 20:22:16 +00001212 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001213 Find_Actual_Block_Device();
1214 Command = "make_ext4fs";
1215 if (!Is_Decrypted && Length != 0) {
1216 // Only use length if we're not decrypted
1217 char len[32];
1218 sprintf(len, "%i", Length);
1219 Command += " -l ";
1220 Command += len;
1221 }
1222 Command += " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001223 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001224 if (TWFunc::Exec_Cmd(Command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001225 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001226 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001227 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001228 return true;
1229 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001230 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001231 return false;
1232 }
1233 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001234 return Wipe_EXT23("ext4");
Dees_Troy38bd7602012-09-14 13:33:53 -04001235
1236 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001237}
1238
1239bool TWPartition::Wipe_FAT() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001240 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001241
Dees_Troy43d8b002012-09-17 16:00:01 -04001242 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001243 if (!UnMount(true))
1244 return false;
1245
Dees_Troy2673cec2013-04-02 20:22:16 +00001246 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001247 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001248 command = "mkdosfs " + Actual_Block_Device;
1249 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001250 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001251 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001252 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001253 return true;
1254 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001255 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001256 return false;
1257 }
1258 return true;
1259 }
1260 else
1261 return Wipe_RMRF();
1262
1263 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001264}
1265
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001266bool TWPartition::Wipe_EXFAT() {
1267 string command, result;
1268
1269 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1270 if (!UnMount(true))
1271 return false;
1272
Dees_Troy2673cec2013-04-02 20:22:16 +00001273 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001274 Find_Actual_Block_Device();
1275 command = "mkexfatfs " + Actual_Block_Device;
1276 if (TWFunc::Exec_Cmd(command, result) == 0) {
1277 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001278 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001279 return true;
1280 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001281 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001282 return false;
1283 }
1284 return true;
1285 }
1286 return false;
1287}
1288
Dees_Troy38bd7602012-09-14 13:33:53 -04001289bool TWPartition::Wipe_MTD() {
1290 if (!UnMount(true))
1291 return false;
1292
Dees_Troy2673cec2013-04-02 20:22:16 +00001293 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001294
1295 mtd_scan_partitions();
1296 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1297 if (mtd == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001298 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001299 return false;
1300 }
1301
1302 MtdWriteContext* ctx = mtd_write_partition(mtd);
1303 if (ctx == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001304 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001305 return false;
1306 }
1307 if (mtd_erase_blocks(ctx, -1) == -1) {
1308 mtd_write_close(ctx);
Dees_Troy2673cec2013-04-02 20:22:16 +00001309 LOGERR("Failed to format '%s'", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001310 return false;
1311 }
1312 if (mtd_write_close(ctx) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001313 LOGERR("Failed to close '%s'", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001314 return false;
1315 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001316 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001317 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001318 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001319 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001320}
1321
1322bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001323 if (!Mount(true))
1324 return false;
1325
Dees_Troy2673cec2013-04-02 20:22:16 +00001326 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001327 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001328 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001329 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001330}
1331
1332bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001333 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001334
1335 // This handles wiping data on devices with "sdcard" in /data/media
1336 if (!Mount(true))
1337 return false;
1338
Dees_Troy2673cec2013-04-02 20:22:16 +00001339 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001340
1341 DIR* d;
1342 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001343 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001344 struct dirent* de;
1345 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001346 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1347 // The media folder is the "internal sdcard"
1348 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
1349 // the media folder for multi-user.
1350 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001351
1352 dir = "/data/";
1353 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001354 if (de->d_type == DT_DIR) {
1355 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001356 } 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 +00001357 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001358 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001359 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001360 }
1361 closedir(d);
Dees_Troy2673cec2013-04-02 20:22:16 +00001362 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001363 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001364 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001365 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001366 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001367}
1368
1369bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001370 char back_name[255], split_index[5];
1371 string Full_FileName, Split_FileName, Tar_Args, Command;
1372 int use_compression, index, backup_count;
1373 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001374 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001375 twrpTar tar;
1376 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001377
Dees_Troy43d8b002012-09-17 16:00:01 -04001378 if (!Mount(true))
1379 return false;
1380
Dees_Troya13d74f2013-03-24 08:54:55 -05001381 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001382 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001383
1384 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy43d8b002012-09-17 16:00:01 -04001385
1386 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1387 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001388 Full_FileName = backup_folder + "/" + Backup_FileName;
1389 if (Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001390 // This backup needs to be split into multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001391 gui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001392 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001393 tar.setdir(back_name);
1394 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001395 backup_count = tar.splitArchiveFork();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001396 if (backup_count == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001397 LOGERR("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001398 return false;
1399 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001400 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001401 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001402 Full_FileName = backup_folder + "/" + Backup_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001403 if (use_compression) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001404 tar.setdir(Backup_Path);
1405 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001406 if (tar.createTarGZFork() != 0)
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001407 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001408 string gzname = Full_FileName + ".gz";
1409 rename(gzname.c_str(), Full_FileName.c_str());
1410 }
1411 else {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001412 tar.setdir(Backup_Path);
1413 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001414 if (tar.createTarFork() != 0)
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001415 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001416 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001417 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001418 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001419 return false;
1420 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001421 }
1422 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001423}
1424
1425bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001426 char back_name[255], backup_size[32];
1427 string Full_FileName, Command, result, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001428 int use_compression;
1429
igoriok87e3d932013-01-31 21:03:53 +02001430 sprintf(backup_size, "%llu", Backup_Size);
1431 DD_BS = backup_size;
1432
Dees_Troyb46a6842012-09-25 11:06:46 -04001433 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001434 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001435
1436 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1437 Backup_FileName = back_name;
1438
1439 Full_FileName = backup_folder + "/" + Backup_FileName;
1440
igoriok87e3d932013-01-31 21:03:53 +02001441 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001442 LOGINFO("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001443 TWFunc::Exec_Cmd(Command, result);
Dees_Troyc154ac22012-10-12 15:36:47 -04001444 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001445 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001446 return false;
1447 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001448 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001449}
1450
1451bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001452 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001453 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001454 int use_compression;
1455
Dees_Troyb46a6842012-09-25 11:06:46 -04001456 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001457 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001458
1459 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1460 Backup_FileName = back_name;
1461
1462 Full_FileName = backup_folder + "/" + Backup_FileName;
1463
1464 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001465 LOGINFO("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001466 TWFunc::Exec_Cmd(Command, result);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001467 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1468 // 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 +00001469 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001470 return false;
1471 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001472 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001473}
1474
Gary Peck43acadf2012-11-21 21:19:01 -08001475bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1476 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001477 int index = 0;
1478 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001479
Dees_Troye58d5262012-09-21 12:27:57 -04001480 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001481 if (!Wipe_AndSec())
1482 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001483 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001484 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001485 if (!Wipe(Restore_File_System))
1486 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001487 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001488 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001489 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001490
1491 if (!Mount(true))
1492 return false;
1493
Dees_Troy4a2a1262012-09-18 09:33:47 -04001494 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001495 if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001496 if (!TWFunc::Path_Exists(Full_FileName)) {
1497 // Backup is multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001498 LOGINFO("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001499 sprintf(split_index, "%03i", index);
1500 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001501 while (TWFunc::Path_Exists(Full_FileName)) {
1502 index++;
Dees_Troy2673cec2013-04-02 20:22:16 +00001503 gui_print("Restoring archive %i...\n", index);
1504 LOGINFO("Restoring '%s'...\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001505 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001506 tar.setdir("/");
1507 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001508 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001509 return false;
1510 sprintf(split_index, "%03i", index);
1511 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1512 }
1513 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001514 LOGERR("Error locating restore file: '%s'\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001515 return false;
1516 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001517 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001518 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001519 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001520 tar.setdir(Backup_Path);
1521 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001522 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001523 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001524 }
1525 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001526}
1527
1528bool TWPartition::Restore_DD(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001529 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001530
Dees_Troyda8b55a2012-12-12 19:18:30 +00001531 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001532 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001533
1534 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001535 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001536 return false;
1537 }
1538 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1539 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001540 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001541 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1542 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1543 return false;
1544 }
1545
Dees_Troy2673cec2013-04-02 20:22:16 +00001546 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001547 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001548 LOGINFO("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001549 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001550 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001551}
1552
1553bool TWPartition::Restore_Flash_Image(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001554 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001555
Dees_Troy2673cec2013-04-02 20:22:16 +00001556 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001557 Full_FileName = restore_folder + "/" + Backup_FileName;
1558 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1559 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001560 LOGINFO("Erase command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001561 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001562 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001563 LOGINFO("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001564 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001565 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001566}
Dees_Troy5bf43922012-09-07 16:07:55 -04001567
1568bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001569 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001570
Dees_Troyab10ee22012-09-21 14:27:30 -04001571 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001572 return false;
1573
Dees_Troy0550cfb2012-10-13 11:56:13 -04001574 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001575 if (Removable || Is_Encrypted) {
1576 if (!Mount(false))
1577 return true;
1578 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001579 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001580
1581 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001582 if (!ret || Size == 0) {
1583 if (!Get_Size_Via_df(Display_Error)) {
1584 if (!Was_Already_Mounted)
1585 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001586 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001587 }
1588 }
Dees_Troy51127312012-09-08 13:08:49 -04001589
Dees_Troy5bf43922012-09-07 16:07:55 -04001590 if (Has_Data_Media) {
1591 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001592 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001593 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1594 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001595 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001596 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001597 int bak = (int)(Backup_Size / 1048576LLU);
1598 int total = (int)(Size / 1048576LLU);
1599 int us = (int)(Used / 1048576LLU);
1600 int fre = (int)(Free / 1048576LLU);
1601 int datmed = (int)(data_media_used / 1048576LLU);
Dees_Troy2673cec2013-04-02 20:22:16 +00001602 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 -04001603 } else {
1604 if (!Was_Already_Mounted)
1605 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001606 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001607 }
Dees_Troye58d5262012-09-21 12:27:57 -04001608 } else if (Has_Android_Secure) {
1609 if (Mount(Display_Error))
1610 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001611 else {
1612 if (!Was_Already_Mounted)
1613 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001614 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001615 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001616 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001617 if (!Was_Already_Mounted)
1618 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001619 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001620}
Dees_Troy38bd7602012-09-14 13:33:53 -04001621
1622void TWPartition::Find_Actual_Block_Device(void) {
1623 if (Is_Decrypted) {
1624 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001625 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001626 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001627 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001628 Is_Present = true;
1629 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001630 return;
1631 }
1632 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001633 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001634 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001635 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001636 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001637 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001638 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001639}
1640
1641void TWPartition::Recreate_Media_Folder(void) {
1642 string Command;
1643
1644 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001645 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001646 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001647 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00001648 LOGINFO("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001649 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1650 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001651 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001652}
Dees_Troye58d5262012-09-21 12:27:57 -04001653
1654void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001655 if (!Has_Android_Secure)
1656 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00001657 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001658 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001659 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001660 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001661 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001662 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1663 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1664 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001665 }
1666}