blob: b3c436f8bf04a1539c76201cabda4ec019024b58 [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"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060045#include "set_metadata.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040046extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040047 #include "mtdutils/mtdutils.h"
48 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000049#ifdef USE_EXT4
50 #include "make_ext4fs.h"
51#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060052
53#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060054 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker71413f42014-02-26 13:36:08 -060055#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040056}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040057#ifdef HAVE_SELINUX
58#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060059#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040060#endif
Dees Troy4159aed2014-02-28 17:24:43 +000061#ifdef HAVE_CAPABILITIES
62#include <sys/capability.h>
63#include <sys/xattr.h>
64#include <linux/xattr.h>
65#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040066
bigbiff bigbiff9c754052013-01-09 09:09:08 -050067using namespace std;
68
Dees_Troya95f55c2013-08-17 13:14:43 +000069extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050070extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000071
Hashcode62bd9e02013-11-19 21:59:42 -080072struct flag_list {
73 const char *name;
74 unsigned flag;
75};
76
77static struct flag_list mount_flags[] = {
78 { "noatime", MS_NOATIME },
79 { "noexec", MS_NOEXEC },
80 { "nosuid", MS_NOSUID },
81 { "nodev", MS_NODEV },
82 { "nodiratime", MS_NODIRATIME },
83 { "ro", MS_RDONLY },
84 { "rw", 0 },
85 { "remount", MS_REMOUNT },
86 { "bind", MS_BIND },
87 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000088#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080089 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000090#endif
91#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080092 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000093#endif
94#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -080095 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000096#endif
97#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -080098 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +000099#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800100 { "sync", MS_SYNCHRONOUS },
101 { "defaults", 0 },
102 { 0, 0 },
103};
104
that9e0593e2014-10-08 00:01:24 +0200105TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400106 Can_Be_Mounted = false;
107 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500108 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200109 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400110 Wipe_During_Factory_Reset = false;
111 Wipe_Available_in_GUI = false;
112 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400113 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400114 SubPartition_Of = "";
115 Symlink_Path = "";
116 Symlink_Mount_Point = "";
117 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400118 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400119 Actual_Block_Device = "";
120 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400121 Alternate_Block_Device = "";
122 Removable = false;
123 Is_Present = false;
124 Length = 0;
125 Size = 0;
126 Used = 0;
127 Free = 0;
128 Backup_Size = 0;
129 Can_Be_Encrypted = false;
130 Is_Encrypted = false;
131 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600132 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400133 Decrypted_Block_Device = "";
134 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500135 Backup_Display_Name = "";
136 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400137 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400138 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400139 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400140 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000141 Can_Encrypt_Backup = false;
142 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400143 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400144 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400145 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500146 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400147 Storage_Path = "";
148 Current_File_System = "";
149 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800150 Mount_Flags = 0;
151 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400152 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000153 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000154 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600155 Crypto_Key_Location = "footer";
Dees_Troy51a0e822012-09-05 15:24:24 -0400156}
157
158TWPartition::~TWPartition(void) {
159 // Do nothing
160}
161
Dees_Troy5bf43922012-09-07 16:07:55 -0400162bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
163 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
164 int line_len = Line.size(), index = 0, item_index = 0;
165 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400166 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400167 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500168 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400169
Dees_Troy51127312012-09-08 13:08:49 -0400170 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500171 if (full_line[index] == 34)
172 skip = !skip;
173 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400174 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400175 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400176 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000177 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400178 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500179 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000180 Display_Name = full_line + 1;
181 Backup_Display_Name = Display_Name;
182 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400183 index = Mount_Point.size();
184 while (index < line_len) {
185 while (index < line_len && full_line[index] == '\0')
186 index++;
187 if (index >= line_len)
188 continue;
189 ptr = full_line + index;
190 if (item_index == 0) {
191 // File System
192 Fstab_File_System = ptr;
193 Current_File_System = ptr;
194 item_index++;
195 } else if (item_index == 1) {
196 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400197 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400198 MTD_Name = ptr;
199 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400200 } else if (Fstab_File_System == "bml") {
201 if (Mount_Point == "/boot")
202 MTD_Name = "boot";
203 else if (Mount_Point == "/recovery")
204 MTD_Name = "recovery";
205 Primary_Block_Device = ptr;
206 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000207 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 -0400208 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400209 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000210 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400211 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000212 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400213 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400214 } else {
215 Primary_Block_Device = ptr;
216 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400217 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400218 item_index++;
219 } else if (item_index > 1) {
220 if (*ptr == '/') {
221 // Alternate Block Device
222 Alternate_Block_Device = ptr;
223 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
224 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
225 // Partition length
226 ptr += 7;
227 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400228 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
229 // Custom flags, save for later so that new values aren't overwritten by defaults
230 ptr += 6;
231 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000232 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400233 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
234 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400235 } else {
236 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000237 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400238 }
239 }
240 while (index < line_len && full_line[index] != '\0')
241 index++;
242 }
243
244 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
245 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000246 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400247 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000248 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400249 return 0;
250 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400251 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400252 Setup_File_System(Display_Error);
253 if (Mount_Point == "/system") {
254 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500255 Backup_Display_Name = Display_Name;
256 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400257 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500258 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400259 } else if (Mount_Point == "/data") {
260 Display_Name = "Data";
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_Troy51127312012-09-08 13:08:49 -0400264 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500265 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000266 Can_Encrypt_Backup = true;
267 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500268 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200269 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400270#ifdef TW_INCLUDE_CRYPTO
271 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400272 char crypto_blkdev[255];
273 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
274 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400275 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400276 DataManager::SetValue(TW_IS_DECRYPTED, 1);
277 Is_Encrypted = true;
278 Is_Decrypted = true;
279 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000280 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400281 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600282 if (Is_Present) {
Ethan Yonker253368a2014-11-25 15:00:52 -0600283 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
Ethan Yonker71413f42014-02-26 13:36:08 -0600284 if (cryptfs_check_footer() == 0) {
285 Is_Encrypted = true;
286 Is_Decrypted = false;
287 Can_Be_Mounted = false;
288 Current_File_System = "emmc";
289 Setup_Image(Display_Error);
290 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
291 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
292 DataManager::SetValue("tw_crypto_display", "");
293 } else {
294 LOGERR("Could not mount /data and unable to find crypto footer.\n");
295 }
296 } else {
297 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
298 }
Gary Peck82599a82012-11-21 16:23:12 -0800299 } else {
300 // Filesystem is not encrypted and the mount
301 // succeeded, so get it back to the original
302 // unmounted state
303 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400304 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500305 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400306 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400307#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500308 if (datamedia)
309 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400310#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400311 } else if (Mount_Point == "/cache") {
312 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500313 Backup_Display_Name = Display_Name;
314 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400315 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400316 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500317 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400318 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000319 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500320 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500321 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400322 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400323 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400324 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400325 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500326 Backup_Display_Name = Display_Name;
327 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400328 Is_SubPartition = true;
329 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400330 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500331 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000332 Can_Encrypt_Backup = true;
333 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400334 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400335 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400336 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500337 Backup_Display_Name = Display_Name;
338 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400339 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400340 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500341 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000342 Can_Encrypt_Backup = true;
343 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400344 } else if (Mount_Point == "/boot") {
345 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500346 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400347 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500348 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400349 }
350#ifdef TW_EXTERNAL_STORAGE_PATH
351 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
352 Is_Storage = true;
353 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400354 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500355 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400356#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000357 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400358 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400359 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500360 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400361#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000362 }
Dees_Troy8170a922012-09-18 15:40:25 -0400363#ifdef TW_INTERNAL_STORAGE_PATH
364 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
365 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500366 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400367 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500368 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400369 }
370#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000371 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400372 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500373 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500374 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400375 }
376#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400377 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400378 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400379 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500380 if (Mount_Point == "/boot") {
381 Display_Name = "Boot";
382 Backup_Display_Name = Display_Name;
383 Can_Be_Backed_Up = true;
384 } else if (Mount_Point == "/recovery") {
385 Display_Name = "Recovery";
386 Backup_Display_Name = Display_Name;
Dees_Troya13d74f2013-03-24 08:54:55 -0500387 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400388 }
389
Dees_Troy51127312012-09-08 13:08:49 -0400390 // Process any custom flags
391 if (Flags.size() > 0)
392 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400393 return true;
394}
395
Hashcode62bd9e02013-11-19 21:59:42 -0800396bool TWPartition::Process_FS_Flags(string& Options, int Flags) {
397 int i;
398 char *p;
399 char *savep;
400 char fs_options[250];
401
402 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
403 Options = "";
404
405 p = strtok_r(fs_options, ",", &savep);
406 while (p) {
407 /* Look for the flag "p" in the flag list "fl"
408 * If not found, the loop exits with fl[i].name being null.
409 */
410 for (i = 0; mount_flags[i].name; i++) {
411 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
412 Flags |= mount_flags[i].flag;
413 break;
414 }
415 }
416
417 if (!mount_flags[i].name) {
418 if (Options.size() > 0)
419 Options += ",";
420 Options += p;
421 }
422 p = strtok_r(NULL, ",", &savep);
423 }
424
425 return true;
426}
427
Dees_Troy51127312012-09-08 13:08:49 -0400428bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
429 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500430 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400431 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500432 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400433
434 strcpy(flags, Flags.c_str());
435 flags_len = Flags.size();
436 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500437 if (flags[index] == 34)
438 skip = !skip;
439 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400440 flags[index] = '\0';
441 }
442
443 index = 0;
444 while (index < flags_len) {
445 while (index < flags_len && flags[index] == '\0')
446 index++;
447 if (index >= flags_len)
448 continue;
449 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500450 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400451 if (strcmp(ptr, "removable") == 0) {
452 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600453 } else if (strncmp(ptr, "storage", 7) == 0) {
454 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600455 Is_Storage = true;
456 } else if (ptr_len == 9) {
457 ptr += 9;
458 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
459 LOGINFO("storage set to true\n");
460 Is_Storage = true;
461 } else {
462 LOGINFO("storage set to false\n");
463 Is_Storage = false;
464 }
465 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500466 } else if (strcmp(ptr, "settingsstorage") == 0) {
467 Is_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500468 } else if (strcmp(ptr, "andsec") == 0) {
469 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400470 } else if (strcmp(ptr, "canbewiped") == 0) {
471 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700472 } else if (strcmp(ptr, "usermrf") == 0) {
473 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500474 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
475 ptr += 7;
476 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
477 Can_Be_Backed_Up = true;
478 else
479 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400480 } else if (strcmp(ptr, "wipeingui") == 0) {
481 Can_Be_Wiped = true;
482 Wipe_Available_in_GUI = true;
483 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
484 Can_Be_Wiped = true;
485 Wipe_Available_in_GUI = true;
486 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500487 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000488 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400489 Is_SubPartition = true;
490 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000491 } else if (strcmp(ptr, "ignoreblkid") == 0) {
492 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000493 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
494 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500495 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400496 ptr += 8;
497 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500498 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
499 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400500 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500501 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400502 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500503 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
504 Display_Name.resize(Display_Name.size() - 1);
505 }
506 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
507 has_storage_name = true;
508 ptr += 11;
509 if (*ptr == '\"') ptr++;
510 Storage_Name = ptr;
511 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
512 Storage_Name.resize(Storage_Name.size() - 1);
513 }
514 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
515 has_backup_name = true;
516 ptr += 10;
517 if (*ptr == '\"') ptr++;
518 Backup_Display_Name = ptr;
519 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
520 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
521 }
522 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400523 ptr += 10;
524 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500525 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400526 ptr += 7;
527 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000528 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
529 ptr += 17;
530 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
531 Can_Encrypt_Backup = true;
532 else
533 Can_Encrypt_Backup = false;
534 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
535 ptr += 21;
536 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
537 Can_Encrypt_Backup = true;
538 Use_Userdata_Encryption = true;
539 } else {
540 Use_Userdata_Encryption = false;
541 }
Hashcode62bd9e02013-11-19 21:59:42 -0800542 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
543 ptr += 8;
544 if (*ptr == '\"') ptr++;
545
546 Mount_Options = ptr;
547 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
548 Mount_Options.resize(Mount_Options.size() - 1);
549 }
550 Process_FS_Flags(Mount_Options, Mount_Flags);
Ethan Yonker253368a2014-11-25 15:00:52 -0600551 } else if ((ptr_len > 12 && strncmp(ptr, "encryptable=", 12) == 0) || (ptr_len > 13 && strncmp(ptr, "forceencrypt=", 13) == 0)) {
552 ptr += 12;
553 if (*ptr == '=') ptr++;
554 if (*ptr == '\"') ptr++;
555
556 Crypto_Key_Location = ptr;
557 if (Crypto_Key_Location.substr(Crypto_Key_Location.size() - 1, 1) == "\"") {
558 Crypto_Key_Location.resize(Crypto_Key_Location.size() - 1);
559 }
560 } else if (ptr_len > 8 && strncmp(ptr, "mounttodecrypt", 14) == 0) {
561 Mount_To_Decrypt = true;
Dees_Troy51127312012-09-08 13:08:49 -0400562 } else {
563 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000564 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400565 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000566 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400567 }
568 while (index < flags_len && flags[index] != '\0')
569 index++;
570 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500571 if (has_display_name && !has_storage_name)
572 Storage_Name = Display_Name;
573 if (!has_display_name && has_storage_name)
574 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000575 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500576 Backup_Display_Name = Display_Name;
577 if (!has_display_name && has_backup_name)
578 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400579 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400580}
581
Dees_Troy5bf43922012-09-07 16:07:55 -0400582bool TWPartition::Is_File_System(string File_System) {
583 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400584 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400585 File_System == "ext4" ||
586 File_System == "vfat" ||
587 File_System == "ntfs" ||
588 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500589 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000590 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400591 File_System == "auto")
592 return true;
593 else
594 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400595}
596
Dees_Troy5bf43922012-09-07 16:07:55 -0400597bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400598 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400599 return true;
600 else
601 return false;
602}
603
Dees_Troy51127312012-09-08 13:08:49 -0400604bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400605 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400606 if (mkdir(Path.c_str(), 0777) == -1) {
607 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000608 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400609 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000610 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400611 return false;
612 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000613 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400614 return true;
615 }
616 }
617 return true;
618}
619
Dees_Troy5bf43922012-09-07 16:07:55 -0400620void TWPartition::Setup_File_System(bool Display_Error) {
621 struct statfs st;
622
623 Can_Be_Mounted = true;
624 Can_Be_Wiped = true;
625
Dees_Troy5bf43922012-09-07 16:07:55 -0400626 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400627 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400628 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
629 Backup_Name = Display_Name;
630 Backup_Method = FILES;
631}
632
633void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400634 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
635 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800636 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400637 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800638 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400639 Backup_Method = FLASH_UTILS;
640 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000641 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 -0400642 if (Find_Partition_Size()) {
643 Used = Size;
644 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400645 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400646 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000647 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400648 else
Dees Troyd932ce12013-10-18 17:12:59 +0000649 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400650 }
651}
652
Dees_Troye58d5262012-09-21 12:27:57 -0400653void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500654 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400655 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500656 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400657 Has_Android_Secure = true;
658 Symlink_Path = Mount_Point + "/.android_secure";
659 Symlink_Mount_Point = "/and-sec";
660 Backup_Path = Symlink_Mount_Point;
661 Make_Dir("/and-sec", true);
662 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600663 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400664}
665
that9e0593e2014-10-08 00:01:24 +0200666void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500667 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
668 Storage_Name = "Internal Storage";
669 Has_Data_Media = true;
670 Is_Storage = true;
671 Is_Settings_Storage = true;
672 Storage_Path = "/data/media";
673 Symlink_Path = Storage_Path;
674 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
675 Make_Dir("/emmc", false);
676 Symlink_Mount_Point = "/emmc";
677 } else {
678 Make_Dir("/sdcard", false);
679 Symlink_Mount_Point = "/sdcard";
680 }
681 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
682 Storage_Path = "/data/media/0";
683 Symlink_Path = Storage_Path;
684 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
685 UnMount(true);
686 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500687 DataManager::SetValue("tw_has_internal", 1);
688 DataManager::SetValue("tw_has_data_media", 1);
689 du.add_absolute_dir("/data/media");
690}
691
Dees_Troy5bf43922012-09-07 16:07:55 -0400692void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
693 char device[512], realDevice[512];
694
695 strcpy(device, Block.c_str());
696 memset(realDevice, 0, sizeof(realDevice));
697 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
698 {
699 strcpy(device, realDevice);
700 memset(realDevice, 0, sizeof(realDevice));
701 }
702
703 if (device[0] != '/') {
704 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000705 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400706 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000707 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400708 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400709 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400710 Block = device;
711 return;
712 }
713}
714
Dees_Troy8e337f32012-10-13 22:07:49 -0400715void TWPartition::Mount_Storage_Retry(void) {
716 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500717 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400718 int retry_count = 5;
719 while (retry_count > 0 && !Mount(false)) {
720 usleep(500000);
721 retry_count--;
722 }
723 Mount(true);
724 }
725}
726
Dees_Troy38bd7602012-09-14 13:33:53 -0400727bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
728 FILE *fp = NULL;
729 char line[255];
730
731 fp = fopen("/proc/mtd", "rt");
732 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000733 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400734 return false;
735 }
736
737 while (fgets(line, sizeof(line), fp) != NULL)
738 {
739 char device[32], label[32];
740 unsigned long size = 0;
741 char* fstype = NULL;
742 int deviceId;
743
744 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
745
746 // Skip header and blank lines
747 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
748 continue;
749
750 // Strip off the trailing " from the label
751 label[strlen(label)-1] = '\0';
752
753 if (strcmp(label, MTD_Name.c_str()) == 0) {
754 // We found our device
755 // Strip off the trailing : from the device
756 device[strlen(device)-1] = '\0';
757 if (sscanf(device,"mtd%d", &deviceId) == 1) {
758 sprintf(device, "/dev/block/mtdblock%d", deviceId);
759 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000760 fclose(fp);
761 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400762 }
763 }
764 }
765 fclose(fp);
766
767 return false;
768}
769
Dees_Troy51127312012-09-08 13:08:49 -0400770bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
771 struct statfs st;
772 string Local_Path = Mount_Point + "/.";
773
774 if (!Mount(Display_Error))
775 return false;
776
777 if (statfs(Local_Path.c_str(), &st) != 0) {
778 if (!Removable) {
779 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000780 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400781 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000782 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400783 }
784 return false;
785 }
786 Size = (st.f_blocks * st.f_bsize);
787 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
788 Free = (st.f_bfree * st.f_bsize);
789 Backup_Size = Used;
790 return true;
791}
792
793bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400794 FILE* fp;
795 char command[255], line[512];
796 int include_block = 1;
797 unsigned int min_len;
798
799 if (!Mount(Display_Error))
800 return false;
801
Dees_Troy38bd7602012-09-14 13:33:53 -0400802 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400803 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200804 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400805 fp = fopen("/tmp/dfoutput.txt", "rt");
806 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000807 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400808 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400809 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400810
811 while (fgets(line, sizeof(line), fp) != NULL)
812 {
813 unsigned long blocks, used, available;
814 char device[64];
815 char tmpString[64];
816
817 if (strncmp(line, "Filesystem", 10) == 0)
818 continue;
819 if (strlen(line) < min_len) {
820 include_block = 0;
821 continue;
822 }
823 if (include_block) {
824 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
825 } else {
826 // The device block string is so long that the df information is on the next line
827 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400828 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400829 while (tmpString[space_count] == 32)
830 space_count++;
831 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
832 }
833
834 // Adjust block size to byte size
835 Size = blocks * 1024ULL;
836 Used = used * 1024ULL;
837 Free = available * 1024ULL;
838 Backup_Size = Used;
839 }
840 fclose(fp);
841 return true;
842}
843
Dees_Troy5bf43922012-09-07 16:07:55 -0400844bool TWPartition::Find_Partition_Size(void) {
845 FILE* fp;
846 char line[512];
847 string tmpdevice;
848
igoriok87e3d932013-01-31 21:03:53 +0200849 fp = fopen("/proc/dumchar_info", "rt");
850 if (fp != NULL) {
851 while (fgets(line, sizeof(line), fp) != NULL)
852 {
853 char label[32], device[32];
854 unsigned long size = 0;
855
thatd43bf2d2014-09-21 23:13:02 +0200856 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200857
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500858 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200859 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
860 continue;
861
862 tmpdevice = "/dev/";
863 tmpdevice += label;
864 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
865 Size = size;
866 fclose(fp);
867 return true;
868 }
869 }
870 }
871
Dees_Troy5bf43922012-09-07 16:07:55 -0400872 // In this case, we'll first get the partitions we care about (with labels)
873 fp = fopen("/proc/partitions", "rt");
874 if (fp == NULL)
875 return false;
876
877 while (fgets(line, sizeof(line), fp) != NULL)
878 {
879 unsigned long major, minor, blocks;
880 char device[512];
881 char tmpString[64];
882
Dees_Troy63c8df72012-09-10 14:02:05 -0400883 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400884 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
885
886 tmpdevice = "/dev/block/";
887 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400888 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400889 // Adjust block size to byte size
890 Size = blocks * 1024ULL;
891 fclose(fp);
892 return true;
893 }
894 }
895 fclose(fp);
896 return false;
897}
898
Dees_Troy5bf43922012-09-07 16:07:55 -0400899bool TWPartition::Is_Mounted(void) {
900 if (!Can_Be_Mounted)
901 return false;
902
903 struct stat st1, st2;
904 string test_path;
905
906 // Check to see if the mount point directory exists
907 test_path = Mount_Point + "/.";
908 if (stat(test_path.c_str(), &st1) != 0) return false;
909
910 // Check to see if the directory above the mount point exists
911 test_path = Mount_Point + "/../.";
912 if (stat(test_path.c_str(), &st2) != 0) return false;
913
914 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
915 int ret = (st1.st_dev != st2.st_dev) ? true : false;
916
917 return ret;
918}
919
920bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000921 int exfat_mounted = 0;
922
Dees_Troy5bf43922012-09-07 16:07:55 -0400923 if (Is_Mounted()) {
924 return true;
925 } else if (!Can_Be_Mounted) {
926 return false;
927 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400928
929 Find_Actual_Block_Device();
930
931 // Check the current file system before mounting
932 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000933 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000934 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 +0000935 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000936 string result;
937 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000938 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000939 Current_File_System = "vfat";
940 } else {
941#ifdef TW_NO_EXFAT_FUSE
942 UnMount(false);
943 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
944 // Some kernels let us mount vfat as exfat which doesn't work out too well
945#else
946 exfat_mounted = 1;
947#endif
948 }
949 }
Dees_Troy22042032012-12-18 21:23:08 +0000950 if (Fstab_File_System == "yaffs2") {
951 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000952 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
953 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
954 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
955 if (Display_Error)
956 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
957 else
958 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
959 return false;
960 } else {
961 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
962 return true;
963 }
964 } else {
965 struct stat st;
966 string test_path = Mount_Point;
967 if (stat(test_path.c_str(), &st) < 0) {
968 if (Display_Error)
969 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
970 else
971 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
972 return false;
973 }
974 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
975 if (new_mode != st.st_mode) {
976 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
977 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
978 if (Display_Error)
979 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
980 else
981 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
982 return false;
983 }
984 }
Dees_Troy22042032012-12-18 21:23:08 +0000985 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000986 }
Dees Troy216e0422014-02-07 03:46:42 +0000987 } 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 +0000988#ifdef TW_NO_EXFAT_FUSE
989 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000990 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000991 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000992 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000993 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000994 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000995 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800996 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 +0000997 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000998 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000999 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001000#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001001 if (!Removable && Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001002 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001003 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001004 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1005 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 +00001006 return false;
1007#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001008 }
1009#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001010 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001011
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001012 if (Removable)
1013 Update_Size(Display_Error);
1014
1015 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001016 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
1017 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001018 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001019 return true;
1020}
1021
1022bool TWPartition::UnMount(bool Display_Error) {
1023 if (Is_Mounted()) {
1024 int never_unmount_system;
1025
1026 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1027 if (never_unmount_system == 1 && Mount_Point == "/system")
1028 return true; // Never unmount system if you're not supposed to unmount it
1029
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001030 if (Is_Storage)
1031 TWFunc::Toggle_MTP(false);
1032
Dees_Troy38bd7602012-09-14 13:33:53 -04001033 if (!Symlink_Mount_Point.empty())
1034 umount(Symlink_Mount_Point.c_str());
1035
Dees_Troyb05ddee2013-01-28 20:24:50 +00001036 umount(Mount_Point.c_str());
1037 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001038 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001039 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001040 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001041 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001042 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001043 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001044 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001045 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001046 } else {
1047 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001048 }
1049}
1050
Gary Peck43acadf2012-11-21 21:19:01 -08001051bool TWPartition::Wipe(string New_File_System) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001052 bool wiped = false, update_crypt = false, recreate_media = true, mtp_toggle = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001053 int check;
1054 string Layout_Filename = Mount_Point + "/.layout_version";
1055
Dees_Troy38bd7602012-09-14 13:33:53 -04001056 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001057 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001058 return false;
1059 }
1060
Dees_Troyc51f1f92012-09-20 15:32:13 -04001061 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001062 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001063
Dees_Troy16c2b312013-01-15 16:51:18 +00001064 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1065 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1066 else
1067 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001068
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001069 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001070 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001071 recreate_media = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001072 mtp_toggle = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001073 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001074 DataManager::GetValue(TW_RM_RF_VAR, check);
1075
Hashcodedabfd492013-08-29 22:45:30 -07001076 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001077 wiped = Wipe_RMRF();
1078 else if (New_File_System == "ext4")
1079 wiped = Wipe_EXT4();
1080 else if (New_File_System == "ext2" || New_File_System == "ext3")
1081 wiped = Wipe_EXT23(New_File_System);
1082 else if (New_File_System == "vfat")
1083 wiped = Wipe_FAT();
1084 else if (New_File_System == "exfat")
1085 wiped = Wipe_EXFAT();
1086 else if (New_File_System == "yaffs2")
1087 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001088 else if (New_File_System == "f2fs")
1089 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001090 else {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001091 if (Is_Storage) {
1092 TWFunc::Toggle_MTP(true);
1093 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001094 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 +00001095 unlink("/.layout_version");
1096 return false;
1097 }
1098 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001099 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001100
Gary Pecke8bc5d72012-12-21 06:45:25 -08001101 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001102 if (Mount_Point == "/cache")
1103 DataManager::Output_Version();
1104
Dees_Troy16c2b312013-01-15 16:51:18 +00001105 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1106 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1107
1108 if (update_crypt) {
1109 Setup_File_System(false);
1110 if (Is_Encrypted && !Is_Decrypted) {
1111 // just wiped an encrypted partition back to its unencrypted state
1112 Is_Encrypted = false;
1113 Is_Decrypted = false;
1114 Decrypted_Block_Device = "";
1115 if (Mount_Point == "/data") {
1116 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1117 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1118 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001119 }
1120 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001121
1122 if (Mount_Point == "/data" && Has_Data_Media && recreate_media) {
1123 Recreate_Media_Folder();
1124 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001125 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001126 if (Is_Storage && mtp_toggle) {
1127 TWFunc::Toggle_MTP(true);
1128 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001129 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001130}
1131
Gary Peck43acadf2012-11-21 21:19:01 -08001132bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001133 if (Is_File_System(Current_File_System))
1134 return Wipe(Current_File_System);
1135 else
1136 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001137}
1138
Dees_Troye58d5262012-09-21 12:27:57 -04001139bool TWPartition::Wipe_AndSec(void) {
1140 if (!Has_Android_Secure)
1141 return false;
1142
Dees_Troye58d5262012-09-21 12:27:57 -04001143 if (!Mount(true))
1144 return false;
1145
Dees_Troy2673cec2013-04-02 20:22:16 +00001146 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001147 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001148 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001149}
1150
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001151bool TWPartition::Can_Repair() {
1152 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/dosfsck"))
1153 return true;
1154 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1155 return true;
1156 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1157 return true;
1158 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1159 return true;
1160 return false;
1161}
1162
1163bool TWPartition::Repair() {
1164 string command;
1165
1166 if (Current_File_System == "vfat") {
1167 if (!TWFunc::Path_Exists("/sbin/dosfsck")) {
1168 gui_print("dosfsck does not exist! Cannot repair!\n");
1169 return false;
1170 }
1171 if (!UnMount(true))
1172 return false;
1173 gui_print("Repairing %s using dosfsck...\n", Display_Name.c_str());
1174 Find_Actual_Block_Device();
1175 command = "/sbin/dosfsck -y " + Actual_Block_Device;
1176 LOGINFO("Repair command: %s\n", command.c_str());
1177 if (TWFunc::Exec_Cmd(command) == 0) {
1178 gui_print("Done.\n");
1179 return true;
1180 } else {
1181 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1182 return false;
1183 }
1184 }
1185 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1186 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
1187 gui_print("e2fsck does not exist! Cannot repair!\n");
1188 return false;
1189 }
1190 if (!UnMount(true))
1191 return false;
1192 gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str());
1193 Find_Actual_Block_Device();
1194 command = "/sbin/e2fsck -p " + Actual_Block_Device;
1195 LOGINFO("Repair command: %s\n", command.c_str());
1196 if (TWFunc::Exec_Cmd(command) == 0) {
1197 gui_print("Done.\n");
1198 return true;
1199 } else {
1200 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1201 return false;
1202 }
1203 }
1204 if (Current_File_System == "exfat") {
1205 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
1206 gui_print("fsck.exfat does not exist! Cannot repair!\n");
1207 return false;
1208 }
1209 if (!UnMount(true))
1210 return false;
1211 gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str());
1212 Find_Actual_Block_Device();
1213 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1214 LOGINFO("Repair command: %s\n", command.c_str());
1215 if (TWFunc::Exec_Cmd(command) == 0) {
1216 gui_print("Done.\n");
1217 return true;
1218 } else {
1219 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1220 return false;
1221 }
1222 }
1223 if (Current_File_System == "f2fs") {
1224 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
1225 gui_print("fsck.f2fs does not exist! Cannot repair!\n");
1226 return false;
1227 }
1228 if (!UnMount(true))
1229 return false;
1230 gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str());
1231 Find_Actual_Block_Device();
1232 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1233 LOGINFO("Repair command: %s\n", command.c_str());
1234 if (TWFunc::Exec_Cmd(command) == 0) {
1235 gui_print("Done.\n");
1236 return true;
1237 } else {
1238 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1239 return false;
1240 }
1241 }
1242 return false;
1243}
1244
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001245bool 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 -04001246 if (Backup_Method == FILES)
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001247 return Backup_Tar(backup_folder, overall_size, other_backups_size);
Dees_Troy38bd7602012-09-14 13:33:53 -04001248 else if (Backup_Method == DD)
1249 return Backup_DD(backup_folder);
1250 else if (Backup_Method == FLASH_UTILS)
1251 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001252 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001253 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001254}
1255
Dees_Troy43d8b002012-09-17 16:00:01 -04001256bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001257 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001258 char split_filename[512];
1259 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001260 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001261
Captain Throwbackff5935f2014-09-23 16:08:34 -04001262 sync();
1263
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001264 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001265 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001266 if (!TWFunc::Path_Exists(Full_Filename)) {
1267 // This is a split archive, we presume
1268 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001269 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001270 md5file = split_filename;
1271 md5file += ".md5";
1272 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001273 LOGERR("No md5 file found for '%s'.\n", split_filename);
1274 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001275 return false;
1276 }
1277 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001278 while (index < 1000) {
1279 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001280 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001281 return false;
1282 }
1283 index++;
1284 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001285 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001286 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001287 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001288 } else {
1289 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001290 md5file = Full_Filename + ".md5";
1291 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001292 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1293 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001294 return false;
1295 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001296 md5sum.setfn(Full_Filename);
1297 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001298 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001299 return false;
1300 } else
1301 return true;
1302 }
1303 return false;
1304}
1305
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001306bool 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 -08001307 string Restore_File_System;
1308
1309 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001310 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001311
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001312 Restore_File_System = Get_Restore_File_System(restore_folder);
1313
1314 if (Is_File_System(Restore_File_System))
1315 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1316 else if (Is_Image(Restore_File_System)) {
1317 *already_restored_size += TWFunc::Get_File_Size(Backup_Name);
1318 if (Restore_File_System == "emmc")
1319 return Restore_DD(restore_folder, total_restore_size, already_restored_size);
1320 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1321 return Restore_Flash_Image(restore_folder, total_restore_size, already_restored_size);
1322 }
1323
1324 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1325 return false;
1326}
1327
1328string TWPartition::Get_Restore_File_System(string restore_folder) {
1329 size_t first_period, second_period;
1330 string Restore_File_System;
1331
Gary Peck43acadf2012-11-21 21:19:01 -08001332 // Parse backup filename to extract the file system before wiping
1333 first_period = Backup_FileName.find(".");
1334 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001335 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001336 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001337 }
1338 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1339 second_period = Restore_File_System.find(".");
1340 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001341 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001342 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001343 }
1344 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001345 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001346 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001347}
1348
1349string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001350 if (Backup_Method == NONE)
1351 return "none";
1352 else if (Backup_Method == FILES)
1353 return "files";
1354 else if (Backup_Method == DD)
1355 return "dd";
1356 else if (Backup_Method == FLASH_UTILS)
1357 return "flash_utils";
1358 else
1359 return "undefined";
1360 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001361}
1362
1363bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001364 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001365 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001366 return 1;
1367}
1368
1369bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001370 bool Save_Data_Media = Has_Data_Media;
1371
1372 if (!UnMount(true))
1373 return false;
1374
Dees_Troy38bd7602012-09-14 13:33:53 -04001375 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001376 Decrypted_Block_Device = "";
1377 Is_Decrypted = false;
1378 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001379 Find_Actual_Block_Device();
1380 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001381 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001382 Has_Data_Media = Save_Data_Media;
1383 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1384 Recreate_Media_Folder();
1385 }
Ethan Yonker83e82572014-04-04 10:59:28 -05001386#ifndef TW_OEM_BUILD
Dees_Troy2673cec2013-04-02 20:22:16 +00001387 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Ethan Yonker83e82572014-04-04 10:59:28 -05001388#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001389 TWFunc::Toggle_MTP(mtp_was_enabled);
Dees_Troy38bd7602012-09-14 13:33:53 -04001390 return true;
1391 } else {
1392 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001393 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001394 return false;
1395 }
1396 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001397}
1398
1399void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001400 const char* type;
1401 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001402
Dees_Troy68cab492012-12-12 19:29:35 +00001403 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1404 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001405
Dees_Troy38bd7602012-09-14 13:33:53 -04001406 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001407 if (!Is_Present)
1408 return;
Dees_Troy51127312012-09-08 13:08:49 -04001409
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001410 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1411 if (blkid_do_fullprobe(pr)) {
1412 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001413 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001414 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001415 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001416
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001417 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001418 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001419 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001420 return;
1421 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001422
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001423 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001424 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001425}
1426
Gary Peck43acadf2012-11-21 21:19:01 -08001427bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001428 if (!UnMount(true))
1429 return false;
1430
Dees_Troy43d8b002012-09-17 16:00:01 -04001431 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001432 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001433
Dees_Troy2673cec2013-04-02 20:22:16 +00001434 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001435 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001436 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001437 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001438 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001439 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001440 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001441 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001442 return true;
1443 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001444 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001445 return false;
1446 }
1447 } else
1448 return Wipe_RMRF();
1449
1450 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001451}
1452
1453bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001454 Find_Actual_Block_Device();
1455 if (!Is_Present) {
1456 LOGERR("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1457 return false;
1458 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001459 if (!UnMount(true))
1460 return false;
1461
Dees_Troyb3265ab2013-08-30 02:59:14 +00001462#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001463 int ret;
1464 char *secontext = NULL;
1465
Dees_Troya95f55c2013-08-17 13:14:43 +00001466 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001467
Dees Troy99c8dbf2014-03-10 16:53:58 +00001468 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001469 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1470 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1471 } else {
1472 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1473 }
1474 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001475 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1476 return false;
1477 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001478 string sedir = Mount_Point + "/lost+found";
1479 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1480 rmdir(sedir.c_str());
1481 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001482 return true;
1483 }
1484#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001485 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001486 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001487
Dees_Troy2673cec2013-04-02 20:22:16 +00001488 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001489 Find_Actual_Block_Device();
1490 Command = "make_ext4fs";
1491 if (!Is_Decrypted && Length != 0) {
1492 // Only use length if we're not decrypted
1493 char len[32];
1494 sprintf(len, "%i", Length);
1495 Command += " -l ";
1496 Command += len;
1497 }
Dees_Troy5295d582013-09-06 15:51:08 +00001498 if (TWFunc::Path_Exists("/file_contexts")) {
1499 Command += " -S /file_contexts";
1500 }
1501 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001502 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001503 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001504 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001505 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001506 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001507 return true;
1508 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001509 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001510 return false;
1511 }
1512 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001513 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001514#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001515 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001516}
1517
1518bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001519 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001520
Dees_Troy43d8b002012-09-17 16:00:01 -04001521 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001522 if (!UnMount(true))
1523 return false;
1524
Dees_Troy2673cec2013-04-02 20:22:16 +00001525 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001526 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001527 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001528 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001529 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001530 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001531 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001532 return true;
1533 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001534 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001535 return false;
1536 }
1537 return true;
1538 }
1539 else
1540 return Wipe_RMRF();
1541
1542 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001543}
1544
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001545bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001546 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001547
1548 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1549 if (!UnMount(true))
1550 return false;
1551
Dees_Troy2673cec2013-04-02 20:22:16 +00001552 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001553 Find_Actual_Block_Device();
1554 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001555 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001556 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001557 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001558 return true;
1559 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001560 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001561 return false;
1562 }
1563 return true;
1564 }
1565 return false;
1566}
1567
Dees_Troy38bd7602012-09-14 13:33:53 -04001568bool TWPartition::Wipe_MTD() {
1569 if (!UnMount(true))
1570 return false;
1571
Dees_Troy2673cec2013-04-02 20:22:16 +00001572 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001573
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001574 mtd_scan_partitions();
1575 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1576 if (mtd == NULL) {
1577 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1578 return false;
1579 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001580
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001581 MtdWriteContext* ctx = mtd_write_partition(mtd);
1582 if (ctx == NULL) {
1583 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1584 return false;
1585 }
1586 if (mtd_erase_blocks(ctx, -1) == -1) {
1587 mtd_write_close(ctx);
1588 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1589 return false;
1590 }
1591 if (mtd_write_close(ctx) != 0) {
1592 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1593 return false;
1594 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001595 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001596 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001597 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001598 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001599}
1600
1601bool TWPartition::Wipe_RMRF() {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001602 if (Is_Storage)
1603 TWFunc::Toggle_MTP(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001604 if (!Mount(true))
1605 return false;
1606
Dees_Troy2673cec2013-04-02 20:22:16 +00001607 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001608 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001609 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001610 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001611}
1612
Dees_Troye5017042013-08-29 16:38:55 +00001613bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001614 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001615
1616 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1617 if (!UnMount(true))
1618 return false;
1619
1620 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1621 Find_Actual_Block_Device();
1622 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001623 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001624 Recreate_AndSec_Folder();
1625 gui_print("Done.\n");
1626 return true;
1627 } else {
1628 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1629 return false;
1630 }
1631 return true;
1632 } else {
1633 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1634 return Wipe_RMRF();
1635 }
1636 return false;
1637}
1638
Dees_Troy51a0e822012-09-05 15:24:24 -04001639bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001640#ifdef TW_OEM_BUILD
1641 // In an OEM Build we want to do a full format
1642 return Wipe_Encryption();
1643#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001644 string dir;
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001645 #ifdef HAVE_SELINUX
1646 fixPermissions perms;
1647 #endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001648
1649 // This handles wiping data on devices with "sdcard" in /data/media
1650 if (!Mount(true))
1651 return false;
1652
Dees_Troy2673cec2013-04-02 20:22:16 +00001653 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001654
1655 DIR* d;
1656 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001657 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001658 struct dirent* de;
1659 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001660 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001661 // The media folder is the "internal sdcard"
1662 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001663 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001664 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001665 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001666
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001667 dir = "/data/";
1668 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001669 if (de->d_type == DT_DIR) {
1670 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001671 } else if (de->d_type == DT_REG || de->d_type == DT_LNK || de->d_type == DT_FIFO || de->d_type == DT_SOCK) {
Dees_Troyce675462013-01-09 19:48:21 +00001672 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001673 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001674 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001675 }
1676 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001677
Dees_Troy2673cec2013-04-02 20:22:16 +00001678 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001679 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001680 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001681 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001682 return false;
Ethan Yonker83e82572014-04-04 10:59:28 -05001683#endif // ifdef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001684}
1685
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001686bool TWPartition::Backup_Tar(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001687 char back_name[255], split_index[5];
1688 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001689 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001690 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001691 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001692 twrpTar tar;
1693 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001694
Dees_Troy43d8b002012-09-17 16:00:01 -04001695 if (!Mount(true))
1696 return false;
1697
Dees_Troya13d74f2013-03-24 08:54:55 -05001698 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001699 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001700
1701 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001702 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001703
Dees_Troy83bd4832013-05-04 12:39:56 +00001704#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1705 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1706 if (use_encryption && Can_Encrypt_Backup) {
1707 tar.use_encryption = use_encryption;
1708 if (Use_Userdata_Encryption)
1709 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001710 string Password;
1711 DataManager::GetValue("tw_backup_password", Password);
1712 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001713 } else {
1714 use_encryption = false;
1715 }
1716#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001717
1718 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1719 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001720 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001721 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001722 Full_FileName = backup_folder + "/" + Backup_FileName;
1723 tar.setdir(Backup_Path);
1724 tar.setfn(Full_FileName);
1725 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001726 tar.partition_name = Backup_Name;
1727 tar.backup_folder = backup_folder;
1728 if (tar.createTarFork(overall_size, other_backups_size) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00001729 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001730 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001731}
1732
1733bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001734 char back_name[255], backup_size[32];
Vojtech Bocek05534202013-09-11 08:11:56 +02001735 string Full_FileName, Command, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001736 int use_compression;
1737
igoriok87e3d932013-01-31 21:03:53 +02001738 sprintf(backup_size, "%llu", Backup_Size);
1739 DD_BS = backup_size;
1740
Dees_Troyb46a6842012-09-25 11:06:46 -04001741 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001742 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001743
1744 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1745 Backup_FileName = back_name;
1746
1747 Full_FileName = backup_folder + "/" + Backup_FileName;
1748
Ethan Yonker448c8dc2014-12-08 15:34:34 -06001749 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + " count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001750 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001751 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001752 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04001753 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001754 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001755 return false;
1756 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001757 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001758}
1759
1760bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001761 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001762 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001763 int use_compression;
1764
Dees_Troyb46a6842012-09-25 11:06:46 -04001765 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001766 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001767
1768 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1769 Backup_FileName = back_name;
1770
1771 Full_FileName = backup_folder + "/" + Backup_FileName;
1772
1773 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001774 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001775 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001776 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001777 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1778 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
Dees_Troy2673cec2013-04-02 20:22:16 +00001779 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001780 return false;
1781 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001782 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001783}
1784
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001785unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
1786 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
1787 if (restore_info.LoadValues() == 0) {
1788 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
1789 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
1790 return Restore_Size;
1791 }
1792 }
1793 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
1794
1795 Full_FileName = restore_folder + "/" + Backup_FileName;
1796
1797 if (Is_Image(Restore_File_System)) {
1798 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
1799 return Restore_Size;
1800 }
1801
1802 twrpTar tar;
1803 tar.setdir(Backup_Path);
1804 tar.setfn(Full_FileName);
1805 tar.backup_name = Backup_Name;
1806#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1807 string Password;
1808 DataManager::GetValue("tw_restore_password", Password);
1809 if (!Password.empty())
1810 tar.setpassword(Password);
1811#endif
1812 tar.partition_name = Backup_Name;
1813 tar.backup_folder = restore_folder;
1814 Restore_Size = tar.get_size();
1815 return Restore_Size;
1816}
1817
1818bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Gary Peck43acadf2012-11-21 21:19:01 -08001819 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001820 int index = 0;
1821 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00001822 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001823
Dees_Troye58d5262012-09-21 12:27:57 -04001824 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001825 if (!Wipe_AndSec())
1826 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001827 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001828 gui_print("Wiping %s...\n", Display_Name.c_str());
Ethan Yonker5eac2222014-06-11 12:22:55 -05001829 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
1830 gui_print("WARNING: This /data backup was made with %s file system!\n", Restore_File_System.c_str());
1831 gui_print("The backup may not boot unless you change back to %s.\n", Restore_File_System.c_str());
1832 if (!Wipe_Data_Without_Wiping_Media())
1833 return false;
1834 } else {
1835 if (!Wipe(Restore_File_System))
1836 return false;
1837 }
Dees_Troye58d5262012-09-21 12:27:57 -04001838 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001839 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001840 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001841
1842 if (!Mount(true))
1843 return false;
1844
Dees_Troy4a2a1262012-09-18 09:33:47 -04001845 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06001846 twrpTar tar;
1847 tar.setdir(Backup_Path);
1848 tar.setfn(Full_FileName);
1849 tar.backup_name = Backup_Name;
1850#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1851 string Password;
1852 DataManager::GetValue("tw_restore_password", Password);
1853 if (!Password.empty())
1854 tar.setpassword(Password);
1855#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001856 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00001857 ret = false;
1858 else
1859 ret = true;
1860#ifdef HAVE_CAPABILITIES
1861 // Restore capabilities to the run-as binary
1862 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
1863 struct vfs_cap_data cap_data;
1864 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
1865
1866 memset(&cap_data, 0, sizeof(cap_data));
1867 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
1868 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
1869 cap_data.data[0].inheritable = 0;
1870 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
1871 cap_data.data[1].inheritable = 0;
1872 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
1873 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
1874 } else {
1875 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
1876 }
1877 }
1878#endif
1879 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001880}
1881
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001882bool TWPartition::Restore_DD(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001883 string Full_FileName, Command;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001884 double display_percent, progress_percent;
1885 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04001886
Dees_Troyda8b55a2012-12-12 19:18:30 +00001887 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001888 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001889
1890 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001891 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001892 return false;
1893 }
1894 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1895 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001896 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001897 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1898 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1899 return false;
1900 }
1901
Dees_Troy2673cec2013-04-02 20:22:16 +00001902 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001903 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001904 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001905 TWFunc::Exec_Cmd(Command);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001906 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
1907 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
1908 DataManager::SetValue("tw_size_progress", size_progress);
1909 progress_percent = (display_percent / 100);
1910 DataManager::SetProgress((float)(progress_percent));
1911 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001912 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001913}
1914
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001915bool TWPartition::Restore_Flash_Image(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001916 string Full_FileName, Command;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001917 double display_percent, progress_percent;
1918 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04001919
Dees_Troy2673cec2013-04-02 20:22:16 +00001920 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001921 Full_FileName = restore_folder + "/" + Backup_FileName;
1922 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1923 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001924 LOGINFO("Erase command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001925 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001926 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001927 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001928 TWFunc::Exec_Cmd(Command);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001929 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
1930 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
1931 DataManager::SetValue("tw_size_progress", size_progress);
1932 progress_percent = (display_percent / 100);
1933 DataManager::SetProgress((float)(progress_percent));
1934 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001935 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001936}
Dees_Troy5bf43922012-09-07 16:07:55 -04001937
1938bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001939 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001940
Dees_Troyab10ee22012-09-21 14:27:30 -04001941 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001942 return false;
1943
Dees_Troy0550cfb2012-10-13 11:56:13 -04001944 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001945 if (Removable || Is_Encrypted) {
1946 if (!Mount(false))
1947 return true;
1948 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001949 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001950
1951 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001952 if (!ret || Size == 0) {
1953 if (!Get_Size_Via_df(Display_Error)) {
1954 if (!Was_Already_Mounted)
1955 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001956 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001957 }
1958 }
Dees_Troy51127312012-09-08 13:08:49 -04001959
Dees_Troy5bf43922012-09-07 16:07:55 -04001960 if (Has_Data_Media) {
1961 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001962 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001963 Used = du.Get_Folder_Size("/data");
1964 Backup_Size = Used;
1965 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04001966 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001967 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001968 } else {
1969 if (!Was_Already_Mounted)
1970 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001971 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001972 }
Dees_Troye58d5262012-09-21 12:27:57 -04001973 } else if (Has_Android_Secure) {
1974 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001975 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001976 else {
1977 if (!Was_Already_Mounted)
1978 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001979 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001980 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001981 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001982 if (!Was_Already_Mounted)
1983 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001984 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001985}
Dees_Troy38bd7602012-09-14 13:33:53 -04001986
1987void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001988 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001989 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001990 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001991 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001992 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001993 Is_Present = true;
1994 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001995 return;
1996 }
1997 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001998 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001999 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002000 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002001 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002002 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002003 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002004}
2005
2006void TWPartition::Recreate_Media_Folder(void) {
2007 string Command;
2008
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05002009 #ifdef HAVE_SELINUX
2010 fixPermissions perms;
2011 #endif
2012
Dees_Troy38bd7602012-09-14 13:33:53 -04002013 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002014 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04002015 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002016 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00002017 LOGINFO("Recreating /data/media folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002018 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002019#ifdef HAVE_SELINUX
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05002020 perms.fixDataInternalContexts();
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002021#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002022 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002023 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002024 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002025 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002026}
Dees_Troye58d5262012-09-21 12:27:57 -04002027
2028void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002029 if (!Has_Android_Secure)
2030 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002031 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002032 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002033 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002034 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002035 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002036 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002037 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002038 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002039 }
2040}
bigbiff7cb4c332014-11-26 20:36:07 -05002041
2042uint64_t TWPartition::Get_Max_FileSize() {
2043 uint64_t maxFileSize = 0;
2044 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2045 const uint64_t constTB = (uint64_t) constGB * 1024;
2046 const uint64_t constPB = (uint64_t) constTB * 1024;
2047 const uint64_t constEB = (uint64_t) constPB * 1024;
2048
2049 if (Current_File_System == "ext4")
2050 maxFileSize = 16 * constTB; //16 TB
2051 else if (Current_File_System == "vfat")
2052 maxFileSize = 4 * constGB; //4 GB
2053 else if (Current_File_System == "ntfs")
2054 maxFileSize = 256 * constTB; //256 TB
2055 if (Current_File_System == "exfat")
2056 maxFileSize = 16 * constPB; //16 PB
2057 else if (Current_File_System == "ext3")
2058 maxFileSize = 2 * constTB; //2 TB
2059 else if (Current_File_System == "f2fs")
2060 maxFileSize = 3.94 * constTB; //3.94 TB
2061 else
2062 maxFileSize = 100000000L;
2063 return maxFileSize - 1;
2064}
2065