blob: 10cfb9f277db5c22da9a0016d53b91356c51f5e7 [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_Troya95f55c2013-08-17 13:14:43 +000052#ifdef USE_EXT4
53 #include "make_ext4fs.h"
54#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040055}
bigbiff bigbiffd0c24c52013-10-11 20:28:00 -040056#ifdef HAVE_SELINUX
57#include "selinux/selinux.h"
58#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040059
bigbiff bigbiff9c754052013-01-09 09:09:08 -050060using namespace std;
61
Dees_Troya95f55c2013-08-17 13:14:43 +000062extern struct selabel_handle *selinux_handle;
63
Dees_Troy51a0e822012-09-05 15:24:24 -040064TWPartition::TWPartition(void) {
65 Can_Be_Mounted = false;
66 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050067 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +020068 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040069 Wipe_During_Factory_Reset = false;
70 Wipe_Available_in_GUI = false;
71 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040072 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040073 SubPartition_Of = "";
74 Symlink_Path = "";
75 Symlink_Mount_Point = "";
76 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040077 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040078 Actual_Block_Device = "";
79 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040080 Alternate_Block_Device = "";
81 Removable = false;
82 Is_Present = false;
83 Length = 0;
84 Size = 0;
85 Used = 0;
86 Free = 0;
87 Backup_Size = 0;
88 Can_Be_Encrypted = false;
89 Is_Encrypted = false;
90 Is_Decrypted = false;
91 Decrypted_Block_Device = "";
92 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -050093 Backup_Display_Name = "";
94 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040095 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040096 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040097 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040098 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +000099 Can_Encrypt_Backup = false;
100 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400101 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400102 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400103 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500104 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400105 Storage_Path = "";
106 Current_File_System = "";
107 Fstab_File_System = "";
108 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000109 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000110 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000111#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
112 EcryptFS_Password = "";
113#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400114}
115
116TWPartition::~TWPartition(void) {
117 // Do nothing
118}
119
Dees_Troy5bf43922012-09-07 16:07:55 -0400120bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
121 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
122 int line_len = Line.size(), index = 0, item_index = 0;
123 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400124 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400125 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500126 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400127
Dees_Troy51127312012-09-08 13:08:49 -0400128 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500129 if (full_line[index] == 34)
130 skip = !skip;
131 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400132 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400133 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400134 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000135 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400136 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500137 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000138 Display_Name = full_line + 1;
139 Backup_Display_Name = Display_Name;
140 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400141 index = Mount_Point.size();
142 while (index < line_len) {
143 while (index < line_len && full_line[index] == '\0')
144 index++;
145 if (index >= line_len)
146 continue;
147 ptr = full_line + index;
148 if (item_index == 0) {
149 // File System
150 Fstab_File_System = ptr;
151 Current_File_System = ptr;
152 item_index++;
153 } else if (item_index == 1) {
154 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400155 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400156 MTD_Name = ptr;
157 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400158 } else if (Fstab_File_System == "bml") {
159 if (Mount_Point == "/boot")
160 MTD_Name = "boot";
161 else if (Mount_Point == "/recovery")
162 MTD_Name = "recovery";
163 Primary_Block_Device = ptr;
164 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000165 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 -0400166 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400167 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000168 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400169 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000170 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400171 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400172 } else {
173 Primary_Block_Device = ptr;
174 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400175 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400176 item_index++;
177 } else if (item_index > 1) {
178 if (*ptr == '/') {
179 // Alternate Block Device
180 Alternate_Block_Device = ptr;
181 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
182 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
183 // Partition length
184 ptr += 7;
185 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400186 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
187 // Custom flags, save for later so that new values aren't overwritten by defaults
188 ptr += 6;
189 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000190 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400191 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
192 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400193 } else {
194 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000195 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400196 }
197 }
198 while (index < line_len && full_line[index] != '\0')
199 index++;
200 }
201
202 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
203 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000204 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400205 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000206 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400207 return 0;
208 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400209 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400210 Setup_File_System(Display_Error);
211 if (Mount_Point == "/system") {
212 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500213 Backup_Display_Name = Display_Name;
214 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400215 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500216 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400217 } else if (Mount_Point == "/data") {
218 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500219 Backup_Display_Name = Display_Name;
220 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400221 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400222 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500223 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000224 Can_Encrypt_Backup = true;
225 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400226#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troya13d74f2013-03-24 08:54:55 -0500227 Storage_Name = "Internal Storage";
Dees_Troy5bf43922012-09-07 16:07:55 -0400228 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400229 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500230 Is_Settings_Storage = true;
Dees_Troy51127312012-09-08 13:08:49 -0400231 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000232 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400233 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
234 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400235 Symlink_Mount_Point = "/emmc";
236 } else {
237 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400238 Symlink_Mount_Point = "/sdcard";
239 }
Dees_Troy16b74352012-11-14 22:27:31 +0000240 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
241 Storage_Path = "/data/media/0";
242 Symlink_Path = Storage_Path;
243 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
244 UnMount(true);
245 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400246#endif
247#ifdef TW_INCLUDE_CRYPTO
248 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400249 char crypto_blkdev[255];
250 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
251 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400252 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400253 DataManager::SetValue(TW_IS_DECRYPTED, 1);
254 Is_Encrypted = true;
255 Is_Decrypted = true;
256 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000257 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400258 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400259 Is_Encrypted = true;
260 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800261 Can_Be_Mounted = false;
262 Current_File_System = "emmc";
263 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400264 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
265 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
266 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800267 } else {
268 // Filesystem is not encrypted and the mount
269 // succeeded, so get it back to the original
270 // unmounted state
271 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400272 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400273 #ifdef RECOVERY_SDCARD_ON_DATA
274 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
275 Recreate_Media_Folder();
276 #endif
277#else
278 #ifdef RECOVERY_SDCARD_ON_DATA
279 Recreate_Media_Folder();
280 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400281#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400282 } else if (Mount_Point == "/cache") {
283 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500284 Backup_Display_Name = Display_Name;
285 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400286 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400287 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500288 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400289 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000290 LOGINFO("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500291 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
292 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400293 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400294 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400295 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400296 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500297 Backup_Display_Name = Display_Name;
298 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400299 Is_SubPartition = true;
300 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400301 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500302 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000303 Can_Encrypt_Backup = true;
304 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400305 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400306 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400307 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500308 Backup_Display_Name = Display_Name;
309 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400310 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400311 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500312 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000313 Can_Encrypt_Backup = true;
314 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400315 } else if (Mount_Point == "/boot") {
316 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500317 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400318 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500319 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400320 }
321#ifdef TW_EXTERNAL_STORAGE_PATH
322 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
323 Is_Storage = true;
324 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400325 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500326 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400327#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000328 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400329 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400330 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500331 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400332#ifndef RECOVERY_SDCARD_ON_DATA
333 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400334 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400335#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400336#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000337 }
Dees_Troy8170a922012-09-18 15:40:25 -0400338#ifdef TW_INTERNAL_STORAGE_PATH
339 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
340 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500341 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400342 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500343 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400344#ifndef RECOVERY_SDCARD_ON_DATA
345 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400346 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400347#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400348 }
349#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000350 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400351 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500352 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500353 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400354#ifndef RECOVERY_SDCARD_ON_DATA
355 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400356 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400357#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400358 }
359#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400360 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400361 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400362 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500363 if (Mount_Point == "/boot") {
364 Display_Name = "Boot";
365 Backup_Display_Name = Display_Name;
366 Can_Be_Backed_Up = true;
367 } else if (Mount_Point == "/recovery") {
368 Display_Name = "Recovery";
369 Backup_Display_Name = Display_Name;
370 Can_Be_Backed_Up = true;
371 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400372 }
373
Dees_Troy51127312012-09-08 13:08:49 -0400374 // Process any custom flags
375 if (Flags.size() > 0)
376 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400377 return true;
378}
379
380bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
381 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500382 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400383 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500384 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400385
386 strcpy(flags, Flags.c_str());
387 flags_len = Flags.size();
388 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500389 if (flags[index] == 34)
390 skip = !skip;
391 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400392 flags[index] = '\0';
393 }
394
395 index = 0;
396 while (index < flags_len) {
397 while (index < flags_len && flags[index] == '\0')
398 index++;
399 if (index >= flags_len)
400 continue;
401 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500402 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400403 if (strcmp(ptr, "removable") == 0) {
404 Removable = true;
405 } else if (strcmp(ptr, "storage") == 0) {
406 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500407 } else if (strcmp(ptr, "settingsstorage") == 0) {
408 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400409 } else if (strcmp(ptr, "canbewiped") == 0) {
410 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700411 } else if (strcmp(ptr, "usermrf") == 0) {
412 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500413 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
414 ptr += 7;
415 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
416 Can_Be_Backed_Up = true;
417 else
418 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400419 } else if (strcmp(ptr, "wipeingui") == 0) {
420 Can_Be_Wiped = true;
421 Wipe_Available_in_GUI = true;
422 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
423 Can_Be_Wiped = true;
424 Wipe_Available_in_GUI = true;
425 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500426 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000427 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400428 Is_SubPartition = true;
429 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000430 } else if (strcmp(ptr, "ignoreblkid") == 0) {
431 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000432 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
433 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500434 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400435 ptr += 8;
436 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500437 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
438 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400439 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500440 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400441 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500442 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
443 Display_Name.resize(Display_Name.size() - 1);
444 }
445 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
446 has_storage_name = true;
447 ptr += 11;
448 if (*ptr == '\"') ptr++;
449 Storage_Name = ptr;
450 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
451 Storage_Name.resize(Storage_Name.size() - 1);
452 }
453 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
454 has_backup_name = true;
455 ptr += 10;
456 if (*ptr == '\"') ptr++;
457 Backup_Display_Name = ptr;
458 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
459 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
460 }
461 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400462 ptr += 10;
463 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500464 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400465 ptr += 7;
466 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000467 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
468 ptr += 17;
469 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
470 Can_Encrypt_Backup = true;
471 else
472 Can_Encrypt_Backup = false;
473 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
474 ptr += 21;
475 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
476 Can_Encrypt_Backup = true;
477 Use_Userdata_Encryption = true;
478 } else {
479 Use_Userdata_Encryption = false;
480 }
Dees_Troy51127312012-09-08 13:08:49 -0400481 } else {
482 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000483 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400484 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000485 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400486 }
487 while (index < flags_len && flags[index] != '\0')
488 index++;
489 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500490 if (has_display_name && !has_storage_name)
491 Storage_Name = Display_Name;
492 if (!has_display_name && has_storage_name)
493 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000494 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500495 Backup_Display_Name = Display_Name;
496 if (!has_display_name && has_backup_name)
497 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400498 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400499}
500
Dees_Troy5bf43922012-09-07 16:07:55 -0400501bool TWPartition::Is_File_System(string File_System) {
502 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400503 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400504 File_System == "ext4" ||
505 File_System == "vfat" ||
506 File_System == "ntfs" ||
507 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500508 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000509 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400510 File_System == "auto")
511 return true;
512 else
513 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400514}
515
Dees_Troy5bf43922012-09-07 16:07:55 -0400516bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400517 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400518 return true;
519 else
520 return false;
521}
522
Dees_Troy51127312012-09-08 13:08:49 -0400523bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400524 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400525 if (mkdir(Path.c_str(), 0777) == -1) {
526 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000527 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400528 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000529 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400530 return false;
531 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000532 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400533 return true;
534 }
535 }
536 return true;
537}
538
Dees_Troy5bf43922012-09-07 16:07:55 -0400539void TWPartition::Setup_File_System(bool Display_Error) {
540 struct statfs st;
541
542 Can_Be_Mounted = true;
543 Can_Be_Wiped = true;
544
Dees_Troy5bf43922012-09-07 16:07:55 -0400545 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400546 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400547 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
548 Backup_Name = Display_Name;
549 Backup_Method = FILES;
550}
551
552void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400553 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
554 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800555 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400556 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800557 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400558 Backup_Method = FLASH_UTILS;
559 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000560 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 -0400561 if (Find_Partition_Size()) {
562 Used = Size;
563 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400564 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400565 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000566 LOGERR("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400567 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000568 LOGINFO("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400569 }
570}
571
Dees_Troye58d5262012-09-21 12:27:57 -0400572void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500573 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400574 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500575 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400576 Has_Android_Secure = true;
577 Symlink_Path = Mount_Point + "/.android_secure";
578 Symlink_Mount_Point = "/and-sec";
579 Backup_Path = Symlink_Mount_Point;
580 Make_Dir("/and-sec", true);
581 Recreate_AndSec_Folder();
582}
583
Dees_Troy5bf43922012-09-07 16:07:55 -0400584void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
585 char device[512], realDevice[512];
586
587 strcpy(device, Block.c_str());
588 memset(realDevice, 0, sizeof(realDevice));
589 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
590 {
591 strcpy(device, realDevice);
592 memset(realDevice, 0, sizeof(realDevice));
593 }
594
595 if (device[0] != '/') {
596 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000597 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400598 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000599 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400600 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400601 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400602 Block = device;
603 return;
604 }
605}
606
Dees_Troy8e337f32012-10-13 22:07:49 -0400607void TWPartition::Mount_Storage_Retry(void) {
608 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500609 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400610 int retry_count = 5;
611 while (retry_count > 0 && !Mount(false)) {
612 usleep(500000);
613 retry_count--;
614 }
615 Mount(true);
616 }
617}
618
Dees_Troy38bd7602012-09-14 13:33:53 -0400619bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
620 FILE *fp = NULL;
621 char line[255];
622
623 fp = fopen("/proc/mtd", "rt");
624 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000625 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400626 return false;
627 }
628
629 while (fgets(line, sizeof(line), fp) != NULL)
630 {
631 char device[32], label[32];
632 unsigned long size = 0;
633 char* fstype = NULL;
634 int deviceId;
635
636 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
637
638 // Skip header and blank lines
639 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
640 continue;
641
642 // Strip off the trailing " from the label
643 label[strlen(label)-1] = '\0';
644
645 if (strcmp(label, MTD_Name.c_str()) == 0) {
646 // We found our device
647 // Strip off the trailing : from the device
648 device[strlen(device)-1] = '\0';
649 if (sscanf(device,"mtd%d", &deviceId) == 1) {
650 sprintf(device, "/dev/block/mtdblock%d", deviceId);
651 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000652 fclose(fp);
653 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400654 }
655 }
656 }
657 fclose(fp);
658
659 return false;
660}
661
Dees_Troy51127312012-09-08 13:08:49 -0400662bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
663 struct statfs st;
664 string Local_Path = Mount_Point + "/.";
665
666 if (!Mount(Display_Error))
667 return false;
668
669 if (statfs(Local_Path.c_str(), &st) != 0) {
670 if (!Removable) {
671 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000672 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400673 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000674 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400675 }
676 return false;
677 }
678 Size = (st.f_blocks * st.f_bsize);
679 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
680 Free = (st.f_bfree * st.f_bsize);
681 Backup_Size = Used;
682 return true;
683}
684
685bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400686 FILE* fp;
687 char command[255], line[512];
688 int include_block = 1;
689 unsigned int min_len;
690
691 if (!Mount(Display_Error))
692 return false;
693
Dees_Troy38bd7602012-09-14 13:33:53 -0400694 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400695 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200696 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400697 fp = fopen("/tmp/dfoutput.txt", "rt");
698 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000699 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400700 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400701 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400702
703 while (fgets(line, sizeof(line), fp) != NULL)
704 {
705 unsigned long blocks, used, available;
706 char device[64];
707 char tmpString[64];
708
709 if (strncmp(line, "Filesystem", 10) == 0)
710 continue;
711 if (strlen(line) < min_len) {
712 include_block = 0;
713 continue;
714 }
715 if (include_block) {
716 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
717 } else {
718 // The device block string is so long that the df information is on the next line
719 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400720 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400721 while (tmpString[space_count] == 32)
722 space_count++;
723 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
724 }
725
726 // Adjust block size to byte size
727 Size = blocks * 1024ULL;
728 Used = used * 1024ULL;
729 Free = available * 1024ULL;
730 Backup_Size = Used;
731 }
732 fclose(fp);
733 return true;
734}
735
Dees_Troy5bf43922012-09-07 16:07:55 -0400736bool TWPartition::Find_Partition_Size(void) {
737 FILE* fp;
738 char line[512];
739 string tmpdevice;
740
igoriok87e3d932013-01-31 21:03:53 +0200741 fp = fopen("/proc/dumchar_info", "rt");
742 if (fp != NULL) {
743 while (fgets(line, sizeof(line), fp) != NULL)
744 {
745 char label[32], device[32];
746 unsigned long size = 0;
747
748 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
749
750 // Skip header, annotation and blank lines
751 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
752 continue;
753
754 tmpdevice = "/dev/";
755 tmpdevice += label;
756 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
757 Size = size;
758 fclose(fp);
759 return true;
760 }
761 }
762 }
763
Dees_Troy5bf43922012-09-07 16:07:55 -0400764 // In this case, we'll first get the partitions we care about (with labels)
765 fp = fopen("/proc/partitions", "rt");
766 if (fp == NULL)
767 return false;
768
769 while (fgets(line, sizeof(line), fp) != NULL)
770 {
771 unsigned long major, minor, blocks;
772 char device[512];
773 char tmpString[64];
774
Dees_Troy63c8df72012-09-10 14:02:05 -0400775 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400776 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
777
778 tmpdevice = "/dev/block/";
779 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400780 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400781 // Adjust block size to byte size
782 Size = blocks * 1024ULL;
783 fclose(fp);
784 return true;
785 }
786 }
787 fclose(fp);
788 return false;
789}
790
Dees_Troy5bf43922012-09-07 16:07:55 -0400791bool TWPartition::Is_Mounted(void) {
792 if (!Can_Be_Mounted)
793 return false;
794
795 struct stat st1, st2;
796 string test_path;
797
798 // Check to see if the mount point directory exists
799 test_path = Mount_Point + "/.";
800 if (stat(test_path.c_str(), &st1) != 0) return false;
801
802 // Check to see if the directory above the mount point exists
803 test_path = Mount_Point + "/../.";
804 if (stat(test_path.c_str(), &st2) != 0) return false;
805
806 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
807 int ret = (st1.st_dev != st2.st_dev) ? true : false;
808
809 return ret;
810}
811
812bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000813 int exfat_mounted = 0;
814
Dees_Troy5bf43922012-09-07 16:07:55 -0400815 if (Is_Mounted()) {
816 return true;
817 } else if (!Can_Be_Mounted) {
818 return false;
819 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400820
821 Find_Actual_Block_Device();
822
823 // Check the current file system before mounting
824 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000825 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000826 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 +0000827 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000828 string result;
829 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000830 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000831 Current_File_System = "vfat";
832 } else {
833#ifdef TW_NO_EXFAT_FUSE
834 UnMount(false);
835 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
836 // Some kernels let us mount vfat as exfat which doesn't work out too well
837#else
838 exfat_mounted = 1;
839#endif
840 }
841 }
Dees_Troy22042032012-12-18 21:23:08 +0000842 if (Fstab_File_System == "yaffs2") {
843 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000844 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
845 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
846 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
847 if (Display_Error)
848 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
849 else
850 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
851 return false;
852 } else {
853 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
854 return true;
855 }
856 } else {
857 struct stat st;
858 string test_path = Mount_Point;
859 if (stat(test_path.c_str(), &st) < 0) {
860 if (Display_Error)
861 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
862 else
863 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
864 return false;
865 }
866 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
867 if (new_mode != st.st_mode) {
868 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
869 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
870 if (Display_Error)
871 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
872 else
873 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
874 return false;
875 }
876 }
Dees_Troy22042032012-12-18 21:23:08 +0000877 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000878 }
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000879 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
880#ifdef TW_NO_EXFAT_FUSE
881 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000882 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000883 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000884 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000885 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000886 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000887 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
888 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 +0000889 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000890 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000891 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000892#endif
893 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000894 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000895 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000896 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
897 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 +0000898 return false;
899#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000900 }
901#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000902 }
903#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
904 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
905 MetaEcfsFile += "/.MetaEcfsFile";
906 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
907 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
908 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000909 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000910 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000911 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000912 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000913 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000914 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400915 }
Dees_Troy066eb302013-08-23 17:20:32 +0000916 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
917 if (Is_Decrypted)
918 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000919 Is_Decrypted = false;
920 }
921#endif
922 if (Removable)
923 Update_Size(Display_Error);
924
925 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +0200926 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
927 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -0400928 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400929 return true;
930}
931
932bool TWPartition::UnMount(bool Display_Error) {
933 if (Is_Mounted()) {
934 int never_unmount_system;
935
936 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
937 if (never_unmount_system == 1 && Mount_Point == "/system")
938 return true; // Never unmount system if you're not supposed to unmount it
939
Dees_Troyc8bafa12013-01-10 15:43:00 +0000940#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
941 if (EcryptFS_Password.size() > 0) {
942 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
943 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000944 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000945 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000946 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000947 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000948 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000949 }
950 }
951#endif
952
Dees_Troy38bd7602012-09-14 13:33:53 -0400953 if (!Symlink_Mount_Point.empty())
954 umount(Symlink_Mount_Point.c_str());
955
Dees_Troyb05ddee2013-01-28 20:24:50 +0000956 umount(Mount_Point.c_str());
957 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400958 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000959 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400960 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000961 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400962 return false;
963 } else
964 return true;
965 } else {
966 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400967 }
968}
969
Gary Peck43acadf2012-11-21 21:19:01 -0800970bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +0000971 bool wiped = false, update_crypt = false;
972 int check;
973 string Layout_Filename = Mount_Point + "/.layout_version";
974
Dees_Troy38bd7602012-09-14 13:33:53 -0400975 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000976 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400977 return false;
978 }
979
Dees_Troyc51f1f92012-09-20 15:32:13 -0400980 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +0000981 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400982
Dees_Troyce675462013-01-09 19:48:21 +0000983#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
984 if (Mount_Point == "/data" && Mount(false)) {
985 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
986 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
987 }
988#endif
989
Dees_Troy16c2b312013-01-15 16:51:18 +0000990 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
991 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
992 else
993 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -0400994
Dees_Troy16c2b312013-01-15 16:51:18 +0000995 if (Has_Data_Media) {
996 wiped = Wipe_Data_Without_Wiping_Media();
997 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -0800998
Dees_Troy16c2b312013-01-15 16:51:18 +0000999 DataManager::GetValue(TW_RM_RF_VAR, check);
1000
Hashcodedabfd492013-08-29 22:45:30 -07001001 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001002 wiped = Wipe_RMRF();
1003 else if (New_File_System == "ext4")
1004 wiped = Wipe_EXT4();
1005 else if (New_File_System == "ext2" || New_File_System == "ext3")
1006 wiped = Wipe_EXT23(New_File_System);
1007 else if (New_File_System == "vfat")
1008 wiped = Wipe_FAT();
1009 else if (New_File_System == "exfat")
1010 wiped = Wipe_EXFAT();
1011 else if (New_File_System == "yaffs2")
1012 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001013 else if (New_File_System == "f2fs")
1014 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001015 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001016 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 +00001017 unlink("/.layout_version");
1018 return false;
1019 }
1020 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001021 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001022
Gary Pecke8bc5d72012-12-21 06:45:25 -08001023 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001024#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1025 if (Mount_Point == "/data" && Mount(false)) {
1026 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1027 Make_Dir("/data/system", true);
1028 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1029 }
1030 }
1031#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001032
Dees_Troy1c1ac442013-01-17 21:42:14 +00001033 if (Mount_Point == "/cache")
1034 DataManager::Output_Version();
1035
Dees_Troy16c2b312013-01-15 16:51:18 +00001036 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1037 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1038
1039 if (update_crypt) {
1040 Setup_File_System(false);
1041 if (Is_Encrypted && !Is_Decrypted) {
1042 // just wiped an encrypted partition back to its unencrypted state
1043 Is_Encrypted = false;
1044 Is_Decrypted = false;
1045 Decrypted_Block_Device = "";
1046 if (Mount_Point == "/data") {
1047 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1048 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1049 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001050 }
1051 }
1052 }
1053 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001054}
1055
Gary Peck43acadf2012-11-21 21:19:01 -08001056bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001057 if (Is_File_System(Current_File_System))
1058 return Wipe(Current_File_System);
1059 else
1060 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001061}
1062
Dees_Troye58d5262012-09-21 12:27:57 -04001063bool TWPartition::Wipe_AndSec(void) {
1064 if (!Has_Android_Secure)
1065 return false;
1066
Dees_Troye58d5262012-09-21 12:27:57 -04001067 if (!Mount(true))
1068 return false;
1069
Dees_Troy2673cec2013-04-02 20:22:16 +00001070 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001071 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001072 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001073}
1074
Dees_Troy51a0e822012-09-05 15:24:24 -04001075bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001076 if (Backup_Method == FILES)
1077 return Backup_Tar(backup_folder);
1078 else if (Backup_Method == DD)
1079 return Backup_DD(backup_folder);
1080 else if (Backup_Method == FLASH_UTILS)
1081 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001082 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001083 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001084}
1085
Dees_Troy43d8b002012-09-17 16:00:01 -04001086bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001087 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001088 char split_filename[512];
1089 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001090 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001091
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001092 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001093 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001094 if (!TWFunc::Path_Exists(Full_Filename)) {
1095 // This is a split archive, we presume
1096 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001097 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001098 md5file = split_filename;
1099 md5file += ".md5";
1100 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001101 LOGERR("No md5 file found for '%s'.\n", split_filename);
1102 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001103 return false;
1104 }
1105 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001106 while (index < 1000) {
1107 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001108 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001109 return false;
1110 }
1111 index++;
1112 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001113 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001114 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001115 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001116 } else {
1117 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001118 md5file = Full_Filename + ".md5";
1119 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001120 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1121 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001122 return false;
1123 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001124 md5sum.setfn(Full_Filename);
1125 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001126 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001127 return false;
1128 } else
1129 return true;
1130 }
1131 return false;
1132}
1133
Dees_Troy51a0e822012-09-05 15:24:24 -04001134bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001135 size_t first_period, second_period;
1136 string Restore_File_System;
1137
1138 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001139 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001140
1141 // Parse backup filename to extract the file system before wiping
1142 first_period = Backup_FileName.find(".");
1143 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001144 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001145 return false;
1146 }
1147 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1148 second_period = Restore_File_System.find(".");
1149 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001150 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001151 return false;
1152 }
1153 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001154 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001155
1156 if (Is_File_System(Restore_File_System))
1157 return Restore_Tar(restore_folder, Restore_File_System);
1158 else if (Is_Image(Restore_File_System)) {
1159 if (Restore_File_System == "emmc")
1160 return Restore_DD(restore_folder);
1161 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1162 return Restore_Flash_Image(restore_folder);
1163 }
1164
Dees_Troy2673cec2013-04-02 20:22:16 +00001165 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001166 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001167}
1168
1169string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001170 if (Backup_Method == NONE)
1171 return "none";
1172 else if (Backup_Method == FILES)
1173 return "files";
1174 else if (Backup_Method == DD)
1175 return "dd";
1176 else if (Backup_Method == FLASH_UTILS)
1177 return "flash_utils";
1178 else
1179 return "undefined";
1180 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001181}
1182
1183bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001184 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001185 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001186 return 1;
1187}
1188
1189bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001190 bool Save_Data_Media = Has_Data_Media;
1191
1192 if (!UnMount(true))
1193 return false;
1194
Dees_Troy38bd7602012-09-14 13:33:53 -04001195 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001196 Decrypted_Block_Device = "";
1197 Is_Decrypted = false;
1198 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001199 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001200 Has_Data_Media = Save_Data_Media;
1201 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1202 Recreate_Media_Folder();
1203 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001204 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001205 return true;
1206 } else {
1207 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001208 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001209 return false;
1210 }
1211 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001212}
1213
1214void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001215 const char* type;
1216 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001217
Dees_Troy68cab492012-12-12 19:29:35 +00001218 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1219 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001220
Dees_Troy38bd7602012-09-14 13:33:53 -04001221 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001222 if (!Is_Present)
1223 return;
Dees_Troy51127312012-09-08 13:08:49 -04001224
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001225 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1226 if (blkid_do_fullprobe(pr)) {
1227 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001228 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001229 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001230 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001231
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001232 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1233 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001234 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001235 return;
1236 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001237
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001238 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001239 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001240}
1241
Gary Peck43acadf2012-11-21 21:19:01 -08001242bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001243 if (!UnMount(true))
1244 return false;
1245
Dees_Troy43d8b002012-09-17 16:00:01 -04001246 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001247 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001248
Dees_Troy2673cec2013-04-02 20:22:16 +00001249 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001250 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001251 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001252 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001253 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001254 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001255 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001256 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001257 return true;
1258 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001259 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001260 return false;
1261 }
1262 } else
1263 return Wipe_RMRF();
1264
1265 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001266}
1267
1268bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001269 if (!UnMount(true))
1270 return false;
1271
Dees_Troyb3265ab2013-08-30 02:59:14 +00001272#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Dees_Troya95f55c2013-08-17 13:14:43 +00001273 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Dees_Troya95f55c2013-08-17 13:14:43 +00001274 if (make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle) != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001275 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1276 return false;
1277 } else {
bigbiff bigbiffd0c24c52013-10-11 20:28:00 -04001278 #ifdef HAVE_SELINUX
1279 string sedir = Mount_Point + "/lost+found";
1280 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1281 rmdir(sedir.c_str());
1282 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
1283 #endif
Dees_Troya95f55c2013-08-17 13:14:43 +00001284 return true;
1285 }
1286#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001287 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001288 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001289
Dees_Troy2673cec2013-04-02 20:22:16 +00001290 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001291 Find_Actual_Block_Device();
1292 Command = "make_ext4fs";
1293 if (!Is_Decrypted && Length != 0) {
1294 // Only use length if we're not decrypted
1295 char len[32];
1296 sprintf(len, "%i", Length);
1297 Command += " -l ";
1298 Command += len;
1299 }
Dees_Troy5295d582013-09-06 15:51:08 +00001300 if (TWFunc::Path_Exists("/file_contexts")) {
1301 Command += " -S /file_contexts";
1302 }
1303 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001304 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001305 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001306 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001307 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001308 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001309 return true;
1310 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001311 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001312 return false;
1313 }
1314 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001315 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001316#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001317 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001318}
1319
1320bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001321 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001322
Dees_Troy43d8b002012-09-17 16:00:01 -04001323 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001324 if (!UnMount(true))
1325 return false;
1326
Dees_Troy2673cec2013-04-02 20:22:16 +00001327 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001328 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001329 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001330 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001331 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001332 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001333 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001334 return true;
1335 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001336 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001337 return false;
1338 }
1339 return true;
1340 }
1341 else
1342 return Wipe_RMRF();
1343
1344 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001345}
1346
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001347bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001348 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001349
1350 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1351 if (!UnMount(true))
1352 return false;
1353
Dees_Troy2673cec2013-04-02 20:22:16 +00001354 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001355 Find_Actual_Block_Device();
1356 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001357 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001358 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001359 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001360 return true;
1361 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001362 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001363 return false;
1364 }
1365 return true;
1366 }
1367 return false;
1368}
1369
Dees_Troy38bd7602012-09-14 13:33:53 -04001370bool TWPartition::Wipe_MTD() {
1371 if (!UnMount(true))
1372 return false;
1373
Dees_Troy2673cec2013-04-02 20:22:16 +00001374 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001375
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001376 mtd_scan_partitions();
1377 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1378 if (mtd == NULL) {
1379 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1380 return false;
1381 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001382
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001383 MtdWriteContext* ctx = mtd_write_partition(mtd);
1384 if (ctx == NULL) {
1385 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1386 return false;
1387 }
1388 if (mtd_erase_blocks(ctx, -1) == -1) {
1389 mtd_write_close(ctx);
1390 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1391 return false;
1392 }
1393 if (mtd_write_close(ctx) != 0) {
1394 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1395 return false;
1396 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001397 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001398 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001399 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001400 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001401}
1402
1403bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001404 if (!Mount(true))
1405 return false;
1406
Dees_Troy2673cec2013-04-02 20:22:16 +00001407 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001408 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001409 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001410 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001411}
1412
Dees_Troye5017042013-08-29 16:38:55 +00001413bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001414 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001415
1416 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1417 if (!UnMount(true))
1418 return false;
1419
1420 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1421 Find_Actual_Block_Device();
1422 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001423 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001424 Recreate_AndSec_Folder();
1425 gui_print("Done.\n");
1426 return true;
1427 } else {
1428 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1429 return false;
1430 }
1431 return true;
1432 } else {
1433 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1434 return Wipe_RMRF();
1435 }
1436 return false;
1437}
1438
Dees_Troy51a0e822012-09-05 15:24:24 -04001439bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001440 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001441
1442 // This handles wiping data on devices with "sdcard" in /data/media
1443 if (!Mount(true))
1444 return false;
1445
Dees_Troy2673cec2013-04-02 20:22:16 +00001446 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001447
1448 DIR* d;
1449 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001450 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001451 struct dirent* de;
1452 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001453 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1454 // The media folder is the "internal sdcard"
1455 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001456 // the media folder for multi-user.
Dees_Troy16b74352012-11-14 22:27:31 +00001457 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001458
1459 dir = "/data/";
1460 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001461 if (de->d_type == DT_DIR) {
1462 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001463 } 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 +00001464 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001465 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001466 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001467 }
1468 closedir(d);
Dees_Troy2673cec2013-04-02 20:22:16 +00001469 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001470 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001471 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001472 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001473 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001474}
1475
1476bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001477 char back_name[255], split_index[5];
1478 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001479 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001480 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001481 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001482 twrpTar tar;
1483 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001484
Dees_Troy43d8b002012-09-17 16:00:01 -04001485 if (!Mount(true))
1486 return false;
1487
Dees_Troya13d74f2013-03-24 08:54:55 -05001488 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001489 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001490
1491 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001492 tar.use_compression = use_compression;
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001493 //exclude Google Music Cache
1494 tar.setexcl("/data/data/com.google.android.music/files");
Dees_Troy83bd4832013-05-04 12:39:56 +00001495#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1496 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1497 if (use_encryption && Can_Encrypt_Backup) {
1498 tar.use_encryption = use_encryption;
1499 if (Use_Userdata_Encryption)
1500 tar.userdata_encryption = use_encryption;
1501 } else {
1502 use_encryption = false;
1503 }
1504#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001505
1506 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1507 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001508 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001509 tar.has_data_media = Has_Data_Media;
1510 if (!use_encryption && Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001511 // This backup needs to be split into multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001512 gui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001513 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001514 tar.setdir(back_name);
1515 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001516 backup_count = tar.splitArchiveFork();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001517 if (backup_count == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001518 LOGERR("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001519 return false;
1520 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001521 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001522 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001523 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001524 tar.setdir(Backup_Path);
1525 tar.setfn(Full_FileName);
1526 if (tar.createTarFork() != 0)
1527 return false;
1528 if (use_compression && !use_encryption) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001529 string gzname = Full_FileName + ".gz";
1530 rename(gzname.c_str(), Full_FileName.c_str());
1531 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001532 if (use_encryption)
1533 Full_FileName += "000";
Dees_Troy7c2dec82012-09-26 09:49:14 -04001534 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001535 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001536 return false;
1537 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001538 }
1539 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001540}
1541
1542bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001543 char back_name[255], backup_size[32];
Vojtech Bocek05534202013-09-11 08:11:56 +02001544 string Full_FileName, Command, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001545 int use_compression;
1546
igoriok87e3d932013-01-31 21:03:53 +02001547 sprintf(backup_size, "%llu", Backup_Size);
1548 DD_BS = backup_size;
1549
Dees_Troyb46a6842012-09-25 11:06:46 -04001550 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001551 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001552
1553 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1554 Backup_FileName = back_name;
1555
1556 Full_FileName = backup_folder + "/" + Backup_FileName;
1557
igoriok87e3d932013-01-31 21:03:53 +02001558 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001559 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001560 TWFunc::Exec_Cmd(Command);
Dees_Troyc154ac22012-10-12 15:36:47 -04001561 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001562 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001563 return false;
1564 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001565 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001566}
1567
1568bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001569 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001570 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001571 int use_compression;
1572
Dees_Troyb46a6842012-09-25 11:06:46 -04001573 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001574 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001575
1576 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1577 Backup_FileName = back_name;
1578
1579 Full_FileName = backup_folder + "/" + Backup_FileName;
1580
1581 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001582 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001583 TWFunc::Exec_Cmd(Command);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001584 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1585 // 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 +00001586 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001587 return false;
1588 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001589 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001590}
1591
Gary Peck43acadf2012-11-21 21:19:01 -08001592bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1593 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001594 int index = 0;
1595 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001596
Dees_Troye58d5262012-09-21 12:27:57 -04001597 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001598 if (!Wipe_AndSec())
1599 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001600 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001601 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001602 if (!Wipe(Restore_File_System))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001603 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001604 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001605 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001606 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001607
1608 if (!Mount(true))
1609 return false;
1610
Dees_Troy4a2a1262012-09-18 09:33:47 -04001611 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001612 /*if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001613 if (!TWFunc::Path_Exists(Full_FileName)) {
1614 // Backup is multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001615 LOGINFO("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001616 sprintf(split_index, "%03i", index);
1617 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001618 while (TWFunc::Path_Exists(Full_FileName)) {
1619 index++;
Dees_Troy2673cec2013-04-02 20:22:16 +00001620 gui_print("Restoring archive %i...\n", index);
1621 LOGINFO("Restoring '%s'...\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001622 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001623 tar.setdir("/");
1624 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001625 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001626 return false;
1627 sprintf(split_index, "%03i", index);
1628 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1629 }
1630 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001631 LOGERR("Error locating restore file: '%s'\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001632 return false;
1633 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001634 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001635 } else {*/
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001636 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001637 tar.setdir(Backup_Path);
1638 tar.setfn(Full_FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +00001639 tar.backup_name = Backup_Name;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001640 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001641 return false;
Dees_Troy83bd4832013-05-04 12:39:56 +00001642 //}
Dees_Troy43d8b002012-09-17 16:00:01 -04001643 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001644}
1645
1646bool TWPartition::Restore_DD(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001647 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001648
Dees_Troyda8b55a2012-12-12 19:18:30 +00001649 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001650 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001651
1652 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001653 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001654 return false;
1655 }
1656 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1657 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001658 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001659 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1660 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1661 return false;
1662 }
1663
Dees_Troy2673cec2013-04-02 20:22:16 +00001664 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001665 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001666 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001667 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001668 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001669}
1670
1671bool TWPartition::Restore_Flash_Image(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001672 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001673
Dees_Troy2673cec2013-04-02 20:22:16 +00001674 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001675 Full_FileName = restore_folder + "/" + Backup_FileName;
1676 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1677 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001678 LOGINFO("Erase command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001679 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001680 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001681 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001682 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001683 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001684}
Dees_Troy5bf43922012-09-07 16:07:55 -04001685
1686bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001687 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001688
Dees_Troyab10ee22012-09-21 14:27:30 -04001689 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001690 return false;
1691
Dees_Troy0550cfb2012-10-13 11:56:13 -04001692 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001693 if (Removable || Is_Encrypted) {
1694 if (!Mount(false))
1695 return true;
1696 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001697 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001698
1699 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001700 if (!ret || Size == 0) {
1701 if (!Get_Size_Via_df(Display_Error)) {
1702 if (!Was_Already_Mounted)
1703 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001704 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001705 }
1706 }
Dees_Troy51127312012-09-08 13:08:49 -04001707
Dees_Troy5bf43922012-09-07 16:07:55 -04001708 if (Has_Data_Media) {
1709 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001710 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001711 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1712 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001713 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001714 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001715 int bak = (int)(Backup_Size / 1048576LLU);
1716 int total = (int)(Size / 1048576LLU);
1717 int us = (int)(Used / 1048576LLU);
1718 int fre = (int)(Free / 1048576LLU);
1719 int datmed = (int)(data_media_used / 1048576LLU);
Dees_Troy2673cec2013-04-02 20:22:16 +00001720 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 -04001721 } else {
1722 if (!Was_Already_Mounted)
1723 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001724 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001725 }
Dees_Troye58d5262012-09-21 12:27:57 -04001726 } else if (Has_Android_Secure) {
1727 if (Mount(Display_Error))
1728 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001729 else {
1730 if (!Was_Already_Mounted)
1731 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001732 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001733 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001734 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001735 if (!Was_Already_Mounted)
1736 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001737 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001738}
Dees_Troy38bd7602012-09-14 13:33:53 -04001739
1740void TWPartition::Find_Actual_Block_Device(void) {
1741 if (Is_Decrypted) {
1742 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001743 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001744 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001745 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001746 Is_Present = true;
1747 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001748 return;
1749 }
1750 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001751 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001752 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001753 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001754 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001755 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001756 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001757}
1758
1759void TWPartition::Recreate_Media_Folder(void) {
1760 string Command;
1761
1762 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001763 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001764 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001765 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00001766 LOGINFO("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001767 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1768 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001769 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001770}
Dees_Troye58d5262012-09-21 12:27:57 -04001771
1772void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001773 if (!Has_Android_Secure)
1774 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00001775 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001776 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001777 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001778 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001779 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001780 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1781 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1782 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001783 }
1784}