blob: b20367f77f0d551b50833d9d8f4e31e889f374ae [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";
Ethan Yonker726a0202014-12-16 20:01:38 -0600156 MTP_Storage_ID = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400157}
158
159TWPartition::~TWPartition(void) {
160 // Do nothing
161}
162
Dees_Troy5bf43922012-09-07 16:07:55 -0400163bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
164 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
165 int line_len = Line.size(), index = 0, item_index = 0;
166 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400167 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400168 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500169 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400170
Dees_Troy51127312012-09-08 13:08:49 -0400171 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500172 if (full_line[index] == 34)
173 skip = !skip;
174 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400175 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400176 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400177 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000178 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400179 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500180 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000181 Display_Name = full_line + 1;
182 Backup_Display_Name = Display_Name;
183 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400184 index = Mount_Point.size();
185 while (index < line_len) {
186 while (index < line_len && full_line[index] == '\0')
187 index++;
188 if (index >= line_len)
189 continue;
190 ptr = full_line + index;
191 if (item_index == 0) {
192 // File System
193 Fstab_File_System = ptr;
194 Current_File_System = ptr;
195 item_index++;
196 } else if (item_index == 1) {
197 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400198 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400199 MTD_Name = ptr;
200 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400201 } else if (Fstab_File_System == "bml") {
202 if (Mount_Point == "/boot")
203 MTD_Name = "boot";
204 else if (Mount_Point == "/recovery")
205 MTD_Name = "recovery";
206 Primary_Block_Device = ptr;
207 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000208 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 -0400209 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400210 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000211 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400212 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000213 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400214 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400215 } else {
216 Primary_Block_Device = ptr;
217 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400218 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400219 item_index++;
220 } else if (item_index > 1) {
221 if (*ptr == '/') {
222 // Alternate Block Device
223 Alternate_Block_Device = ptr;
224 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
225 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
226 // Partition length
227 ptr += 7;
228 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400229 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
230 // Custom flags, save for later so that new values aren't overwritten by defaults
231 ptr += 6;
232 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000233 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400234 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
235 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400236 } else {
237 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000238 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400239 }
240 }
241 while (index < line_len && full_line[index] != '\0')
242 index++;
243 }
244
245 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
246 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000247 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400248 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000249 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400250 return 0;
251 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400252 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400253 Setup_File_System(Display_Error);
254 if (Mount_Point == "/system") {
255 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500256 Backup_Display_Name = Display_Name;
257 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400258 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500259 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400260 } else if (Mount_Point == "/data") {
261 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500262 Backup_Display_Name = Display_Name;
263 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400264 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400265 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500266 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000267 Can_Encrypt_Backup = true;
268 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500269 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200270 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400271#ifdef TW_INCLUDE_CRYPTO
272 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400273 char crypto_blkdev[255];
274 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
275 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400276 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400277 DataManager::SetValue(TW_IS_DECRYPTED, 1);
278 Is_Encrypted = true;
279 Is_Decrypted = true;
280 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000281 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400282 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600283 if (Is_Present) {
Ethan Yonker253368a2014-11-25 15:00:52 -0600284 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 -0600285 if (cryptfs_check_footer() == 0) {
286 Is_Encrypted = true;
287 Is_Decrypted = false;
288 Can_Be_Mounted = false;
289 Current_File_System = "emmc";
290 Setup_Image(Display_Error);
291 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
292 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
293 DataManager::SetValue("tw_crypto_display", "");
294 } else {
295 LOGERR("Could not mount /data and unable to find crypto footer.\n");
296 }
297 } else {
298 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
299 }
Gary Peck82599a82012-11-21 16:23:12 -0800300 } else {
301 // Filesystem is not encrypted and the mount
302 // succeeded, so get it back to the original
303 // unmounted state
304 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400305 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500306 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400307 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400308#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500309 if (datamedia)
310 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400311#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400312 } else if (Mount_Point == "/cache") {
313 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500314 Backup_Display_Name = Display_Name;
315 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400316 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400317 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500318 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400319 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000320 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500321 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500322 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400323 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400324 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400325 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400326 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500327 Backup_Display_Name = Display_Name;
328 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400329 Is_SubPartition = true;
330 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400331 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500332 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000333 Can_Encrypt_Backup = true;
334 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400335 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400336 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400337 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500338 Backup_Display_Name = Display_Name;
339 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400340 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400341 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500342 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000343 Can_Encrypt_Backup = true;
344 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400345 } else if (Mount_Point == "/boot") {
346 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500347 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400348 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500349 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400350 }
351#ifdef TW_EXTERNAL_STORAGE_PATH
352 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
353 Is_Storage = true;
354 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400355 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500356 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400357#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000358 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400359 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400360 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500361 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400362#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000363 }
Dees_Troy8170a922012-09-18 15:40:25 -0400364#ifdef TW_INTERNAL_STORAGE_PATH
365 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
366 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500367 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400368 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500369 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400370 }
371#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000372 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400373 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500374 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500375 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400376 }
377#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400378 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400379 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400380 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500381 if (Mount_Point == "/boot") {
382 Display_Name = "Boot";
383 Backup_Display_Name = Display_Name;
384 Can_Be_Backed_Up = true;
385 } else if (Mount_Point == "/recovery") {
386 Display_Name = "Recovery";
387 Backup_Display_Name = Display_Name;
Dees_Troya13d74f2013-03-24 08:54:55 -0500388 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400389 }
390
Dees_Troy51127312012-09-08 13:08:49 -0400391 // Process any custom flags
392 if (Flags.size() > 0)
393 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400394 return true;
395}
396
Hashcode62bd9e02013-11-19 21:59:42 -0800397bool TWPartition::Process_FS_Flags(string& Options, int Flags) {
398 int i;
399 char *p;
400 char *savep;
401 char fs_options[250];
402
403 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
404 Options = "";
405
406 p = strtok_r(fs_options, ",", &savep);
407 while (p) {
408 /* Look for the flag "p" in the flag list "fl"
409 * If not found, the loop exits with fl[i].name being null.
410 */
411 for (i = 0; mount_flags[i].name; i++) {
412 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
413 Flags |= mount_flags[i].flag;
414 break;
415 }
416 }
417
418 if (!mount_flags[i].name) {
419 if (Options.size() > 0)
420 Options += ",";
421 Options += p;
422 }
423 p = strtok_r(NULL, ",", &savep);
424 }
425
426 return true;
427}
428
Dees_Troy51127312012-09-08 13:08:49 -0400429bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
430 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500431 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400432 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500433 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400434
435 strcpy(flags, Flags.c_str());
436 flags_len = Flags.size();
437 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500438 if (flags[index] == 34)
439 skip = !skip;
440 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400441 flags[index] = '\0';
442 }
443
444 index = 0;
445 while (index < flags_len) {
446 while (index < flags_len && flags[index] == '\0')
447 index++;
448 if (index >= flags_len)
449 continue;
450 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500451 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400452 if (strcmp(ptr, "removable") == 0) {
453 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600454 } else if (strncmp(ptr, "storage", 7) == 0) {
455 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600456 Is_Storage = true;
457 } else if (ptr_len == 9) {
458 ptr += 9;
459 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
460 LOGINFO("storage set to true\n");
461 Is_Storage = true;
462 } else {
463 LOGINFO("storage set to false\n");
464 Is_Storage = false;
465 }
466 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500467 } else if (strcmp(ptr, "settingsstorage") == 0) {
468 Is_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500469 } else if (strcmp(ptr, "andsec") == 0) {
470 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400471 } else if (strcmp(ptr, "canbewiped") == 0) {
472 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700473 } else if (strcmp(ptr, "usermrf") == 0) {
474 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500475 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
476 ptr += 7;
477 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
478 Can_Be_Backed_Up = true;
479 else
480 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400481 } else if (strcmp(ptr, "wipeingui") == 0) {
482 Can_Be_Wiped = true;
483 Wipe_Available_in_GUI = true;
484 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
485 Can_Be_Wiped = true;
486 Wipe_Available_in_GUI = true;
487 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500488 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000489 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400490 Is_SubPartition = true;
491 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000492 } else if (strcmp(ptr, "ignoreblkid") == 0) {
493 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000494 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
495 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500496 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400497 ptr += 8;
498 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500499 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
500 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400501 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500502 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400503 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500504 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
505 Display_Name.resize(Display_Name.size() - 1);
506 }
507 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
508 has_storage_name = true;
509 ptr += 11;
510 if (*ptr == '\"') ptr++;
511 Storage_Name = ptr;
512 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
513 Storage_Name.resize(Storage_Name.size() - 1);
514 }
515 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
516 has_backup_name = true;
517 ptr += 10;
518 if (*ptr == '\"') ptr++;
519 Backup_Display_Name = ptr;
520 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
521 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
522 }
523 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400524 ptr += 10;
525 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500526 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400527 ptr += 7;
528 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000529 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
530 ptr += 17;
531 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
532 Can_Encrypt_Backup = true;
533 else
534 Can_Encrypt_Backup = false;
535 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
536 ptr += 21;
537 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
538 Can_Encrypt_Backup = true;
539 Use_Userdata_Encryption = true;
540 } else {
541 Use_Userdata_Encryption = false;
542 }
Hashcode62bd9e02013-11-19 21:59:42 -0800543 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
544 ptr += 8;
545 if (*ptr == '\"') ptr++;
546
547 Mount_Options = ptr;
548 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
549 Mount_Options.resize(Mount_Options.size() - 1);
550 }
551 Process_FS_Flags(Mount_Options, Mount_Flags);
Ethan Yonker253368a2014-11-25 15:00:52 -0600552 } else if ((ptr_len > 12 && strncmp(ptr, "encryptable=", 12) == 0) || (ptr_len > 13 && strncmp(ptr, "forceencrypt=", 13) == 0)) {
553 ptr += 12;
554 if (*ptr == '=') ptr++;
555 if (*ptr == '\"') ptr++;
556
557 Crypto_Key_Location = ptr;
558 if (Crypto_Key_Location.substr(Crypto_Key_Location.size() - 1, 1) == "\"") {
559 Crypto_Key_Location.resize(Crypto_Key_Location.size() - 1);
560 }
561 } else if (ptr_len > 8 && strncmp(ptr, "mounttodecrypt", 14) == 0) {
562 Mount_To_Decrypt = true;
Dees_Troy51127312012-09-08 13:08:49 -0400563 } else {
564 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000565 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400566 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000567 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400568 }
569 while (index < flags_len && flags[index] != '\0')
570 index++;
571 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500572 if (has_display_name && !has_storage_name)
573 Storage_Name = Display_Name;
574 if (!has_display_name && has_storage_name)
575 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000576 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500577 Backup_Display_Name = Display_Name;
578 if (!has_display_name && has_backup_name)
579 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400580 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400581}
582
Dees_Troy5bf43922012-09-07 16:07:55 -0400583bool TWPartition::Is_File_System(string File_System) {
584 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400585 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400586 File_System == "ext4" ||
587 File_System == "vfat" ||
588 File_System == "ntfs" ||
589 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500590 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000591 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400592 File_System == "auto")
593 return true;
594 else
595 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400596}
597
Dees_Troy5bf43922012-09-07 16:07:55 -0400598bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400599 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400600 return true;
601 else
602 return false;
603}
604
Dees_Troy51127312012-09-08 13:08:49 -0400605bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400606 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400607 if (mkdir(Path.c_str(), 0777) == -1) {
608 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000609 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400610 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000611 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400612 return false;
613 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000614 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400615 return true;
616 }
617 }
618 return true;
619}
620
Dees_Troy5bf43922012-09-07 16:07:55 -0400621void TWPartition::Setup_File_System(bool Display_Error) {
622 struct statfs st;
623
624 Can_Be_Mounted = true;
625 Can_Be_Wiped = true;
626
Dees_Troy5bf43922012-09-07 16:07:55 -0400627 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400628 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400629 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
630 Backup_Name = Display_Name;
631 Backup_Method = FILES;
632}
633
634void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400635 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
636 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800637 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400638 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800639 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400640 Backup_Method = FLASH_UTILS;
641 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000642 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 -0400643 if (Find_Partition_Size()) {
644 Used = Size;
645 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400646 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400647 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000648 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400649 else
Dees Troyd932ce12013-10-18 17:12:59 +0000650 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400651 }
652}
653
Dees_Troye58d5262012-09-21 12:27:57 -0400654void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500655 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400656 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500657 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400658 Has_Android_Secure = true;
659 Symlink_Path = Mount_Point + "/.android_secure";
660 Symlink_Mount_Point = "/and-sec";
661 Backup_Path = Symlink_Mount_Point;
662 Make_Dir("/and-sec", true);
663 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600664 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400665}
666
that9e0593e2014-10-08 00:01:24 +0200667void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500668 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
669 Storage_Name = "Internal Storage";
670 Has_Data_Media = true;
671 Is_Storage = true;
672 Is_Settings_Storage = true;
673 Storage_Path = "/data/media";
674 Symlink_Path = Storage_Path;
675 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
676 Make_Dir("/emmc", false);
677 Symlink_Mount_Point = "/emmc";
678 } else {
679 Make_Dir("/sdcard", false);
680 Symlink_Mount_Point = "/sdcard";
681 }
682 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
683 Storage_Path = "/data/media/0";
684 Symlink_Path = Storage_Path;
685 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
686 UnMount(true);
687 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500688 DataManager::SetValue("tw_has_internal", 1);
689 DataManager::SetValue("tw_has_data_media", 1);
690 du.add_absolute_dir("/data/media");
691}
692
Dees_Troy5bf43922012-09-07 16:07:55 -0400693void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
694 char device[512], realDevice[512];
695
696 strcpy(device, Block.c_str());
697 memset(realDevice, 0, sizeof(realDevice));
698 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
699 {
700 strcpy(device, realDevice);
701 memset(realDevice, 0, sizeof(realDevice));
702 }
703
704 if (device[0] != '/') {
705 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000706 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400707 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000708 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400709 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400710 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400711 Block = device;
712 return;
713 }
714}
715
Dees_Troy8e337f32012-10-13 22:07:49 -0400716void TWPartition::Mount_Storage_Retry(void) {
717 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500718 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400719 int retry_count = 5;
720 while (retry_count > 0 && !Mount(false)) {
721 usleep(500000);
722 retry_count--;
723 }
724 Mount(true);
725 }
726}
727
Dees_Troy38bd7602012-09-14 13:33:53 -0400728bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
729 FILE *fp = NULL;
730 char line[255];
731
732 fp = fopen("/proc/mtd", "rt");
733 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000734 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400735 return false;
736 }
737
738 while (fgets(line, sizeof(line), fp) != NULL)
739 {
740 char device[32], label[32];
741 unsigned long size = 0;
742 char* fstype = NULL;
743 int deviceId;
744
745 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
746
747 // Skip header and blank lines
748 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
749 continue;
750
751 // Strip off the trailing " from the label
752 label[strlen(label)-1] = '\0';
753
754 if (strcmp(label, MTD_Name.c_str()) == 0) {
755 // We found our device
756 // Strip off the trailing : from the device
757 device[strlen(device)-1] = '\0';
758 if (sscanf(device,"mtd%d", &deviceId) == 1) {
759 sprintf(device, "/dev/block/mtdblock%d", deviceId);
760 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000761 fclose(fp);
762 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400763 }
764 }
765 }
766 fclose(fp);
767
768 return false;
769}
770
Dees_Troy51127312012-09-08 13:08:49 -0400771bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
772 struct statfs st;
773 string Local_Path = Mount_Point + "/.";
774
775 if (!Mount(Display_Error))
776 return false;
777
778 if (statfs(Local_Path.c_str(), &st) != 0) {
779 if (!Removable) {
780 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000781 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400782 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000783 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400784 }
785 return false;
786 }
787 Size = (st.f_blocks * st.f_bsize);
788 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
789 Free = (st.f_bfree * st.f_bsize);
790 Backup_Size = Used;
791 return true;
792}
793
794bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400795 FILE* fp;
796 char command[255], line[512];
797 int include_block = 1;
798 unsigned int min_len;
799
800 if (!Mount(Display_Error))
801 return false;
802
Dees_Troy38bd7602012-09-14 13:33:53 -0400803 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400804 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200805 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400806 fp = fopen("/tmp/dfoutput.txt", "rt");
807 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000808 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400809 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400810 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400811
812 while (fgets(line, sizeof(line), fp) != NULL)
813 {
814 unsigned long blocks, used, available;
815 char device[64];
816 char tmpString[64];
817
818 if (strncmp(line, "Filesystem", 10) == 0)
819 continue;
820 if (strlen(line) < min_len) {
821 include_block = 0;
822 continue;
823 }
824 if (include_block) {
825 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
826 } else {
827 // The device block string is so long that the df information is on the next line
828 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400829 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400830 while (tmpString[space_count] == 32)
831 space_count++;
832 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
833 }
834
835 // Adjust block size to byte size
836 Size = blocks * 1024ULL;
837 Used = used * 1024ULL;
838 Free = available * 1024ULL;
839 Backup_Size = Used;
840 }
841 fclose(fp);
842 return true;
843}
844
Dees_Troy5bf43922012-09-07 16:07:55 -0400845bool TWPartition::Find_Partition_Size(void) {
846 FILE* fp;
847 char line[512];
848 string tmpdevice;
849
igoriok87e3d932013-01-31 21:03:53 +0200850 fp = fopen("/proc/dumchar_info", "rt");
851 if (fp != NULL) {
852 while (fgets(line, sizeof(line), fp) != NULL)
853 {
854 char label[32], device[32];
855 unsigned long size = 0;
856
thatd43bf2d2014-09-21 23:13:02 +0200857 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200858
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500859 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200860 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
861 continue;
862
863 tmpdevice = "/dev/";
864 tmpdevice += label;
865 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
866 Size = size;
867 fclose(fp);
868 return true;
869 }
870 }
871 }
872
Dees_Troy5bf43922012-09-07 16:07:55 -0400873 // In this case, we'll first get the partitions we care about (with labels)
874 fp = fopen("/proc/partitions", "rt");
875 if (fp == NULL)
876 return false;
877
878 while (fgets(line, sizeof(line), fp) != NULL)
879 {
880 unsigned long major, minor, blocks;
881 char device[512];
882 char tmpString[64];
883
Dees_Troy63c8df72012-09-10 14:02:05 -0400884 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400885 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
886
887 tmpdevice = "/dev/block/";
888 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400889 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400890 // Adjust block size to byte size
891 Size = blocks * 1024ULL;
892 fclose(fp);
893 return true;
894 }
895 }
896 fclose(fp);
897 return false;
898}
899
Dees_Troy5bf43922012-09-07 16:07:55 -0400900bool TWPartition::Is_Mounted(void) {
901 if (!Can_Be_Mounted)
902 return false;
903
904 struct stat st1, st2;
905 string test_path;
906
907 // Check to see if the mount point directory exists
908 test_path = Mount_Point + "/.";
909 if (stat(test_path.c_str(), &st1) != 0) return false;
910
911 // Check to see if the directory above the mount point exists
912 test_path = Mount_Point + "/../.";
913 if (stat(test_path.c_str(), &st2) != 0) return false;
914
915 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
916 int ret = (st1.st_dev != st2.st_dev) ? true : false;
917
918 return ret;
919}
920
921bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000922 int exfat_mounted = 0;
923
Dees_Troy5bf43922012-09-07 16:07:55 -0400924 if (Is_Mounted()) {
925 return true;
926 } else if (!Can_Be_Mounted) {
927 return false;
928 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400929
930 Find_Actual_Block_Device();
931
932 // Check the current file system before mounting
933 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000934 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000935 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 +0000936 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000937 string result;
938 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000939 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000940 Current_File_System = "vfat";
941 } else {
942#ifdef TW_NO_EXFAT_FUSE
943 UnMount(false);
944 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
945 // Some kernels let us mount vfat as exfat which doesn't work out too well
946#else
947 exfat_mounted = 1;
948#endif
949 }
950 }
Dees_Troy22042032012-12-18 21:23:08 +0000951 if (Fstab_File_System == "yaffs2") {
952 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000953 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
954 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
955 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
956 if (Display_Error)
957 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
958 else
959 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
960 return false;
961 } else {
962 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
963 return true;
964 }
965 } else {
966 struct stat st;
967 string test_path = Mount_Point;
968 if (stat(test_path.c_str(), &st) < 0) {
969 if (Display_Error)
970 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
971 else
972 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
973 return false;
974 }
975 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
976 if (new_mode != st.st_mode) {
977 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
978 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
979 if (Display_Error)
980 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
981 else
982 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
983 return false;
984 }
985 }
Dees_Troy22042032012-12-18 21:23:08 +0000986 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000987 }
Dees Troy216e0422014-02-07 03:46:42 +0000988 } 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 +0000989#ifdef TW_NO_EXFAT_FUSE
990 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000991 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000992 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000993 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000994 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000995 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000996 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800997 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 +0000998 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000999 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001000 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001001#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001002 if (!Removable && Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001003 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001004 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001005 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1006 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 +00001007 return false;
1008#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001009 }
1010#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001011 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001012
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001013 if (Removable)
1014 Update_Size(Display_Error);
1015
1016 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001017 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
1018 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001019 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001020 return true;
1021}
1022
1023bool TWPartition::UnMount(bool Display_Error) {
1024 if (Is_Mounted()) {
1025 int never_unmount_system;
1026
1027 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1028 if (never_unmount_system == 1 && Mount_Point == "/system")
1029 return true; // Never unmount system if you're not supposed to unmount it
1030
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001031 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001032 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001033
Dees_Troy38bd7602012-09-14 13:33:53 -04001034 if (!Symlink_Mount_Point.empty())
1035 umount(Symlink_Mount_Point.c_str());
1036
Dees_Troyb05ddee2013-01-28 20:24:50 +00001037 umount(Mount_Point.c_str());
1038 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001039 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001040 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001041 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001042 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001043 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001044 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001045 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001046 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001047 } else {
1048 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001049 }
1050}
1051
Gary Peck43acadf2012-11-21 21:19:01 -08001052bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001053 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001054 int check;
1055 string Layout_Filename = Mount_Point + "/.layout_version";
1056
Dees_Troy38bd7602012-09-14 13:33:53 -04001057 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001058 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001059 return false;
1060 }
1061
Dees_Troyc51f1f92012-09-20 15:32:13 -04001062 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001063 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001064
Dees_Troy16c2b312013-01-15 16:51:18 +00001065 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1066 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1067 else
1068 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001069
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001070 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001071 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001072 recreate_media = 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 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001091 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 +00001092 unlink("/.layout_version");
1093 return false;
1094 }
1095 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001096 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001097
Gary Pecke8bc5d72012-12-21 06:45:25 -08001098 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001099 if (Mount_Point == "/cache")
1100 DataManager::Output_Version();
1101
Dees_Troy16c2b312013-01-15 16:51:18 +00001102 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1103 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1104
1105 if (update_crypt) {
1106 Setup_File_System(false);
1107 if (Is_Encrypted && !Is_Decrypted) {
1108 // just wiped an encrypted partition back to its unencrypted state
1109 Is_Encrypted = false;
1110 Is_Decrypted = false;
1111 Decrypted_Block_Device = "";
1112 if (Mount_Point == "/data") {
1113 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1114 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1115 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001116 }
1117 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001118
1119 if (Mount_Point == "/data" && Has_Data_Media && recreate_media) {
1120 Recreate_Media_Folder();
1121 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001122 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001123 if (Is_Storage) {
1124 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001125 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001126 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001127}
1128
Gary Peck43acadf2012-11-21 21:19:01 -08001129bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001130 if (Is_File_System(Current_File_System))
1131 return Wipe(Current_File_System);
1132 else
1133 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001134}
1135
Dees_Troye58d5262012-09-21 12:27:57 -04001136bool TWPartition::Wipe_AndSec(void) {
1137 if (!Has_Android_Secure)
1138 return false;
1139
Dees_Troye58d5262012-09-21 12:27:57 -04001140 if (!Mount(true))
1141 return false;
1142
Dees_Troy2673cec2013-04-02 20:22:16 +00001143 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001144 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001145 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001146}
1147
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001148bool TWPartition::Can_Repair() {
1149 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/dosfsck"))
1150 return true;
1151 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1152 return true;
1153 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1154 return true;
1155 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1156 return true;
1157 return false;
1158}
1159
1160bool TWPartition::Repair() {
1161 string command;
1162
1163 if (Current_File_System == "vfat") {
1164 if (!TWFunc::Path_Exists("/sbin/dosfsck")) {
1165 gui_print("dosfsck does not exist! Cannot repair!\n");
1166 return false;
1167 }
1168 if (!UnMount(true))
1169 return false;
1170 gui_print("Repairing %s using dosfsck...\n", Display_Name.c_str());
1171 Find_Actual_Block_Device();
1172 command = "/sbin/dosfsck -y " + Actual_Block_Device;
1173 LOGINFO("Repair command: %s\n", command.c_str());
1174 if (TWFunc::Exec_Cmd(command) == 0) {
1175 gui_print("Done.\n");
1176 return true;
1177 } else {
1178 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1179 return false;
1180 }
1181 }
1182 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1183 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
1184 gui_print("e2fsck does not exist! Cannot repair!\n");
1185 return false;
1186 }
1187 if (!UnMount(true))
1188 return false;
1189 gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str());
1190 Find_Actual_Block_Device();
1191 command = "/sbin/e2fsck -p " + Actual_Block_Device;
1192 LOGINFO("Repair command: %s\n", command.c_str());
1193 if (TWFunc::Exec_Cmd(command) == 0) {
1194 gui_print("Done.\n");
1195 return true;
1196 } else {
1197 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1198 return false;
1199 }
1200 }
1201 if (Current_File_System == "exfat") {
1202 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
1203 gui_print("fsck.exfat does not exist! Cannot repair!\n");
1204 return false;
1205 }
1206 if (!UnMount(true))
1207 return false;
1208 gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str());
1209 Find_Actual_Block_Device();
1210 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1211 LOGINFO("Repair command: %s\n", command.c_str());
1212 if (TWFunc::Exec_Cmd(command) == 0) {
1213 gui_print("Done.\n");
1214 return true;
1215 } else {
1216 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1217 return false;
1218 }
1219 }
1220 if (Current_File_System == "f2fs") {
1221 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
1222 gui_print("fsck.f2fs does not exist! Cannot repair!\n");
1223 return false;
1224 }
1225 if (!UnMount(true))
1226 return false;
1227 gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str());
1228 Find_Actual_Block_Device();
1229 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1230 LOGINFO("Repair command: %s\n", command.c_str());
1231 if (TWFunc::Exec_Cmd(command) == 0) {
1232 gui_print("Done.\n");
1233 return true;
1234 } else {
1235 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1236 return false;
1237 }
1238 }
1239 return false;
1240}
1241
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001242bool 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 -04001243 if (Backup_Method == FILES)
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001244 return Backup_Tar(backup_folder, overall_size, other_backups_size);
Dees_Troy38bd7602012-09-14 13:33:53 -04001245 else if (Backup_Method == DD)
1246 return Backup_DD(backup_folder);
1247 else if (Backup_Method == FLASH_UTILS)
1248 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001249 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001250 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001251}
1252
Dees_Troy43d8b002012-09-17 16:00:01 -04001253bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001254 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001255 char split_filename[512];
1256 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001257 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001258
Captain Throwbackff5935f2014-09-23 16:08:34 -04001259 sync();
1260
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001261 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001262 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001263 if (!TWFunc::Path_Exists(Full_Filename)) {
1264 // This is a split archive, we presume
1265 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001266 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001267 md5file = split_filename;
1268 md5file += ".md5";
1269 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001270 LOGERR("No md5 file found for '%s'.\n", split_filename);
1271 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001272 return false;
1273 }
1274 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001275 while (index < 1000) {
1276 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001277 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001278 return false;
1279 }
1280 index++;
1281 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001282 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001283 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001284 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001285 } else {
1286 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001287 md5file = Full_Filename + ".md5";
1288 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001289 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1290 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001291 return false;
1292 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001293 md5sum.setfn(Full_Filename);
1294 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001295 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001296 return false;
1297 } else
1298 return true;
1299 }
1300 return false;
1301}
1302
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001303bool 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 -08001304 string Restore_File_System;
1305
1306 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001307 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001308
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001309 Restore_File_System = Get_Restore_File_System(restore_folder);
1310
1311 if (Is_File_System(Restore_File_System))
1312 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1313 else if (Is_Image(Restore_File_System)) {
1314 *already_restored_size += TWFunc::Get_File_Size(Backup_Name);
1315 if (Restore_File_System == "emmc")
1316 return Restore_DD(restore_folder, total_restore_size, already_restored_size);
1317 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1318 return Restore_Flash_Image(restore_folder, total_restore_size, already_restored_size);
1319 }
1320
1321 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1322 return false;
1323}
1324
1325string TWPartition::Get_Restore_File_System(string restore_folder) {
1326 size_t first_period, second_period;
1327 string Restore_File_System;
1328
Gary Peck43acadf2012-11-21 21:19:01 -08001329 // Parse backup filename to extract the file system before wiping
1330 first_period = Backup_FileName.find(".");
1331 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001332 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001333 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001334 }
1335 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1336 second_period = Restore_File_System.find(".");
1337 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001338 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001339 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001340 }
1341 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001342 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001343 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001344}
1345
1346string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001347 if (Backup_Method == NONE)
1348 return "none";
1349 else if (Backup_Method == FILES)
1350 return "files";
1351 else if (Backup_Method == DD)
1352 return "dd";
1353 else if (Backup_Method == FLASH_UTILS)
1354 return "flash_utils";
1355 else
1356 return "undefined";
1357 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001358}
1359
1360bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001361 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001362 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001363 return 1;
1364}
1365
1366bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001367 bool Save_Data_Media = Has_Data_Media;
1368
1369 if (!UnMount(true))
1370 return false;
1371
Dees_Troy38bd7602012-09-14 13:33:53 -04001372 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001373 Decrypted_Block_Device = "";
1374 Is_Decrypted = false;
1375 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001376 Find_Actual_Block_Device();
Gary Pecke8bc5d72012-12-21 06:45:25 -08001377 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001378 Has_Data_Media = Save_Data_Media;
1379 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1380 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001381 if (Mount(false))
1382 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001383 }
Ethan Yonker83e82572014-04-04 10:59:28 -05001384#ifndef TW_OEM_BUILD
Dees_Troy2673cec2013-04-02 20:22:16 +00001385 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Ethan Yonker83e82572014-04-04 10:59:28 -05001386#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001387 return true;
1388 } else {
1389 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001390 LOGERR("Unable to format to remove encryption.\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06001391 if (Has_Data_Media && Mount(false))
1392 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001393 return false;
1394 }
1395 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001396}
1397
1398void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001399 const char* type;
1400 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001401
Dees_Troy68cab492012-12-12 19:29:35 +00001402 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1403 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001404
Dees_Troy38bd7602012-09-14 13:33:53 -04001405 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001406 if (!Is_Present)
1407 return;
Dees_Troy51127312012-09-08 13:08:49 -04001408
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001409 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1410 if (blkid_do_fullprobe(pr)) {
1411 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001412 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001413 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001414 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001415
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001416 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001417 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001418 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001419 return;
1420 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001421
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001422 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001423 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001424}
1425
Gary Peck43acadf2012-11-21 21:19:01 -08001426bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001427 if (!UnMount(true))
1428 return false;
1429
Dees_Troy43d8b002012-09-17 16:00:01 -04001430 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001431 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001432
Dees_Troy2673cec2013-04-02 20:22:16 +00001433 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001434 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001435 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001436 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001437 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001438 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001439 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001440 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001441 return true;
1442 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001443 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001444 return false;
1445 }
1446 } else
1447 return Wipe_RMRF();
1448
1449 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001450}
1451
1452bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001453 Find_Actual_Block_Device();
1454 if (!Is_Present) {
1455 LOGERR("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1456 return false;
1457 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001458 if (!UnMount(true))
1459 return false;
1460
Dees_Troyb3265ab2013-08-30 02:59:14 +00001461#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001462 int ret;
1463 char *secontext = NULL;
1464
Dees_Troya95f55c2013-08-17 13:14:43 +00001465 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001466
Dees Troy99c8dbf2014-03-10 16:53:58 +00001467 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001468 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1469 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1470 } else {
1471 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1472 }
1473 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001474 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1475 return false;
1476 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001477 string sedir = Mount_Point + "/lost+found";
1478 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1479 rmdir(sedir.c_str());
1480 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001481 return true;
1482 }
1483#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001484 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001485 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001486
Dees_Troy2673cec2013-04-02 20:22:16 +00001487 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001488 Find_Actual_Block_Device();
1489 Command = "make_ext4fs";
1490 if (!Is_Decrypted && Length != 0) {
1491 // Only use length if we're not decrypted
1492 char len[32];
1493 sprintf(len, "%i", Length);
1494 Command += " -l ";
1495 Command += len;
1496 }
Dees_Troy5295d582013-09-06 15:51:08 +00001497 if (TWFunc::Path_Exists("/file_contexts")) {
1498 Command += " -S /file_contexts";
1499 }
1500 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001501 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001502 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001503 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001504 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001505 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001506 return true;
1507 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001508 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001509 return false;
1510 }
1511 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001512 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001513#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001514 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001515}
1516
1517bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001518 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001519
Dees_Troy43d8b002012-09-17 16:00:01 -04001520 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001521 if (!UnMount(true))
1522 return false;
1523
Dees_Troy2673cec2013-04-02 20:22:16 +00001524 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001525 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001526 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001527 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001528 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001529 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001530 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001531 return true;
1532 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001533 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001534 return false;
1535 }
1536 return true;
1537 }
1538 else
1539 return Wipe_RMRF();
1540
1541 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001542}
1543
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001544bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001545 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001546
1547 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1548 if (!UnMount(true))
1549 return false;
1550
Dees_Troy2673cec2013-04-02 20:22:16 +00001551 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001552 Find_Actual_Block_Device();
1553 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001554 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001555 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001556 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001557 return true;
1558 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001559 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001560 return false;
1561 }
1562 return true;
1563 }
1564 return false;
1565}
1566
Dees_Troy38bd7602012-09-14 13:33:53 -04001567bool TWPartition::Wipe_MTD() {
1568 if (!UnMount(true))
1569 return false;
1570
Dees_Troy2673cec2013-04-02 20:22:16 +00001571 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001572
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001573 mtd_scan_partitions();
1574 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1575 if (mtd == NULL) {
1576 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1577 return false;
1578 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001579
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001580 MtdWriteContext* ctx = mtd_write_partition(mtd);
1581 if (ctx == NULL) {
1582 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1583 return false;
1584 }
1585 if (mtd_erase_blocks(ctx, -1) == -1) {
1586 mtd_write_close(ctx);
1587 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1588 return false;
1589 }
1590 if (mtd_write_close(ctx) != 0) {
1591 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1592 return false;
1593 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001594 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001595 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001596 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001597 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001598}
1599
1600bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001601 if (!Mount(true))
1602 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06001603 // This is the only wipe that leaves the partition mounted, so we
1604 // must manually remove the partition from MTP if it is a storage
1605 // partition.
1606 if (Is_Storage)
1607 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001608
Dees_Troy2673cec2013-04-02 20:22:16 +00001609 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001610 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001611 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001612 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001613}
1614
Dees_Troye5017042013-08-29 16:38:55 +00001615bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001616 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001617
1618 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1619 if (!UnMount(true))
1620 return false;
1621
1622 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1623 Find_Actual_Block_Device();
1624 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001625 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001626 Recreate_AndSec_Folder();
1627 gui_print("Done.\n");
1628 return true;
1629 } else {
1630 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1631 return false;
1632 }
1633 return true;
1634 } else {
1635 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1636 return Wipe_RMRF();
1637 }
1638 return false;
1639}
1640
Dees_Troy51a0e822012-09-05 15:24:24 -04001641bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001642#ifdef TW_OEM_BUILD
1643 // In an OEM Build we want to do a full format
1644 return Wipe_Encryption();
1645#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001646 string dir;
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001647 #ifdef HAVE_SELINUX
1648 fixPermissions perms;
1649 #endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001650
1651 // This handles wiping data on devices with "sdcard" in /data/media
1652 if (!Mount(true))
1653 return false;
1654
Dees_Troy2673cec2013-04-02 20:22:16 +00001655 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001656
1657 DIR* d;
1658 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001659 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001660 struct dirent* de;
1661 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001662 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001663 // The media folder is the "internal sdcard"
1664 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001665 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001666 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001667 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001668
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001669 dir = "/data/";
1670 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001671 if (de->d_type == DT_DIR) {
1672 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001673 } 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 +00001674 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001675 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001676 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001677 }
1678 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001679
Dees_Troy2673cec2013-04-02 20:22:16 +00001680 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001681 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001682 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001683 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001684 return false;
Ethan Yonker83e82572014-04-04 10:59:28 -05001685#endif // ifdef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001686}
1687
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001688bool 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 -04001689 char back_name[255], split_index[5];
1690 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001691 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001692 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001693 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001694 twrpTar tar;
1695 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001696
Dees_Troy43d8b002012-09-17 16:00:01 -04001697 if (!Mount(true))
1698 return false;
1699
Dees_Troya13d74f2013-03-24 08:54:55 -05001700 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001701 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001702
1703 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001704 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001705
Dees_Troy83bd4832013-05-04 12:39:56 +00001706#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1707 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1708 if (use_encryption && Can_Encrypt_Backup) {
1709 tar.use_encryption = use_encryption;
1710 if (Use_Userdata_Encryption)
1711 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001712 string Password;
1713 DataManager::GetValue("tw_backup_password", Password);
1714 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001715 } else {
1716 use_encryption = false;
1717 }
1718#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001719
1720 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1721 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001722 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001723 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001724 Full_FileName = backup_folder + "/" + Backup_FileName;
1725 tar.setdir(Backup_Path);
1726 tar.setfn(Full_FileName);
1727 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001728 tar.partition_name = Backup_Name;
1729 tar.backup_folder = backup_folder;
1730 if (tar.createTarFork(overall_size, other_backups_size) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00001731 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001732 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001733}
1734
1735bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001736 char back_name[255], backup_size[32];
Vojtech Bocek05534202013-09-11 08:11:56 +02001737 string Full_FileName, Command, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001738 int use_compression;
1739
igoriok87e3d932013-01-31 21:03:53 +02001740 sprintf(backup_size, "%llu", Backup_Size);
1741 DD_BS = backup_size;
1742
Dees_Troyb46a6842012-09-25 11:06:46 -04001743 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001744 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001745
1746 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1747 Backup_FileName = back_name;
1748
1749 Full_FileName = backup_folder + "/" + Backup_FileName;
1750
Ethan Yonker448c8dc2014-12-08 15:34:34 -06001751 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + " count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001752 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001753 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001754 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04001755 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001756 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001757 return false;
1758 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001759 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001760}
1761
1762bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001763 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001764 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001765 int use_compression;
1766
Dees_Troyb46a6842012-09-25 11:06:46 -04001767 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001768 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001769
1770 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1771 Backup_FileName = back_name;
1772
1773 Full_FileName = backup_folder + "/" + Backup_FileName;
1774
1775 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001776 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001777 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001778 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001779 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1780 // 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 +00001781 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001782 return false;
1783 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001784 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001785}
1786
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001787unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
1788 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
1789 if (restore_info.LoadValues() == 0) {
1790 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
1791 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
1792 return Restore_Size;
1793 }
1794 }
1795 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
1796
1797 Full_FileName = restore_folder + "/" + Backup_FileName;
1798
1799 if (Is_Image(Restore_File_System)) {
1800 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
1801 return Restore_Size;
1802 }
1803
1804 twrpTar tar;
1805 tar.setdir(Backup_Path);
1806 tar.setfn(Full_FileName);
1807 tar.backup_name = Backup_Name;
1808#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1809 string Password;
1810 DataManager::GetValue("tw_restore_password", Password);
1811 if (!Password.empty())
1812 tar.setpassword(Password);
1813#endif
1814 tar.partition_name = Backup_Name;
1815 tar.backup_folder = restore_folder;
1816 Restore_Size = tar.get_size();
1817 return Restore_Size;
1818}
1819
1820bool 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 -08001821 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001822 int index = 0;
1823 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00001824 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001825
Dees_Troye58d5262012-09-21 12:27:57 -04001826 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001827 if (!Wipe_AndSec())
1828 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001829 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001830 gui_print("Wiping %s...\n", Display_Name.c_str());
Ethan Yonker5eac2222014-06-11 12:22:55 -05001831 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
1832 gui_print("WARNING: This /data backup was made with %s file system!\n", Restore_File_System.c_str());
1833 gui_print("The backup may not boot unless you change back to %s.\n", Restore_File_System.c_str());
1834 if (!Wipe_Data_Without_Wiping_Media())
1835 return false;
1836 } else {
1837 if (!Wipe(Restore_File_System))
1838 return false;
1839 }
Dees_Troye58d5262012-09-21 12:27:57 -04001840 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001841 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001842 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001843
1844 if (!Mount(true))
1845 return false;
1846
Dees_Troy4a2a1262012-09-18 09:33:47 -04001847 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06001848 twrpTar tar;
1849 tar.setdir(Backup_Path);
1850 tar.setfn(Full_FileName);
1851 tar.backup_name = Backup_Name;
1852#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1853 string Password;
1854 DataManager::GetValue("tw_restore_password", Password);
1855 if (!Password.empty())
1856 tar.setpassword(Password);
1857#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001858 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00001859 ret = false;
1860 else
1861 ret = true;
1862#ifdef HAVE_CAPABILITIES
1863 // Restore capabilities to the run-as binary
1864 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
1865 struct vfs_cap_data cap_data;
1866 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
1867
1868 memset(&cap_data, 0, sizeof(cap_data));
1869 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
1870 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
1871 cap_data.data[0].inheritable = 0;
1872 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
1873 cap_data.data[1].inheritable = 0;
1874 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
1875 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
1876 } else {
1877 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
1878 }
1879 }
1880#endif
1881 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001882}
1883
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001884bool 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 +02001885 string Full_FileName, Command;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001886 double display_percent, progress_percent;
1887 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04001888
Dees_Troyda8b55a2012-12-12 19:18:30 +00001889 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001890 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001891
1892 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001893 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001894 return false;
1895 }
1896 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1897 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001898 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001899 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1900 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1901 return false;
1902 }
1903
Dees_Troy2673cec2013-04-02 20:22:16 +00001904 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001905 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001906 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001907 TWFunc::Exec_Cmd(Command);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001908 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
1909 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
1910 DataManager::SetValue("tw_size_progress", size_progress);
1911 progress_percent = (display_percent / 100);
1912 DataManager::SetProgress((float)(progress_percent));
1913 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001914 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001915}
1916
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001917bool 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 +02001918 string Full_FileName, Command;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001919 double display_percent, progress_percent;
1920 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04001921
Dees_Troy2673cec2013-04-02 20:22:16 +00001922 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001923 Full_FileName = restore_folder + "/" + Backup_FileName;
1924 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1925 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001926 LOGINFO("Erase command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001927 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001928 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001929 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001930 TWFunc::Exec_Cmd(Command);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001931 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
1932 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
1933 DataManager::SetValue("tw_size_progress", size_progress);
1934 progress_percent = (display_percent / 100);
1935 DataManager::SetProgress((float)(progress_percent));
1936 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001937 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001938}
Dees_Troy5bf43922012-09-07 16:07:55 -04001939
1940bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001941 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001942
Dees_Troyab10ee22012-09-21 14:27:30 -04001943 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001944 return false;
1945
Dees_Troy0550cfb2012-10-13 11:56:13 -04001946 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001947 if (Removable || Is_Encrypted) {
1948 if (!Mount(false))
1949 return true;
1950 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001951 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001952
1953 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001954 if (!ret || Size == 0) {
1955 if (!Get_Size_Via_df(Display_Error)) {
1956 if (!Was_Already_Mounted)
1957 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001958 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001959 }
1960 }
Dees_Troy51127312012-09-08 13:08:49 -04001961
Dees_Troy5bf43922012-09-07 16:07:55 -04001962 if (Has_Data_Media) {
1963 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001964 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001965 Used = du.Get_Folder_Size("/data");
1966 Backup_Size = Used;
1967 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04001968 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001969 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001970 } else {
1971 if (!Was_Already_Mounted)
1972 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001973 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001974 }
Dees_Troye58d5262012-09-21 12:27:57 -04001975 } else if (Has_Android_Secure) {
1976 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001977 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001978 else {
1979 if (!Was_Already_Mounted)
1980 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001981 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001982 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001983 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001984 if (!Was_Already_Mounted)
1985 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001986 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001987}
Dees_Troy38bd7602012-09-14 13:33:53 -04001988
1989void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001990 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001991 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001992 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001993 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001994 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001995 Is_Present = true;
1996 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001997 return;
1998 }
1999 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002000 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002001 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002002 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002003 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002004 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002005 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002006}
2007
2008void TWPartition::Recreate_Media_Folder(void) {
2009 string Command;
2010
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05002011 #ifdef HAVE_SELINUX
2012 fixPermissions perms;
2013 #endif
2014
Dees_Troy38bd7602012-09-14 13:33:53 -04002015 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002016 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04002017 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002018 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00002019 LOGINFO("Recreating /data/media folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002020 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002021#ifdef HAVE_SELINUX
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05002022 perms.fixDataInternalContexts();
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002023#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002024 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002025 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002026 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002027 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002028}
Dees_Troye58d5262012-09-21 12:27:57 -04002029
2030void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002031 if (!Has_Android_Secure)
2032 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002033 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002034 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002035 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002036 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002037 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002038 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002039 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002040 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002041 }
2042}
bigbiff7cb4c332014-11-26 20:36:07 -05002043
2044uint64_t TWPartition::Get_Max_FileSize() {
2045 uint64_t maxFileSize = 0;
2046 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2047 const uint64_t constTB = (uint64_t) constGB * 1024;
2048 const uint64_t constPB = (uint64_t) constTB * 1024;
2049 const uint64_t constEB = (uint64_t) constPB * 1024;
2050
2051 if (Current_File_System == "ext4")
2052 maxFileSize = 16 * constTB; //16 TB
2053 else if (Current_File_System == "vfat")
2054 maxFileSize = 4 * constGB; //4 GB
2055 else if (Current_File_System == "ntfs")
2056 maxFileSize = 256 * constTB; //256 TB
2057 if (Current_File_System == "exfat")
2058 maxFileSize = 16 * constPB; //16 PB
2059 else if (Current_File_System == "ext3")
2060 maxFileSize = 2 * constTB; //2 TB
2061 else if (Current_File_System == "f2fs")
2062 maxFileSize = 3.94 * constTB; //3.94 TB
2063 else
2064 maxFileSize = 100000000L;
2065 return maxFileSize - 1;
2066}
2067