blob: 1a62bbae8709cbdb0d526edb7f0742e386141d57 [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;
76
Hashcode62bd9e02013-11-19 21:59:42 -080077struct flag_list {
78 const char *name;
79 unsigned flag;
80};
81
82static struct flag_list mount_flags[] = {
83 { "noatime", MS_NOATIME },
84 { "noexec", MS_NOEXEC },
85 { "nosuid", MS_NOSUID },
86 { "nodev", MS_NODEV },
87 { "nodiratime", MS_NODIRATIME },
88 { "ro", MS_RDONLY },
89 { "rw", 0 },
90 { "remount", MS_REMOUNT },
91 { "bind", MS_BIND },
92 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000093#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080094 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000095#endif
96#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080097 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000098#endif
99#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800100 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#endif
102#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800103 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800105 { "sync", MS_SYNCHRONOUS },
106 { "defaults", 0 },
107 { 0, 0 },
108};
109
Dees_Troy51a0e822012-09-05 15:24:24 -0400110TWPartition::TWPartition(void) {
111 Can_Be_Mounted = false;
112 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500113 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200114 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400115 Wipe_During_Factory_Reset = false;
116 Wipe_Available_in_GUI = false;
117 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400118 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400119 SubPartition_Of = "";
120 Symlink_Path = "";
121 Symlink_Mount_Point = "";
122 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400123 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400124 Actual_Block_Device = "";
125 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400126 Alternate_Block_Device = "";
127 Removable = false;
128 Is_Present = false;
129 Length = 0;
130 Size = 0;
131 Used = 0;
132 Free = 0;
133 Backup_Size = 0;
134 Can_Be_Encrypted = false;
135 Is_Encrypted = false;
136 Is_Decrypted = false;
137 Decrypted_Block_Device = "";
138 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500139 Backup_Display_Name = "";
140 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400141 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400142 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400143 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400144 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000145 Can_Encrypt_Backup = false;
146 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400147 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400148 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400149 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500150 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400151 Storage_Path = "";
152 Current_File_System = "";
153 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800154 Mount_Flags = 0;
155 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400156 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000157 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000158 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000159#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
160 EcryptFS_Password = "";
161#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400162}
163
164TWPartition::~TWPartition(void) {
165 // Do nothing
166}
167
Dees_Troy5bf43922012-09-07 16:07:55 -0400168bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
169 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
170 int line_len = Line.size(), index = 0, item_index = 0;
171 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400172 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400173 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500174 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400175
Dees_Troy51127312012-09-08 13:08:49 -0400176 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500177 if (full_line[index] == 34)
178 skip = !skip;
179 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400180 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400181 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400182 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000183 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400184 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500185 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000186 Display_Name = full_line + 1;
187 Backup_Display_Name = Display_Name;
188 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400189 index = Mount_Point.size();
190 while (index < line_len) {
191 while (index < line_len && full_line[index] == '\0')
192 index++;
193 if (index >= line_len)
194 continue;
195 ptr = full_line + index;
196 if (item_index == 0) {
197 // File System
198 Fstab_File_System = ptr;
199 Current_File_System = ptr;
200 item_index++;
201 } else if (item_index == 1) {
202 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400203 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400204 MTD_Name = ptr;
205 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400206 } else if (Fstab_File_System == "bml") {
207 if (Mount_Point == "/boot")
208 MTD_Name = "boot";
209 else if (Mount_Point == "/recovery")
210 MTD_Name = "recovery";
211 Primary_Block_Device = ptr;
212 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000213 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 -0400214 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400215 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000216 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400217 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000218 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400219 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400220 } else {
221 Primary_Block_Device = ptr;
222 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400223 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400224 item_index++;
225 } else if (item_index > 1) {
226 if (*ptr == '/') {
227 // Alternate Block Device
228 Alternate_Block_Device = ptr;
229 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
230 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
231 // Partition length
232 ptr += 7;
233 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400234 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
235 // Custom flags, save for later so that new values aren't overwritten by defaults
236 ptr += 6;
237 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000238 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400239 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
240 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400241 } else {
242 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000243 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400244 }
245 }
246 while (index < line_len && full_line[index] != '\0')
247 index++;
248 }
249
250 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
251 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000252 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400253 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000254 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400255 return 0;
256 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400257 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400258 Setup_File_System(Display_Error);
259 if (Mount_Point == "/system") {
260 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500261 Backup_Display_Name = Display_Name;
262 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400263 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500264 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400265 } else if (Mount_Point == "/data") {
266 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500267 Backup_Display_Name = Display_Name;
268 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400269 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400270 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500271 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000272 Can_Encrypt_Backup = true;
273 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400274#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troya13d74f2013-03-24 08:54:55 -0500275 Storage_Name = "Internal Storage";
Dees_Troy5bf43922012-09-07 16:07:55 -0400276 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400277 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500278 Is_Settings_Storage = true;
Dees_Troy51127312012-09-08 13:08:49 -0400279 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000280 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400281 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
282 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400283 Symlink_Mount_Point = "/emmc";
284 } else {
285 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400286 Symlink_Mount_Point = "/sdcard";
287 }
Dees_Troy16b74352012-11-14 22:27:31 +0000288 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
289 Storage_Path = "/data/media/0";
290 Symlink_Path = Storage_Path;
291 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
292 UnMount(true);
293 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400294#endif
295#ifdef TW_INCLUDE_CRYPTO
296 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400297 char crypto_blkdev[255];
298 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
299 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400300 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400301 DataManager::SetValue(TW_IS_DECRYPTED, 1);
302 Is_Encrypted = true;
303 Is_Decrypted = true;
304 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000305 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400306 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600307 if (Is_Present) {
308#ifdef TW_INCLUDE_JB_CRYPTO
309 // No extra flags needed
310#else
311 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
312 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
313 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
314 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
315 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
316 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
317#ifdef CRYPTO_SD_FS_TYPE
318 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
319 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
320 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
321#endif
322 property_set("rw.km_fips_status", "ready");
323#endif
324 if (cryptfs_check_footer() == 0) {
325 Is_Encrypted = true;
326 Is_Decrypted = false;
327 Can_Be_Mounted = false;
328 Current_File_System = "emmc";
329 Setup_Image(Display_Error);
330 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
331 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
332 DataManager::SetValue("tw_crypto_display", "");
333 } else {
334 LOGERR("Could not mount /data and unable to find crypto footer.\n");
335 }
336 } else {
337 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
338 }
Gary Peck82599a82012-11-21 16:23:12 -0800339 } else {
340 // Filesystem is not encrypted and the mount
341 // succeeded, so get it back to the original
342 // unmounted state
343 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400344 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400345 #ifdef RECOVERY_SDCARD_ON_DATA
346 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
347 Recreate_Media_Folder();
348 #endif
349#else
350 #ifdef RECOVERY_SDCARD_ON_DATA
351 Recreate_Media_Folder();
352 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400353#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400354 } else if (Mount_Point == "/cache") {
355 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500356 Backup_Display_Name = Display_Name;
357 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400358 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400359 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500360 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400361 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000362 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500363 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500364 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400365 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400366 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400367 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400368 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500369 Backup_Display_Name = Display_Name;
370 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400371 Is_SubPartition = true;
372 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400373 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500374 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000375 Can_Encrypt_Backup = true;
376 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400377 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400378 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400379 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500380 Backup_Display_Name = Display_Name;
381 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400382 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400383 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500384 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000385 Can_Encrypt_Backup = true;
386 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400387 } else if (Mount_Point == "/boot") {
388 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500389 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400390 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500391 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400392 }
393#ifdef TW_EXTERNAL_STORAGE_PATH
394 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
395 Is_Storage = true;
396 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400397 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500398 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400399#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000400 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400401 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400402 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500403 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400404#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000405 }
Dees_Troy8170a922012-09-18 15:40:25 -0400406#ifdef TW_INTERNAL_STORAGE_PATH
407 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
408 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500409 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400410 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500411 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400412 }
413#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000414 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400415 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500416 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500417 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400418 }
419#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400420 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400421 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400422 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500423 if (Mount_Point == "/boot") {
424 Display_Name = "Boot";
425 Backup_Display_Name = Display_Name;
426 Can_Be_Backed_Up = true;
427 } else if (Mount_Point == "/recovery") {
428 Display_Name = "Recovery";
429 Backup_Display_Name = Display_Name;
Dees_Troya13d74f2013-03-24 08:54:55 -0500430 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400431 }
432
Dees_Troy51127312012-09-08 13:08:49 -0400433 // Process any custom flags
434 if (Flags.size() > 0)
435 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400436 return true;
437}
438
Hashcode62bd9e02013-11-19 21:59:42 -0800439bool TWPartition::Process_FS_Flags(string& Options, int Flags) {
440 int i;
441 char *p;
442 char *savep;
443 char fs_options[250];
444
445 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
446 Options = "";
447
448 p = strtok_r(fs_options, ",", &savep);
449 while (p) {
450 /* Look for the flag "p" in the flag list "fl"
451 * If not found, the loop exits with fl[i].name being null.
452 */
453 for (i = 0; mount_flags[i].name; i++) {
454 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
455 Flags |= mount_flags[i].flag;
456 break;
457 }
458 }
459
460 if (!mount_flags[i].name) {
461 if (Options.size() > 0)
462 Options += ",";
463 Options += p;
464 }
465 p = strtok_r(NULL, ",", &savep);
466 }
467
468 return true;
469}
470
Dees_Troy51127312012-09-08 13:08:49 -0400471bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
472 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500473 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400474 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500475 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400476
477 strcpy(flags, Flags.c_str());
478 flags_len = Flags.size();
479 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500480 if (flags[index] == 34)
481 skip = !skip;
482 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400483 flags[index] = '\0';
484 }
485
486 index = 0;
487 while (index < flags_len) {
488 while (index < flags_len && flags[index] == '\0')
489 index++;
490 if (index >= flags_len)
491 continue;
492 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500493 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400494 if (strcmp(ptr, "removable") == 0) {
495 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600496 } else if (strncmp(ptr, "storage", 7) == 0) {
497 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600498 Is_Storage = true;
499 } else if (ptr_len == 9) {
500 ptr += 9;
501 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
502 LOGINFO("storage set to true\n");
503 Is_Storage = true;
504 } else {
505 LOGINFO("storage set to false\n");
506 Is_Storage = false;
507 }
508 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500509 } else if (strcmp(ptr, "settingsstorage") == 0) {
510 Is_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500511 } else if (strcmp(ptr, "andsec") == 0) {
512 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400513 } else if (strcmp(ptr, "canbewiped") == 0) {
514 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700515 } else if (strcmp(ptr, "usermrf") == 0) {
516 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500517 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
518 ptr += 7;
519 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
520 Can_Be_Backed_Up = true;
521 else
522 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400523 } else if (strcmp(ptr, "wipeingui") == 0) {
524 Can_Be_Wiped = true;
525 Wipe_Available_in_GUI = true;
526 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
527 Can_Be_Wiped = true;
528 Wipe_Available_in_GUI = true;
529 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500530 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000531 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400532 Is_SubPartition = true;
533 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000534 } else if (strcmp(ptr, "ignoreblkid") == 0) {
535 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000536 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
537 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500538 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400539 ptr += 8;
540 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500541 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
542 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400543 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500544 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400545 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500546 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
547 Display_Name.resize(Display_Name.size() - 1);
548 }
549 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
550 has_storage_name = true;
551 ptr += 11;
552 if (*ptr == '\"') ptr++;
553 Storage_Name = ptr;
554 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
555 Storage_Name.resize(Storage_Name.size() - 1);
556 }
557 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
558 has_backup_name = true;
559 ptr += 10;
560 if (*ptr == '\"') ptr++;
561 Backup_Display_Name = ptr;
562 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
563 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
564 }
565 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400566 ptr += 10;
567 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500568 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400569 ptr += 7;
570 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000571 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
572 ptr += 17;
573 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
574 Can_Encrypt_Backup = true;
575 else
576 Can_Encrypt_Backup = false;
577 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
578 ptr += 21;
579 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
580 Can_Encrypt_Backup = true;
581 Use_Userdata_Encryption = true;
582 } else {
583 Use_Userdata_Encryption = false;
584 }
Hashcode62bd9e02013-11-19 21:59:42 -0800585 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
586 ptr += 8;
587 if (*ptr == '\"') ptr++;
588
589 Mount_Options = ptr;
590 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
591 Mount_Options.resize(Mount_Options.size() - 1);
592 }
593 Process_FS_Flags(Mount_Options, Mount_Flags);
Dees_Troy51127312012-09-08 13:08:49 -0400594 } else {
595 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000596 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400597 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000598 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400599 }
600 while (index < flags_len && flags[index] != '\0')
601 index++;
602 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500603 if (has_display_name && !has_storage_name)
604 Storage_Name = Display_Name;
605 if (!has_display_name && has_storage_name)
606 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000607 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500608 Backup_Display_Name = Display_Name;
609 if (!has_display_name && has_backup_name)
610 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400611 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400612}
613
Dees_Troy5bf43922012-09-07 16:07:55 -0400614bool TWPartition::Is_File_System(string File_System) {
615 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400616 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400617 File_System == "ext4" ||
618 File_System == "vfat" ||
619 File_System == "ntfs" ||
620 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500621 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000622 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400623 File_System == "auto")
624 return true;
625 else
626 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400627}
628
Dees_Troy5bf43922012-09-07 16:07:55 -0400629bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400630 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400631 return true;
632 else
633 return false;
634}
635
Dees_Troy51127312012-09-08 13:08:49 -0400636bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400637 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400638 if (mkdir(Path.c_str(), 0777) == -1) {
639 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000640 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400641 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000642 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400643 return false;
644 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000645 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400646 return true;
647 }
648 }
649 return true;
650}
651
Dees_Troy5bf43922012-09-07 16:07:55 -0400652void TWPartition::Setup_File_System(bool Display_Error) {
653 struct statfs st;
654
655 Can_Be_Mounted = true;
656 Can_Be_Wiped = true;
657
Dees_Troy5bf43922012-09-07 16:07:55 -0400658 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400659 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400660 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
661 Backup_Name = Display_Name;
662 Backup_Method = FILES;
663}
664
665void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400666 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
667 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800668 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400669 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800670 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400671 Backup_Method = FLASH_UTILS;
672 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000673 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 -0400674 if (Find_Partition_Size()) {
675 Used = Size;
676 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400677 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400678 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000679 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400680 else
Dees Troyd932ce12013-10-18 17:12:59 +0000681 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400682 }
683}
684
Dees_Troye58d5262012-09-21 12:27:57 -0400685void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500686 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400687 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500688 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400689 Has_Android_Secure = true;
690 Symlink_Path = Mount_Point + "/.android_secure";
691 Symlink_Mount_Point = "/and-sec";
692 Backup_Path = Symlink_Mount_Point;
693 Make_Dir("/and-sec", true);
694 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600695 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400696}
697
Dees_Troy5bf43922012-09-07 16:07:55 -0400698void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
699 char device[512], realDevice[512];
700
701 strcpy(device, Block.c_str());
702 memset(realDevice, 0, sizeof(realDevice));
703 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
704 {
705 strcpy(device, realDevice);
706 memset(realDevice, 0, sizeof(realDevice));
707 }
708
709 if (device[0] != '/') {
710 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000711 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400712 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000713 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400714 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400715 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400716 Block = device;
717 return;
718 }
719}
720
Dees_Troy8e337f32012-10-13 22:07:49 -0400721void TWPartition::Mount_Storage_Retry(void) {
722 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500723 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400724 int retry_count = 5;
725 while (retry_count > 0 && !Mount(false)) {
726 usleep(500000);
727 retry_count--;
728 }
729 Mount(true);
730 }
731}
732
Dees_Troy38bd7602012-09-14 13:33:53 -0400733bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
734 FILE *fp = NULL;
735 char line[255];
736
737 fp = fopen("/proc/mtd", "rt");
738 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000739 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400740 return false;
741 }
742
743 while (fgets(line, sizeof(line), fp) != NULL)
744 {
745 char device[32], label[32];
746 unsigned long size = 0;
747 char* fstype = NULL;
748 int deviceId;
749
750 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
751
752 // Skip header and blank lines
753 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
754 continue;
755
756 // Strip off the trailing " from the label
757 label[strlen(label)-1] = '\0';
758
759 if (strcmp(label, MTD_Name.c_str()) == 0) {
760 // We found our device
761 // Strip off the trailing : from the device
762 device[strlen(device)-1] = '\0';
763 if (sscanf(device,"mtd%d", &deviceId) == 1) {
764 sprintf(device, "/dev/block/mtdblock%d", deviceId);
765 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000766 fclose(fp);
767 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400768 }
769 }
770 }
771 fclose(fp);
772
773 return false;
774}
775
Dees_Troy51127312012-09-08 13:08:49 -0400776bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
777 struct statfs st;
778 string Local_Path = Mount_Point + "/.";
779
780 if (!Mount(Display_Error))
781 return false;
782
783 if (statfs(Local_Path.c_str(), &st) != 0) {
784 if (!Removable) {
785 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000786 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400787 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000788 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400789 }
790 return false;
791 }
792 Size = (st.f_blocks * st.f_bsize);
793 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
794 Free = (st.f_bfree * st.f_bsize);
795 Backup_Size = Used;
796 return true;
797}
798
799bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400800 FILE* fp;
801 char command[255], line[512];
802 int include_block = 1;
803 unsigned int min_len;
804
805 if (!Mount(Display_Error))
806 return false;
807
Dees_Troy38bd7602012-09-14 13:33:53 -0400808 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400809 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200810 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400811 fp = fopen("/tmp/dfoutput.txt", "rt");
812 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000813 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400814 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400815 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400816
817 while (fgets(line, sizeof(line), fp) != NULL)
818 {
819 unsigned long blocks, used, available;
820 char device[64];
821 char tmpString[64];
822
823 if (strncmp(line, "Filesystem", 10) == 0)
824 continue;
825 if (strlen(line) < min_len) {
826 include_block = 0;
827 continue;
828 }
829 if (include_block) {
830 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
831 } else {
832 // The device block string is so long that the df information is on the next line
833 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400834 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400835 while (tmpString[space_count] == 32)
836 space_count++;
837 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
838 }
839
840 // Adjust block size to byte size
841 Size = blocks * 1024ULL;
842 Used = used * 1024ULL;
843 Free = available * 1024ULL;
844 Backup_Size = Used;
845 }
846 fclose(fp);
847 return true;
848}
849
Dees_Troy5bf43922012-09-07 16:07:55 -0400850bool TWPartition::Find_Partition_Size(void) {
851 FILE* fp;
852 char line[512];
853 string tmpdevice;
854
igoriok87e3d932013-01-31 21:03:53 +0200855 fp = fopen("/proc/dumchar_info", "rt");
856 if (fp != NULL) {
857 while (fgets(line, sizeof(line), fp) != NULL)
858 {
859 char label[32], device[32];
860 unsigned long size = 0;
861
862 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
863
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500864 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200865 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
866 continue;
867
868 tmpdevice = "/dev/";
869 tmpdevice += label;
870 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
871 Size = size;
872 fclose(fp);
873 return true;
874 }
875 }
876 }
877
Dees_Troy5bf43922012-09-07 16:07:55 -0400878 // In this case, we'll first get the partitions we care about (with labels)
879 fp = fopen("/proc/partitions", "rt");
880 if (fp == NULL)
881 return false;
882
883 while (fgets(line, sizeof(line), fp) != NULL)
884 {
885 unsigned long major, minor, blocks;
886 char device[512];
887 char tmpString[64];
888
Dees_Troy63c8df72012-09-10 14:02:05 -0400889 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400890 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
891
892 tmpdevice = "/dev/block/";
893 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400894 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400895 // Adjust block size to byte size
896 Size = blocks * 1024ULL;
897 fclose(fp);
898 return true;
899 }
900 }
901 fclose(fp);
902 return false;
903}
904
Dees_Troy5bf43922012-09-07 16:07:55 -0400905bool TWPartition::Is_Mounted(void) {
906 if (!Can_Be_Mounted)
907 return false;
908
909 struct stat st1, st2;
910 string test_path;
911
912 // Check to see if the mount point directory exists
913 test_path = Mount_Point + "/.";
914 if (stat(test_path.c_str(), &st1) != 0) return false;
915
916 // Check to see if the directory above the mount point exists
917 test_path = Mount_Point + "/../.";
918 if (stat(test_path.c_str(), &st2) != 0) return false;
919
920 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
921 int ret = (st1.st_dev != st2.st_dev) ? true : false;
922
923 return ret;
924}
925
926bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000927 int exfat_mounted = 0;
928
Dees_Troy5bf43922012-09-07 16:07:55 -0400929 if (Is_Mounted()) {
930 return true;
931 } else if (!Can_Be_Mounted) {
932 return false;
933 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400934
935 Find_Actual_Block_Device();
936
937 // Check the current file system before mounting
938 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000939 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000940 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 +0000941 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000942 string result;
943 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000944 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000945 Current_File_System = "vfat";
946 } else {
947#ifdef TW_NO_EXFAT_FUSE
948 UnMount(false);
949 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
950 // Some kernels let us mount vfat as exfat which doesn't work out too well
951#else
952 exfat_mounted = 1;
953#endif
954 }
955 }
Dees_Troy22042032012-12-18 21:23:08 +0000956 if (Fstab_File_System == "yaffs2") {
957 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000958 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
959 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
960 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
961 if (Display_Error)
962 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
963 else
964 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
965 return false;
966 } else {
967 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
968 return true;
969 }
970 } else {
971 struct stat st;
972 string test_path = Mount_Point;
973 if (stat(test_path.c_str(), &st) < 0) {
974 if (Display_Error)
975 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
976 else
977 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
978 return false;
979 }
980 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
981 if (new_mode != st.st_mode) {
982 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
983 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
984 if (Display_Error)
985 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
986 else
987 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
988 return false;
989 }
990 }
Dees_Troy22042032012-12-18 21:23:08 +0000991 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000992 }
Dees Troy216e0422014-02-07 03:46:42 +0000993 } 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 +0000994#ifdef TW_NO_EXFAT_FUSE
995 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000996 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000997 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000998 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000999 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001000 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001001 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -08001002 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 +00001003 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001004 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001005 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001006#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001007 if (!Removable && Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001008 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001009 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001010 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1011 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 +00001012 return false;
1013#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001014 }
1015#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001016 }
1017#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1018 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1019 MetaEcfsFile += "/.MetaEcfsFile";
1020 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
1021 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
1022 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001023 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001024 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001025 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001026 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001027 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001028 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -04001029 }
Dees_Troy066eb302013-08-23 17:20:32 +00001030 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
1031 if (Is_Decrypted)
1032 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001033 Is_Decrypted = false;
1034 }
1035#endif
1036 if (Removable)
1037 Update_Size(Display_Error);
1038
1039 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001040 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
1041 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001042 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001043 return true;
1044}
1045
1046bool TWPartition::UnMount(bool Display_Error) {
1047 if (Is_Mounted()) {
1048 int never_unmount_system;
1049
1050 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1051 if (never_unmount_system == 1 && Mount_Point == "/system")
1052 return true; // Never unmount system if you're not supposed to unmount it
1053
Dees_Troyc8bafa12013-01-10 15:43:00 +00001054#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1055 if (EcryptFS_Password.size() > 0) {
1056 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
1057 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001058 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001059 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001060 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001061 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001062 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001063 }
1064 }
1065#endif
1066
Dees_Troy38bd7602012-09-14 13:33:53 -04001067 if (!Symlink_Mount_Point.empty())
1068 umount(Symlink_Mount_Point.c_str());
1069
Dees_Troyb05ddee2013-01-28 20:24:50 +00001070 umount(Mount_Point.c_str());
1071 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001072 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001073 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001074 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001075 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001076 return false;
1077 } else
1078 return true;
1079 } else {
1080 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001081 }
1082}
1083
Gary Peck43acadf2012-11-21 21:19:01 -08001084bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001085 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001086 int check;
1087 string Layout_Filename = Mount_Point + "/.layout_version";
1088
Dees_Troy38bd7602012-09-14 13:33:53 -04001089 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001090 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001091 return false;
1092 }
1093
Dees_Troyc51f1f92012-09-20 15:32:13 -04001094 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001095 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001096
Dees_Troyce675462013-01-09 19:48:21 +00001097#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1098 if (Mount_Point == "/data" && Mount(false)) {
1099 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
1100 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
1101 }
1102#endif
1103
Dees_Troy16c2b312013-01-15 16:51:18 +00001104 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1105 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1106 else
1107 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001108
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001109 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001110 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001111 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001112 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001113
Dees_Troy16c2b312013-01-15 16:51:18 +00001114 DataManager::GetValue(TW_RM_RF_VAR, check);
1115
Hashcodedabfd492013-08-29 22:45:30 -07001116 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001117 wiped = Wipe_RMRF();
1118 else if (New_File_System == "ext4")
1119 wiped = Wipe_EXT4();
1120 else if (New_File_System == "ext2" || New_File_System == "ext3")
1121 wiped = Wipe_EXT23(New_File_System);
1122 else if (New_File_System == "vfat")
1123 wiped = Wipe_FAT();
1124 else if (New_File_System == "exfat")
1125 wiped = Wipe_EXFAT();
1126 else if (New_File_System == "yaffs2")
1127 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001128 else if (New_File_System == "f2fs")
1129 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001130 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001131 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 +00001132 unlink("/.layout_version");
1133 return false;
1134 }
1135 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001136 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001137
Gary Pecke8bc5d72012-12-21 06:45:25 -08001138 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001139#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1140 if (Mount_Point == "/data" && Mount(false)) {
1141 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1142 Make_Dir("/data/system", true);
1143 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1144 }
1145 }
1146#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001147
Dees_Troy1c1ac442013-01-17 21:42:14 +00001148 if (Mount_Point == "/cache")
1149 DataManager::Output_Version();
1150
Dees_Troy16c2b312013-01-15 16:51:18 +00001151 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1152 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1153
1154 if (update_crypt) {
1155 Setup_File_System(false);
1156 if (Is_Encrypted && !Is_Decrypted) {
1157 // just wiped an encrypted partition back to its unencrypted state
1158 Is_Encrypted = false;
1159 Is_Decrypted = false;
1160 Decrypted_Block_Device = "";
1161 if (Mount_Point == "/data") {
1162 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1163 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1164 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001165 }
1166 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001167
1168 if (Mount_Point == "/data" && Has_Data_Media && recreate_media) {
1169 Recreate_Media_Folder();
1170 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001171 }
1172 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001173}
1174
Gary Peck43acadf2012-11-21 21:19:01 -08001175bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001176 if (Is_File_System(Current_File_System))
1177 return Wipe(Current_File_System);
1178 else
1179 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001180}
1181
Dees_Troye58d5262012-09-21 12:27:57 -04001182bool TWPartition::Wipe_AndSec(void) {
1183 if (!Has_Android_Secure)
1184 return false;
1185
Dees_Troye58d5262012-09-21 12:27:57 -04001186 if (!Mount(true))
1187 return false;
1188
Dees_Troy2673cec2013-04-02 20:22:16 +00001189 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001190 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001191 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001192}
1193
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001194bool TWPartition::Can_Repair() {
1195 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/dosfsck"))
1196 return true;
1197 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1198 return true;
1199 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1200 return true;
1201 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1202 return true;
1203 return false;
1204}
1205
1206bool TWPartition::Repair() {
1207 string command;
1208
1209 if (Current_File_System == "vfat") {
1210 if (!TWFunc::Path_Exists("/sbin/dosfsck")) {
1211 gui_print("dosfsck does not exist! Cannot repair!\n");
1212 return false;
1213 }
1214 if (!UnMount(true))
1215 return false;
1216 gui_print("Repairing %s using dosfsck...\n", Display_Name.c_str());
1217 Find_Actual_Block_Device();
1218 command = "/sbin/dosfsck -y " + Actual_Block_Device;
1219 LOGINFO("Repair command: %s\n", command.c_str());
1220 if (TWFunc::Exec_Cmd(command) == 0) {
1221 gui_print("Done.\n");
1222 return true;
1223 } else {
1224 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1225 return false;
1226 }
1227 }
1228 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1229 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
1230 gui_print("e2fsck does not exist! Cannot repair!\n");
1231 return false;
1232 }
1233 if (!UnMount(true))
1234 return false;
1235 gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str());
1236 Find_Actual_Block_Device();
1237 command = "/sbin/e2fsck -p " + Actual_Block_Device;
1238 LOGINFO("Repair command: %s\n", command.c_str());
1239 if (TWFunc::Exec_Cmd(command) == 0) {
1240 gui_print("Done.\n");
1241 return true;
1242 } else {
1243 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1244 return false;
1245 }
1246 }
1247 if (Current_File_System == "exfat") {
1248 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
1249 gui_print("fsck.exfat does not exist! Cannot repair!\n");
1250 return false;
1251 }
1252 if (!UnMount(true))
1253 return false;
1254 gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str());
1255 Find_Actual_Block_Device();
1256 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1257 LOGINFO("Repair command: %s\n", command.c_str());
1258 if (TWFunc::Exec_Cmd(command) == 0) {
1259 gui_print("Done.\n");
1260 return true;
1261 } else {
1262 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1263 return false;
1264 }
1265 }
1266 if (Current_File_System == "f2fs") {
1267 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
1268 gui_print("fsck.f2fs does not exist! Cannot repair!\n");
1269 return false;
1270 }
1271 if (!UnMount(true))
1272 return false;
1273 gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str());
1274 Find_Actual_Block_Device();
1275 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1276 LOGINFO("Repair command: %s\n", command.c_str());
1277 if (TWFunc::Exec_Cmd(command) == 0) {
1278 gui_print("Done.\n");
1279 return true;
1280 } else {
1281 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1282 return false;
1283 }
1284 }
1285 return false;
1286}
1287
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001288bool 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 -04001289 if (Backup_Method == FILES)
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001290 return Backup_Tar(backup_folder, overall_size, other_backups_size);
Dees_Troy38bd7602012-09-14 13:33:53 -04001291 else if (Backup_Method == DD)
1292 return Backup_DD(backup_folder);
1293 else if (Backup_Method == FLASH_UTILS)
1294 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001295 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001296 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001297}
1298
Dees_Troy43d8b002012-09-17 16:00:01 -04001299bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001300 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001301 char split_filename[512];
1302 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001303 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001304
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001305 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001306 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001307 if (!TWFunc::Path_Exists(Full_Filename)) {
1308 // This is a split archive, we presume
1309 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001310 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001311 md5file = split_filename;
1312 md5file += ".md5";
1313 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001314 LOGERR("No md5 file found for '%s'.\n", split_filename);
1315 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001316 return false;
1317 }
1318 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001319 while (index < 1000) {
1320 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001321 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001322 return false;
1323 }
1324 index++;
1325 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001326 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001327 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001328 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001329 } else {
1330 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001331 md5file = Full_Filename + ".md5";
1332 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001333 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1334 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001335 return false;
1336 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001337 md5sum.setfn(Full_Filename);
1338 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001339 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001340 return false;
1341 } else
1342 return true;
1343 }
1344 return false;
1345}
1346
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001347bool 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 -08001348 string Restore_File_System;
1349
1350 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001351 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001352
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001353 Restore_File_System = Get_Restore_File_System(restore_folder);
1354
1355 if (Is_File_System(Restore_File_System))
1356 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1357 else if (Is_Image(Restore_File_System)) {
1358 *already_restored_size += TWFunc::Get_File_Size(Backup_Name);
1359 if (Restore_File_System == "emmc")
1360 return Restore_DD(restore_folder, total_restore_size, already_restored_size);
1361 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1362 return Restore_Flash_Image(restore_folder, total_restore_size, already_restored_size);
1363 }
1364
1365 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1366 return false;
1367}
1368
1369string TWPartition::Get_Restore_File_System(string restore_folder) {
1370 size_t first_period, second_period;
1371 string Restore_File_System;
1372
Gary Peck43acadf2012-11-21 21:19:01 -08001373 // Parse backup filename to extract the file system before wiping
1374 first_period = Backup_FileName.find(".");
1375 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001376 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001377 return false;
1378 }
1379 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1380 second_period = Restore_File_System.find(".");
1381 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001382 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001383 return false;
1384 }
1385 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001386 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001387 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001388}
1389
1390string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001391 if (Backup_Method == NONE)
1392 return "none";
1393 else if (Backup_Method == FILES)
1394 return "files";
1395 else if (Backup_Method == DD)
1396 return "dd";
1397 else if (Backup_Method == FLASH_UTILS)
1398 return "flash_utils";
1399 else
1400 return "undefined";
1401 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001402}
1403
1404bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001405 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001406 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001407 return 1;
1408}
1409
1410bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001411 bool Save_Data_Media = Has_Data_Media;
1412
1413 if (!UnMount(true))
1414 return false;
1415
Dees_Troy38bd7602012-09-14 13:33:53 -04001416 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001417 Decrypted_Block_Device = "";
1418 Is_Decrypted = false;
1419 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001420 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001421 Has_Data_Media = Save_Data_Media;
1422 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1423 Recreate_Media_Folder();
1424 }
Ethan Yonker83e82572014-04-04 10:59:28 -05001425#ifndef TW_OEM_BUILD
Dees_Troy2673cec2013-04-02 20:22:16 +00001426 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Ethan Yonker83e82572014-04-04 10:59:28 -05001427#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001428 return true;
1429 } else {
1430 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001431 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001432 return false;
1433 }
1434 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001435}
1436
1437void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001438 const char* type;
1439 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001440
Dees_Troy68cab492012-12-12 19:29:35 +00001441 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1442 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001443
Dees_Troy38bd7602012-09-14 13:33:53 -04001444 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001445 if (!Is_Present)
1446 return;
Dees_Troy51127312012-09-08 13:08:49 -04001447
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001448 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1449 if (blkid_do_fullprobe(pr)) {
1450 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001451 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001452 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001453 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001454
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001455 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001456 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001457 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001458 return;
1459 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001460
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001461 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001462 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001463}
1464
Gary Peck43acadf2012-11-21 21:19:01 -08001465bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001466 if (!UnMount(true))
1467 return false;
1468
Dees_Troy43d8b002012-09-17 16:00:01 -04001469 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001470 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001471
Dees_Troy2673cec2013-04-02 20:22:16 +00001472 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001473 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001474 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001475 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001476 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001477 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001478 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001479 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001480 return true;
1481 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001482 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001483 return false;
1484 }
1485 } else
1486 return Wipe_RMRF();
1487
1488 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001489}
1490
1491bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001492 if (!UnMount(true))
1493 return false;
1494
Dees_Troyb3265ab2013-08-30 02:59:14 +00001495#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001496 int ret;
1497 char *secontext = NULL;
1498
Dees_Troya95f55c2013-08-17 13:14:43 +00001499 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001500
Dees Troy99c8dbf2014-03-10 16:53:58 +00001501 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001502 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1503 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1504 } else {
1505 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1506 }
1507 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001508 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1509 return false;
1510 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001511 string sedir = Mount_Point + "/lost+found";
1512 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1513 rmdir(sedir.c_str());
1514 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001515 return true;
1516 }
1517#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001518 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001519 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001520
Dees_Troy2673cec2013-04-02 20:22:16 +00001521 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001522 Find_Actual_Block_Device();
1523 Command = "make_ext4fs";
1524 if (!Is_Decrypted && Length != 0) {
1525 // Only use length if we're not decrypted
1526 char len[32];
1527 sprintf(len, "%i", Length);
1528 Command += " -l ";
1529 Command += len;
1530 }
Dees_Troy5295d582013-09-06 15:51:08 +00001531 if (TWFunc::Path_Exists("/file_contexts")) {
1532 Command += " -S /file_contexts";
1533 }
1534 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001535 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001536 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001537 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001538 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001539 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001540 return true;
1541 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001542 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001543 return false;
1544 }
1545 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001546 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001547#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001548 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001549}
1550
1551bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001552 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001553
Dees_Troy43d8b002012-09-17 16:00:01 -04001554 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001555 if (!UnMount(true))
1556 return false;
1557
Dees_Troy2673cec2013-04-02 20:22:16 +00001558 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001559 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001560 command = "mkdosfs " + Actual_Block_Device;
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 = "vfat";
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 return true;
1571 }
1572 else
1573 return Wipe_RMRF();
1574
1575 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001576}
1577
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001578bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001579 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001580
1581 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1582 if (!UnMount(true))
1583 return false;
1584
Dees_Troy2673cec2013-04-02 20:22:16 +00001585 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001586 Find_Actual_Block_Device();
1587 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001588 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001589 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001590 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001591 return true;
1592 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001593 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001594 return false;
1595 }
1596 return true;
1597 }
1598 return false;
1599}
1600
Dees_Troy38bd7602012-09-14 13:33:53 -04001601bool TWPartition::Wipe_MTD() {
1602 if (!UnMount(true))
1603 return false;
1604
Dees_Troy2673cec2013-04-02 20:22:16 +00001605 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001606
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001607 mtd_scan_partitions();
1608 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1609 if (mtd == NULL) {
1610 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1611 return false;
1612 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001613
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001614 MtdWriteContext* ctx = mtd_write_partition(mtd);
1615 if (ctx == NULL) {
1616 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1617 return false;
1618 }
1619 if (mtd_erase_blocks(ctx, -1) == -1) {
1620 mtd_write_close(ctx);
1621 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1622 return false;
1623 }
1624 if (mtd_write_close(ctx) != 0) {
1625 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1626 return false;
1627 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001628 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001629 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001630 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001631 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001632}
1633
1634bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001635 if (!Mount(true))
1636 return false;
1637
Dees_Troy2673cec2013-04-02 20:22:16 +00001638 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001639 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001640 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001641 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001642}
1643
Dees_Troye5017042013-08-29 16:38:55 +00001644bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001645 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001646
1647 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1648 if (!UnMount(true))
1649 return false;
1650
1651 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1652 Find_Actual_Block_Device();
1653 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001654 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001655 Recreate_AndSec_Folder();
1656 gui_print("Done.\n");
1657 return true;
1658 } else {
1659 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1660 return false;
1661 }
1662 return true;
1663 } else {
1664 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1665 return Wipe_RMRF();
1666 }
1667 return false;
1668}
1669
Dees_Troy51a0e822012-09-05 15:24:24 -04001670bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001671#ifdef TW_OEM_BUILD
1672 // In an OEM Build we want to do a full format
1673 return Wipe_Encryption();
1674#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001675 string dir;
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001676 #ifdef HAVE_SELINUX
1677 fixPermissions perms;
1678 #endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001679
1680 // This handles wiping data on devices with "sdcard" in /data/media
1681 if (!Mount(true))
1682 return false;
1683
Dees_Troy2673cec2013-04-02 20:22:16 +00001684 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001685
1686 DIR* d;
1687 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001688 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001689 struct dirent* de;
1690 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001691 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001692 // The media folder is the "internal sdcard"
1693 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001694 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001695 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001696 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001697
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001698 dir = "/data/";
1699 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001700 if (de->d_type == DT_DIR) {