blob: b5aacccf6f2cfe921534e24bdcec13512f746701 [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
bigbiff7b4c7a62015-01-01 19:44:14 -050034#include "libblkid/include/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;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600157 Can_Flash_Img = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400158}
159
160TWPartition::~TWPartition(void) {
161 // Do nothing
162}
163
Dees_Troy5bf43922012-09-07 16:07:55 -0400164bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
165 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
166 int line_len = Line.size(), index = 0, item_index = 0;
167 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400168 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400169 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500170 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400171
Dees_Troy51127312012-09-08 13:08:49 -0400172 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500173 if (full_line[index] == 34)
174 skip = !skip;
175 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400176 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400177 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400178 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000179 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400180 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500181 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000182 Display_Name = full_line + 1;
183 Backup_Display_Name = Display_Name;
184 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400185 index = Mount_Point.size();
186 while (index < line_len) {
187 while (index < line_len && full_line[index] == '\0')
188 index++;
189 if (index >= line_len)
190 continue;
191 ptr = full_line + index;
192 if (item_index == 0) {
193 // File System
194 Fstab_File_System = ptr;
195 Current_File_System = ptr;
196 item_index++;
197 } else if (item_index == 1) {
198 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400199 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400200 MTD_Name = ptr;
201 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400202 } else if (Fstab_File_System == "bml") {
203 if (Mount_Point == "/boot")
204 MTD_Name = "boot";
205 else if (Mount_Point == "/recovery")
206 MTD_Name = "recovery";
207 Primary_Block_Device = ptr;
208 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000209 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 -0400210 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400211 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000212 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400213 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000214 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400215 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400216 } else {
217 Primary_Block_Device = ptr;
218 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400219 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400220 item_index++;
221 } else if (item_index > 1) {
222 if (*ptr == '/') {
223 // Alternate Block Device
224 Alternate_Block_Device = ptr;
225 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
226 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
227 // Partition length
228 ptr += 7;
229 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400230 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
231 // Custom flags, save for later so that new values aren't overwritten by defaults
232 ptr += 6;
233 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000234 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400235 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
236 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400237 } else {
238 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000239 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400240 }
241 }
242 while (index < line_len && full_line[index] != '\0')
243 index++;
244 }
245
246 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
247 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000248 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400249 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000250 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400251 return 0;
252 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400253 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400254 Setup_File_System(Display_Error);
255 if (Mount_Point == "/system") {
256 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500257 Backup_Display_Name = Display_Name;
258 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400259 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500260 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 } else if (Mount_Point == "/data") {
Dees Troyc657cc02015-01-16 22:48:47 +0000262 UnMount(false); // added in case /data is mounted as tmpfs for qcom hardware decrypt
Dees_Troy5bf43922012-09-07 16:07:55 -0400263 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500264 Backup_Display_Name = Display_Name;
265 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400266 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400267 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500268 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000269 Can_Encrypt_Backup = true;
270 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500271 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200272 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400273#ifdef TW_INCLUDE_CRYPTO
274 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400275 char crypto_blkdev[255];
276 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
277 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400278 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400279 DataManager::SetValue(TW_IS_DECRYPTED, 1);
280 Is_Encrypted = true;
281 Is_Decrypted = true;
282 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000283 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400284 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600285 if (Is_Present) {
Ethan Yonker253368a2014-11-25 15:00:52 -0600286 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 -0600287 if (cryptfs_check_footer() == 0) {
288 Is_Encrypted = true;
289 Is_Decrypted = false;
290 Can_Be_Mounted = false;
291 Current_File_System = "emmc";
292 Setup_Image(Display_Error);
293 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
Vojtech Bocek7e11ac52015-03-05 23:21:49 +0100294 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
Ethan Yonker71413f42014-02-26 13:36:08 -0600295 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
296 DataManager::SetValue("tw_crypto_display", "");
297 } else {
298 LOGERR("Could not mount /data and unable to find crypto footer.\n");
299 }
300 } else {
301 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
302 }
Gary Peck82599a82012-11-21 16:23:12 -0800303 } else {
304 // Filesystem is not encrypted and the mount
305 // succeeded, so get it back to the original
306 // unmounted state
307 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400308 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500309 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400310 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400311#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500312 if (datamedia)
313 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400314#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400315 } else if (Mount_Point == "/cache") {
316 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500317 Backup_Display_Name = Display_Name;
318 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400319 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400320 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500321 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400322 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000323 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500324 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500325 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400326 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400327 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400328 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400329 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500330 Backup_Display_Name = Display_Name;
331 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400332 Is_SubPartition = true;
333 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400334 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500335 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000336 Can_Encrypt_Backup = true;
337 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400338 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400339 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400340 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500341 Backup_Display_Name = Display_Name;
342 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400343 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400344 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500345 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000346 Can_Encrypt_Backup = true;
347 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400348 } else if (Mount_Point == "/boot") {
349 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500350 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400351 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500352 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400353 }
354#ifdef TW_EXTERNAL_STORAGE_PATH
355 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
356 Is_Storage = true;
357 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400358 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500359 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400360#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000361 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400362 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400363 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500364 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400365#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000366 }
Dees_Troy8170a922012-09-18 15:40:25 -0400367#ifdef TW_INTERNAL_STORAGE_PATH
368 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
369 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500370 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400371 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500372 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400373 }
374#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000375 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400376 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500377 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500378 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400379 }
380#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400381 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400382 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400383 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500384 if (Mount_Point == "/boot") {
385 Display_Name = "Boot";
386 Backup_Display_Name = Display_Name;
387 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600388 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500389 } else if (Mount_Point == "/recovery") {
390 Display_Name = "Recovery";
391 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600392 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500393 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400394 }
395
Dees_Troy51127312012-09-08 13:08:49 -0400396 // Process any custom flags
397 if (Flags.size() > 0)
398 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400399 return true;
400}
401
Hashcode62bd9e02013-11-19 21:59:42 -0800402bool TWPartition::Process_FS_Flags(string& Options, int Flags) {
403 int i;
404 char *p;
405 char *savep;
406 char fs_options[250];
407
408 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
409 Options = "";
410
411 p = strtok_r(fs_options, ",", &savep);
412 while (p) {
413 /* Look for the flag "p" in the flag list "fl"
414 * If not found, the loop exits with fl[i].name being null.
415 */
416 for (i = 0; mount_flags[i].name; i++) {
417 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
418 Flags |= mount_flags[i].flag;
419 break;
420 }
421 }
422
423 if (!mount_flags[i].name) {
424 if (Options.size() > 0)
425 Options += ",";
426 Options += p;
427 }
428 p = strtok_r(NULL, ",", &savep);
429 }
430
431 return true;
432}
433
Dees_Troy51127312012-09-08 13:08:49 -0400434bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
435 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500436 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400437 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500438 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400439
440 strcpy(flags, Flags.c_str());
441 flags_len = Flags.size();
442 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500443 if (flags[index] == 34)
444 skip = !skip;
445 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400446 flags[index] = '\0';
447 }
448
449 index = 0;
450 while (index < flags_len) {
451 while (index < flags_len && flags[index] == '\0')
452 index++;
453 if (index >= flags_len)
454 continue;
455 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500456 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400457 if (strcmp(ptr, "removable") == 0) {
458 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600459 } else if (strncmp(ptr, "storage", 7) == 0) {
460 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600461 Is_Storage = true;
462 } else if (ptr_len == 9) {
463 ptr += 9;
464 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
465 LOGINFO("storage set to true\n");
466 Is_Storage = true;
467 } else {
468 LOGINFO("storage set to false\n");
469 Is_Storage = false;
470 }
471 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500472 } else if (strcmp(ptr, "settingsstorage") == 0) {
473 Is_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500474 } else if (strcmp(ptr, "andsec") == 0) {
475 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400476 } else if (strcmp(ptr, "canbewiped") == 0) {
477 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700478 } else if (strcmp(ptr, "usermrf") == 0) {
479 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500480 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
481 ptr += 7;
482 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
483 Can_Be_Backed_Up = true;
484 else
485 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400486 } else if (strcmp(ptr, "wipeingui") == 0) {
487 Can_Be_Wiped = true;
488 Wipe_Available_in_GUI = true;
489 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
490 Can_Be_Wiped = true;
491 Wipe_Available_in_GUI = true;
492 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500493 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000494 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400495 Is_SubPartition = true;
496 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000497 } else if (strcmp(ptr, "ignoreblkid") == 0) {
498 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000499 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
500 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500501 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400502 ptr += 8;
503 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500504 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
505 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400506 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500507 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400508 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500509 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
510 Display_Name.resize(Display_Name.size() - 1);
511 }
512 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
513 has_storage_name = true;
514 ptr += 11;
515 if (*ptr == '\"') ptr++;
516 Storage_Name = ptr;
517 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
518 Storage_Name.resize(Storage_Name.size() - 1);
519 }
520 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
521 has_backup_name = true;
522 ptr += 10;
523 if (*ptr == '\"') ptr++;
524 Backup_Display_Name = ptr;
525 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
526 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
527 }
528 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400529 ptr += 10;
530 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500531 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400532 ptr += 7;
533 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000534 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
535 ptr += 17;
536 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
537 Can_Encrypt_Backup = true;
538 else
539 Can_Encrypt_Backup = false;
540 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
541 ptr += 21;
542 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
543 Can_Encrypt_Backup = true;
544 Use_Userdata_Encryption = true;
545 } else {
546 Use_Userdata_Encryption = false;
547 }
Hashcode62bd9e02013-11-19 21:59:42 -0800548 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
549 ptr += 8;
550 if (*ptr == '\"') ptr++;
551
552 Mount_Options = ptr;
553 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
554 Mount_Options.resize(Mount_Options.size() - 1);
555 }
556 Process_FS_Flags(Mount_Options, Mount_Flags);
Ethan Yonker253368a2014-11-25 15:00:52 -0600557 } else if ((ptr_len > 12 && strncmp(ptr, "encryptable=", 12) == 0) || (ptr_len > 13 && strncmp(ptr, "forceencrypt=", 13) == 0)) {
558 ptr += 12;
559 if (*ptr == '=') ptr++;
560 if (*ptr == '\"') ptr++;
561
562 Crypto_Key_Location = ptr;
563 if (Crypto_Key_Location.substr(Crypto_Key_Location.size() - 1, 1) == "\"") {
564 Crypto_Key_Location.resize(Crypto_Key_Location.size() - 1);
565 }
566 } else if (ptr_len > 8 && strncmp(ptr, "mounttodecrypt", 14) == 0) {
567 Mount_To_Decrypt = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600568 } else if (strncmp(ptr, "flashimg", 8) == 0) {
569 if (ptr_len == 8) {
570 Can_Flash_Img = true;
571 } else if (ptr_len == 10) {
572 ptr += 9;
573 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
574 Can_Flash_Img = true;
575 } else {
576 Can_Flash_Img = false;
577 }
578 }
Dees_Troy51127312012-09-08 13:08:49 -0400579 } else {
580 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000581 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400582 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000583 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400584 }
585 while (index < flags_len && flags[index] != '\0')
586 index++;
587 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500588 if (has_display_name && !has_storage_name)
589 Storage_Name = Display_Name;
590 if (!has_display_name && has_storage_name)
591 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000592 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500593 Backup_Display_Name = Display_Name;
594 if (!has_display_name && has_backup_name)
595 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400596 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400597}
598
Dees_Troy5bf43922012-09-07 16:07:55 -0400599bool TWPartition::Is_File_System(string File_System) {
600 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400601 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400602 File_System == "ext4" ||
603 File_System == "vfat" ||
604 File_System == "ntfs" ||
605 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500606 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000607 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400608 File_System == "auto")
609 return true;
610 else
611 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400612}
613
Dees_Troy5bf43922012-09-07 16:07:55 -0400614bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400615 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400616 return true;
617 else
618 return false;
619}
620
Dees_Troy51127312012-09-08 13:08:49 -0400621bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400622 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400623 if (mkdir(Path.c_str(), 0777) == -1) {
624 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000625 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400626 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000627 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400628 return false;
629 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000630 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400631 return true;
632 }
633 }
634 return true;
635}
636
Dees_Troy5bf43922012-09-07 16:07:55 -0400637void TWPartition::Setup_File_System(bool Display_Error) {
638 struct statfs st;
639
640 Can_Be_Mounted = true;
641 Can_Be_Wiped = true;
642
Dees_Troy5bf43922012-09-07 16:07:55 -0400643 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400644 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400645 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
646 Backup_Name = Display_Name;
647 Backup_Method = FILES;
648}
649
650void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400651 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
652 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800653 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400654 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800655 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400656 Backup_Method = FLASH_UTILS;
657 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000658 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 -0400659 if (Find_Partition_Size()) {
660 Used = Size;
661 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400662 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400663 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000664 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400665 else
Dees Troyd932ce12013-10-18 17:12:59 +0000666 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400667 }
668}
669
Dees_Troye58d5262012-09-21 12:27:57 -0400670void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500671 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400672 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500673 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400674 Has_Android_Secure = true;
675 Symlink_Path = Mount_Point + "/.android_secure";
676 Symlink_Mount_Point = "/and-sec";
677 Backup_Path = Symlink_Mount_Point;
678 Make_Dir("/and-sec", true);
679 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600680 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400681}
682
that9e0593e2014-10-08 00:01:24 +0200683void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500684 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
685 Storage_Name = "Internal Storage";
686 Has_Data_Media = true;
687 Is_Storage = true;
688 Is_Settings_Storage = true;
689 Storage_Path = "/data/media";
690 Symlink_Path = Storage_Path;
691 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
692 Make_Dir("/emmc", false);
693 Symlink_Mount_Point = "/emmc";
694 } else {
695 Make_Dir("/sdcard", false);
696 Symlink_Mount_Point = "/sdcard";
697 }
698 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
699 Storage_Path = "/data/media/0";
700 Symlink_Path = Storage_Path;
701 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
702 UnMount(true);
703 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500704 DataManager::SetValue("tw_has_internal", 1);
705 DataManager::SetValue("tw_has_data_media", 1);
706 du.add_absolute_dir("/data/media");
707}
708
Dees_Troy5bf43922012-09-07 16:07:55 -0400709void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
710 char device[512], realDevice[512];
711
712 strcpy(device, Block.c_str());
713 memset(realDevice, 0, sizeof(realDevice));
714 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
715 {
716 strcpy(device, realDevice);
717 memset(realDevice, 0, sizeof(realDevice));
718 }
719
720 if (device[0] != '/') {
721 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000722 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400723 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000724 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400725 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400726 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400727 Block = device;
728 return;
729 }
730}
731
Dees_Troy8e337f32012-10-13 22:07:49 -0400732void TWPartition::Mount_Storage_Retry(void) {
733 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500734 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400735 int retry_count = 5;
736 while (retry_count > 0 && !Mount(false)) {
737 usleep(500000);
738 retry_count--;
739 }
740 Mount(true);
741 }
742}
743
Dees_Troy38bd7602012-09-14 13:33:53 -0400744bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
745 FILE *fp = NULL;
746 char line[255];
747
748 fp = fopen("/proc/mtd", "rt");
749 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000750 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400751 return false;
752 }
753
754 while (fgets(line, sizeof(line), fp) != NULL)
755 {
756 char device[32], label[32];
757 unsigned long size = 0;
758 char* fstype = NULL;
759 int deviceId;
760
761 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
762
763 // Skip header and blank lines
764 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
765 continue;
766
767 // Strip off the trailing " from the label
768 label[strlen(label)-1] = '\0';
769
770 if (strcmp(label, MTD_Name.c_str()) == 0) {
771 // We found our device
772 // Strip off the trailing : from the device
773 device[strlen(device)-1] = '\0';
774 if (sscanf(device,"mtd%d", &deviceId) == 1) {
775 sprintf(device, "/dev/block/mtdblock%d", deviceId);
776 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000777 fclose(fp);
778 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400779 }
780 }
781 }
782 fclose(fp);
783
784 return false;
785}
786
Dees_Troy51127312012-09-08 13:08:49 -0400787bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
788 struct statfs st;
789 string Local_Path = Mount_Point + "/.";
790
791 if (!Mount(Display_Error))
792 return false;
793
794 if (statfs(Local_Path.c_str(), &st) != 0) {
795 if (!Removable) {
796 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000797 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400798 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000799 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400800 }
801 return false;
802 }
803 Size = (st.f_blocks * st.f_bsize);
804 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
805 Free = (st.f_bfree * st.f_bsize);
806 Backup_Size = Used;
807 return true;
808}
809
810bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400811 FILE* fp;
812 char command[255], line[512];
813 int include_block = 1;
814 unsigned int min_len;
815
816 if (!Mount(Display_Error))
817 return false;
818
Dees_Troy38bd7602012-09-14 13:33:53 -0400819 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400820 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200821 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400822 fp = fopen("/tmp/dfoutput.txt", "rt");
823 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000824 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400825 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400826 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400827
828 while (fgets(line, sizeof(line), fp) != NULL)
829 {
830 unsigned long blocks, used, available;
831 char device[64];
832 char tmpString[64];
833
834 if (strncmp(line, "Filesystem", 10) == 0)
835 continue;
836 if (strlen(line) < min_len) {
837 include_block = 0;
838 continue;
839 }
840 if (include_block) {
841 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
842 } else {
843 // The device block string is so long that the df information is on the next line
844 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400845 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400846 while (tmpString[space_count] == 32)
847 space_count++;
848 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
849 }
850
851 // Adjust block size to byte size
852 Size = blocks * 1024ULL;
853 Used = used * 1024ULL;
854 Free = available * 1024ULL;
855 Backup_Size = Used;
856 }
857 fclose(fp);
858 return true;
859}
860
Dees_Troy5bf43922012-09-07 16:07:55 -0400861bool TWPartition::Find_Partition_Size(void) {
862 FILE* fp;
863 char line[512];
864 string tmpdevice;
865
igoriok87e3d932013-01-31 21:03:53 +0200866 fp = fopen("/proc/dumchar_info", "rt");
867 if (fp != NULL) {
868 while (fgets(line, sizeof(line), fp) != NULL)
869 {
870 char label[32], device[32];
871 unsigned long size = 0;
872
thatd43bf2d2014-09-21 23:13:02 +0200873 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200874
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500875 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200876 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
877 continue;
878
879 tmpdevice = "/dev/";
880 tmpdevice += label;
881 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
882 Size = size;
883 fclose(fp);
884 return true;
885 }
886 }
887 }
888
Dees_Troy5bf43922012-09-07 16:07:55 -0400889 // In this case, we'll first get the partitions we care about (with labels)
890 fp = fopen("/proc/partitions", "rt");
891 if (fp == NULL)
892 return false;
893
894 while (fgets(line, sizeof(line), fp) != NULL)
895 {
896 unsigned long major, minor, blocks;
897 char device[512];
898 char tmpString[64];
899
Dees_Troy63c8df72012-09-10 14:02:05 -0400900 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400901 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
902
903 tmpdevice = "/dev/block/";
904 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400905 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400906 // Adjust block size to byte size
907 Size = blocks * 1024ULL;
908 fclose(fp);
909 return true;
910 }
911 }
912 fclose(fp);
913 return false;
914}
915
Dees_Troy5bf43922012-09-07 16:07:55 -0400916bool TWPartition::Is_Mounted(void) {
917 if (!Can_Be_Mounted)
918 return false;
919
920 struct stat st1, st2;
921 string test_path;
922
923 // Check to see if the mount point directory exists
924 test_path = Mount_Point + "/.";
925 if (stat(test_path.c_str(), &st1) != 0) return false;
926
927 // Check to see if the directory above the mount point exists
928 test_path = Mount_Point + "/../.";
929 if (stat(test_path.c_str(), &st2) != 0) return false;
930
931 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
932 int ret = (st1.st_dev != st2.st_dev) ? true : false;
933
934 return ret;
935}
936
937bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000938 int exfat_mounted = 0;
939
Dees_Troy5bf43922012-09-07 16:07:55 -0400940 if (Is_Mounted()) {
941 return true;
942 } else if (!Can_Be_Mounted) {
943 return false;
944 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400945
946 Find_Actual_Block_Device();
947
948 // Check the current file system before mounting
949 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000950 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000951 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 +0000952 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000953 string result;
954 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000955 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000956 Current_File_System = "vfat";
957 } else {
958#ifdef TW_NO_EXFAT_FUSE
959 UnMount(false);
960 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
961 // Some kernels let us mount vfat as exfat which doesn't work out too well
962#else
963 exfat_mounted = 1;
964#endif
965 }
966 }
Dees_Troy22042032012-12-18 21:23:08 +0000967 if (Fstab_File_System == "yaffs2") {
968 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000969 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
970 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
971 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
972 if (Display_Error)
973 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
974 else
975 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
976 return false;
977 } else {
978 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
979 return true;
980 }
981 } else {
982 struct stat st;
983 string test_path = Mount_Point;
984 if (stat(test_path.c_str(), &st) < 0) {
985 if (Display_Error)
986 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
987 else
988 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
989 return false;
990 }
991 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
992 if (new_mode != st.st_mode) {
993 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
994 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
995 if (Display_Error)
996 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
997 else
998 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
999 return false;
1000 }
1001 }
Dees_Troy22042032012-12-18 21:23:08 +00001002 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001003 }
Dees Troy216e0422014-02-07 03:46:42 +00001004 } 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 +00001005#ifdef TW_NO_EXFAT_FUSE
1006 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001007 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001008 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001009 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001010 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001011 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001012 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -08001013 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 +00001014 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001015 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001016 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001017#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001018 if (!Removable && Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001019 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001020 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001021 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1022 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 +00001023 return false;
1024#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001025 }
1026#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001027 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001028
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001029 if (Removable)
1030 Update_Size(Display_Error);
1031
1032 if (!Symlink_Mount_Point.empty()) {
codelover2a3d4ce2015-03-14 20:26:49 +08001033 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001034 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001035 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001036 return true;
1037}
1038
1039bool TWPartition::UnMount(bool Display_Error) {
1040 if (Is_Mounted()) {
1041 int never_unmount_system;
1042
1043 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1044 if (never_unmount_system == 1 && Mount_Point == "/system")
1045 return true; // Never unmount system if you're not supposed to unmount it
1046
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001047 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001048 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001049
Dees_Troy38bd7602012-09-14 13:33:53 -04001050 if (!Symlink_Mount_Point.empty())
1051 umount(Symlink_Mount_Point.c_str());
1052
Dees_Troyb05ddee2013-01-28 20:24:50 +00001053 umount(Mount_Point.c_str());
1054 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001055 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001056 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001057 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001058 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001059 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001060 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001061 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001062 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001063 } else {
1064 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001065 }
1066}
1067
Gary Peck43acadf2012-11-21 21:19:01 -08001068bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001069 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001070 int check;
1071 string Layout_Filename = Mount_Point + "/.layout_version";
1072
Dees_Troy38bd7602012-09-14 13:33:53 -04001073 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001074 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001075 return false;
1076 }
1077
Dees_Troyc51f1f92012-09-20 15:32:13 -04001078 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001079 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001080
Dees_Troy16c2b312013-01-15 16:51:18 +00001081 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1082 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1083 else
1084 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001085
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001086 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001087 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001088 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001089 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001090 DataManager::GetValue(TW_RM_RF_VAR, check);
1091
Hashcodedabfd492013-08-29 22:45:30 -07001092 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001093 wiped = Wipe_RMRF();
1094 else if (New_File_System == "ext4")
1095 wiped = Wipe_EXT4();
1096 else if (New_File_System == "ext2" || New_File_System == "ext3")
1097 wiped = Wipe_EXT23(New_File_System);
1098 else if (New_File_System == "vfat")
1099 wiped = Wipe_FAT();
1100 else if (New_File_System == "exfat")
1101 wiped = Wipe_EXFAT();
1102 else if (New_File_System == "yaffs2")
1103 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001104 else if (New_File_System == "f2fs")
1105 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001106 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001107 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 +00001108 unlink("/.layout_version");
1109 return false;
1110 }
1111 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001112 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001113
Gary Pecke8bc5d72012-12-21 06:45:25 -08001114 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001115 if (Mount_Point == "/cache")
1116 DataManager::Output_Version();
1117
Dees_Troy16c2b312013-01-15 16:51:18 +00001118 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1119 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1120
1121 if (update_crypt) {
1122 Setup_File_System(false);
1123 if (Is_Encrypted && !Is_Decrypted) {
1124 // just wiped an encrypted partition back to its unencrypted state
1125 Is_Encrypted = false;
1126 Is_Decrypted = false;
1127 Decrypted_Block_Device = "";
1128 if (Mount_Point == "/data") {
1129 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1130 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1131 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001132 }
1133 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001134
1135 if (Mount_Point == "/data" && Has_Data_Media && recreate_media) {
1136 Recreate_Media_Folder();
1137 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001138 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001139 if (Is_Storage) {
1140 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001141 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001142 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001143}
1144
Gary Peck43acadf2012-11-21 21:19:01 -08001145bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001146 if (Is_File_System(Current_File_System))
1147 return Wipe(Current_File_System);
1148 else
1149 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001150}
1151
Dees_Troye58d5262012-09-21 12:27:57 -04001152bool TWPartition::Wipe_AndSec(void) {
1153 if (!Has_Android_Secure)
1154 return false;
1155
Dees_Troye58d5262012-09-21 12:27:57 -04001156 if (!Mount(true))
1157 return false;
1158
Dees_Troy2673cec2013-04-02 20:22:16 +00001159 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001160 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001161 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001162}
1163
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001164bool TWPartition::Can_Repair() {
1165 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/dosfsck"))
1166 return true;
1167 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1168 return true;
1169 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1170 return true;
1171 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1172 return true;
1173 return false;
1174}
1175
1176bool TWPartition::Repair() {
1177 string command;
1178
1179 if (Current_File_System == "vfat") {
1180 if (!TWFunc::Path_Exists("/sbin/dosfsck")) {
1181 gui_print("dosfsck does not exist! Cannot repair!\n");
1182 return false;
1183 }
1184 if (!UnMount(true))
1185 return false;
1186 gui_print("Repairing %s using dosfsck...\n", Display_Name.c_str());
1187 Find_Actual_Block_Device();
1188 command = "/sbin/dosfsck -y " + Actual_Block_Device;
1189 LOGINFO("Repair command: %s\n", command.c_str());
1190 if (TWFunc::Exec_Cmd(command) == 0) {
1191 gui_print("Done.\n");
1192 return true;
1193 } else {
1194 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1195 return false;
1196 }
1197 }
1198 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1199 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
1200 gui_print("e2fsck does not exist! Cannot repair!\n");
1201 return false;
1202 }
1203 if (!UnMount(true))
1204 return false;
1205 gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str());
1206 Find_Actual_Block_Device();
1207 command = "/sbin/e2fsck -p " + Actual_Block_Device;
1208 LOGINFO("Repair command: %s\n", command.c_str());
1209 if (TWFunc::Exec_Cmd(command) == 0) {
1210 gui_print("Done.\n");
1211 return true;
1212 } else {
1213 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1214 return false;
1215 }
1216 }
1217 if (Current_File_System == "exfat") {
1218 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
1219 gui_print("fsck.exfat does not exist! Cannot repair!\n");
1220 return false;
1221 }
1222 if (!UnMount(true))
1223 return false;
1224 gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str());
1225 Find_Actual_Block_Device();
1226 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1227 LOGINFO("Repair command: %s\n", command.c_str());
1228 if (TWFunc::Exec_Cmd(command) == 0) {
1229 gui_print("Done.\n");
1230 return true;
1231 } else {
1232 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1233 return false;
1234 }
1235 }
1236 if (Current_File_System == "f2fs") {
1237 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
1238 gui_print("fsck.f2fs does not exist! Cannot repair!\n");
1239 return false;
1240 }
1241 if (!UnMount(true))
1242 return false;
1243 gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str());
1244 Find_Actual_Block_Device();
1245 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1246 LOGINFO("Repair command: %s\n", command.c_str());
1247 if (TWFunc::Exec_Cmd(command) == 0) {
1248 gui_print("Done.\n");
1249 return true;
1250 } else {
1251 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1252 return false;
1253 }
1254 }
1255 return false;
1256}
1257
bigbiff7abc5fe2015-01-17 16:53:12 -05001258bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
1259 if (Backup_Method == FILES) {
1260 return Backup_Tar(backup_folder, overall_size, other_backups_size, tar_fork_pid);
1261 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001262 else if (Backup_Method == DD)
1263 return Backup_DD(backup_folder);
1264 else if (Backup_Method == FLASH_UTILS)
1265 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001266 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001267 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001268}
1269
Dees_Troy43d8b002012-09-17 16:00:01 -04001270bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001271 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001272 char split_filename[512];
1273 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001274 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001275
Captain Throwbackff5935f2014-09-23 16:08:34 -04001276 sync();
1277
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001278 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001279 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001280 if (!TWFunc::Path_Exists(Full_Filename)) {
1281 // This is a split archive, we presume
1282 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001283 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001284 md5file = split_filename;
1285 md5file += ".md5";
1286 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001287 LOGERR("No md5 file found for '%s'.\n", split_filename);
1288 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001289 return false;
1290 }
1291 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001292 while (index < 1000) {
1293 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001294 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001295 return false;
1296 }
1297 index++;
1298 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001299 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001300 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001301 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001302 } else {
1303 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001304 md5file = Full_Filename + ".md5";
1305 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001306 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1307 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001308 return false;
1309 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001310 md5sum.setfn(Full_Filename);
1311 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001312 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001313 return false;
1314 } else
1315 return true;
1316 }
1317 return false;
1318}
1319
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001320bool 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 -08001321 string Restore_File_System;
1322
1323 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001324 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001325
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001326 Restore_File_System = Get_Restore_File_System(restore_folder);
1327
1328 if (Is_File_System(Restore_File_System))
1329 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1330 else if (Is_Image(Restore_File_System)) {
Ethan Yonkerf66324f2015-01-09 16:46:57 -06001331 return Restore_Image(restore_folder, total_restore_size, already_restored_size, Restore_File_System);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001332 }
1333
1334 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1335 return false;
1336}
1337
1338string TWPartition::Get_Restore_File_System(string restore_folder) {
1339 size_t first_period, second_period;
1340 string Restore_File_System;
1341
Gary Peck43acadf2012-11-21 21:19:01 -08001342 // Parse backup filename to extract the file system before wiping
1343 first_period = Backup_FileName.find(".");
1344 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001345 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001346 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001347 }
1348 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1349 second_period = Restore_File_System.find(".");
1350 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001351 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001352 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001353 }
1354 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001355 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001356 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001357}
1358
1359string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001360 if (Backup_Method == NONE)
1361 return "none";
1362 else if (Backup_Method == FILES)
1363 return "files";
1364 else if (Backup_Method == DD)
1365 return "dd";
1366 else if (Backup_Method == FLASH_UTILS)
1367 return "flash_utils";
1368 else
1369 return "undefined";
1370 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001371}
1372
1373bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001374 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001375 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001376 return 1;
1377}
1378
1379bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001380 bool Save_Data_Media = Has_Data_Media;
1381
1382 if (!UnMount(true))
1383 return false;
1384
Dees_Troy38bd7602012-09-14 13:33:53 -04001385 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001386 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001387#ifdef TW_INCLUDE_CRYPTO
1388 if (Is_Decrypted) {
1389 if (!UnMount(true))
1390 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001391 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001392 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1393 }
1394 }
1395#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001396 Is_Decrypted = false;
1397 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001398 Find_Actual_Block_Device();
Gary Pecke8bc5d72012-12-21 06:45:25 -08001399 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001400 Has_Data_Media = Save_Data_Media;
1401 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1402 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001403 if (Mount(false))
1404 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001405 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001406 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001407#ifndef TW_OEM_BUILD
Dees_Troy2673cec2013-04-02 20:22:16 +00001408 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Ethan Yonker83e82572014-04-04 10:59:28 -05001409#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001410 return true;
1411 } else {
1412 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001413 LOGERR("Unable to format to remove encryption.\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06001414 if (Has_Data_Media && Mount(false))
1415 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001416 return false;
1417 }
1418 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001419}
1420
1421void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001422 const char* type;
1423 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001424
Dees_Troy68cab492012-12-12 19:29:35 +00001425 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1426 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001427
Dees_Troy38bd7602012-09-14 13:33:53 -04001428 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001429 if (!Is_Present)
1430 return;
Dees_Troy51127312012-09-08 13:08:49 -04001431
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001432 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1433 if (blkid_do_fullprobe(pr)) {
1434 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001435 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001436 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001437 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001438
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001439 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001440 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001441 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001442 return;
1443 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001444
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001445 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001446 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001447}
1448
Gary Peck43acadf2012-11-21 21:19:01 -08001449bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001450 if (!UnMount(true))
1451 return false;
1452
Dees_Troy43d8b002012-09-17 16:00:01 -04001453 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001454 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001455
Dees_Troy2673cec2013-04-02 20:22:16 +00001456 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001457 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001458 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001459 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001460 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001461 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001462 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001463 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001464 return true;
1465 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001466 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001467 return false;
1468 }
1469 } else
1470 return Wipe_RMRF();
1471
1472 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001473}
1474
1475bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001476 Find_Actual_Block_Device();
1477 if (!Is_Present) {
1478 LOGERR("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1479 return false;
1480 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001481 if (!UnMount(true))
1482 return false;
1483
Dees_Troyb3265ab2013-08-30 02:59:14 +00001484#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001485 int ret;
1486 char *secontext = NULL;
1487
Dees_Troya95f55c2013-08-17 13:14:43 +00001488 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001489
Dees Troy99c8dbf2014-03-10 16:53:58 +00001490 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001491 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1492 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1493 } else {
1494 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1495 }
1496 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001497 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1498 return false;
1499 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001500 string sedir = Mount_Point + "/lost+found";
1501 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1502 rmdir(sedir.c_str());
1503 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001504 return true;
1505 }
1506#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001507 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001508 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001509
Dees_Troy2673cec2013-04-02 20:22:16 +00001510 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001511 Find_Actual_Block_Device();
1512 Command = "make_ext4fs";
1513 if (!Is_Decrypted && Length != 0) {
1514 // Only use length if we're not decrypted
1515 char len[32];
1516 sprintf(len, "%i", Length);
1517 Command += " -l ";
1518 Command += len;
1519 }
Dees_Troy5295d582013-09-06 15:51:08 +00001520 if (TWFunc::Path_Exists("/file_contexts")) {
1521 Command += " -S /file_contexts";
1522 }
1523 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001524 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001525 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001526 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001527 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001528 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001529 return true;
1530 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001531 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001532 return false;
1533 }
1534 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001535 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001536#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001537 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001538}
1539
1540bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001541 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001542
Dees_Troy43d8b002012-09-17 16:00:01 -04001543 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001544 if (!UnMount(true))
1545 return false;
1546
Dees_Troy2673cec2013-04-02 20:22:16 +00001547 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001548 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001549 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001550 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001551 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001552 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001553 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001554 return true;
1555 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001556 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001557 return false;
1558 }
1559 return true;
1560 }
1561 else
1562 return Wipe_RMRF();
1563
1564 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001565}
1566
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001567bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001568 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001569
1570 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1571 if (!UnMount(true))
1572 return false;
1573
Dees_Troy2673cec2013-04-02 20:22:16 +00001574 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001575 Find_Actual_Block_Device();
1576 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001577 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001578 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001579 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001580 return true;
1581 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001582 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001583 return false;
1584 }
1585 return true;
1586 }
1587 return false;
1588}
1589
Dees_Troy38bd7602012-09-14 13:33:53 -04001590bool TWPartition::Wipe_MTD() {
1591 if (!UnMount(true))
1592 return false;
1593
Dees_Troy2673cec2013-04-02 20:22:16 +00001594 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001595
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001596 mtd_scan_partitions();
1597 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1598 if (mtd == NULL) {
1599 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1600 return false;
1601 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001602
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001603 MtdWriteContext* ctx = mtd_write_partition(mtd);
1604 if (ctx == NULL) {
1605 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1606 return false;
1607 }
1608 if (mtd_erase_blocks(ctx, -1) == -1) {
1609 mtd_write_close(ctx);
1610 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1611 return false;
1612 }
1613 if (mtd_write_close(ctx) != 0) {
1614 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1615 return false;
1616 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001617 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001618 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001619 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001620 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001621}
1622
1623bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001624 if (!Mount(true))
1625 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06001626 // This is the only wipe that leaves the partition mounted, so we
1627 // must manually remove the partition from MTP if it is a storage
1628 // partition.
1629 if (Is_Storage)
1630 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001631
Dees_Troy2673cec2013-04-02 20:22:16 +00001632 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001633 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001634 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001635 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001636}
1637
Dees_Troye5017042013-08-29 16:38:55 +00001638bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001639 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001640
1641 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1642 if (!UnMount(true))
1643 return false;
1644
1645 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1646 Find_Actual_Block_Device();
dhacker29a3fa75f2015-01-17 19:42:33 -05001647 command = "mkfs.f2fs -t 1";
1648 if (!Is_Decrypted && Length != 0) {
1649 // Only use length if we're not decrypted
1650 char len[32];
1651 int mod_length = Length;
1652 if (Length < 0)
1653 mod_length *= -1;
1654 sprintf(len, "%i", mod_length);
1655 command += " -r ";
1656 command += len;
1657 }
1658 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001659 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001660 Recreate_AndSec_Folder();
1661 gui_print("Done.\n");
1662 return true;
1663 } else {
1664 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1665 return false;
1666 }
1667 return true;
1668 } else {
1669 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1670 return Wipe_RMRF();
1671 }
1672 return false;
1673}
1674
Dees_Troy51a0e822012-09-05 15:24:24 -04001675bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001676#ifdef TW_OEM_BUILD
1677 // In an OEM Build we want to do a full format
1678 return Wipe_Encryption();
1679#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001680 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001681
1682 // This handles wiping data on devices with "sdcard" in /data/media
1683 if (!Mount(true))
1684 return false;
1685
Dees_Troy2673cec2013-04-02 20:22:16 +00001686 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001687
1688 DIR* d;
1689 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001690 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001691 struct dirent* de;
1692 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001693 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001694 // The media folder is the "internal sdcard"
1695 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001696 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001697 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001698 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001699
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001700 dir = "/data/";
1701 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001702 if (de->d_type == DT_DIR) {
1703 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001704 } 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 +00001705 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001706 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001707 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001708 }
1709 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001710
Dees_Troy2673cec2013-04-02 20:22:16 +00001711 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001712 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001713 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001714 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001715 return false;
Ethan Yonker83e82572014-04-04 10:59:28 -05001716#endif // ifdef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001717}
1718
bigbiff7abc5fe2015-01-17 16:53:12 -05001719bool TWPartition::Backup_Tar(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001720 char back_name[255], split_index[5];
1721 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001722 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001723 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001724 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001725 twrpTar tar;
1726 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001727
Dees_Troy43d8b002012-09-17 16:00:01 -04001728 if (!Mount(true))
1729 return false;
1730
Dees_Troya13d74f2013-03-24 08:54:55 -05001731 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001732 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001733
1734 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001735 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001736
Dees_Troy83bd4832013-05-04 12:39:56 +00001737#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1738 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1739 if (use_encryption && Can_Encrypt_Backup) {
1740 tar.use_encryption = use_encryption;
1741 if (Use_Userdata_Encryption)
1742 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001743 string Password;
1744 DataManager::GetValue("tw_backup_password", Password);
1745 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001746 } else {
1747 use_encryption = false;
1748 }
1749#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001750
1751 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1752 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001753 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001754 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001755 Full_FileName = backup_folder + "/" + Backup_FileName;
1756 tar.setdir(Backup_Path);
1757 tar.setfn(Full_FileName);
1758 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001759 tar.partition_name = Backup_Name;
1760 tar.backup_folder = backup_folder;
bigbiff7abc5fe2015-01-17 16:53:12 -05001761 if (tar.createTarFork(overall_size, other_backups_size, tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00001762 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001763 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001764}
1765
1766bool TWPartition::Backup_DD(string backup_folder) {
codelover352b75e2015-03-29 02:44:07 +08001767 char back_name[255], block_size[32], dd_count[32];
1768 string Full_FileName, Command, DD_BS, DD_COUNT;
Dees_Troy43d8b002012-09-17 16:00:01 -04001769 int use_compression;
codelover352b75e2015-03-29 02:44:07 +08001770 unsigned long long DD_Block_Size, DD_Count;
Dees_Troy43d8b002012-09-17 16:00:01 -04001771
codelover352b75e2015-03-29 02:44:07 +08001772 DD_Block_Size = 16 * 1024 * 1024;
1773 while (Backup_Size % DD_Block_Size != 0) DD_Block_Size >>= 1;
1774
1775 DD_Count = Backup_Size / DD_Block_Size;
1776
1777 sprintf(dd_count, "%llu", DD_Count);
1778 DD_COUNT = dd_count;
1779
1780 sprintf(block_size, "%llu", DD_Block_Size);
1781 DD_BS = block_size;
igoriok87e3d932013-01-31 21:03:53 +02001782
Dees_Troyb46a6842012-09-25 11:06:46 -04001783 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001784 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001785
1786 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1787 Backup_FileName = back_name;
1788
1789 Full_FileName = backup_folder + "/" + Backup_FileName;
1790
codelover352b75e2015-03-29 02:44:07 +08001791 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + " count=" + DD_COUNT;
Dees_Troy2673cec2013-04-02 20:22:16 +00001792 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001793 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001794 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04001795 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001796 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001797 return false;
1798 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001799 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001800}
1801
1802bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001803 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001804 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001805 int use_compression;
1806
Dees_Troyb46a6842012-09-25 11:06:46 -04001807 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001808 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001809
1810 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1811 Backup_FileName = back_name;
1812
1813 Full_FileName = backup_folder + "/" + Backup_FileName;
1814
1815 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001816 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001817 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001818 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001819 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1820 // 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 +00001821 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001822 return false;
1823 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001824 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001825}
1826
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001827unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
1828 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
1829 if (restore_info.LoadValues() == 0) {
1830 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
1831 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
1832 return Restore_Size;
1833 }
1834 }
1835 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
1836
1837 Full_FileName = restore_folder + "/" + Backup_FileName;
1838
1839 if (Is_Image(Restore_File_System)) {
1840 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
1841 return Restore_Size;
1842 }
1843
1844 twrpTar tar;
1845 tar.setdir(Backup_Path);
1846 tar.setfn(Full_FileName);
1847 tar.backup_name = Backup_Name;
1848#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1849 string Password;
1850 DataManager::GetValue("tw_restore_password", Password);
1851 if (!Password.empty())
1852 tar.setpassword(Password);
1853#endif
1854 tar.partition_name = Backup_Name;
1855 tar.backup_folder = restore_folder;
1856 Restore_Size = tar.get_size();
1857 return Restore_Size;
1858}
1859
1860bool 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 -08001861 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001862 int index = 0;
1863 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00001864 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001865
Dees_Troye58d5262012-09-21 12:27:57 -04001866 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001867 if (!Wipe_AndSec())
1868 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001869 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001870 gui_print("Wiping %s...\n", Display_Name.c_str());
Ethan Yonker5eac2222014-06-11 12:22:55 -05001871 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
1872 gui_print("WARNING: This /data backup was made with %s file system!\n", Restore_File_System.c_str());
1873 gui_print("The backup may not boot unless you change back to %s.\n", Restore_File_System.c_str());
1874 if (!Wipe_Data_Without_Wiping_Media())
1875 return false;
1876 } else {
1877 if (!Wipe(Restore_File_System))
1878 return false;
1879 }
Dees_Troye58d5262012-09-21 12:27:57 -04001880 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001881 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001882 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001883
1884 if (!Mount(true))
1885 return false;
1886
Dees_Troy4a2a1262012-09-18 09:33:47 -04001887 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06001888 twrpTar tar;
1889 tar.setdir(Backup_Path);
1890 tar.setfn(Full_FileName);
1891 tar.backup_name = Backup_Name;
1892#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1893 string Password;
1894 DataManager::GetValue("tw_restore_password", Password);
1895 if (!Password.empty())
1896 tar.setpassword(Password);
1897#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001898 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00001899 ret = false;
1900 else
1901 ret = true;
1902#ifdef HAVE_CAPABILITIES
1903 // Restore capabilities to the run-as binary
1904 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
1905 struct vfs_cap_data cap_data;
1906 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
1907
1908 memset(&cap_data, 0, sizeof(cap_data));
1909 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
1910 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
1911 cap_data.data[0].inheritable = 0;
1912 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
1913 cap_data.data[1].inheritable = 0;
1914 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
1915 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
1916 } else {
1917 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
1918 }
1919 }
1920#endif
1921 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001922}
1923
Ethan Yonker96af84a2015-01-05 14:58:36 -06001924bool TWPartition::Restore_Image(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size, string Restore_File_System) {
1925 string Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001926 double display_percent, progress_percent;
1927 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04001928
Dees_Troyda8b55a2012-12-12 19:18:30 +00001929 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001930 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001931
Ethan Yonker96af84a2015-01-05 14:58:36 -06001932 if (Restore_File_System == "emmc") {
1933 if (!Flash_Image_DD(Full_FileName))
1934 return false;
1935 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
1936 if (!Flash_Image_FI(Full_FileName))
1937 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08001938 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001939 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
1940 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
1941 DataManager::SetValue("tw_size_progress", size_progress);
1942 progress_percent = (display_percent / 100);
1943 DataManager::SetProgress((float)(progress_percent));
1944 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001945 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001946}
Dees_Troy5bf43922012-09-07 16:07:55 -04001947
1948bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001949 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001950
Dees_Troyab10ee22012-09-21 14:27:30 -04001951 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001952 return false;
1953
Dees_Troy0550cfb2012-10-13 11:56:13 -04001954 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001955 if (Removable || Is_Encrypted) {
1956 if (!Mount(false))
1957 return true;
1958 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001959 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001960
1961 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001962 if (!ret || Size == 0) {
1963 if (!Get_Size_Via_df(Display_Error)) {
1964 if (!Was_Already_Mounted)
1965 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001966 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001967 }
1968 }
Dees_Troy51127312012-09-08 13:08:49 -04001969
Dees_Troy5bf43922012-09-07 16:07:55 -04001970 if (Has_Data_Media) {
1971 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001972 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001973 Used = du.Get_Folder_Size("/data");
1974 Backup_Size = Used;
1975 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04001976 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001977 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001978 } else {
1979 if (!Was_Already_Mounted)
1980 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001981 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001982 }
Dees_Troye58d5262012-09-21 12:27:57 -04001983 } else if (Has_Android_Secure) {
1984 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001985 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001986 else {
1987 if (!Was_Already_Mounted)
1988 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001989 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001990 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001991 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001992 if (!Was_Already_Mounted)
1993 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001994 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001995}
Dees_Troy38bd7602012-09-14 13:33:53 -04001996
1997void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001998 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001999 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002000 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002001 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002002 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002003 Is_Present = true;
2004 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002005 return;
2006 }
2007 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002008 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002009 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002010 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002011 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002012 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002013 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002014}
2015
2016void TWPartition::Recreate_Media_Folder(void) {
2017 string Command;
2018
2019 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002020 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04002021 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002022 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00002023 LOGINFO("Recreating /data/media folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002024 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002025#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002026 // Attempt to set the correct SELinux contexts on the folder
2027 fixPermissions perms;
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05002028 perms.fixDataInternalContexts();
thata3d31fb2014-12-21 22:27:40 +01002029 // Afterwards, we will try to set the
2030 // default metadata that we were hopefully able to get during
2031 // early boot.
2032 tw_set_default_metadata("/data/media");
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002033#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002034 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002035 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002036 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002037 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002038}
Dees_Troye58d5262012-09-21 12:27:57 -04002039
2040void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002041 if (!Has_Android_Secure)
2042 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002043 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002044 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002045 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002046 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002047 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002048 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002049 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002050 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002051 }
2052}
bigbiff7cb4c332014-11-26 20:36:07 -05002053
2054uint64_t TWPartition::Get_Max_FileSize() {
2055 uint64_t maxFileSize = 0;
2056 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2057 const uint64_t constTB = (uint64_t) constGB * 1024;
2058 const uint64_t constPB = (uint64_t) constTB * 1024;
2059 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002060 if (Current_File_System == "ext4")
2061 maxFileSize = 16 * constTB; //16 TB
2062 else if (Current_File_System == "vfat")
2063 maxFileSize = 4 * constGB; //4 GB
2064 else if (Current_File_System == "ntfs")
2065 maxFileSize = 256 * constTB; //256 TB
2066 else if (Current_File_System == "exfat")
2067 maxFileSize = 16 * constPB; //16 PB
2068 else if (Current_File_System == "ext3")
2069 maxFileSize = 2 * constTB; //2 TB
2070 else if (Current_File_System == "f2fs")
2071 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002072 else
2073 maxFileSize = 100000000L;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002074 LOGINFO("Get_Max_FileSize::maxFileSize: %llu\n", maxFileSize);
bigbiff7cb4c332014-11-26 20:36:07 -05002075 return maxFileSize - 1;
2076}
2077
Ethan Yonker96af84a2015-01-05 14:58:36 -06002078bool TWPartition::Flash_Image(string Filename) {
2079 string Restore_File_System;
2080
2081 LOGINFO("Image filename is: %s\n", Filename.c_str());
2082
2083 if (Backup_Method == FILES) {
2084 LOGERR("Cannot flash images to file systems\n");
2085 return false;
2086 } else if (!Can_Flash_Img) {
2087 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2088 return false;
2089 } else {
2090 if (!Find_Partition_Size()) {
2091 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2092 return false;
2093 }
2094 unsigned long long image_size = TWFunc::Get_File_Size(Filename);
2095 if (image_size > Size) {
2096 LOGERR("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
2097 image_size, Filename.c_str(), Actual_Block_Device.c_str(), Size);
2098 return false;
2099 }
2100 if (Backup_Method == DD)
2101 return Flash_Image_DD(Filename);
2102 else if (Backup_Method == FLASH_UTILS)
2103 return Flash_Image_FI(Filename);
2104 }
2105
2106 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2107 return false;
2108}
2109
2110bool TWPartition::Flash_Image_DD(string Filename) {
2111 string Command;
2112
2113 gui_print("Flashing %s...\n", Display_Name.c_str());
codelover352b75e2015-03-29 02:44:07 +08002114 Command = "dd bs=8388608 if='" + Filename + "' of=" + Actual_Block_Device;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002115 LOGINFO("Flash command: '%s'\n", Command.c_str());
2116 TWFunc::Exec_Cmd(Command);
2117 return true;
2118}
2119
2120bool TWPartition::Flash_Image_FI(string Filename) {
2121 string Command;
2122
2123 gui_print("Flashing %s...\n", Display_Name.c_str());
2124 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2125 Command = "erase_image " + MTD_Name;
2126 LOGINFO("Erase command: '%s'\n", Command.c_str());
2127 TWFunc::Exec_Cmd(Command);
2128 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2129 LOGINFO("Flash command: '%s'\n", Command.c_str());
2130 TWFunc::Exec_Cmd(Command);
2131 return true;
2132}