blob: 2195d9d3857ecc514a915c4c5a8b0114a9252fc9 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002 Copyright 2013 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 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"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050042#include "twrpDU.hpp"
bigbiff bigbiff6b600f92014-01-05 18:13:43 -050043#include "fixPermissions.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050044#include "infomanager.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040045extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040046 #include "mtdutils/mtdutils.h"
47 #include "mtdutils/mounts.h"
Dees_Troy85f44ed2013-01-09 18:42:36 +000048#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
49 #include "crypto/libcrypt_samsung/include/libcrypt_samsung.h"
50#endif
Dees_Troya95f55c2013-08-17 13:14:43 +000051#ifdef USE_EXT4
52 #include "make_ext4fs.h"
53#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060054
55#ifdef TW_INCLUDE_CRYPTO
56 #ifdef TW_INCLUDE_JB_CRYPTO
57 #include "crypto/jb/cryptfs.h"
58 #else
59 #include "crypto/ics/cryptfs.h"
60 #endif
61#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040062}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040063#ifdef HAVE_SELINUX
64#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060065#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040066#endif
Dees Troy4159aed2014-02-28 17:24:43 +000067#ifdef HAVE_CAPABILITIES
68#include <sys/capability.h>
69#include <sys/xattr.h>
70#include <linux/xattr.h>
71#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040072
bigbiff bigbiff9c754052013-01-09 09:09:08 -050073using namespace std;
74
Dees_Troya95f55c2013-08-17 13:14:43 +000075extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050076extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000077
Hashcode62bd9e02013-11-19 21:59:42 -080078struct flag_list {
79 const char *name;
80 unsigned flag;
81};
82
83static struct flag_list mount_flags[] = {
84 { "noatime", MS_NOATIME },
85 { "noexec", MS_NOEXEC },
86 { "nosuid", MS_NOSUID },
87 { "nodev", MS_NODEV },
88 { "nodiratime", MS_NODIRATIME },
89 { "ro", MS_RDONLY },
90 { "rw", 0 },
91 { "remount", MS_REMOUNT },
92 { "bind", MS_BIND },
93 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000094#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080095 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000096#endif
97#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080098 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000099#endif
100#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800101 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000102#endif
103#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800104 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000105#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800106 { "sync", MS_SYNCHRONOUS },
107 { "defaults", 0 },
108 { 0, 0 },
109};
110
that9e0593e2014-10-08 00:01:24 +0200111TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400112 Can_Be_Mounted = false;
113 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500114 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200115 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400116 Wipe_During_Factory_Reset = false;
117 Wipe_Available_in_GUI = false;
118 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400119 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400120 SubPartition_Of = "";
121 Symlink_Path = "";
122 Symlink_Mount_Point = "";
123 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400124 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400125 Actual_Block_Device = "";
126 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400127 Alternate_Block_Device = "";
128 Removable = false;
129 Is_Present = false;
130 Length = 0;
131 Size = 0;
132 Used = 0;
133 Free = 0;
134 Backup_Size = 0;
135 Can_Be_Encrypted = false;
136 Is_Encrypted = false;
137 Is_Decrypted = false;
138 Decrypted_Block_Device = "";
139 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500140 Backup_Display_Name = "";
141 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400142 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400143 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400144 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400145 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000146 Can_Encrypt_Backup = false;
147 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400148 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400149 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400150 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500151 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400152 Storage_Path = "";
153 Current_File_System = "";
154 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800155 Mount_Flags = 0;
156 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400157 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000158 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000159 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000160#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
161 EcryptFS_Password = "";
162#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400163}
164
165TWPartition::~TWPartition(void) {
166 // Do nothing
167}
168
Dees_Troy5bf43922012-09-07 16:07:55 -0400169bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
170 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
171 int line_len = Line.size(), index = 0, item_index = 0;
172 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400173 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400174 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500175 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400176
Dees_Troy51127312012-09-08 13:08:49 -0400177 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500178 if (full_line[index] == 34)
179 skip = !skip;
180 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400181 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400182 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400183 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000184 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400185 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500186 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000187 Display_Name = full_line + 1;
188 Backup_Display_Name = Display_Name;
189 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400190 index = Mount_Point.size();
191 while (index < line_len) {
192 while (index < line_len && full_line[index] == '\0')
193 index++;
194 if (index >= line_len)
195 continue;
196 ptr = full_line + index;
197 if (item_index == 0) {
198 // File System
199 Fstab_File_System = ptr;
200 Current_File_System = ptr;
201 item_index++;
202 } else if (item_index == 1) {
203 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400204 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400205 MTD_Name = ptr;
206 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400207 } else if (Fstab_File_System == "bml") {
208 if (Mount_Point == "/boot")
209 MTD_Name = "boot";
210 else if (Mount_Point == "/recovery")
211 MTD_Name = "recovery";
212 Primary_Block_Device = ptr;
213 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000214 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 -0400215 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400216 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000217 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400218 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000219 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400220 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400221 } else {
222 Primary_Block_Device = ptr;
223 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400224 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400225 item_index++;
226 } else if (item_index > 1) {
227 if (*ptr == '/') {
228 // Alternate Block Device
229 Alternate_Block_Device = ptr;
230 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
231 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
232 // Partition length
233 ptr += 7;
234 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400235 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
236 // Custom flags, save for later so that new values aren't overwritten by defaults
237 ptr += 6;
238 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000239 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400240 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
241 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400242 } else {
243 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000244 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400245 }
246 }
247 while (index < line_len && full_line[index] != '\0')
248 index++;
249 }
250
251 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
252 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000253 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400254 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000255 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400256 return 0;
257 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400258 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400259 Setup_File_System(Display_Error);
260 if (Mount_Point == "/system") {
261 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500262 Backup_Display_Name = Display_Name;
263 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400264 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500265 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400266 } else if (Mount_Point == "/data") {
267 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500268 Backup_Display_Name = Display_Name;
269 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400270 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400271 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500272 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000273 Can_Encrypt_Backup = true;
274 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500275 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200276 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400277#ifdef TW_INCLUDE_CRYPTO
278 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400279 char crypto_blkdev[255];
280 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
281 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400282 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400283 DataManager::SetValue(TW_IS_DECRYPTED, 1);
284 Is_Encrypted = true;
285 Is_Decrypted = true;
286 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000287 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400288 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600289 if (Is_Present) {
290#ifdef TW_INCLUDE_JB_CRYPTO
291 // No extra flags needed
292#else
293 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
294 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
295 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
296 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
297 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
298 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
299#ifdef CRYPTO_SD_FS_TYPE
300 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
301 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
302 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
303#endif
304 property_set("rw.km_fips_status", "ready");
305#endif
306 if (cryptfs_check_footer() == 0) {
307 Is_Encrypted = true;
308 Is_Decrypted = false;
309 Can_Be_Mounted = false;
310 Current_File_System = "emmc";
311 Setup_Image(Display_Error);
312 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
313 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
314 DataManager::SetValue("tw_crypto_display", "");
315 } else {
316 LOGERR("Could not mount /data and unable to find crypto footer.\n");
317 }
318 } else {
319 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
320 }
Gary Peck82599a82012-11-21 16:23:12 -0800321 } else {
322 // Filesystem is not encrypted and the mount
323 // succeeded, so get it back to the original
324 // unmounted state
325 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400326 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500327 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400328 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400329#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500330 if (datamedia)
331 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400332#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400333 } else if (Mount_Point == "/cache") {
334 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500335 Backup_Display_Name = Display_Name;
336 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400337 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400338 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500339 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400340 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000341 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500342 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500343 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400344 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400345 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400346 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400347 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500348 Backup_Display_Name = Display_Name;
349 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400350 Is_SubPartition = true;
351 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400352 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500353 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000354 Can_Encrypt_Backup = true;
355 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400356 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400357 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400358 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500359 Backup_Display_Name = Display_Name;
360 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400361 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400362 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500363 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000364 Can_Encrypt_Backup = true;
365 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400366 } else if (Mount_Point == "/boot") {
367 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500368 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400369 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500370 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400371 }
372#ifdef TW_EXTERNAL_STORAGE_PATH
373 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
374 Is_Storage = true;
375 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400376 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500377 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400378#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000379 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400380 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400381 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500382 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400383#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000384 }
Dees_Troy8170a922012-09-18 15:40:25 -0400385#ifdef TW_INTERNAL_STORAGE_PATH
386 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
387 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500388 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400389 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500390 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400391 }
392#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000393 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400394 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500395 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500396 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400397 }
398#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400399 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400400 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400401 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500402 if (Mount_Point == "/boot") {
403 Display_Name = "Boot";
404 Backup_Display_Name = Display_Name;
405 Can_Be_Backed_Up = true;
406 } else if (Mount_Point == "/recovery") {
407 Display_Name = "Recovery";
408 Backup_Display_Name = Display_Name;
Dees_Troya13d74f2013-03-24 08:54:55 -0500409 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400410 }
411
Dees_Troy51127312012-09-08 13:08:49 -0400412 // Process any custom flags
413 if (Flags.size() > 0)
414 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400415 return true;
416}
417
Hashcode62bd9e02013-11-19 21:59:42 -0800418bool TWPartition::Process_FS_Flags(string& Options, int Flags) {
419 int i;
420 char *p;
421 char *savep;
422 char fs_options[250];
423
424 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
425 Options = "";
426
427 p = strtok_r(fs_options, ",", &savep);
428 while (p) {
429 /* Look for the flag "p" in the flag list "fl"
430 * If not found, the loop exits with fl[i].name being null.
431 */
432 for (i = 0; mount_flags[i].name; i++) {
433 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
434 Flags |= mount_flags[i].flag;
435 break;
436 }
437 }
438
439 if (!mount_flags[i].name) {
440 if (Options.size() > 0)
441 Options += ",";
442 Options += p;
443 }
444 p = strtok_r(NULL, ",", &savep);
445 }
446
447 return true;
448}
449
Dees_Troy51127312012-09-08 13:08:49 -0400450bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
451 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500452 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400453 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500454 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400455
456 strcpy(flags, Flags.c_str());
457 flags_len = Flags.size();
458 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500459 if (flags[index] == 34)
460 skip = !skip;
461 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400462 flags[index] = '\0';
463 }
464
465 index = 0;
466 while (index < flags_len) {
467 while (index < flags_len && flags[index] == '\0')
468 index++;
469 if (index >= flags_len)
470 continue;
471 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500472 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400473 if (strcmp(ptr, "removable") == 0) {
474 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600475 } else if (strncmp(ptr, "storage", 7) == 0) {
476 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600477 Is_Storage = true;
478 } else if (ptr_len == 9) {
479 ptr += 9;
480 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
481 LOGINFO("storage set to true\n");
482 Is_Storage = true;
483 } else {
484 LOGINFO("storage set to false\n");
485 Is_Storage = false;
486 }
487 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500488 } else if (strcmp(ptr, "settingsstorage") == 0) {
489 Is_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500490 } else if (strcmp(ptr, "andsec") == 0) {
491 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400492 } else if (strcmp(ptr, "canbewiped") == 0) {
493 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700494 } else if (strcmp(ptr, "usermrf") == 0) {
495 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500496 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
497 ptr += 7;
498 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
499 Can_Be_Backed_Up = true;
500 else
501 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400502 } else if (strcmp(ptr, "wipeingui") == 0) {
503 Can_Be_Wiped = true;
504 Wipe_Available_in_GUI = true;
505 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
506 Can_Be_Wiped = true;
507 Wipe_Available_in_GUI = true;
508 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500509 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000510 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400511 Is_SubPartition = true;
512 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000513 } else if (strcmp(ptr, "ignoreblkid") == 0) {
514 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000515 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
516 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500517 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400518 ptr += 8;
519 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500520 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
521 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400522 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500523 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400524 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500525 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
526 Display_Name.resize(Display_Name.size() - 1);
527 }
528 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
529 has_storage_name = true;
530 ptr += 11;
531 if (*ptr == '\"') ptr++;
532 Storage_Name = ptr;
533 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
534 Storage_Name.resize(Storage_Name.size() - 1);
535 }
536 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
537 has_backup_name = true;
538 ptr += 10;
539 if (*ptr == '\"') ptr++;
540 Backup_Display_Name = ptr;
541 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
542 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
543 }
544 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400545 ptr += 10;
546 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500547 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400548 ptr += 7;
549 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000550 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
551 ptr += 17;
552 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
553 Can_Encrypt_Backup = true;
554 else
555 Can_Encrypt_Backup = false;
556 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
557 ptr += 21;
558 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
559 Can_Encrypt_Backup = true;
560 Use_Userdata_Encryption = true;
561 } else {
562 Use_Userdata_Encryption = false;
563 }
Hashcode62bd9e02013-11-19 21:59:42 -0800564 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
565 ptr += 8;
566 if (*ptr == '\"') ptr++;
567
568 Mount_Options = ptr;
569 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
570 Mount_Options.resize(Mount_Options.size() - 1);
571 }
572 Process_FS_Flags(Mount_Options, Mount_Flags);
Dees_Troy51127312012-09-08 13:08:49 -0400573 } else {
574 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000575 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400576 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000577 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400578 }
579 while (index < flags_len && flags[index] != '\0')
580 index++;
581 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500582 if (has_display_name && !has_storage_name)
583 Storage_Name = Display_Name;
584 if (!has_display_name && has_storage_name)
585 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000586 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500587 Backup_Display_Name = Display_Name;
588 if (!has_display_name && has_backup_name)
589 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400590 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400591}
592
Dees_Troy5bf43922012-09-07 16:07:55 -0400593bool TWPartition::Is_File_System(string File_System) {
594 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400595 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400596 File_System == "ext4" ||
597 File_System == "vfat" ||
598 File_System == "ntfs" ||
599 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500600 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000601 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400602 File_System == "auto")
603 return true;
604 else
605 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400606}
607
Dees_Troy5bf43922012-09-07 16:07:55 -0400608bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400609 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400610 return true;
611 else
612 return false;
613}
614
Dees_Troy51127312012-09-08 13:08:49 -0400615bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400616 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400617 if (mkdir(Path.c_str(), 0777) == -1) {
618 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000619 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400620 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000621 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400622 return false;
623 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000624 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400625 return true;
626 }
627 }
628 return true;
629}
630
Dees_Troy5bf43922012-09-07 16:07:55 -0400631void TWPartition::Setup_File_System(bool Display_Error) {
632 struct statfs st;
633
634 Can_Be_Mounted = true;
635 Can_Be_Wiped = true;
636
Dees_Troy5bf43922012-09-07 16:07:55 -0400637 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400638 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400639 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
640 Backup_Name = Display_Name;
641 Backup_Method = FILES;
642}
643
644void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400645 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
646 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800647 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400648 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800649 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400650 Backup_Method = FLASH_UTILS;
651 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000652 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 -0400653 if (Find_Partition_Size()) {
654 Used = Size;
655 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400656 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400657 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000658 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400659 else
Dees Troyd932ce12013-10-18 17:12:59 +0000660 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400661 }
662}
663
Dees_Troye58d5262012-09-21 12:27:57 -0400664void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500665 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400666 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500667 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400668 Has_Android_Secure = true;
669 Symlink_Path = Mount_Point + "/.android_secure";
670 Symlink_Mount_Point = "/and-sec";
671 Backup_Path = Symlink_Mount_Point;
672 Make_Dir("/and-sec", true);
673 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600674 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400675}
676
that9e0593e2014-10-08 00:01:24 +0200677void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500678 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
679 Storage_Name = "Internal Storage";
680 Has_Data_Media = true;
681 Is_Storage = true;
682 Is_Settings_Storage = true;
683 Storage_Path = "/data/media";
684 Symlink_Path = Storage_Path;
685 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
686 Make_Dir("/emmc", false);
687 Symlink_Mount_Point = "/emmc";
688 } else {
689 Make_Dir("/sdcard", false);
690 Symlink_Mount_Point = "/sdcard";
691 }
692 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
693 Storage_Path = "/data/media/0";
694 Symlink_Path = Storage_Path;
695 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
696 UnMount(true);
697 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500698 DataManager::SetValue("tw_has_internal", 1);
699 DataManager::SetValue("tw_has_data_media", 1);
700 du.add_absolute_dir("/data/media");
701}
702
Dees_Troy5bf43922012-09-07 16:07:55 -0400703void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
704 char device[512], realDevice[512];
705
706 strcpy(device, Block.c_str());
707 memset(realDevice, 0, sizeof(realDevice));
708 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
709 {
710 strcpy(device, realDevice);
711 memset(realDevice, 0, sizeof(realDevice));
712 }
713
714 if (device[0] != '/') {
715 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000716 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400717 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000718 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400719 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400720 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400721 Block = device;
722 return;
723 }
724}
725
Dees_Troy8e337f32012-10-13 22:07:49 -0400726void TWPartition::Mount_Storage_Retry(void) {
727 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500728 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400729 int retry_count = 5;
730 while (retry_count > 0 && !Mount(false)) {
731 usleep(500000);
732 retry_count--;
733 }
734 Mount(true);
735 }
736}
737
Dees_Troy38bd7602012-09-14 13:33:53 -0400738bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
739 FILE *fp = NULL;
740 char line[255];
741
742 fp = fopen("/proc/mtd", "rt");
743 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000744 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400745 return false;
746 }
747
748 while (fgets(line, sizeof(line), fp) != NULL)
749 {
750 char device[32], label[32];
751 unsigned long size = 0;
752 char* fstype = NULL;
753 int deviceId;
754
755 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
756
757 // Skip header and blank lines
758 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
759 continue;
760
761 // Strip off the trailing " from the label
762 label[strlen(label)-1] = '\0';
763
764 if (strcmp(label, MTD_Name.c_str()) == 0) {
765 // We found our device
766 // Strip off the trailing : from the device
767 device[strlen(device)-1] = '\0';
768 if (sscanf(device,"mtd%d", &deviceId) == 1) {
769 sprintf(device, "/dev/block/mtdblock%d", deviceId);
770 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000771 fclose(fp);
772 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400773 }
774 }
775 }
776 fclose(fp);
777
778 return false;
779}
780
Dees_Troy51127312012-09-08 13:08:49 -0400781bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
782 struct statfs st;
783 string Local_Path = Mount_Point + "/.";
784
785 if (!Mount(Display_Error))
786 return false;
787
788 if (statfs(Local_Path.c_str(), &st) != 0) {
789 if (!Removable) {
790 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000791 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400792 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000793 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400794 }
795 return false;
796 }
797 Size = (st.f_blocks * st.f_bsize);
798 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
799 Free = (st.f_bfree * st.f_bsize);
800 Backup_Size = Used;
801 return true;
802}
803
804bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400805 FILE* fp;
806 char command[255], line[512];
807 int include_block = 1;
808 unsigned int min_len;
809
810 if (!Mount(Display_Error))
811 return false;
812
Dees_Troy38bd7602012-09-14 13:33:53 -0400813 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400814 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200815 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400816 fp = fopen("/tmp/dfoutput.txt", "rt");
817 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000818 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400819 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400820 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400821
822 while (fgets(line, sizeof(line), fp) != NULL)
823 {
824 unsigned long blocks, used, available;
825 char device[64];
826 char tmpString[64];
827
828 if (strncmp(line, "Filesystem", 10) == 0)
829 continue;
830 if (strlen(line) < min_len) {
831 include_block = 0;
832 continue;
833 }
834 if (include_block) {
835 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
836 } else {
837 // The device block string is so long that the df information is on the next line
838 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400839 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400840 while (tmpString[space_count] == 32)
841 space_count++;
842 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
843 }
844
845 // Adjust block size to byte size
846 Size = blocks * 1024ULL;
847 Used = used * 1024ULL;
848 Free = available * 1024ULL;
849 Backup_Size = Used;
850 }
851 fclose(fp);
852 return true;
853}
854
Dees_Troy5bf43922012-09-07 16:07:55 -0400855bool TWPartition::Find_Partition_Size(void) {
856 FILE* fp;
857 char line[512];
858 string tmpdevice;
859
igoriok87e3d932013-01-31 21:03:53 +0200860 fp = fopen("/proc/dumchar_info", "rt");
861 if (fp != NULL) {
862 while (fgets(line, sizeof(line), fp) != NULL)
863 {
864 char label[32], device[32];
865 unsigned long size = 0;
866
thatd43bf2d2014-09-21 23:13:02 +0200867 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200868
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500869 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200870 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
871 continue;
872
873 tmpdevice = "/dev/";
874 tmpdevice += label;
875 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
876 Size = size;
877 fclose(fp);
878 return true;
879 }
880 }
881 }
882
Dees_Troy5bf43922012-09-07 16:07:55 -0400883 // In this case, we'll first get the partitions we care about (with labels)
884 fp = fopen("/proc/partitions", "rt");
885 if (fp == NULL)
886 return false;
887
888 while (fgets(line, sizeof(line), fp) != NULL)
889 {
890 unsigned long major, minor, blocks;
891 char device[512];
892 char tmpString[64];
893
Dees_Troy63c8df72012-09-10 14:02:05 -0400894 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400895 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
896
897 tmpdevice = "/dev/block/";
898 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400899 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400900 // Adjust block size to byte size
901 Size = blocks * 1024ULL;
902 fclose(fp);
903 return true;
904 }
905 }
906 fclose(fp);
907 return false;
908}
909
Dees_Troy5bf43922012-09-07 16:07:55 -0400910bool TWPartition::Is_Mounted(void) {
911 if (!Can_Be_Mounted)
912 return false;
913
914 struct stat st1, st2;
915 string test_path;
916
917 // Check to see if the mount point directory exists
918 test_path = Mount_Point + "/.";
919 if (stat(test_path.c_str(), &st1) != 0) return false;
920
921 // Check to see if the directory above the mount point exists
922 test_path = Mount_Point + "/../.";
923 if (stat(test_path.c_str(), &st2) != 0) return false;
924
925 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
926 int ret = (st1.st_dev != st2.st_dev) ? true : false;
927
928 return ret;
929}
930
931bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000932 int exfat_mounted = 0;
933
Dees_Troy5bf43922012-09-07 16:07:55 -0400934 if (Is_Mounted()) {
935 return true;
936 } else if (!Can_Be_Mounted) {
937 return false;
938 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400939
940 Find_Actual_Block_Device();
941
942 // Check the current file system before mounting
943 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000944 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000945 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 +0000946 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000947 string result;
948 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000949 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000950 Current_File_System = "vfat";
951 } else {
952#ifdef TW_NO_EXFAT_FUSE
953 UnMount(false);
954 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
955 // Some kernels let us mount vfat as exfat which doesn't work out too well
956#else
957 exfat_mounted = 1;
958#endif
959 }
960 }
Dees_Troy22042032012-12-18 21:23:08 +0000961 if (Fstab_File_System == "yaffs2") {
962 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000963 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
964 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
965 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
966 if (Display_Error)
967 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
968 else
969 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
970 return false;
971 } else {
972 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
973 return true;
974 }
975 } else {
976 struct stat st;
977 string test_path = Mount_Point;
978 if (stat(test_path.c_str(), &st) < 0) {
979 if (Display_Error)
980 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
981 else
982 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
983 return false;
984 }
985 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
986 if (new_mode != st.st_mode) {
987 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
988 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
989 if (Display_Error)
990 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
991 else
992 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
993 return false;
994 }
995 }
Dees_Troy22042032012-12-18 21:23:08 +0000996 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000997 }
Dees Troy216e0422014-02-07 03:46:42 +0000998 } 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 && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), Mount_Flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000999#ifdef TW_NO_EXFAT_FUSE
1000 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001001 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001002 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001003 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001004 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001005 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001006 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -08001007 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 +00001008 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001009 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001010 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001011#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001012 if (!Removable && Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001013 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001014 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001015 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1016 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 +00001017 return false;
1018#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001019 }
1020#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001021 }
1022#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1023 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1024 MetaEcfsFile += "/.MetaEcfsFile";
1025 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
1026 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
1027 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001028 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001029 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001030 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001031 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001032 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001033 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -04001034 }
Dees_Troy066eb302013-08-23 17:20:32 +00001035 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
1036 if (Is_Decrypted)
1037 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001038 Is_Decrypted = false;
1039 }
1040#endif
1041 if (Removable)
1042 Update_Size(Display_Error);
1043
1044 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001045 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
1046 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001047 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001048 return true;
1049}
1050
1051bool TWPartition::UnMount(bool Display_Error) {
1052 if (Is_Mounted()) {
1053 int never_unmount_system;
1054
1055 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1056 if (never_unmount_system == 1 && Mount_Point == "/system")
1057 return true; // Never unmount system if you're not supposed to unmount it
1058
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001059 if (Is_Storage)
1060 TWFunc::Toggle_MTP(false);
1061
Dees_Troyc8bafa12013-01-10 15:43:00 +00001062#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1063 if (EcryptFS_Password.size() > 0) {
1064 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
1065 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001066 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001067 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001068 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001069 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001070 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001071 }
1072 }
1073#endif
1074
Dees_Troy38bd7602012-09-14 13:33:53 -04001075 if (!Symlink_Mount_Point.empty())
1076 umount(Symlink_Mount_Point.c_str());
1077
Dees_Troyb05ddee2013-01-28 20:24:50 +00001078 umount(Mount_Point.c_str());
1079 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001080 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001081 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001082 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001083 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001084 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001085 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001086 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001087 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001088 } else {
1089 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001090 }
1091}
1092
Gary Peck43acadf2012-11-21 21:19:01 -08001093bool TWPartition::Wipe(string New_File_System) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001094 bool wiped = false, update_crypt = false, recreate_media = true, mtp_toggle = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001095 int check;
1096 string Layout_Filename = Mount_Point + "/.layout_version";
1097
Dees_Troy38bd7602012-09-14 13:33:53 -04001098 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001099 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001100 return false;
1101 }
1102
Dees_Troyc51f1f92012-09-20 15:32:13 -04001103 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001104 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001105
Dees_Troyce675462013-01-09 19:48:21 +00001106#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1107 if (Mount_Point == "/data" && Mount(false)) {
1108 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
1109 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
1110 }
1111#endif
1112
Dees_Troy16c2b312013-01-15 16:51:18 +00001113 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1114 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1115 else
1116 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001117
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001118 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001119 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001120 recreate_media = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001121 mtp_toggle = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001122 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001123 DataManager::GetValue(TW_RM_RF_VAR, check);
1124
Hashcodedabfd492013-08-29 22:45:30 -07001125 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001126 wiped = Wipe_RMRF();
1127 else if (New_File_System == "ext4")
1128 wiped = Wipe_EXT4();
1129 else if (New_File_System == "ext2" || New_File_System == "ext3")
1130 wiped = Wipe_EXT23(New_File_System);
1131 else if (New_File_System == "vfat")
1132 wiped = Wipe_FAT();
1133 else if (New_File_System == "exfat")
1134 wiped = Wipe_EXFAT();
1135 else if (New_File_System == "yaffs2")
1136 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001137 else if (New_File_System == "f2fs")
1138 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001139 else {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001140 if (Is_Storage) {
1141 TWFunc::Toggle_MTP(true);
1142 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001143 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 +00001144 unlink("/.layout_version");
1145 return false;
1146 }
1147 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001148 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001149
Gary Pecke8bc5d72012-12-21 06:45:25 -08001150 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001151#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1152 if (Mount_Point == "/data" && Mount(false)) {
1153 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1154 Make_Dir("/data/system", true);
1155 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1156 }
1157 }
1158#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001159
Dees_Troy1c1ac442013-01-17 21:42:14 +00001160 if (Mount_Point == "/cache")
1161 DataManager::Output_Version();
1162
Dees_Troy16c2b312013-01-15 16:51:18 +00001163 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1164 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1165
1166 if (update_crypt) {
1167 Setup_File_System(false);
1168 if (Is_Encrypted && !Is_Decrypted) {
1169 // just wiped an encrypted partition back to its unencrypted state
1170 Is_Encrypted = false;
1171 Is_Decrypted = false;
1172 Decrypted_Block_Device = "";
1173 if (Mount_Point == "/data") {
1174 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1175 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1176 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001177 }
1178 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001179
1180 if (Mount_Point == "/data" && Has_Data_Media && recreate_media) {
1181 Recreate_Media_Folder();
1182 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001183 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001184 if (Is_Storage && mtp_toggle) {
1185 TWFunc::Toggle_MTP(true);
1186 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001187 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001188}
1189
Gary Peck43acadf2012-11-21 21:19:01 -08001190bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001191 if (Is_File_System(Current_File_System))
1192 return Wipe(Current_File_System);
1193 else
1194 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001195}
1196
Dees_Troye58d5262012-09-21 12:27:57 -04001197bool TWPartition::Wipe_AndSec(void) {
1198 if (!Has_Android_Secure)
1199 return false;
1200
Dees_Troye58d5262012-09-21 12:27:57 -04001201 if (!Mount(true))
1202 return false;
1203
Dees_Troy2673cec2013-04-02 20:22:16 +00001204 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001205 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001206 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001207}
1208
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001209bool TWPartition::Can_Repair() {
1210 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/dosfsck"))
1211 return true;
1212 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1213 return true;
1214 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1215 return true;
1216 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1217 return true;
1218 return false;
1219}
1220
1221bool TWPartition::Repair() {
1222 string command;
1223
1224 if (Current_File_System == "vfat") {
1225 if (!TWFunc::Path_Exists("/sbin/dosfsck")) {
1226 gui_print("dosfsck does not exist! Cannot repair!\n");
1227 return false;
1228 }
1229 if (!UnMount(true))
1230 return false;
1231 gui_print("Repairing %s using dosfsck...\n", Display_Name.c_str());
1232 Find_Actual_Block_Device();
1233 command = "/sbin/dosfsck -y " + Actual_Block_Device;
1234 LOGINFO("Repair command: %s\n", command.c_str());
1235 if (TWFunc::Exec_Cmd(command) == 0) {
1236 gui_print("Done.\n");
1237 return true;
1238 } else {
1239 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1240 return false;
1241 }
1242 }
1243 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1244 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
1245 gui_print("e2fsck does not exist! Cannot repair!\n");
1246 return false;
1247 }
1248 if (!UnMount(true))
1249 return false;
1250 gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str());
1251 Find_Actual_Block_Device();
1252 command = "/sbin/e2fsck -p " + Actual_Block_Device;
1253 LOGINFO("Repair command: %s\n", command.c_str());
1254 if (TWFunc::Exec_Cmd(command) == 0) {
1255 gui_print("Done.\n");
1256 return true;
1257 } else {
1258 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1259 return false;
1260 }
1261 }
1262 if (Current_File_System == "exfat") {
1263 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
1264 gui_print("fsck.exfat does not exist! Cannot repair!\n");
1265 return false;
1266 }
1267 if (!UnMount(true))
1268 return false;
1269 gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str());
1270 Find_Actual_Block_Device();
1271 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1272 LOGINFO("Repair command: %s\n", command.c_str());
1273 if (TWFunc::Exec_Cmd(command) == 0) {
1274 gui_print("Done.\n");
1275 return true;
1276 } else {
1277 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1278 return false;
1279 }
1280 }
1281 if (Current_File_System == "f2fs") {
1282 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
1283 gui_print("fsck.f2fs does not exist! Cannot repair!\n");
1284 return false;
1285 }
1286 if (!UnMount(true))
1287 return false;
1288 gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str());
1289 Find_Actual_Block_Device();
1290 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1291 LOGINFO("Repair command: %s\n", command.c_str());
1292 if (TWFunc::Exec_Cmd(command) == 0) {
1293 gui_print("Done.\n");
1294 return true;
1295 } else {
1296 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1297 return false;
1298 }
1299 }
1300 return false;
1301}
1302
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001303bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001304 if (Backup_Method == FILES)
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001305 return Backup_Tar(backup_folder, overall_size, other_backups_size);
Dees_Troy38bd7602012-09-14 13:33:53 -04001306 else if (Backup_Method == DD)
1307 return Backup_DD(backup_folder);
1308 else if (Backup_Method == FLASH_UTILS)
1309 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001310 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001311 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001312}
1313
Dees_Troy43d8b002012-09-17 16:00:01 -04001314bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001315 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001316 char split_filename[512];
1317 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001318 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001319
Captain Throwbackff5935f2014-09-23 16:08:34 -04001320 sync();
1321
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001322 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001323 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001324 if (!TWFunc::Path_Exists(Full_Filename)) {
1325 // This is a split archive, we presume
1326 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001327 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001328 md5file = split_filename;
1329 md5file += ".md5";
1330 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001331 LOGERR("No md5 file found for '%s'.\n", split_filename);
1332 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001333 return false;
1334 }
1335 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001336 while (index < 1000) {
1337 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001338 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001339 return false;
1340 }
1341 index++;
1342 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001343 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001344 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001345 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001346 } else {
1347 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001348 md5file = Full_Filename + ".md5";
1349 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001350 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1351 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001352 return false;
1353 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001354 md5sum.setfn(Full_Filename);
1355 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001356 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001357 return false;
1358 } else
1359 return true;
1360 }
1361 return false;
1362}
1363
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001364bool TWPartition::Restore(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Gary Peck43acadf2012-11-21 21:19:01 -08001365 string Restore_File_System;
1366
1367 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001368 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001369
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001370 Restore_File_System = Get_Restore_File_System(restore_folder);
1371
1372 if (Is_File_System(Restore_File_System))
1373 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1374 else if (Is_Image(Restore_File_System)) {
1375 *already_restored_size += TWFunc::Get_File_Size(Backup_Name);
1376 if (Restore_File_System == "emmc")
1377 return Restore_DD(restore_folder, total_restore_size, already_restored_size);
1378 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1379 return Restore_Flash_Image(restore_folder, total_restore_size, already_restored_size);
1380 }
1381
1382 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1383 return false;
1384}
1385
1386string TWPartition::Get_Restore_File_System(string restore_folder) {
1387 size_t first_period, second_period;
1388 string Restore_File_System;
1389
Gary Peck43acadf2012-11-21 21:19:01 -08001390 // Parse backup filename to extract the file system before wiping
1391 first_period = Backup_FileName.find(".");
1392 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001393 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001394 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001395 }
1396 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1397 second_period = Restore_File_System.find(".");
1398 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001399 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001400 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001401 }
1402 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001403 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001404 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001405}
1406
1407string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001408 if (Backup_Method == NONE)
1409 return "none";
1410 else if (Backup_Method == FILES)
1411 return "files";
1412 else if (Backup_Method == DD)
1413 return "dd";
1414 else if (Backup_Method == FLASH_UTILS)
1415 return "flash_utils";
1416 else
1417 return "undefined";
1418 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001419}
1420
1421bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001422 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001423 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001424 return 1;
1425}
1426
1427bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001428 bool Save_Data_Media = Has_Data_Media;
1429
1430 if (!UnMount(true))
1431 return false;
1432
Dees_Troy38bd7602012-09-14 13:33:53 -04001433 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001434 Decrypted_Block_Device = "";
1435 Is_Decrypted = false;
1436 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001437 Find_Actual_Block_Device();
1438 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001439 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001440 Has_Data_Media = Save_Data_Media;
1441 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1442 Recreate_Media_Folder();
1443 }
Ethan Yonker83e82572014-04-04 10:59:28 -05001444#ifndef TW_OEM_BUILD
Dees_Troy2673cec2013-04-02 20:22:16 +00001445 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Ethan Yonker83e82572014-04-04 10:59:28 -05001446#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001447 TWFunc::Toggle_MTP(mtp_was_enabled);
Dees_Troy38bd7602012-09-14 13:33:53 -04001448 return true;
1449 } else {
1450 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001451 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001452 return false;
1453 }
1454 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001455}
1456
1457void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001458 const char* type;
1459 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001460
Dees_Troy68cab492012-12-12 19:29:35 +00001461 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1462 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001463
Dees_Troy38bd7602012-09-14 13:33:53 -04001464 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001465 if (!Is_Present)
1466 return;
Dees_Troy51127312012-09-08 13:08:49 -04001467
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001468 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1469 if (blkid_do_fullprobe(pr)) {
1470 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001471 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001472 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001473 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001474
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001475 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001476 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001477 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001478 return;
1479 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001480
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001481 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001482 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001483}
1484
Gary Peck43acadf2012-11-21 21:19:01 -08001485bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001486 if (!UnMount(true))
1487 return false;
1488
Dees_Troy43d8b002012-09-17 16:00:01 -04001489 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001490 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001491
Dees_Troy2673cec2013-04-02 20:22:16 +00001492 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001493 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001494 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001495 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001496 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001497 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001498 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001499 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001500 return true;
1501 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001502 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001503 return false;
1504 }
1505 } else
1506 return Wipe_RMRF();
1507
1508 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001509}
1510
1511bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001512 Find_Actual_Block_Device();
1513 if (!Is_Present) {
1514 LOGERR("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1515 return false;
1516 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001517 if (!UnMount(true))
1518 return false;
1519
Dees_Troyb3265ab2013-08-30 02:59:14 +00001520#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001521 int ret;
1522 char *secontext = NULL;
1523
Dees_Troya95f55c2013-08-17 13:14:43 +00001524 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001525
Dees Troy99c8dbf2014-03-10 16:53:58 +00001526 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001527 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1528 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1529 } else {
1530 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1531 }
1532 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001533 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1534 return false;
1535 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001536 string sedir = Mount_Point + "/lost+found";
1537 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1538 rmdir(sedir.c_str());
1539 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001540 return true;
1541 }
1542#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001543 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001544 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001545
Dees_Troy2673cec2013-04-02 20:22:16 +00001546 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001547 Find_Actual_Block_Device();
1548 Command = "make_ext4fs";
1549 if (!Is_Decrypted && Length != 0) {
1550 // Only use length if we're not decrypted
1551 char len[32];
1552 sprintf(len, "%i", Length);
1553 Command += " -l ";
1554 Command += len;
1555 }
Dees_Troy5295d582013-09-06 15:51:08 +00001556 if (TWFunc::Path_Exists("/file_contexts")) {
1557 Command += " -S /file_contexts";
1558 }
1559 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001560 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001561 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001562 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001563 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001564 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001565 return true;
1566 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001567 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001568 return false;
1569 }
1570 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001571 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001572#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001573 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001574}
1575
1576bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001577 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001578
Dees_Troy43d8b002012-09-17 16:00:01 -04001579 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001580 if (!UnMount(true))
1581 return false;
1582
Dees_Troy2673cec2013-04-02 20:22:16 +00001583 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001584 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001585 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001586 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001587 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001588 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001589 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001590 return true;
1591 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001592 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001593 return false;
1594 }
1595 return true;
1596 }
1597 else
1598 return Wipe_RMRF();
1599
1600 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001601}
1602
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001603bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001604 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001605
1606 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1607 if (!UnMount(true))
1608 return false;
1609
Dees_Troy2673cec2013-04-02 20:22:16 +00001610 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001611 Find_Actual_Block_Device();
1612 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001613 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001614 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001615 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001616 return true;
1617 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001618 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001619 return false;
1620 }
1621 return true;
1622 }
1623 return false;
1624}
1625
Dees_Troy38bd7602012-09-14 13:33:53 -04001626bool TWPartition::Wipe_MTD() {
1627 if (!UnMount(true))
1628 return false;
1629
Dees_Troy2673cec2013-04-02 20:22:16 +00001630 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001631
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001632 mtd_scan_partitions();
1633 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1634 if (mtd == NULL) {
1635 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1636 return false;
1637 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001638
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001639 MtdWriteContext* ctx = mtd_write_partition(mtd);
1640 if (ctx == NULL) {
1641 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1642 return false;
1643 }
1644 if (mtd_erase_blocks(ctx, -1) == -1) {
1645 mtd_write_close(ctx);
1646 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1647 return false;
1648 }
1649 if (mtd_write_close(ctx) != 0) {
1650 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1651 return false;
1652 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001653 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001654 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001655 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001656 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001657}
1658
1659bool TWPartition::Wipe_RMRF() {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001660 if (Is_Storage)
1661 TWFunc::Toggle_MTP(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001662 if (!Mount(true))
1663 return false;
1664
Dees_Troy2673cec2013-04-02 20:22:16 +00001665 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001666 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001667 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001668 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001669}
1670
Dees_Troye5017042013-08-29 16:38:55 +00001671bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001672 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001673
1674 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1675 if (!UnMount(true))
1676 return false;
1677
1678 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1679 Find_Actual_Block_Device();
1680 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001681 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001682 Recreate_AndSec_Folder();
1683 gui_print("Done.\n");
1684 return true;
1685 } else {
1686 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1687 return false;
1688 }
1689 return true;
1690 } else {
1691 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1692 return Wipe_RMRF();
1693 }
1694 return false;
1695}
1696
Dees_Troy51a0e822012-09-05 15:24:24 -04001697bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001698