blob: 7cf0ab3fb1a04dcb4c3a01d81bfb8b5fa4cf43ec [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/* Partition class for TWRP
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 *
17 * The code was written from scratch by Dees_Troy dees_troy at
18 * yahoo
19 *
20 * Copyright (c) 2012
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040028#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040029#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040030#include <dirent.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050031#include <iostream>
32#include <sstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Dees_Troy657c3092012-09-10 20:32:10 -040034#ifdef TW_INCLUDE_CRYPTO
35 #include "cutils/properties.h"
36#endif
37
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050038#include "libblkid/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000040#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040041#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040042#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050044#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050045#include "twrpTar.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040046extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040047 #include "mtdutils/mtdutils.h"
48 #include "mtdutils/mounts.h"
Dees_Troy85f44ed2013-01-09 18:42:36 +000049#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
50 #include "crypto/libcrypt_samsung/include/libcrypt_samsung.h"
51#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040052}
Dees_Troy51a0e822012-09-05 15:24:24 -040053
bigbiff bigbiff9c754052013-01-09 09:09:08 -050054using namespace std;
55
Dees_Troy51a0e822012-09-05 15:24:24 -040056TWPartition::TWPartition(void) {
57 Can_Be_Mounted = false;
58 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050059 Can_Be_Backed_Up = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040060 Wipe_During_Factory_Reset = false;
61 Wipe_Available_in_GUI = false;
62 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040063 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040064 SubPartition_Of = "";
65 Symlink_Path = "";
66 Symlink_Mount_Point = "";
67 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040068 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040069 Actual_Block_Device = "";
70 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040071 Alternate_Block_Device = "";
72 Removable = false;
73 Is_Present = false;
74 Length = 0;
75 Size = 0;
76 Used = 0;
77 Free = 0;
78 Backup_Size = 0;
79 Can_Be_Encrypted = false;
80 Is_Encrypted = false;
81 Is_Decrypted = false;
82 Decrypted_Block_Device = "";
83 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -050084 Backup_Display_Name = "";
85 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040086 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040087 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040088 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040089 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +000090 Can_Encrypt_Backup = false;
91 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040092 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -040093 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040094 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050095 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040096 Storage_Path = "";
97 Current_File_System = "";
98 Fstab_File_System = "";
99 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000100 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000101 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000102#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
103 EcryptFS_Password = "";
104#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400105}
106
107TWPartition::~TWPartition(void) {
108 // Do nothing
109}
110
Dees_Troy5bf43922012-09-07 16:07:55 -0400111bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
112 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
113 int line_len = Line.size(), index = 0, item_index = 0;
114 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400115 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400116 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500117 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400118
Dees_Troy51127312012-09-08 13:08:49 -0400119 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500120 if (full_line[index] == 34)
121 skip = !skip;
122 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400123 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400124 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400125 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000126 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400127 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500128 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000129 Display_Name = full_line + 1;
130 Backup_Display_Name = Display_Name;
131 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400132 index = Mount_Point.size();
133 while (index < line_len) {
134 while (index < line_len && full_line[index] == '\0')
135 index++;
136 if (index >= line_len)
137 continue;
138 ptr = full_line + index;
139 if (item_index == 0) {
140 // File System
141 Fstab_File_System = ptr;
142 Current_File_System = ptr;
143 item_index++;
144 } else if (item_index == 1) {
145 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400146 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400147 MTD_Name = ptr;
148 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400149 } else if (Fstab_File_System == "bml") {
150 if (Mount_Point == "/boot")
151 MTD_Name = "boot";
152 else if (Mount_Point == "/recovery")
153 MTD_Name = "recovery";
154 Primary_Block_Device = ptr;
155 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000156 LOGERR("Until we get better BML support, you will have to find and provide the full block device path to the BML devices e.g. /dev/block/bml9 instead of the partition name\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400157 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400158 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000159 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400160 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000161 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400162 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400163 } else {
164 Primary_Block_Device = ptr;
165 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400166 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400167 item_index++;
168 } else if (item_index > 1) {
169 if (*ptr == '/') {
170 // Alternate Block Device
171 Alternate_Block_Device = ptr;
172 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
173 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
174 // Partition length
175 ptr += 7;
176 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400177 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
178 // Custom flags, save for later so that new values aren't overwritten by defaults
179 ptr += 6;
180 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000181 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400182 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
183 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400184 } else {
185 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000186 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400187 }
188 }
189 while (index < line_len && full_line[index] != '\0')
190 index++;
191 }
192
193 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
194 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000195 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400196 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000197 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400198 return 0;
199 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400200 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400201 Setup_File_System(Display_Error);
202 if (Mount_Point == "/system") {
203 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500204 Backup_Display_Name = Display_Name;
205 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400206 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500207 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400208 } else if (Mount_Point == "/data") {
209 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500210 Backup_Display_Name = Display_Name;
211 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400212 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400213 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500214 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000215 Can_Encrypt_Backup = true;
216 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400217#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troya13d74f2013-03-24 08:54:55 -0500218 Storage_Name = "Internal Storage";
Dees_Troy5bf43922012-09-07 16:07:55 -0400219 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400220 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500221 Is_Settings_Storage = true;
Dees_Troy51127312012-09-08 13:08:49 -0400222 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000223 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400224 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
225 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400226 Symlink_Mount_Point = "/emmc";
227 } else {
228 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400229 Symlink_Mount_Point = "/sdcard";
230 }
Dees_Troy16b74352012-11-14 22:27:31 +0000231 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
232 Storage_Path = "/data/media/0";
233 Symlink_Path = Storage_Path;
234 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
235 UnMount(true);
236 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400237#endif
238#ifdef TW_INCLUDE_CRYPTO
239 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400240 char crypto_blkdev[255];
241 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
242 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400243 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400244 DataManager::SetValue(TW_IS_DECRYPTED, 1);
245 Is_Encrypted = true;
246 Is_Decrypted = true;
247 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000248 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400249 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400250 Is_Encrypted = true;
251 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800252 Can_Be_Mounted = false;
253 Current_File_System = "emmc";
254 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400255 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
256 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
257 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800258 } else {
259 // Filesystem is not encrypted and the mount
260 // succeeded, so get it back to the original
261 // unmounted state
262 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400263 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400264 #ifdef RECOVERY_SDCARD_ON_DATA
265 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
266 Recreate_Media_Folder();
267 #endif
268#else
269 #ifdef RECOVERY_SDCARD_ON_DATA
270 Recreate_Media_Folder();
271 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400272#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400273 } else if (Mount_Point == "/cache") {
274 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500275 Backup_Display_Name = Display_Name;
276 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400277 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400278 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500279 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400280 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000281 LOGINFO("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500282 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
283 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400284 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400285 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400286 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400287 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500288 Backup_Display_Name = Display_Name;
289 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400290 Is_SubPartition = true;
291 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400292 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500293 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000294 Can_Encrypt_Backup = true;
295 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400296 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400297 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400298 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500299 Backup_Display_Name = Display_Name;
300 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400301 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400302 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500303 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000304 Can_Encrypt_Backup = true;
305 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400306 } else if (Mount_Point == "/boot") {
307 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500308 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400309 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500310 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400311 }
312#ifdef TW_EXTERNAL_STORAGE_PATH
313 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
314 Is_Storage = true;
315 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400316 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500317 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400318#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000319 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400320 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400321 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500322 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400323#ifndef RECOVERY_SDCARD_ON_DATA
324 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400325 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400326#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400327#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000328 }
Dees_Troy8170a922012-09-18 15:40:25 -0400329#ifdef TW_INTERNAL_STORAGE_PATH
330 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
331 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500332 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400333 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500334 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400335#ifndef RECOVERY_SDCARD_ON_DATA
336 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400337 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400338#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400339 }
340#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000341 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400342 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500343 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500344 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400345#ifndef RECOVERY_SDCARD_ON_DATA
346 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400347 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400348#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400349 }
350#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400351 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400352 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400353 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500354 if (Mount_Point == "/boot") {
355 Display_Name = "Boot";
356 Backup_Display_Name = Display_Name;
357 Can_Be_Backed_Up = true;
358 } else if (Mount_Point == "/recovery") {
359 Display_Name = "Recovery";
360 Backup_Display_Name = Display_Name;
361 Can_Be_Backed_Up = true;
362 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400363 }
364
Dees_Troy51127312012-09-08 13:08:49 -0400365 // Process any custom flags
366 if (Flags.size() > 0)
367 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400368 return true;
369}
370
371bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
372 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500373 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400374 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500375 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400376
377 strcpy(flags, Flags.c_str());
378 flags_len = Flags.size();
379 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500380 if (flags[index] == 34)
381 skip = !skip;
382 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400383 flags[index] = '\0';
384 }
385
386 index = 0;
387 while (index < flags_len) {
388 while (index < flags_len && flags[index] == '\0')
389 index++;
390 if (index >= flags_len)
391 continue;
392 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500393 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400394 if (strcmp(ptr, "removable") == 0) {
395 Removable = true;
396 } else if (strcmp(ptr, "storage") == 0) {
397 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500398 } else if (strcmp(ptr, "settingsstorage") == 0) {
399 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400400 } else if (strcmp(ptr, "canbewiped") == 0) {
401 Can_Be_Wiped = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500402 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
403 ptr += 7;
404 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
405 Can_Be_Backed_Up = true;
406 else
407 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400408 } else if (strcmp(ptr, "wipeingui") == 0) {
409 Can_Be_Wiped = true;
410 Wipe_Available_in_GUI = true;
411 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
412 Can_Be_Wiped = true;
413 Wipe_Available_in_GUI = true;
414 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500415 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000416 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400417 Is_SubPartition = true;
418 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000419 } else if (strcmp(ptr, "ignoreblkid") == 0) {
420 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000421 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
422 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500423 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400424 ptr += 8;
425 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500426 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
427 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400428 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500429 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400430 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500431 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
432 Display_Name.resize(Display_Name.size() - 1);
433 }
434 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
435 has_storage_name = true;
436 ptr += 11;
437 if (*ptr == '\"') ptr++;
438 Storage_Name = ptr;
439 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
440 Storage_Name.resize(Storage_Name.size() - 1);
441 }
442 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
443 has_backup_name = true;
444 ptr += 10;
445 if (*ptr == '\"') ptr++;
446 Backup_Display_Name = ptr;
447 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
448 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
449 }
450 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400451 ptr += 10;
452 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500453 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400454 ptr += 7;
455 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000456 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
457 ptr += 17;
458 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
459 Can_Encrypt_Backup = true;
460 else
461 Can_Encrypt_Backup = false;
462 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
463 ptr += 21;
464 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
465 Can_Encrypt_Backup = true;
466 Use_Userdata_Encryption = true;
467 } else {
468 Use_Userdata_Encryption = false;
469 }
Dees_Troy51127312012-09-08 13:08:49 -0400470 } else {
471 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000472 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400473 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000474 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400475 }
476 while (index < flags_len && flags[index] != '\0')
477 index++;
478 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500479 if (has_display_name && !has_storage_name)
480 Storage_Name = Display_Name;
481 if (!has_display_name && has_storage_name)
482 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000483 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500484 Backup_Display_Name = Display_Name;
485 if (!has_display_name && has_backup_name)
486 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400487 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400488}
489
Dees_Troy5bf43922012-09-07 16:07:55 -0400490bool TWPartition::Is_File_System(string File_System) {
491 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400492 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400493 File_System == "ext4" ||
494 File_System == "vfat" ||
495 File_System == "ntfs" ||
496 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500497 File_System == "exfat" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400498 File_System == "auto")
499 return true;
500 else
501 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400502}
503
Dees_Troy5bf43922012-09-07 16:07:55 -0400504bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400505 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400506 return true;
507 else
508 return false;
509}
510
Dees_Troy51127312012-09-08 13:08:49 -0400511bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400512 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400513 if (mkdir(Path.c_str(), 0777) == -1) {
514 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000515 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400516 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000517 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400518 return false;
519 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000520 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400521 return true;
522 }
523 }
524 return true;
525}
526
Dees_Troy5bf43922012-09-07 16:07:55 -0400527void TWPartition::Setup_File_System(bool Display_Error) {
528 struct statfs st;
529
530 Can_Be_Mounted = true;
531 Can_Be_Wiped = true;
532
Dees_Troy5bf43922012-09-07 16:07:55 -0400533 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400534 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400535 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
536 Backup_Name = Display_Name;
537 Backup_Method = FILES;
538}
539
540void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400541 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
542 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800543 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400544 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800545 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400546 Backup_Method = FLASH_UTILS;
547 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000548 LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400549 if (Find_Partition_Size()) {
550 Used = Size;
551 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400552 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400553 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000554 LOGERR("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400555 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000556 LOGINFO("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400557 }
558}
559
Dees_Troye58d5262012-09-21 12:27:57 -0400560void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500561 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400562 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500563 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400564 Has_Android_Secure = true;
565 Symlink_Path = Mount_Point + "/.android_secure";
566 Symlink_Mount_Point = "/and-sec";
567 Backup_Path = Symlink_Mount_Point;
568 Make_Dir("/and-sec", true);
569 Recreate_AndSec_Folder();
570}
571
Dees_Troy5bf43922012-09-07 16:07:55 -0400572void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
573 char device[512], realDevice[512];
574
575 strcpy(device, Block.c_str());
576 memset(realDevice, 0, sizeof(realDevice));
577 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
578 {
579 strcpy(device, realDevice);
580 memset(realDevice, 0, sizeof(realDevice));
581 }
582
583 if (device[0] != '/') {
584 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000585 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400586 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000587 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400588 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400589 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400590 Block = device;
591 return;
592 }
593}
594
Dees_Troy8e337f32012-10-13 22:07:49 -0400595void TWPartition::Mount_Storage_Retry(void) {
596 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500597 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400598 int retry_count = 5;
599 while (retry_count > 0 && !Mount(false)) {
600 usleep(500000);
601 retry_count--;
602 }
603 Mount(true);
604 }
605}
606
Dees_Troy38bd7602012-09-14 13:33:53 -0400607bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
608 FILE *fp = NULL;
609 char line[255];
610
611 fp = fopen("/proc/mtd", "rt");
612 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000613 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400614 return false;
615 }
616
617 while (fgets(line, sizeof(line), fp) != NULL)
618 {
619 char device[32], label[32];
620 unsigned long size = 0;
621 char* fstype = NULL;
622 int deviceId;
623
624 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
625
626 // Skip header and blank lines
627 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
628 continue;
629
630 // Strip off the trailing " from the label
631 label[strlen(label)-1] = '\0';
632
633 if (strcmp(label, MTD_Name.c_str()) == 0) {
634 // We found our device
635 // Strip off the trailing : from the device
636 device[strlen(device)-1] = '\0';
637 if (sscanf(device,"mtd%d", &deviceId) == 1) {
638 sprintf(device, "/dev/block/mtdblock%d", deviceId);
639 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000640 fclose(fp);
641 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400642 }
643 }
644 }
645 fclose(fp);
646
647 return false;
648}
649
Dees_Troy51127312012-09-08 13:08:49 -0400650bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
651 struct statfs st;
652 string Local_Path = Mount_Point + "/.";
653
654 if (!Mount(Display_Error))
655 return false;
656
657 if (statfs(Local_Path.c_str(), &st) != 0) {
658 if (!Removable) {
659 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000660 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400661 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000662 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400663 }
664 return false;
665 }
666 Size = (st.f_blocks * st.f_bsize);
667 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
668 Free = (st.f_bfree * st.f_bsize);
669 Backup_Size = Used;
670 return true;
671}
672
673bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400674 FILE* fp;
675 char command[255], line[512];
676 int include_block = 1;
677 unsigned int min_len;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500678 string result;
Dees_Troy5bf43922012-09-07 16:07:55 -0400679
680 if (!Mount(Display_Error))
681 return false;
682
Dees_Troy38bd7602012-09-14 13:33:53 -0400683 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400684 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500685 TWFunc::Exec_Cmd(command, result);
Dees_Troy51127312012-09-08 13:08:49 -0400686 fp = fopen("/tmp/dfoutput.txt", "rt");
687 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000688 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400689 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400690 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400691
692 while (fgets(line, sizeof(line), fp) != NULL)
693 {
694 unsigned long blocks, used, available;
695 char device[64];
696 char tmpString[64];
697
698 if (strncmp(line, "Filesystem", 10) == 0)
699 continue;
700 if (strlen(line) < min_len) {
701 include_block = 0;
702 continue;
703 }
704 if (include_block) {
705 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
706 } else {
707 // The device block string is so long that the df information is on the next line
708 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400709 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400710 while (tmpString[space_count] == 32)
711 space_count++;
712 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
713 }
714
715 // Adjust block size to byte size
716 Size = blocks * 1024ULL;
717 Used = used * 1024ULL;
718 Free = available * 1024ULL;
719 Backup_Size = Used;
720 }
721 fclose(fp);
722 return true;
723}
724
Dees_Troy5bf43922012-09-07 16:07:55 -0400725bool TWPartition::Find_Partition_Size(void) {
726 FILE* fp;
727 char line[512];
728 string tmpdevice;
729
igoriok87e3d932013-01-31 21:03:53 +0200730 fp = fopen("/proc/dumchar_info", "rt");
731 if (fp != NULL) {
732 while (fgets(line, sizeof(line), fp) != NULL)
733 {
734 char label[32], device[32];
735 unsigned long size = 0;
736
737 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
738
739 // Skip header, annotation and blank lines
740 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
741 continue;
742
743 tmpdevice = "/dev/";
744 tmpdevice += label;
745 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
746 Size = size;
747 fclose(fp);
748 return true;
749 }
750 }
751 }
752
Dees_Troy5bf43922012-09-07 16:07:55 -0400753 // In this case, we'll first get the partitions we care about (with labels)
754 fp = fopen("/proc/partitions", "rt");
755 if (fp == NULL)
756 return false;
757
758 while (fgets(line, sizeof(line), fp) != NULL)
759 {
760 unsigned long major, minor, blocks;
761 char device[512];
762 char tmpString[64];
763
Dees_Troy63c8df72012-09-10 14:02:05 -0400764 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400765 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
766
767 tmpdevice = "/dev/block/";
768 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400769 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400770 // Adjust block size to byte size
771 Size = blocks * 1024ULL;
772 fclose(fp);
773 return true;
774 }
775 }
776 fclose(fp);
777 return false;
778}
779
Dees_Troy5bf43922012-09-07 16:07:55 -0400780bool TWPartition::Is_Mounted(void) {
781 if (!Can_Be_Mounted)
782 return false;
783
784 struct stat st1, st2;
785 string test_path;
786
787 // Check to see if the mount point directory exists
788 test_path = Mount_Point + "/.";
789 if (stat(test_path.c_str(), &st1) != 0) return false;
790
791 // Check to see if the directory above the mount point exists
792 test_path = Mount_Point + "/../.";
793 if (stat(test_path.c_str(), &st2) != 0) return false;
794
795 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
796 int ret = (st1.st_dev != st2.st_dev) ? true : false;
797
798 return ret;
799}
800
801bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000802 int exfat_mounted = 0;
803
Dees_Troy5bf43922012-09-07 16:07:55 -0400804 if (Is_Mounted()) {
805 return true;
806 } else if (!Can_Be_Mounted) {
807 return false;
808 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400809
810 Find_Actual_Block_Device();
811
812 // Check the current file system before mounting
813 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000814 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000815 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 +0000816 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000817 string result;
818 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000819 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000820 Current_File_System = "vfat";
821 } else {
822#ifdef TW_NO_EXFAT_FUSE
823 UnMount(false);
824 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
825 // Some kernels let us mount vfat as exfat which doesn't work out too well
826#else
827 exfat_mounted = 1;
828#endif
829 }
830 }
Dees_Troy22042032012-12-18 21:23:08 +0000831 if (Fstab_File_System == "yaffs2") {
832 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000833 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
834 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
835 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
836 if (Display_Error)
837 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
838 else
839 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
840 return false;
841 } else {
842 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
843 return true;
844 }
845 } else {
846 struct stat st;
847 string test_path = Mount_Point;
848 if (stat(test_path.c_str(), &st) < 0) {
849 if (Display_Error)
850 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
851 else
852 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
853 return false;
854 }
855 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
856 if (new_mode != st.st_mode) {
857 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
858 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
859 if (Display_Error)
860 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
861 else
862 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
863 return false;
864 }
865 }
Dees_Troy22042032012-12-18 21:23:08 +0000866 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000867 }
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000868 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
869#ifdef TW_NO_EXFAT_FUSE
870 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000871 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000872 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000873 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000874 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000875 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000876 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
877 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 +0000878 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000879 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000880 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000881#endif
882 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000883 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000884 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000885 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
886 LOGINFO("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000887 return false;
888#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000889 }
890#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000891 }
892#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
893 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
894 MetaEcfsFile += "/.MetaEcfsFile";
895 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
896 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
897 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000898 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000899 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000900 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000901 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000902 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000903 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400904 }
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000905 } else {
906 Is_Decrypted = false;
907 }
908#endif
909 if (Removable)
910 Update_Size(Display_Error);
911
912 if (!Symlink_Mount_Point.empty()) {
913 string Command, Result;
914 Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
915 TWFunc::Exec_Cmd(Command, Result);
Dees_Troy5bf43922012-09-07 16:07:55 -0400916 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400917 return true;
918}
919
920bool TWPartition::UnMount(bool Display_Error) {
921 if (Is_Mounted()) {
922 int never_unmount_system;
923
924 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
925 if (never_unmount_system == 1 && Mount_Point == "/system")
926 return true; // Never unmount system if you're not supposed to unmount it
927
Dees_Troyc8bafa12013-01-10 15:43:00 +0000928#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
929 if (EcryptFS_Password.size() > 0) {
930 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
931 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000932 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000933 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000934 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000935 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000936 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000937 }
938 }
939#endif
940
Dees_Troy38bd7602012-09-14 13:33:53 -0400941 if (!Symlink_Mount_Point.empty())
942 umount(Symlink_Mount_Point.c_str());
943
Dees_Troyb05ddee2013-01-28 20:24:50 +0000944 umount(Mount_Point.c_str());
945 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400946 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000947 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400948 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000949 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400950 return false;
951 } else
952 return true;
953 } else {
954 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400955 }
956}
957
Gary Peck43acadf2012-11-21 21:19:01 -0800958bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +0000959 bool wiped = false, update_crypt = false;
960 int check;
961 string Layout_Filename = Mount_Point + "/.layout_version";
962
Dees_Troy38bd7602012-09-14 13:33:53 -0400963 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000964 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400965 return false;
966 }
967
Dees_Troyc51f1f92012-09-20 15:32:13 -0400968 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +0000969 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400970
Dees_Troyce675462013-01-09 19:48:21 +0000971#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
972 if (Mount_Point == "/data" && Mount(false)) {
973 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
974 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
975 }
976#endif
977
Dees_Troy16c2b312013-01-15 16:51:18 +0000978 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
979 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
980 else
981 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -0400982
Dees_Troy16c2b312013-01-15 16:51:18 +0000983 if (Has_Data_Media) {
984 wiped = Wipe_Data_Without_Wiping_Media();
985 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -0800986
Dees_Troy16c2b312013-01-15 16:51:18 +0000987 DataManager::GetValue(TW_RM_RF_VAR, check);
988
989 if (check)
990 wiped = Wipe_RMRF();
991 else if (New_File_System == "ext4")
992 wiped = Wipe_EXT4();
993 else if (New_File_System == "ext2" || New_File_System == "ext3")
994 wiped = Wipe_EXT23(New_File_System);
995 else if (New_File_System == "vfat")
996 wiped = Wipe_FAT();
997 else if (New_File_System == "exfat")
998 wiped = Wipe_EXFAT();
999 else if (New_File_System == "yaffs2")
1000 wiped = Wipe_MTD();
1001 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001002 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 +00001003 unlink("/.layout_version");
1004 return false;
1005 }
1006 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001007 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001008
Gary Pecke8bc5d72012-12-21 06:45:25 -08001009 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001010#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1011 if (Mount_Point == "/data" && Mount(false)) {
1012 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1013 Make_Dir("/data/system", true);
1014 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1015 }
1016 }
1017#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001018
Dees_Troy1c1ac442013-01-17 21:42:14 +00001019 if (Mount_Point == "/cache")
1020 DataManager::Output_Version();
1021
Dees_Troy16c2b312013-01-15 16:51:18 +00001022 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1023 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1024
1025 if (update_crypt) {
1026 Setup_File_System(false);
1027 if (Is_Encrypted && !Is_Decrypted) {
1028 // just wiped an encrypted partition back to its unencrypted state
1029 Is_Encrypted = false;
1030 Is_Decrypted = false;
1031 Decrypted_Block_Device = "";
1032 if (Mount_Point == "/data") {
1033 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1034 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1035 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001036 }
1037 }
1038 }
1039 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001040}
1041
Gary Peck43acadf2012-11-21 21:19:01 -08001042bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001043 if (Is_File_System(Current_File_System))
1044 return Wipe(Current_File_System);
1045 else
1046 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001047}
1048
Dees_Troye58d5262012-09-21 12:27:57 -04001049bool TWPartition::Wipe_AndSec(void) {
1050 if (!Has_Android_Secure)
1051 return false;
1052
Dees_Troye58d5262012-09-21 12:27:57 -04001053 if (!Mount(true))
1054 return false;
1055
Dees_Troy2673cec2013-04-02 20:22:16 +00001056 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001057 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Dees_Troye58d5262012-09-21 12:27:57 -04001058 return true;
1059}
1060
Dees_Troy51a0e822012-09-05 15:24:24 -04001061bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001062 if (Backup_Method == FILES)
1063 return Backup_Tar(backup_folder);
1064 else if (Backup_Method == DD)
1065 return Backup_DD(backup_folder);
1066 else if (Backup_Method == FLASH_UTILS)
1067 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001068 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001069 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001070}
1071
Dees_Troy43d8b002012-09-17 16:00:01 -04001072bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001073 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001074 char split_filename[512];
1075 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001076 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001077
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001078 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001079 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001080 if (!TWFunc::Path_Exists(Full_Filename)) {
1081 // This is a split archive, we presume
1082 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001083 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001084 md5file = split_filename;
1085 md5file += ".md5";
1086 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001087 LOGERR("No md5 file found for '%s'.\n", split_filename);
1088 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001089 return false;
1090 }
1091 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001092 while (index < 1000) {
1093 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001094 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001095 return false;
1096 }
1097 index++;
1098 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001099 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001100 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001101 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001102 } else {
1103 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001104 md5file = Full_Filename + ".md5";
1105 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001106 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1107 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001108 return false;
1109 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001110 md5sum.setfn(Full_Filename);
1111 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001112 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001113 return false;
1114 } else
1115 return true;
1116 }
1117 return false;
1118}
1119
Dees_Troy51a0e822012-09-05 15:24:24 -04001120bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001121 size_t first_period, second_period;
1122 string Restore_File_System;
1123
1124 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001125 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001126
1127 // Parse backup filename to extract the file system before wiping
1128 first_period = Backup_FileName.find(".");
1129 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001130 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001131 return false;
1132 }
1133 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1134 second_period = Restore_File_System.find(".");
1135 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001136 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001137 return false;
1138 }
1139 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001140 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001141
1142 if (Is_File_System(Restore_File_System))
1143 return Restore_Tar(restore_folder, Restore_File_System);
1144 else if (Is_Image(Restore_File_System)) {
1145 if (Restore_File_System == "emmc")
1146 return Restore_DD(restore_folder);
1147 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1148 return Restore_Flash_Image(restore_folder);
1149 }
1150
Dees_Troy2673cec2013-04-02 20:22:16 +00001151 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001152 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001153}
1154
1155string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001156 if (Backup_Method == NONE)
1157 return "none";
1158 else if (Backup_Method == FILES)
1159 return "files";
1160 else if (Backup_Method == DD)
1161 return "dd";
1162 else if (Backup_Method == FLASH_UTILS)
1163 return "flash_utils";
1164 else
1165 return "undefined";
1166 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001167}
1168
1169bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001170 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001171 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001172 return 1;
1173}
1174
1175bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001176 bool Save_Data_Media = Has_Data_Media;
1177
1178 if (!UnMount(true))
1179 return false;
1180
Dees_Troy38bd7602012-09-14 13:33:53 -04001181 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001182 Decrypted_Block_Device = "";
1183 Is_Decrypted = false;
1184 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001185 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001186 Has_Data_Media = Save_Data_Media;
1187 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1188 Recreate_Media_Folder();
1189 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001190 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001191 return true;
1192 } else {
1193 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001194 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001195 return false;
1196 }
1197 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001198}
1199
1200void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001201 const char* type;
1202 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001203
Dees_Troy68cab492012-12-12 19:29:35 +00001204 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1205 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001206
Dees_Troy38bd7602012-09-14 13:33:53 -04001207 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001208 if (!Is_Present)
1209 return;
Dees_Troy51127312012-09-08 13:08:49 -04001210
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001211 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1212 if (blkid_do_fullprobe(pr)) {
1213 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001214 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001215 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001216 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001217
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001218 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1219 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001220 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001221 return;
1222 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001223
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001224 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001225 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001226}
1227
Gary Peck43acadf2012-11-21 21:19:01 -08001228bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001229 if (!UnMount(true))
1230 return false;
1231
Dees_Troy43d8b002012-09-17 16:00:01 -04001232 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001233 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001234
Dees_Troy2673cec2013-04-02 20:22:16 +00001235 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001236 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001237 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001238 LOGINFO("mke2fs command: %s\n", command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001239 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001240 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001241 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001242 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001243 return true;
1244 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001245 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001246 return false;
1247 }
1248 } else
1249 return Wipe_RMRF();
1250
1251 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001252}
1253
1254bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001255 if (!UnMount(true))
1256 return false;
1257
Dees_Troy43d8b002012-09-17 16:00:01 -04001258 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001259 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001260
Dees_Troy2673cec2013-04-02 20:22:16 +00001261 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001262 Find_Actual_Block_Device();
1263 Command = "make_ext4fs";
1264 if (!Is_Decrypted && Length != 0) {
1265 // Only use length if we're not decrypted
1266 char len[32];
1267 sprintf(len, "%i", Length);
1268 Command += " -l ";
1269 Command += len;
1270 }
1271 Command += " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001272 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001273 if (TWFunc::Exec_Cmd(Command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001274 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001275 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001276 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001277 return true;
1278 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001279 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001280 return false;
1281 }
1282 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001283 return Wipe_EXT23("ext4");
Dees_Troy38bd7602012-09-14 13:33:53 -04001284
1285 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001286}
1287
1288bool TWPartition::Wipe_FAT() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001289 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001290
Dees_Troy43d8b002012-09-17 16:00:01 -04001291 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001292 if (!UnMount(true))
1293 return false;
1294
Dees_Troy2673cec2013-04-02 20:22:16 +00001295 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001296 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001297 command = "mkdosfs " + Actual_Block_Device;
1298 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001299 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001300 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001301 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001302 return true;
1303 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001304 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001305 return false;
1306 }
1307 return true;
1308 }
1309 else
1310 return Wipe_RMRF();
1311
1312 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001313}
1314
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001315bool TWPartition::Wipe_EXFAT() {
1316 string command, result;
1317
1318 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1319 if (!UnMount(true))
1320 return false;
1321
Dees_Troy2673cec2013-04-02 20:22:16 +00001322 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001323 Find_Actual_Block_Device();
1324 command = "mkexfatfs " + Actual_Block_Device;
1325 if (TWFunc::Exec_Cmd(command, result) == 0) {
1326 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001327 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001328 return true;
1329 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001330 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001331 return false;
1332 }
1333 return true;
1334 }
1335 return false;
1336}
1337
Dees_Troy38bd7602012-09-14 13:33:53 -04001338bool TWPartition::Wipe_MTD() {
1339 if (!UnMount(true))
1340 return false;
1341
Dees_Troy2673cec2013-04-02 20:22:16 +00001342 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001343
1344 mtd_scan_partitions();
1345 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1346 if (mtd == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001347 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001348 return false;
1349 }
1350
1351 MtdWriteContext* ctx = mtd_write_partition(mtd);
1352 if (ctx == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001353 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001354 return false;
1355 }
1356 if (mtd_erase_blocks(ctx, -1) == -1) {
1357 mtd_write_close(ctx);
Dees_Troy2673cec2013-04-02 20:22:16 +00001358 LOGERR("Failed to format '%s'", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001359 return false;
1360 }
1361 if (mtd_write_close(ctx) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001362 LOGERR("Failed to close '%s'", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001363 return false;
1364 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001365 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001366 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001367 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001368 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001369}
1370
1371bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001372 if (!Mount(true))
1373 return false;
1374
Dees_Troy2673cec2013-04-02 20:22:16 +00001375 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001376 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001377 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001378 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001379}
1380
1381bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001382 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001383
1384 // This handles wiping data on devices with "sdcard" in /data/media
1385 if (!Mount(true))
1386 return false;
1387
Dees_Troy2673cec2013-04-02 20:22:16 +00001388 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001389
1390 DIR* d;
1391 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001392 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001393 struct dirent* de;
1394 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001395 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1396 // The media folder is the "internal sdcard"
1397 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
1398 // the media folder for multi-user.
1399 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001400
1401 dir = "/data/";
1402 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001403 if (de->d_type == DT_DIR) {
1404 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001405 } 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 +00001406 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001407 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001408 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001409 }
1410 closedir(d);
Dees_Troy2673cec2013-04-02 20:22:16 +00001411 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001412 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001413 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001414 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001415 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001416}
1417
1418bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001419 char back_name[255], split_index[5];
1420 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001421 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001422 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001423 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001424 twrpTar tar;
1425 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001426
Dees_Troy43d8b002012-09-17 16:00:01 -04001427 if (!Mount(true))
1428 return false;
1429
Dees_Troya13d74f2013-03-24 08:54:55 -05001430 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001431 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001432
1433 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001434 tar.use_compression = use_compression;
1435#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1436 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1437 if (use_encryption && Can_Encrypt_Backup) {
1438 tar.use_encryption = use_encryption;
1439 if (Use_Userdata_Encryption)
1440 tar.userdata_encryption = use_encryption;
1441 } else {
1442 use_encryption = false;
1443 }
1444#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001445
1446 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1447 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001448 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001449 tar.has_data_media = Has_Data_Media;
1450 if (!use_encryption && Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001451 // This backup needs to be split into multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001452 gui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001453 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001454 tar.setdir(back_name);
1455 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001456 backup_count = tar.splitArchiveFork();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001457 if (backup_count == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001458 LOGERR("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001459 return false;
1460 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001461 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001462 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001463 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001464 tar.setdir(Backup_Path);
1465 tar.setfn(Full_FileName);
1466 if (tar.createTarFork() != 0)
1467 return false;
1468 if (use_compression && !use_encryption) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001469 string gzname = Full_FileName + ".gz";
1470 rename(gzname.c_str(), Full_FileName.c_str());
1471 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001472 if (use_encryption)
1473 Full_FileName += "000";
Dees_Troy7c2dec82012-09-26 09:49:14 -04001474 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001475 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001476 return false;
1477 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001478 }
1479 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001480}
1481
1482bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001483 char back_name[255], backup_size[32];
1484 string Full_FileName, Command, result, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001485 int use_compression;
1486
igoriok87e3d932013-01-31 21:03:53 +02001487 sprintf(backup_size, "%llu", Backup_Size);
1488 DD_BS = backup_size;
1489
Dees_Troyb46a6842012-09-25 11:06:46 -04001490 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001491 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001492
1493 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1494 Backup_FileName = back_name;
1495
1496 Full_FileName = backup_folder + "/" + Backup_FileName;
1497
igoriok87e3d932013-01-31 21:03:53 +02001498 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001499 LOGINFO("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001500 TWFunc::Exec_Cmd(Command, result);
Dees_Troyc154ac22012-10-12 15:36:47 -04001501 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001502 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001503 return false;
1504 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001505 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001506}
1507
1508bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001509 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001510 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001511 int use_compression;
1512
Dees_Troyb46a6842012-09-25 11:06:46 -04001513 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001514 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001515
1516 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1517 Backup_FileName = back_name;
1518
1519 Full_FileName = backup_folder + "/" + Backup_FileName;
1520
1521 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001522 LOGINFO("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001523 TWFunc::Exec_Cmd(Command, result);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001524 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1525 // 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 +00001526 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001527 return false;
1528 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001529 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001530}
1531
Gary Peck43acadf2012-11-21 21:19:01 -08001532bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1533 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001534 int index = 0;
1535 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001536
Dees_Troye58d5262012-09-21 12:27:57 -04001537 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001538 if (!Wipe_AndSec())
1539 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001540 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001541 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001542 if (!Wipe(Restore_File_System))
1543 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001544 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001545 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001546 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001547
1548 if (!Mount(true))
1549 return false;
1550
Dees_Troy4a2a1262012-09-18 09:33:47 -04001551 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001552 /*if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001553 if (!TWFunc::Path_Exists(Full_FileName)) {
1554 // Backup is multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001555 LOGINFO("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001556 sprintf(split_index, "%03i", index);
1557 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001558 while (TWFunc::Path_Exists(Full_FileName)) {
1559 index++;
Dees_Troy2673cec2013-04-02 20:22:16 +00001560 gui_print("Restoring archive %i...\n", index);
1561 LOGINFO("Restoring '%s'...\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001562 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001563 tar.setdir("/");
1564 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001565 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001566 return false;
1567 sprintf(split_index, "%03i", index);
1568 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1569 }
1570 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001571 LOGERR("Error locating restore file: '%s'\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001572 return false;
1573 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001574 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001575 } else {*/
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001576 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001577 tar.setdir(Backup_Path);
1578 tar.setfn(Full_FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +00001579 tar.backup_name = Backup_Name;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001580 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001581 return false;
Dees_Troy83bd4832013-05-04 12:39:56 +00001582 //}
Dees_Troy43d8b002012-09-17 16:00:01 -04001583 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001584}
1585
1586bool TWPartition::Restore_DD(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001587 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001588
Dees_Troyda8b55a2012-12-12 19:18:30 +00001589 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001590 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001591
1592 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001593 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001594 return false;
1595 }
1596 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1597 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001598 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001599 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1600 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1601 return false;
1602 }
1603
Dees_Troy2673cec2013-04-02 20:22:16 +00001604 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001605 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001606 LOGINFO("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001607 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001608 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001609}
1610
1611bool TWPartition::Restore_Flash_Image(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001612 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001613
Dees_Troy2673cec2013-04-02 20:22:16 +00001614 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001615 Full_FileName = restore_folder + "/" + Backup_FileName;
1616 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1617 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001618 LOGINFO("Erase command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001619 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001620 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001621 LOGINFO("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001622 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001623 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001624}
Dees_Troy5bf43922012-09-07 16:07:55 -04001625
1626bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001627 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001628
Dees_Troyab10ee22012-09-21 14:27:30 -04001629 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001630 return false;
1631
Dees_Troy0550cfb2012-10-13 11:56:13 -04001632 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001633 if (Removable || Is_Encrypted) {
1634 if (!Mount(false))
1635 return true;
1636 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001637 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001638
1639 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001640 if (!ret || Size == 0) {
1641 if (!Get_Size_Via_df(Display_Error)) {
1642 if (!Was_Already_Mounted)
1643 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001644 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001645 }
1646 }
Dees_Troy51127312012-09-08 13:08:49 -04001647
Dees_Troy5bf43922012-09-07 16:07:55 -04001648 if (Has_Data_Media) {
1649 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001650 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001651 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1652 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001653 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001654 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001655 int bak = (int)(Backup_Size / 1048576LLU);
1656 int total = (int)(Size / 1048576LLU);
1657 int us = (int)(Used / 1048576LLU);
1658 int fre = (int)(Free / 1048576LLU);
1659 int datmed = (int)(data_media_used / 1048576LLU);
Dees_Troy2673cec2013-04-02 20:22:16 +00001660 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 -04001661 } else {
1662 if (!Was_Already_Mounted)
1663 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001664 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001665 }
Dees_Troye58d5262012-09-21 12:27:57 -04001666 } else if (Has_Android_Secure) {
1667 if (Mount(Display_Error))
1668 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001669 else {
1670 if (!Was_Already_Mounted)
1671 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001672 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001673 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001674 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001675 if (!Was_Already_Mounted)
1676 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001677 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001678}
Dees_Troy38bd7602012-09-14 13:33:53 -04001679
1680void TWPartition::Find_Actual_Block_Device(void) {
1681 if (Is_Decrypted) {
1682 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001683 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001684 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001685 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001686 Is_Present = true;
1687 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001688 return;
1689 }
1690 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001691 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001692 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001693 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001694 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001695 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001696 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001697}
1698
1699void TWPartition::Recreate_Media_Folder(void) {
1700 string Command;
1701
1702 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001703 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001704 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001705 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00001706 LOGINFO("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001707 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1708 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001709 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001710}
Dees_Troye58d5262012-09-21 12:27:57 -04001711
1712void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001713 if (!Has_Android_Secure)
1714 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00001715 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001716 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001717 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001718 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001719 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001720 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1721 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1722 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001723 }
1724}