blob: 061a3e18971161c94af6cebff4c5294cd3117892 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050027#include <iostream>
28#include <sstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040029
Dees_Troy657c3092012-09-10 20:32:10 -040030#ifdef TW_INCLUDE_CRYPTO
31 #include "cutils/properties.h"
32#endif
33
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050034#include "libblkid/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000036#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040037#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040038#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040039#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050040#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041#include "twrpTar.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040042extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040043 #include "mtdutils/mtdutils.h"
44 #include "mtdutils/mounts.h"
Dees_Troy85f44ed2013-01-09 18:42:36 +000045#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
46 #include "crypto/libcrypt_samsung/include/libcrypt_samsung.h"
47#endif
Dees_Troya95f55c2013-08-17 13:14:43 +000048#ifdef USE_EXT4
49 #include "make_ext4fs.h"
50#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040051}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040052#ifdef HAVE_SELINUX
53#include "selinux/selinux.h"
54#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040055
bigbiff bigbiff9c754052013-01-09 09:09:08 -050056using namespace std;
57
Dees_Troya95f55c2013-08-17 13:14:43 +000058extern struct selabel_handle *selinux_handle;
59
Hashcode62bd9e02013-11-19 21:59:42 -080060struct flag_list {
61 const char *name;
62 unsigned flag;
63};
64
65static struct flag_list mount_flags[] = {
66 { "noatime", MS_NOATIME },
67 { "noexec", MS_NOEXEC },
68 { "nosuid", MS_NOSUID },
69 { "nodev", MS_NODEV },
70 { "nodiratime", MS_NODIRATIME },
71 { "ro", MS_RDONLY },
72 { "rw", 0 },
73 { "remount", MS_REMOUNT },
74 { "bind", MS_BIND },
75 { "rec", MS_REC },
76 { "unbindable", MS_UNBINDABLE },
77 { "private", MS_PRIVATE },
78 { "slave", MS_SLAVE },
79 { "shared", MS_SHARED },
80 { "sync", MS_SYNCHRONOUS },
81 { "defaults", 0 },
82 { 0, 0 },
83};
84
Dees_Troy51a0e822012-09-05 15:24:24 -040085TWPartition::TWPartition(void) {
86 Can_Be_Mounted = false;
87 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050088 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +020089 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040090 Wipe_During_Factory_Reset = false;
91 Wipe_Available_in_GUI = false;
92 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040093 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040094 SubPartition_Of = "";
95 Symlink_Path = "";
96 Symlink_Mount_Point = "";
97 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040098 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040099 Actual_Block_Device = "";
100 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400101 Alternate_Block_Device = "";
102 Removable = false;
103 Is_Present = false;
104 Length = 0;
105 Size = 0;
106 Used = 0;
107 Free = 0;
108 Backup_Size = 0;
109 Can_Be_Encrypted = false;
110 Is_Encrypted = false;
111 Is_Decrypted = false;
112 Decrypted_Block_Device = "";
113 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500114 Backup_Display_Name = "";
115 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400116 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400117 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400118 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400119 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000120 Can_Encrypt_Backup = false;
121 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400122 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400123 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400124 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500125 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400126 Storage_Path = "";
127 Current_File_System = "";
128 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800129 Mount_Flags = 0;
130 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400131 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000132 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000133 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000134#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
135 EcryptFS_Password = "";
136#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400137}
138
139TWPartition::~TWPartition(void) {
140 // Do nothing
141}
142
Dees_Troy5bf43922012-09-07 16:07:55 -0400143bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
144 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
145 int line_len = Line.size(), index = 0, item_index = 0;
146 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400147 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400148 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500149 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400150
Dees_Troy51127312012-09-08 13:08:49 -0400151 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500152 if (full_line[index] == 34)
153 skip = !skip;
154 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400155 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400156 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400157 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000158 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400159 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500160 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000161 Display_Name = full_line + 1;
162 Backup_Display_Name = Display_Name;
163 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400164 index = Mount_Point.size();
165 while (index < line_len) {
166 while (index < line_len && full_line[index] == '\0')
167 index++;
168 if (index >= line_len)
169 continue;
170 ptr = full_line + index;
171 if (item_index == 0) {
172 // File System
173 Fstab_File_System = ptr;
174 Current_File_System = ptr;
175 item_index++;
176 } else if (item_index == 1) {
177 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400178 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400179 MTD_Name = ptr;
180 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400181 } else if (Fstab_File_System == "bml") {
182 if (Mount_Point == "/boot")
183 MTD_Name = "boot";
184 else if (Mount_Point == "/recovery")
185 MTD_Name = "recovery";
186 Primary_Block_Device = ptr;
187 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000188 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 -0400189 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400190 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000191 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400192 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000193 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400194 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400195 } else {
196 Primary_Block_Device = ptr;
197 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400198 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400199 item_index++;
200 } else if (item_index > 1) {
201 if (*ptr == '/') {
202 // Alternate Block Device
203 Alternate_Block_Device = ptr;
204 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
205 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
206 // Partition length
207 ptr += 7;
208 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400209 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
210 // Custom flags, save for later so that new values aren't overwritten by defaults
211 ptr += 6;
212 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000213 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400214 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
215 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400216 } else {
217 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000218 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400219 }
220 }
221 while (index < line_len && full_line[index] != '\0')
222 index++;
223 }
224
225 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
226 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000227 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400228 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000229 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400230 return 0;
231 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400232 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400233 Setup_File_System(Display_Error);
234 if (Mount_Point == "/system") {
235 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500236 Backup_Display_Name = Display_Name;
237 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400238 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500239 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400240 } else if (Mount_Point == "/data") {
241 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500242 Backup_Display_Name = Display_Name;
243 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400244 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400245 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500246 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000247 Can_Encrypt_Backup = true;
248 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400249#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troya13d74f2013-03-24 08:54:55 -0500250 Storage_Name = "Internal Storage";
Dees_Troy5bf43922012-09-07 16:07:55 -0400251 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400252 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500253 Is_Settings_Storage = true;
Dees_Troy51127312012-09-08 13:08:49 -0400254 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000255 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400256 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
257 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400258 Symlink_Mount_Point = "/emmc";
259 } else {
260 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400261 Symlink_Mount_Point = "/sdcard";
262 }
Dees_Troy16b74352012-11-14 22:27:31 +0000263 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
264 Storage_Path = "/data/media/0";
265 Symlink_Path = Storage_Path;
266 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
267 UnMount(true);
268 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400269#endif
270#ifdef TW_INCLUDE_CRYPTO
271 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400272 char crypto_blkdev[255];
273 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
274 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400275 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400276 DataManager::SetValue(TW_IS_DECRYPTED, 1);
277 Is_Encrypted = true;
278 Is_Decrypted = true;
279 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000280 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400281 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400282 Is_Encrypted = true;
283 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800284 Can_Be_Mounted = false;
285 Current_File_System = "emmc";
286 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400287 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
288 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
289 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800290 } else {
291 // Filesystem is not encrypted and the mount
292 // succeeded, so get it back to the original
293 // unmounted state
294 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400295 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400296 #ifdef RECOVERY_SDCARD_ON_DATA
297 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
298 Recreate_Media_Folder();
299 #endif
300#else
301 #ifdef RECOVERY_SDCARD_ON_DATA
302 Recreate_Media_Folder();
303 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400304#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400305 } else if (Mount_Point == "/cache") {
306 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500307 Backup_Display_Name = Display_Name;
308 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400309 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400310 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500311 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400312 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000313 LOGINFO("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500314 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
315 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400316 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400317 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400318 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400319 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500320 Backup_Display_Name = Display_Name;
321 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400322 Is_SubPartition = true;
323 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400324 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500325 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000326 Can_Encrypt_Backup = true;
327 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400328 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400329 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400330 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500331 Backup_Display_Name = Display_Name;
332 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400333 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400334 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500335 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000336 Can_Encrypt_Backup = true;
337 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400338 } else if (Mount_Point == "/boot") {
339 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500340 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400341 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500342 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400343 }
344#ifdef TW_EXTERNAL_STORAGE_PATH
345 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
346 Is_Storage = true;
347 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400348 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500349 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400350#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000351 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400352 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400353 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500354 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400355#ifndef RECOVERY_SDCARD_ON_DATA
356 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400357 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400358#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400359#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000360 }
Dees_Troy8170a922012-09-18 15:40:25 -0400361#ifdef TW_INTERNAL_STORAGE_PATH
362 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
363 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500364 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400365 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500366 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400367#ifndef RECOVERY_SDCARD_ON_DATA
368 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400369 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400370#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400371 }
372#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000373 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400374 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500375 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500376 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400377#ifndef RECOVERY_SDCARD_ON_DATA
378 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400379 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400380#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400381 }
382#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400383 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400384 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400385 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500386 if (Mount_Point == "/boot") {
387 Display_Name = "Boot";
388 Backup_Display_Name = Display_Name;
389 Can_Be_Backed_Up = true;
390 } else if (Mount_Point == "/recovery") {
391 Display_Name = "Recovery";
392 Backup_Display_Name = Display_Name;
393 Can_Be_Backed_Up = true;
394 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400395 }
396
Dees_Troy51127312012-09-08 13:08:49 -0400397 // Process any custom flags
398 if (Flags.size() > 0)
399 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400400 return true;
401}
402
Hashcode62bd9e02013-11-19 21:59:42 -0800403bool TWPartition::Process_FS_Flags(string& Options, int Flags) {
404 int i;
405 char *p;
406 char *savep;
407 char fs_options[250];
408
409 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
410 Options = "";
411
412 p = strtok_r(fs_options, ",", &savep);
413 while (p) {
414 /* Look for the flag "p" in the flag list "fl"
415 * If not found, the loop exits with fl[i].name being null.
416 */
417 for (i = 0; mount_flags[i].name; i++) {
418 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
419 Flags |= mount_flags[i].flag;
420 break;
421 }
422 }
423
424 if (!mount_flags[i].name) {
425 if (Options.size() > 0)
426 Options += ",";
427 Options += p;
428 }
429 p = strtok_r(NULL, ",", &savep);
430 }
431
432 return true;
433}
434
Dees_Troy51127312012-09-08 13:08:49 -0400435bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
436 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500437 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400438 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500439 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400440
441 strcpy(flags, Flags.c_str());
442 flags_len = Flags.size();
443 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500444 if (flags[index] == 34)
445 skip = !skip;
446 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400447 flags[index] = '\0';
448 }
449
450 index = 0;
451 while (index < flags_len) {
452 while (index < flags_len && flags[index] == '\0')
453 index++;
454 if (index >= flags_len)
455 continue;
456 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500457 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400458 if (strcmp(ptr, "removable") == 0) {
459 Removable = true;
460 } else if (strcmp(ptr, "storage") == 0) {
461 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500462 } else if (strcmp(ptr, "settingsstorage") == 0) {
463 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400464 } else if (strcmp(ptr, "canbewiped") == 0) {
465 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700466 } else if (strcmp(ptr, "usermrf") == 0) {
467 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500468 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
469 ptr += 7;
470 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
471 Can_Be_Backed_Up = true;
472 else
473 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400474 } else if (strcmp(ptr, "wipeingui") == 0) {
475 Can_Be_Wiped = true;
476 Wipe_Available_in_GUI = true;
477 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
478 Can_Be_Wiped = true;
479 Wipe_Available_in_GUI = true;
480 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500481 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000482 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400483 Is_SubPartition = true;
484 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000485 } else if (strcmp(ptr, "ignoreblkid") == 0) {
486 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000487 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
488 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500489 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400490 ptr += 8;
491 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500492 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
493 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400494 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500495 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400496 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500497 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
498 Display_Name.resize(Display_Name.size() - 1);
499 }
500 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
501 has_storage_name = true;
502 ptr += 11;
503 if (*ptr == '\"') ptr++;
504 Storage_Name = ptr;
505 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
506 Storage_Name.resize(Storage_Name.size() - 1);
507 }
508 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
509 has_backup_name = true;
510 ptr += 10;
511 if (*ptr == '\"') ptr++;
512 Backup_Display_Name = ptr;
513 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
514 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
515 }
516 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400517 ptr += 10;
518 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500519 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400520 ptr += 7;
521 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000522 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
523 ptr += 17;
524 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
525 Can_Encrypt_Backup = true;
526 else
527 Can_Encrypt_Backup = false;
528 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
529 ptr += 21;
530 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
531 Can_Encrypt_Backup = true;
532 Use_Userdata_Encryption = true;
533 } else {
534 Use_Userdata_Encryption = false;
535 }
Hashcode62bd9e02013-11-19 21:59:42 -0800536 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
537 ptr += 8;
538 if (*ptr == '\"') ptr++;
539
540 Mount_Options = ptr;
541 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
542 Mount_Options.resize(Mount_Options.size() - 1);
543 }
544 Process_FS_Flags(Mount_Options, Mount_Flags);
Dees_Troy51127312012-09-08 13:08:49 -0400545 } else {
546 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000547 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400548 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000549 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400550 }
551 while (index < flags_len && flags[index] != '\0')
552 index++;
553 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500554 if (has_display_name && !has_storage_name)
555 Storage_Name = Display_Name;
556 if (!has_display_name && has_storage_name)
557 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000558 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500559 Backup_Display_Name = Display_Name;
560 if (!has_display_name && has_backup_name)
561 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400562 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400563}
564
Dees_Troy5bf43922012-09-07 16:07:55 -0400565bool TWPartition::Is_File_System(string File_System) {
566 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400567 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400568 File_System == "ext4" ||
569 File_System == "vfat" ||
570 File_System == "ntfs" ||
571 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500572 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000573 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400574 File_System == "auto")
575 return true;
576 else
577 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400578}
579
Dees_Troy5bf43922012-09-07 16:07:55 -0400580bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400581 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400582 return true;
583 else
584 return false;
585}
586
Dees_Troy51127312012-09-08 13:08:49 -0400587bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400588 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400589 if (mkdir(Path.c_str(), 0777) == -1) {
590 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000591 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400592 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000593 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400594 return false;
595 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000596 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400597 return true;
598 }
599 }
600 return true;
601}
602
Dees_Troy5bf43922012-09-07 16:07:55 -0400603void TWPartition::Setup_File_System(bool Display_Error) {
604 struct statfs st;
605
606 Can_Be_Mounted = true;
607 Can_Be_Wiped = true;
608
Dees_Troy5bf43922012-09-07 16:07:55 -0400609 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400610 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400611 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
612 Backup_Name = Display_Name;
613 Backup_Method = FILES;
614}
615
616void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400617 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
618 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800619 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400620 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800621 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400622 Backup_Method = FLASH_UTILS;
623 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000624 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 -0400625 if (Find_Partition_Size()) {
626 Used = Size;
627 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400628 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400629 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000630 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400631 else
Dees Troyd932ce12013-10-18 17:12:59 +0000632 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400633 }
634}
635
Dees_Troye58d5262012-09-21 12:27:57 -0400636void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500637 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400638 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500639 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400640 Has_Android_Secure = true;
641 Symlink_Path = Mount_Point + "/.android_secure";
642 Symlink_Mount_Point = "/and-sec";
643 Backup_Path = Symlink_Mount_Point;
644 Make_Dir("/and-sec", true);
645 Recreate_AndSec_Folder();
646}
647
Dees_Troy5bf43922012-09-07 16:07:55 -0400648void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
649 char device[512], realDevice[512];
650
651 strcpy(device, Block.c_str());
652 memset(realDevice, 0, sizeof(realDevice));
653 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
654 {
655 strcpy(device, realDevice);
656 memset(realDevice, 0, sizeof(realDevice));
657 }
658
659 if (device[0] != '/') {
660 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000661 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400662 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000663 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400664 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400665 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400666 Block = device;
667 return;
668 }
669}
670
Dees_Troy8e337f32012-10-13 22:07:49 -0400671void TWPartition::Mount_Storage_Retry(void) {
672 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500673 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400674 int retry_count = 5;
675 while (retry_count > 0 && !Mount(false)) {
676 usleep(500000);
677 retry_count--;
678 }
679 Mount(true);
680 }
681}
682
Dees_Troy38bd7602012-09-14 13:33:53 -0400683bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
684 FILE *fp = NULL;
685 char line[255];
686
687 fp = fopen("/proc/mtd", "rt");
688 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000689 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400690 return false;
691 }
692
693 while (fgets(line, sizeof(line), fp) != NULL)
694 {
695 char device[32], label[32];
696 unsigned long size = 0;
697 char* fstype = NULL;
698 int deviceId;
699
700 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
701
702 // Skip header and blank lines
703 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
704 continue;
705
706 // Strip off the trailing " from the label
707 label[strlen(label)-1] = '\0';
708
709 if (strcmp(label, MTD_Name.c_str()) == 0) {
710 // We found our device
711 // Strip off the trailing : from the device
712 device[strlen(device)-1] = '\0';
713 if (sscanf(device,"mtd%d", &deviceId) == 1) {
714 sprintf(device, "/dev/block/mtdblock%d", deviceId);
715 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000716 fclose(fp);
717 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400718 }
719 }
720 }
721 fclose(fp);
722
723 return false;
724}
725
Dees_Troy51127312012-09-08 13:08:49 -0400726bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
727 struct statfs st;
728 string Local_Path = Mount_Point + "/.";
729
730 if (!Mount(Display_Error))
731 return false;
732
733 if (statfs(Local_Path.c_str(), &st) != 0) {
734 if (!Removable) {
735 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000736 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400737 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000738 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400739 }
740 return false;
741 }
742 Size = (st.f_blocks * st.f_bsize);
743 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
744 Free = (st.f_bfree * st.f_bsize);
745 Backup_Size = Used;
746 return true;
747}
748
749bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400750 FILE* fp;
751 char command[255], line[512];
752 int include_block = 1;
753 unsigned int min_len;
754
755 if (!Mount(Display_Error))
756 return false;
757
Dees_Troy38bd7602012-09-14 13:33:53 -0400758 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400759 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200760 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400761 fp = fopen("/tmp/dfoutput.txt", "rt");
762 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000763 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400764 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400765 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400766
767 while (fgets(line, sizeof(line), fp) != NULL)
768 {
769 unsigned long blocks, used, available;
770 char device[64];
771 char tmpString[64];
772
773 if (strncmp(line, "Filesystem", 10) == 0)
774 continue;
775 if (strlen(line) < min_len) {
776 include_block = 0;
777 continue;
778 }
779 if (include_block) {
780 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
781 } else {
782 // The device block string is so long that the df information is on the next line
783 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400784 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400785 while (tmpString[space_count] == 32)
786 space_count++;
787 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
788 }
789
790 // Adjust block size to byte size
791 Size = blocks * 1024ULL;
792 Used = used * 1024ULL;
793 Free = available * 1024ULL;
794 Backup_Size = Used;
795 }
796 fclose(fp);
797 return true;
798}
799
Dees_Troy5bf43922012-09-07 16:07:55 -0400800bool TWPartition::Find_Partition_Size(void) {
801 FILE* fp;
802 char line[512];
803 string tmpdevice;
804
igoriok87e3d932013-01-31 21:03:53 +0200805 fp = fopen("/proc/dumchar_info", "rt");
806 if (fp != NULL) {
807 while (fgets(line, sizeof(line), fp) != NULL)
808 {
809 char label[32], device[32];
810 unsigned long size = 0;
811
812 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
813
814 // Skip header, annotation and blank lines
815 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
816 continue;
817
818 tmpdevice = "/dev/";
819 tmpdevice += label;
820 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
821 Size = size;
822 fclose(fp);
823 return true;
824 }
825 }
826 }
827
Dees_Troy5bf43922012-09-07 16:07:55 -0400828 // In this case, we'll first get the partitions we care about (with labels)
829 fp = fopen("/proc/partitions", "rt");
830 if (fp == NULL)
831 return false;
832
833 while (fgets(line, sizeof(line), fp) != NULL)
834 {
835 unsigned long major, minor, blocks;
836 char device[512];
837 char tmpString[64];
838
Dees_Troy63c8df72012-09-10 14:02:05 -0400839 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400840 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
841
842 tmpdevice = "/dev/block/";
843 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400844 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400845 // Adjust block size to byte size
846 Size = blocks * 1024ULL;
847 fclose(fp);
848 return true;
849 }
850 }
851 fclose(fp);
852 return false;
853}
854
Dees_Troy5bf43922012-09-07 16:07:55 -0400855bool TWPartition::Is_Mounted(void) {
856 if (!Can_Be_Mounted)
857 return false;
858
859 struct stat st1, st2;
860 string test_path;
861
862 // Check to see if the mount point directory exists
863 test_path = Mount_Point + "/.";
864 if (stat(test_path.c_str(), &st1) != 0) return false;
865
866 // Check to see if the directory above the mount point exists
867 test_path = Mount_Point + "/../.";
868 if (stat(test_path.c_str(), &st2) != 0) return false;
869
870 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
871 int ret = (st1.st_dev != st2.st_dev) ? true : false;
872
873 return ret;
874}
875
876bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000877 int exfat_mounted = 0;
878
Dees_Troy5bf43922012-09-07 16:07:55 -0400879 if (Is_Mounted()) {
880 return true;
881 } else if (!Can_Be_Mounted) {
882 return false;
883 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400884
885 Find_Actual_Block_Device();
886
887 // Check the current file system before mounting
888 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000889 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000890 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 +0000891 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000892 string result;
893 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000894 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000895 Current_File_System = "vfat";
896 } else {
897#ifdef TW_NO_EXFAT_FUSE
898 UnMount(false);
899 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
900 // Some kernels let us mount vfat as exfat which doesn't work out too well
901#else
902 exfat_mounted = 1;
903#endif
904 }
905 }
Dees_Troy22042032012-12-18 21:23:08 +0000906 if (Fstab_File_System == "yaffs2") {
907 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000908 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
909 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
910 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
911 if (Display_Error)
912 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
913 else
914 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
915 return false;
916 } else {
917 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
918 return true;
919 }
920 } else {
921 struct stat st;
922 string test_path = Mount_Point;
923 if (stat(test_path.c_str(), &st) < 0) {
924 if (Display_Error)
925 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
926 else
927 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
928 return false;
929 }
930 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
931 if (new_mode != st.st_mode) {
932 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
933 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
934 if (Display_Error)
935 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
936 else
937 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
938 return false;
939 }
940 }
Dees_Troy22042032012-12-18 21:23:08 +0000941 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000942 }
Hashcode62bd9e02013-11-19 21:59:42 -0800943 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), Mount_Flags, Mount_Options.c_str()) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000944#ifdef TW_NO_EXFAT_FUSE
945 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000946 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000947 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000948 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000949 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000950 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000951 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800952 LOGINFO("Actual block device: '%s', current file system: '%s', flags: 0x%8x, options: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str(), Mount_Flags, Mount_Options.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000953 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000954 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000955 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000956#endif
957 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000958 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000959 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000960 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
961 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 +0000962 return false;
963#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000964 }
965#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000966 }
967#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
968 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
969 MetaEcfsFile += "/.MetaEcfsFile";
970 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
971 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
972 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000973 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000974 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000975 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000976 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000977 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000978 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400979 }
Dees_Troy066eb302013-08-23 17:20:32 +0000980 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
981 if (Is_Decrypted)
982 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000983 Is_Decrypted = false;
984 }
985#endif
986 if (Removable)
987 Update_Size(Display_Error);
988
989 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +0200990 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
991 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -0400992 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400993 return true;
994}
995
996bool TWPartition::UnMount(bool Display_Error) {
997 if (Is_Mounted()) {
998 int never_unmount_system;
999
1000 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1001 if (never_unmount_system == 1 && Mount_Point == "/system")
1002 return true; // Never unmount system if you're not supposed to unmount it
1003
Dees_Troyc8bafa12013-01-10 15:43:00 +00001004#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1005 if (EcryptFS_Password.size() > 0) {
1006 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
1007 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001008 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001009 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001010 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001011 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001012 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001013 }
1014 }
1015#endif
1016
Dees_Troy38bd7602012-09-14 13:33:53 -04001017 if (!Symlink_Mount_Point.empty())
1018 umount(Symlink_Mount_Point.c_str());
1019
Dees_Troyb05ddee2013-01-28 20:24:50 +00001020 umount(Mount_Point.c_str());
1021 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001022 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001023 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001024 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001025 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001026 return false;
1027 } else
1028 return true;
1029 } else {
1030 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001031 }
1032}
1033
Gary Peck43acadf2012-11-21 21:19:01 -08001034bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001035 bool wiped = false, update_crypt = false;
1036 int check;
1037 string Layout_Filename = Mount_Point + "/.layout_version";
1038
Dees_Troy38bd7602012-09-14 13:33:53 -04001039 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001040 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001041 return false;
1042 }
1043
Dees_Troyc51f1f92012-09-20 15:32:13 -04001044 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001045 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001046
Dees_Troyce675462013-01-09 19:48:21 +00001047#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1048 if (Mount_Point == "/data" && Mount(false)) {
1049 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
1050 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
1051 }
1052#endif
1053
Dees_Troy16c2b312013-01-15 16:51:18 +00001054 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1055 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1056 else
1057 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001058
Dees_Troy16c2b312013-01-15 16:51:18 +00001059 if (Has_Data_Media) {
1060 wiped = Wipe_Data_Without_Wiping_Media();
1061 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001062
Dees_Troy16c2b312013-01-15 16:51:18 +00001063 DataManager::GetValue(TW_RM_RF_VAR, check);
1064
Hashcodedabfd492013-08-29 22:45:30 -07001065 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001066 wiped = Wipe_RMRF();
1067 else if (New_File_System == "ext4")
1068 wiped = Wipe_EXT4();
1069 else if (New_File_System == "ext2" || New_File_System == "ext3")
1070 wiped = Wipe_EXT23(New_File_System);
1071 else if (New_File_System == "vfat")
1072 wiped = Wipe_FAT();
1073 else if (New_File_System == "exfat")
1074 wiped = Wipe_EXFAT();
1075 else if (New_File_System == "yaffs2")
1076 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001077 else if (New_File_System == "f2fs")
1078 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001079 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001080 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 +00001081 unlink("/.layout_version");
1082 return false;
1083 }
1084 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001085 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001086
Gary Pecke8bc5d72012-12-21 06:45:25 -08001087 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001088#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1089 if (Mount_Point == "/data" && Mount(false)) {
1090 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1091 Make_Dir("/data/system", true);
1092 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1093 }
1094 }
1095#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001096
Dees_Troy1c1ac442013-01-17 21:42:14 +00001097 if (Mount_Point == "/cache")
1098 DataManager::Output_Version();
1099
Dees_Troy16c2b312013-01-15 16:51:18 +00001100 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1101 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1102
1103 if (update_crypt) {
1104 Setup_File_System(false);
1105 if (Is_Encrypted && !Is_Decrypted) {
1106 // just wiped an encrypted partition back to its unencrypted state
1107 Is_Encrypted = false;
1108 Is_Decrypted = false;
1109 Decrypted_Block_Device = "";
1110 if (Mount_Point == "/data") {
1111 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1112 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1113 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001114 }
1115 }
1116 }
1117 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001118}
1119
Gary Peck43acadf2012-11-21 21:19:01 -08001120bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001121 if (Is_File_System(Current_File_System))
1122 return Wipe(Current_File_System);
1123 else
1124 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001125}
1126
Dees_Troye58d5262012-09-21 12:27:57 -04001127bool TWPartition::Wipe_AndSec(void) {
1128 if (!Has_Android_Secure)
1129 return false;
1130
Dees_Troye58d5262012-09-21 12:27:57 -04001131 if (!Mount(true))
1132 return false;
1133
Dees_Troy2673cec2013-04-02 20:22:16 +00001134 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001135 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001136 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001137}
1138
Dees_Troy51a0e822012-09-05 15:24:24 -04001139bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001140 if (Backup_Method == FILES)
1141 return Backup_Tar(backup_folder);
1142 else if (Backup_Method == DD)
1143 return Backup_DD(backup_folder);
1144 else if (Backup_Method == FLASH_UTILS)
1145 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001146 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001147 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001148}
1149
Dees_Troy43d8b002012-09-17 16:00:01 -04001150bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001151 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001152 char split_filename[512];
1153 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001154 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001155
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001156 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001157 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001158 if (!TWFunc::Path_Exists(Full_Filename)) {
1159 // This is a split archive, we presume
1160 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001161 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001162 md5file = split_filename;
1163 md5file += ".md5";
1164 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001165 LOGERR("No md5 file found for '%s'.\n", split_filename);
1166 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001167 return false;
1168 }
1169 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001170 while (index < 1000) {
1171 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001172 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001173 return false;
1174 }
1175 index++;
1176 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001177 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001178 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001179 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001180 } else {
1181 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001182 md5file = Full_Filename + ".md5";
1183 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001184 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1185 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001186 return false;
1187 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001188 md5sum.setfn(Full_Filename);
1189 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001190 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001191 return false;
1192 } else
1193 return true;
1194 }
1195 return false;
1196}
1197
Dees_Troy51a0e822012-09-05 15:24:24 -04001198bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001199 size_t first_period, second_period;
1200 string Restore_File_System;
1201
1202 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001203 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001204
1205 // Parse backup filename to extract the file system before wiping
1206 first_period = Backup_FileName.find(".");
1207 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001208 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001209 return false;
1210 }
1211 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1212 second_period = Restore_File_System.find(".");
1213 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001214 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001215 return false;
1216 }
1217 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001218 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001219
1220 if (Is_File_System(Restore_File_System))
1221 return Restore_Tar(restore_folder, Restore_File_System);
1222 else if (Is_Image(Restore_File_System)) {
1223 if (Restore_File_System == "emmc")
1224 return Restore_DD(restore_folder);
1225 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1226 return Restore_Flash_Image(restore_folder);
1227 }
1228
Dees_Troy2673cec2013-04-02 20:22:16 +00001229 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001230 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001231}
1232
1233string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001234 if (Backup_Method == NONE)
1235 return "none";
1236 else if (Backup_Method == FILES)
1237 return "files";
1238 else if (Backup_Method == DD)
1239 return "dd";
1240 else if (Backup_Method == FLASH_UTILS)
1241 return "flash_utils";
1242 else
1243 return "undefined";
1244 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001245}
1246
1247bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001248 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001249 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001250 return 1;
1251}
1252
1253bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001254 bool Save_Data_Media = Has_Data_Media;
1255
1256 if (!UnMount(true))
1257 return false;
1258
Dees_Troy38bd7602012-09-14 13:33:53 -04001259 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001260 Decrypted_Block_Device = "";
1261 Is_Decrypted = false;
1262 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001263 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001264 Has_Data_Media = Save_Data_Media;
1265 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1266 Recreate_Media_Folder();
1267 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001268 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001269 return true;
1270 } else {
1271 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001272 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001273 return false;
1274 }
1275 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001276}
1277
1278void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001279 const char* type;
1280 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001281
Dees_Troy68cab492012-12-12 19:29:35 +00001282 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1283 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001284
Dees_Troy38bd7602012-09-14 13:33:53 -04001285 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001286 if (!Is_Present)
1287 return;
Dees_Troy51127312012-09-08 13:08:49 -04001288
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001289 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1290 if (blkid_do_fullprobe(pr)) {
1291 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001292 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001293 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001294 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001295
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001296 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1297 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001298 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001299 return;
1300 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001301
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001302 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001303 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001304}
1305
Gary Peck43acadf2012-11-21 21:19:01 -08001306bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001307 if (!UnMount(true))
1308 return false;
1309
Dees_Troy43d8b002012-09-17 16:00:01 -04001310 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001311 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001312
Dees_Troy2673cec2013-04-02 20:22:16 +00001313 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001314 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001315 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001316 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001317 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001318 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001319 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001320 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001321 return true;
1322 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001323 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001324 return false;
1325 }
1326 } else
1327 return Wipe_RMRF();
1328
1329 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001330}
1331
1332bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001333 if (!UnMount(true))
1334 return false;
1335
Dees_Troyb3265ab2013-08-30 02:59:14 +00001336#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Dees_Troya95f55c2013-08-17 13:14:43 +00001337 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Dees_Troya95f55c2013-08-17 13:14:43 +00001338 if (make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle) != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001339 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1340 return false;
1341 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001342 #ifdef HAVE_SELINUX
1343 string sedir = Mount_Point + "/lost+found";
1344 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1345 rmdir(sedir.c_str());
1346 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
1347 #endif
Dees_Troya95f55c2013-08-17 13:14:43 +00001348 return true;
1349 }
1350#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001351 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001352 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001353
Dees_Troy2673cec2013-04-02 20:22:16 +00001354 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001355 Find_Actual_Block_Device();
1356 Command = "make_ext4fs";
1357 if (!Is_Decrypted && Length != 0) {
1358 // Only use length if we're not decrypted
1359 char len[32];
1360 sprintf(len, "%i", Length);
1361 Command += " -l ";
1362 Command += len;
1363 }
Dees_Troy5295d582013-09-06 15:51:08 +00001364 if (TWFunc::Path_Exists("/file_contexts")) {
1365 Command += " -S /file_contexts";
1366 }
1367 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001368 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001369 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001370 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001371 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001372 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001373 return true;
1374 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001375 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001376 return false;
1377 }
1378 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001379 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001380#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001381 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001382}
1383
1384bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001385 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001386
Dees_Troy43d8b002012-09-17 16:00:01 -04001387 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001388 if (!UnMount(true))
1389 return false;
1390
Dees_Troy2673cec2013-04-02 20:22:16 +00001391 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001392 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001393 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001394 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001395 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001396 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001397 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001398 return true;
1399 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001400 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001401 return false;
1402 }
1403 return true;
1404 }
1405 else
1406 return Wipe_RMRF();
1407
1408 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001409}
1410
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001411bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001412 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001413
1414 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1415 if (!UnMount(true))
1416 return false;
1417
Dees_Troy2673cec2013-04-02 20:22:16 +00001418 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001419 Find_Actual_Block_Device();
1420 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001421 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001422 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001423 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001424 return true;
1425 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001426 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001427 return false;
1428 }
1429 return true;
1430 }
1431 return false;
1432}
1433
Dees_Troy38bd7602012-09-14 13:33:53 -04001434bool TWPartition::Wipe_MTD() {
1435 if (!UnMount(true))
1436 return false;
1437
Dees_Troy2673cec2013-04-02 20:22:16 +00001438 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001439
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001440 mtd_scan_partitions();
1441 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1442 if (mtd == NULL) {
1443 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1444 return false;
1445 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001446
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001447 MtdWriteContext* ctx = mtd_write_partition(mtd);
1448 if (ctx == NULL) {
1449 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1450 return false;
1451 }
1452 if (mtd_erase_blocks(ctx, -1) == -1) {
1453 mtd_write_close(ctx);
1454 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1455 return false;
1456 }
1457 if (mtd_write_close(ctx) != 0) {
1458 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1459 return false;
1460 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001461 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001462 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001463 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001464 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001465}
1466
1467bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001468 if (!Mount(true))
1469 return false;
1470
Dees_Troy2673cec2013-04-02 20:22:16 +00001471 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001472 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001473 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001474 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001475}
1476
Dees_Troye5017042013-08-29 16:38:55 +00001477bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001478 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001479
1480 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1481 if (!UnMount(true))
1482 return false;
1483
1484 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1485 Find_Actual_Block_Device();
1486 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001487 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001488 Recreate_AndSec_Folder();
1489 gui_print("Done.\n");
1490 return true;
1491 } else {
1492 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1493 return false;
1494 }
1495 return true;
1496 } else {
1497 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1498 return Wipe_RMRF();
1499 }
1500 return false;
1501}
1502
Dees_Troy51a0e822012-09-05 15:24:24 -04001503bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001504 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001505
1506 // This handles wiping data on devices with "sdcard" in /data/media
1507 if (!Mount(true))
1508 return false;
1509
Dees_Troy2673cec2013-04-02 20:22:16 +00001510 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001511
1512 DIR* d;
1513 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001514 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001515 struct dirent* de;
1516 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001517 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1518 // The media folder is the "internal sdcard"
1519 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001520 // the media folder for multi-user.
Dees_Troy16b74352012-11-14 22:27:31 +00001521 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001522
1523 dir = "/data/";
1524 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001525 if (de->d_type == DT_DIR) {
1526 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001527 } 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 +00001528 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001529 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001530 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001531 }
1532 closedir(d);
Dees_Troy2673cec2013-04-02 20:22:16 +00001533 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001534 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001535 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001536 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001537 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001538}
1539
1540bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001541 char back_name[255], split_index[5];
1542 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001543 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001544 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001545 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001546 twrpTar tar;
1547 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001548
Dees_Troy43d8b002012-09-17 16:00:01 -04001549 if (!Mount(true))
1550 return false;
1551
Dees_Troya13d74f2013-03-24 08:54:55 -05001552 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001553 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001554
1555 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001556 tar.use_compression = use_compression;
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001557 //exclude Google Music Cache
1558 tar.setexcl("/data/data/com.google.android.music/files");
Dees_Troy83bd4832013-05-04 12:39:56 +00001559#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1560 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1561 if (use_encryption && Can_Encrypt_Backup) {
1562 tar.use_encryption = use_encryption;
1563 if (Use_Userdata_Encryption)
1564 tar.userdata_encryption = use_encryption;
1565 } else {
1566 use_encryption = false;
1567 }
1568#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001569
1570 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1571 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001572 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001573 tar.has_data_media = Has_Data_Media;
1574 if (!use_encryption && Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001575 // This backup needs to be split into multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001576 gui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001577 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001578 tar.setdir(back_name);
1579 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001580 backup_count = tar.splitArchiveFork();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001581 if (backup_count == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001582 LOGERR("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001583 return false;
1584 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001585 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001586 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001587 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001588 tar.setdir(Backup_Path);
1589 tar.setfn(Full_FileName);
1590 if (tar.createTarFork() != 0)
1591 return false;
1592 if (use_compression && !use_encryption) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001593 string gzname = Full_FileName + ".gz";
1594 rename(gzname.c_str(), Full_FileName.c_str());
1595 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001596 if (use_encryption)
1597 Full_FileName += "000";
Dees_Troy7c2dec82012-09-26 09:49:14 -04001598 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001599 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001600 return false;
1601 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001602 }
1603 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001604}
1605
1606bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001607 char back_name[255], backup_size[32];
Vojtech Bocek05534202013-09-11 08:11:56 +02001608 string Full_FileName, Command, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001609 int use_compression;
1610
igoriok87e3d932013-01-31 21:03:53 +02001611 sprintf(backup_size, "%llu", Backup_Size);
1612 DD_BS = backup_size;
1613
Dees_Troyb46a6842012-09-25 11:06:46 -04001614 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001615 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001616
1617 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1618 Backup_FileName = back_name;
1619
1620 Full_FileName = backup_folder + "/" + Backup_FileName;
1621
igoriok87e3d932013-01-31 21:03:53 +02001622 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001623 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001624 TWFunc::Exec_Cmd(Command);
Dees_Troyc154ac22012-10-12 15:36:47 -04001625 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001626 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001627 return false;
1628 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001629 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001630}
1631
1632bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001633 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001634 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001635 int use_compression;
1636
Dees_Troyb46a6842012-09-25 11:06:46 -04001637 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001638 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001639
1640 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1641 Backup_FileName = back_name;
1642
1643 Full_FileName = backup_folder + "/" + Backup_FileName;
1644
1645 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001646 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001647 TWFunc::Exec_Cmd(Command);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001648 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1649 // 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 +00001650 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001651 return false;
1652 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001653 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001654}
1655
Gary Peck43acadf2012-11-21 21:19:01 -08001656bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1657 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001658 int index = 0;
1659 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001660
Dees_Troye58d5262012-09-21 12:27:57 -04001661 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001662 if (!Wipe_AndSec())
1663 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001664 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001665 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001666 if (!Wipe(Restore_File_System))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001667 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001668 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001669 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001670 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001671
1672 if (!Mount(true))
1673 return false;
1674
Dees_Troy4a2a1262012-09-18 09:33:47 -04001675 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001676 /*if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001677 if (!TWFunc::Path_Exists(Full_FileName)) {
1678 // Backup is multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001679 LOGINFO("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001680 sprintf(split_index, "%03i", index);
1681 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001682 while (TWFunc::Path_Exists(Full_FileName)) {
1683 index++;
Dees_Troy2673cec2013-04-02 20:22:16 +00001684 gui_print("Restoring archive %i...\n", index);
1685 LOGINFO("Restoring '%s'...\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001686 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001687 tar.setdir("/");
1688 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001689 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001690 return false;
1691 sprintf(split_index, "%03i", index);
1692 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1693 }
1694 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001695 LOGERR("Error locating restore file: '%s'\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001696 return false;
1697 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001698 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001699 } else {*/
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001700 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001701 tar.setdir(Backup_Path);
1702 tar.setfn(Full_FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +00001703 tar.backup_name = Backup_Name;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001704 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001705 return false;
Dees_Troy83bd4832013-05-04 12:39:56 +00001706 //}
Dees_Troy43d8b002012-09-17 16:00:01 -04001707 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001708}
1709
1710bool TWPartition::Restore_DD(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001711 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001712
Dees_Troyda8b55a2012-12-12 19:18:30 +00001713 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001714 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001715
1716 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001717 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001718 return false;
1719 }
1720 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1721 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001722 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001723 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1724 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1725 return false;
1726 }
1727
Dees_Troy2673cec2013-04-02 20:22:16 +00001728 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001729 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001730 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001731 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001732 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001733}
1734
1735bool TWPartition::Restore_Flash_Image(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001736 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001737
Dees_Troy2673cec2013-04-02 20:22:16 +00001738 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001739 Full_FileName = restore_folder + "/" + Backup_FileName;
1740 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1741 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001742 LOGINFO("Erase command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001743 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001744 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001745 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001746 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001747 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001748}
Dees_Troy5bf43922012-09-07 16:07:55 -04001749
1750bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001751 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001752
Dees_Troyab10ee22012-09-21 14:27:30 -04001753 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001754 return false;
1755
Dees_Troy0550cfb2012-10-13 11:56:13 -04001756 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001757 if (Removable || Is_Encrypted) {
1758 if (!Mount(false))
1759 return true;
1760 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001761 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001762
1763 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001764 if (!ret || Size == 0) {
1765 if (!Get_Size_Via_df(Display_Error)) {
1766 if (!Was_Already_Mounted)
1767 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001768 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001769 }
1770 }
Dees_Troy51127312012-09-08 13:08:49 -04001771
Dees_Troy5bf43922012-09-07 16:07:55 -04001772 if (Has_Data_Media) {
1773 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001774 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001775 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1776 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001777 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001778 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001779 int bak = (int)(Backup_Size / 1048576LLU);
1780 int total = (int)(Size / 1048576LLU);
1781 int us = (int)(Used / 1048576LLU);
1782 int fre = (int)(Free / 1048576LLU);
1783 int datmed = (int)(data_media_used / 1048576LLU);
Dees_Troy2673cec2013-04-02 20:22:16 +00001784 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 -04001785 } else {
1786 if (!Was_Already_Mounted)
1787 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001788 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001789 }
Dees_Troye58d5262012-09-21 12:27:57 -04001790 } else if (Has_Android_Secure) {
1791 if (Mount(Display_Error))
1792 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001793 else {
1794 if (!Was_Already_Mounted)
1795 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001796 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001797 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001798 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001799 if (!Was_Already_Mounted)
1800 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001801 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001802}
Dees_Troy38bd7602012-09-14 13:33:53 -04001803
1804void TWPartition::Find_Actual_Block_Device(void) {
1805 if (Is_Decrypted) {
1806 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001807 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001808 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001809 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001810 Is_Present = true;
1811 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001812 return;
1813 }
1814 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001815 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001816 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001817 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001818 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001819 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001820 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001821}
1822
1823void TWPartition::Recreate_Media_Folder(void) {
1824 string Command;
1825
1826 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001827 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001828 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001829 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00001830 LOGINFO("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001831 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1832 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001833 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001834}
Dees_Troye58d5262012-09-21 12:27:57 -04001835
1836void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001837 if (!Has_Android_Secure)
1838 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00001839 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001840 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001841 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001842 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001843 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001844 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1845 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1846 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001847 }
1848}