blob: 248ee9bc239e1008a5c20c4086baa2336cc1f27f [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 }
thatc7572eb2015-03-31 18:55:30 +02001004 }
1005
1006 string mount_fs = Current_File_System;
1007 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1008 mount_fs = "texfat";
1009
1010 if (!exfat_mounted &&
1011 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), Mount_Flags, Mount_Options.c_str()) != 0 &&
1012 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), Mount_Flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001013#ifdef TW_NO_EXFAT_FUSE
1014 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001015 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001016 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001017 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001018 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001019 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001020 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -08001021 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 +00001022 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001023 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001024 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001025#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001026 if (!Removable && Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001027 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001028 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001029 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1030 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 +00001031 return false;
1032#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001033 }
1034#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001035 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001036
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001037 if (Removable)
1038 Update_Size(Display_Error);
1039
1040 if (!Symlink_Mount_Point.empty()) {
codelover2a3d4ce2015-03-14 20:26:49 +08001041 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001042 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001043 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001044 return true;
1045}
1046
1047bool TWPartition::UnMount(bool Display_Error) {
1048 if (Is_Mounted()) {
1049 int never_unmount_system;
1050
1051 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1052 if (never_unmount_system == 1 && Mount_Point == "/system")
1053 return true; // Never unmount system if you're not supposed to unmount it
1054
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001055 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001056 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001057
Dees_Troy38bd7602012-09-14 13:33:53 -04001058 if (!Symlink_Mount_Point.empty())
1059 umount(Symlink_Mount_Point.c_str());
1060
Dees_Troyb05ddee2013-01-28 20:24:50 +00001061 umount(Mount_Point.c_str());
1062 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001063 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001064 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001065 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001066 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001067 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001068 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001069 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001070 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001071 } else {
1072 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001073 }
1074}
1075
Gary Peck43acadf2012-11-21 21:19:01 -08001076bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001077 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001078 int check;
1079 string Layout_Filename = Mount_Point + "/.layout_version";
1080
Dees_Troy38bd7602012-09-14 13:33:53 -04001081 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001082 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001083 return false;
1084 }
1085
Dees_Troyc51f1f92012-09-20 15:32:13 -04001086 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001087 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001088
Dees_Troy16c2b312013-01-15 16:51:18 +00001089 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1090 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1091 else
1092 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001093
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001094 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001095 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001096 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001097 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001098 DataManager::GetValue(TW_RM_RF_VAR, check);
1099
Hashcodedabfd492013-08-29 22:45:30 -07001100 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001101 wiped = Wipe_RMRF();
1102 else if (New_File_System == "ext4")
1103 wiped = Wipe_EXT4();
1104 else if (New_File_System == "ext2" || New_File_System == "ext3")
1105 wiped = Wipe_EXT23(New_File_System);
1106 else if (New_File_System == "vfat")
1107 wiped = Wipe_FAT();
1108 else if (New_File_System == "exfat")
1109 wiped = Wipe_EXFAT();
1110 else if (New_File_System == "yaffs2")
1111 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001112 else if (New_File_System == "f2fs")
1113 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001114 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001115 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 +00001116 unlink("/.layout_version");
1117 return false;
1118 }
1119 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001120 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001121
Gary Pecke8bc5d72012-12-21 06:45:25 -08001122 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001123 if (Mount_Point == "/cache")
1124 DataManager::Output_Version();
1125
Dees_Troy16c2b312013-01-15 16:51:18 +00001126 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1127 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1128
1129 if (update_crypt) {
1130 Setup_File_System(false);
1131 if (Is_Encrypted && !Is_Decrypted) {
1132 // just wiped an encrypted partition back to its unencrypted state
1133 Is_Encrypted = false;
1134 Is_Decrypted = false;
1135 Decrypted_Block_Device = "";
1136 if (Mount_Point == "/data") {
1137 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1138 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1139 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001140 }
1141 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001142
1143 if (Mount_Point == "/data" && Has_Data_Media && recreate_media) {
1144 Recreate_Media_Folder();
1145 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001146 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001147 if (Is_Storage) {
1148 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001149 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001150 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001151}
1152
Gary Peck43acadf2012-11-21 21:19:01 -08001153bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001154 if (Is_File_System(Current_File_System))
1155 return Wipe(Current_File_System);
1156 else
1157 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001158}
1159
Dees_Troye58d5262012-09-21 12:27:57 -04001160bool TWPartition::Wipe_AndSec(void) {
1161 if (!Has_Android_Secure)
1162 return false;
1163
Dees_Troye58d5262012-09-21 12:27:57 -04001164 if (!Mount(true))
1165 return false;
1166
Dees_Troy2673cec2013-04-02 20:22:16 +00001167 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001168 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001169 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001170}
1171
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001172bool TWPartition::Can_Repair() {
1173 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/dosfsck"))
1174 return true;
1175 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1176 return true;
1177 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1178 return true;
1179 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1180 return true;
1181 return false;
1182}
1183
1184bool TWPartition::Repair() {
1185 string command;
1186
1187 if (Current_File_System == "vfat") {
1188 if (!TWFunc::Path_Exists("/sbin/dosfsck")) {
1189 gui_print("dosfsck does not exist! Cannot repair!\n");
1190 return false;
1191 }
1192 if (!UnMount(true))
1193 return false;
1194 gui_print("Repairing %s using dosfsck...\n", Display_Name.c_str());
1195 Find_Actual_Block_Device();
1196 command = "/sbin/dosfsck -y " + Actual_Block_Device;
1197 LOGINFO("Repair command: %s\n", command.c_str());
1198 if (TWFunc::Exec_Cmd(command) == 0) {
1199 gui_print("Done.\n");
1200 return true;
1201 } else {
1202 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1203 return false;
1204 }
1205 }
1206 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1207 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
1208 gui_print("e2fsck does not exist! Cannot repair!\n");
1209 return false;
1210 }
1211 if (!UnMount(true))
1212 return false;
1213 gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str());
1214 Find_Actual_Block_Device();
1215 command = "/sbin/e2fsck -p " + Actual_Block_Device;
1216 LOGINFO("Repair command: %s\n", command.c_str());
1217 if (TWFunc::Exec_Cmd(command) == 0) {
1218 gui_print("Done.\n");
1219 return true;
1220 } else {
1221 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1222 return false;
1223 }
1224 }
1225 if (Current_File_System == "exfat") {
1226 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
1227 gui_print("fsck.exfat does not exist! Cannot repair!\n");
1228 return false;
1229 }
1230 if (!UnMount(true))
1231 return false;
1232 gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str());
1233 Find_Actual_Block_Device();
1234 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1235 LOGINFO("Repair command: %s\n", command.c_str());
1236 if (TWFunc::Exec_Cmd(command) == 0) {
1237 gui_print("Done.\n");
1238 return true;
1239 } else {
1240 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1241 return false;
1242 }
1243 }
1244 if (Current_File_System == "f2fs") {
1245 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
1246 gui_print("fsck.f2fs does not exist! Cannot repair!\n");
1247 return false;
1248 }
1249 if (!UnMount(true))
1250 return false;
1251 gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str());
1252 Find_Actual_Block_Device();
1253 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1254 LOGINFO("Repair command: %s\n", command.c_str());
1255 if (TWFunc::Exec_Cmd(command) == 0) {
1256 gui_print("Done.\n");
1257 return true;
1258 } else {
1259 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1260 return false;
1261 }
1262 }
1263 return false;
1264}
1265
bigbiff7abc5fe2015-01-17 16:53:12 -05001266bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
1267 if (Backup_Method == FILES) {
1268 return Backup_Tar(backup_folder, overall_size, other_backups_size, tar_fork_pid);
1269 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001270 else if (Backup_Method == DD)
1271 return Backup_DD(backup_folder);
1272 else if (Backup_Method == FLASH_UTILS)
1273 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001274 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001275 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001276}
1277
Dees_Troy43d8b002012-09-17 16:00:01 -04001278bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001279 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001280 char split_filename[512];
1281 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001282 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001283
Captain Throwbackff5935f2014-09-23 16:08:34 -04001284 sync();
1285
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001286 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001287 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001288 if (!TWFunc::Path_Exists(Full_Filename)) {
1289 // This is a split archive, we presume
1290 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001291 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001292 md5file = split_filename;
1293 md5file += ".md5";
1294 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001295 LOGERR("No md5 file found for '%s'.\n", split_filename);
1296 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001297 return false;
1298 }
1299 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001300 while (index < 1000) {
1301 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001302 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001303 return false;
1304 }
1305 index++;
1306 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001307 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001308 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001309 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001310 } else {
1311 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001312 md5file = Full_Filename + ".md5";
1313 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001314 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1315 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001316 return false;
1317 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001318 md5sum.setfn(Full_Filename);
1319 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001320 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001321 return false;
1322 } else
1323 return true;
1324 }
1325 return false;
1326}
1327
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001328bool 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 -08001329 string Restore_File_System;
1330
1331 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001332 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001333
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001334 Restore_File_System = Get_Restore_File_System(restore_folder);
1335
1336 if (Is_File_System(Restore_File_System))
1337 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1338 else if (Is_Image(Restore_File_System)) {
Ethan Yonkerf66324f2015-01-09 16:46:57 -06001339 return Restore_Image(restore_folder, total_restore_size, already_restored_size, Restore_File_System);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001340 }
1341
1342 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1343 return false;
1344}
1345
1346string TWPartition::Get_Restore_File_System(string restore_folder) {
1347 size_t first_period, second_period;
1348 string Restore_File_System;
1349
Gary Peck43acadf2012-11-21 21:19:01 -08001350 // Parse backup filename to extract the file system before wiping
1351 first_period = Backup_FileName.find(".");
1352 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001353 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001354 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001355 }
1356 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1357 second_period = Restore_File_System.find(".");
1358 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001359 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001360 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001361 }
1362 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001363 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001364 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001365}
1366
1367string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001368 if (Backup_Method == NONE)
1369 return "none";
1370 else if (Backup_Method == FILES)
1371 return "files";
1372 else if (Backup_Method == DD)
1373 return "dd";
1374 else if (Backup_Method == FLASH_UTILS)
1375 return "flash_utils";
1376 else
1377 return "undefined";
1378 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001379}
1380
1381bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001382 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001383 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001384 return 1;
1385}
1386
1387bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001388 bool Save_Data_Media = Has_Data_Media;
1389
1390 if (!UnMount(true))
1391 return false;
1392
Dees_Troy38bd7602012-09-14 13:33:53 -04001393 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001394 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001395#ifdef TW_INCLUDE_CRYPTO
1396 if (Is_Decrypted) {
1397 if (!UnMount(true))
1398 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001399 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001400 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1401 }
1402 }
1403#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001404 Is_Decrypted = false;
1405 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001406 Find_Actual_Block_Device();
Gary Pecke8bc5d72012-12-21 06:45:25 -08001407 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001408 Has_Data_Media = Save_Data_Media;
1409 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1410 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001411 if (Mount(false))
1412 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001413 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001414 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001415#ifndef TW_OEM_BUILD
Dees_Troy2673cec2013-04-02 20:22:16 +00001416 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Ethan Yonker83e82572014-04-04 10:59:28 -05001417#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001418 return true;
1419 } else {
1420 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001421 LOGERR("Unable to format to remove encryption.\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06001422 if (Has_Data_Media && Mount(false))
1423 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001424 return false;
1425 }
1426 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001427}
1428
1429void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001430 const char* type;
1431 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001432
Dees_Troy68cab492012-12-12 19:29:35 +00001433 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1434 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001435
Dees_Troy38bd7602012-09-14 13:33:53 -04001436 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001437 if (!Is_Present)
1438 return;
Dees_Troy51127312012-09-08 13:08:49 -04001439
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001440 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1441 if (blkid_do_fullprobe(pr)) {
1442 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001443 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001444 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001445 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001446
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001447 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001448 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001449 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001450 return;
1451 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001452
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001453 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001454 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001455}
1456
Gary Peck43acadf2012-11-21 21:19:01 -08001457bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001458 if (!UnMount(true))
1459 return false;
1460
Dees_Troy43d8b002012-09-17 16:00:01 -04001461 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001462 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001463
Dees_Troy2673cec2013-04-02 20:22:16 +00001464 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001465 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001466 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001467 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001468 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001469 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001470 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001471 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001472 return true;
1473 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001474 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001475 return false;
1476 }
1477 } else
1478 return Wipe_RMRF();
1479
1480 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001481}
1482
1483bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001484 Find_Actual_Block_Device();
1485 if (!Is_Present) {
1486 LOGERR("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1487 return false;
1488 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001489 if (!UnMount(true))
1490 return false;
1491
Dees_Troyb3265ab2013-08-30 02:59:14 +00001492#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001493 int ret;
1494 char *secontext = NULL;
1495
Dees_Troya95f55c2013-08-17 13:14:43 +00001496 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001497
Dees Troy99c8dbf2014-03-10 16:53:58 +00001498 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001499 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1500 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1501 } else {
1502 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1503 }
1504 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001505 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1506 return false;
1507 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001508 string sedir = Mount_Point + "/lost+found";
1509 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1510 rmdir(sedir.c_str());
1511 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001512 return true;
1513 }
1514#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001515 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001516 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001517
Dees_Troy2673cec2013-04-02 20:22:16 +00001518 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001519 Find_Actual_Block_Device();
1520 Command = "make_ext4fs";
1521 if (!Is_Decrypted && Length != 0) {
1522 // Only use length if we're not decrypted
1523 char len[32];
1524 sprintf(len, "%i", Length);
1525 Command += " -l ";
1526 Command += len;
1527 }
Dees_Troy5295d582013-09-06 15:51:08 +00001528 if (TWFunc::Path_Exists("/file_contexts")) {
1529 Command += " -S /file_contexts";
1530 }
1531 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001532 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001533 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001534 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001535 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001536 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001537 return true;
1538 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001539 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001540 return false;
1541 }
1542 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001543 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001544#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001545 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001546}
1547
1548bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001549 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001550
Dees_Troy43d8b002012-09-17 16:00:01 -04001551 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001552 if (!UnMount(true))
1553 return false;
1554
Dees_Troy2673cec2013-04-02 20:22:16 +00001555 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001556 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001557 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001558 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001559 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001560 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001561 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001562 return true;
1563 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001564 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001565 return false;
1566 }
1567 return true;
1568 }
1569 else
1570 return Wipe_RMRF();
1571
1572 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001573}
1574
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001575bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001576 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001577
1578 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1579 if (!UnMount(true))
1580 return false;
1581
Dees_Troy2673cec2013-04-02 20:22:16 +00001582 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001583 Find_Actual_Block_Device();
1584 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001585 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001586 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001587 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001588 return true;
1589 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001590 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001591 return false;
1592 }
1593 return true;
1594 }
1595 return false;
1596}
1597
Dees_Troy38bd7602012-09-14 13:33:53 -04001598bool TWPartition::Wipe_MTD() {
1599 if (!UnMount(true))
1600 return false;
1601
Dees_Troy2673cec2013-04-02 20:22:16 +00001602 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001603
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001604 mtd_scan_partitions();
1605 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1606 if (mtd == NULL) {
1607 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1608 return false;
1609 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001610
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001611 MtdWriteContext* ctx = mtd_write_partition(mtd);
1612 if (ctx == NULL) {
1613 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1614 return false;
1615 }
1616 if (mtd_erase_blocks(ctx, -1) == -1) {
1617 mtd_write_close(ctx);
1618 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1619 return false;
1620 }
1621 if (mtd_write_close(ctx) != 0) {
1622 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1623 return false;
1624 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001625 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001626 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001627 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001628 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001629}
1630
1631bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001632 if (!Mount(true))
1633 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06001634 // This is the only wipe that leaves the partition mounted, so we
1635 // must manually remove the partition from MTP if it is a storage
1636 // partition.
1637 if (Is_Storage)
1638 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001639
Dees_Troy2673cec2013-04-02 20:22:16 +00001640 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001641 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001642 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001643 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001644}
1645
Dees_Troye5017042013-08-29 16:38:55 +00001646bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001647 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001648
1649 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1650 if (!UnMount(true))
1651 return false;
1652
1653 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1654 Find_Actual_Block_Device();
dhacker29a3fa75f2015-01-17 19:42:33 -05001655 command = "mkfs.f2fs -t 1";
1656 if (!Is_Decrypted && Length != 0) {
1657 // Only use length if we're not decrypted
1658 char len[32];
1659 int mod_length = Length;
1660 if (Length < 0)
1661 mod_length *= -1;
1662 sprintf(len, "%i", mod_length);
1663 command += " -r ";
1664 command += len;
1665 }
1666 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001667 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001668 Recreate_AndSec_Folder();
1669 gui_print("Done.\n");
1670 return true;
1671 } else {
1672 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1673 return false;
1674 }
1675 return true;
1676 } else {
1677 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1678 return Wipe_RMRF();
1679 }
1680 return false;
1681}
1682
Dees_Troy51a0e822012-09-05 15:24:24 -04001683bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001684#ifdef TW_OEM_BUILD
1685 // In an OEM Build we want to do a full format
1686 return Wipe_Encryption();
1687#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001688 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001689
1690 // This handles wiping data on devices with "sdcard" in /data/media
1691 if (!Mount(true))
1692 return false;
1693
Dees_Troy2673cec2013-04-02 20:22:16 +00001694 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001695
1696 DIR* d;
1697 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001698 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001699 struct dirent* de;
1700 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001701 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001702 // The media folder is the "internal sdcard"
1703 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001704 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001705 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001706 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001707
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001708 dir = "/data/";
1709 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001710 if (de->d_type == DT_DIR) {
1711 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001712 } 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 +00001713 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001714 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001715 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001716 }
1717 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001718
Dees_Troy2673cec2013-04-02 20:22:16 +00001719 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001720 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001721 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001722 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001723 return false;
Ethan Yonker83e82572014-04-04 10:59:28 -05001724#endif // ifdef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001725}
1726
bigbiff7abc5fe2015-01-17 16:53:12 -05001727bool 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 -04001728 char back_name[255], split_index[5];
1729 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001730 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001731 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001732 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001733 twrpTar tar;
1734 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001735
Dees_Troy43d8b002012-09-17 16:00:01 -04001736 if (!Mount(true))
1737 return false;
1738
Dees_Troya13d74f2013-03-24 08:54:55 -05001739 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001740 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001741
1742 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001743 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001744
Dees_Troy83bd4832013-05-04 12:39:56 +00001745#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1746 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1747 if (use_encryption && Can_Encrypt_Backup) {
1748 tar.use_encryption = use_encryption;
1749 if (Use_Userdata_Encryption)
1750 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001751 string Password;
1752 DataManager::GetValue("tw_backup_password", Password);
1753 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001754 } else {
1755 use_encryption = false;
1756 }
1757#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001758
1759 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1760 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001761 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001762 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001763 Full_FileName = backup_folder + "/" + Backup_FileName;
1764 tar.setdir(Backup_Path);
1765 tar.setfn(Full_FileName);
1766 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001767 tar.partition_name = Backup_Name;
1768 tar.backup_folder = backup_folder;
bigbiff7abc5fe2015-01-17 16:53:12 -05001769 if (tar.createTarFork(overall_size, other_backups_size, tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00001770 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001771 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001772}
1773
1774bool TWPartition::Backup_DD(string backup_folder) {
codelover352b75e2015-03-29 02:44:07 +08001775 char back_name[255], block_size[32], dd_count[32];
1776 string Full_FileName, Command, DD_BS, DD_COUNT;
Dees_Troy43d8b002012-09-17 16:00:01 -04001777 int use_compression;
codelover352b75e2015-03-29 02:44:07 +08001778 unsigned long long DD_Block_Size, DD_Count;
Dees_Troy43d8b002012-09-17 16:00:01 -04001779
codelover352b75e2015-03-29 02:44:07 +08001780 DD_Block_Size = 16 * 1024 * 1024;
1781 while (Backup_Size % DD_Block_Size != 0) DD_Block_Size >>= 1;
1782
1783 DD_Count = Backup_Size / DD_Block_Size;
1784
1785 sprintf(dd_count, "%llu", DD_Count);
1786 DD_COUNT = dd_count;
1787
1788 sprintf(block_size, "%llu", DD_Block_Size);
1789 DD_BS = block_size;
igoriok87e3d932013-01-31 21:03:53 +02001790
Dees_Troyb46a6842012-09-25 11:06:46 -04001791 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001792 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001793
1794 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1795 Backup_FileName = back_name;
1796
1797 Full_FileName = backup_folder + "/" + Backup_FileName;
1798
codelover352b75e2015-03-29 02:44:07 +08001799 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + " count=" + DD_COUNT;
Dees_Troy2673cec2013-04-02 20:22:16 +00001800 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001801 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001802 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04001803 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001804 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001805 return false;
1806 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001807 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001808}
1809
1810bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001811 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001812 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001813 int use_compression;
1814
Dees_Troyb46a6842012-09-25 11:06:46 -04001815 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001816 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001817
1818 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1819 Backup_FileName = back_name;
1820
1821 Full_FileName = backup_folder + "/" + Backup_FileName;
1822
1823 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001824 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001825 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001826 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001827 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1828 // 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 +00001829 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001830 return false;
1831 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001832 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001833}
1834
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001835unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
1836 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
1837 if (restore_info.LoadValues() == 0) {
1838 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
1839 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
1840 return Restore_Size;
1841 }
1842 }
1843 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
1844
1845 Full_FileName = restore_folder + "/" + Backup_FileName;
1846
1847 if (Is_Image(Restore_File_System)) {
1848 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
1849 return Restore_Size;
1850 }
1851
1852 twrpTar tar;
1853 tar.setdir(Backup_Path);
1854 tar.setfn(Full_FileName);
1855 tar.backup_name = Backup_Name;
1856#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1857 string Password;
1858 DataManager::GetValue("tw_restore_password", Password);
1859 if (!Password.empty())
1860 tar.setpassword(Password);
1861#endif
1862 tar.partition_name = Backup_Name;
1863 tar.backup_folder = restore_folder;
1864 Restore_Size = tar.get_size();
1865 return Restore_Size;
1866}
1867
1868bool 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 -08001869 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001870 int index = 0;
1871 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00001872 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001873
Dees_Troye58d5262012-09-21 12:27:57 -04001874 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001875 if (!Wipe_AndSec())
1876 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001877 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001878 gui_print("Wiping %s...\n", Display_Name.c_str());
Ethan Yonker5eac2222014-06-11 12:22:55 -05001879 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
1880 gui_print("WARNING: This /data backup was made with %s file system!\n", Restore_File_System.c_str());
1881 gui_print("The backup may not boot unless you change back to %s.\n", Restore_File_System.c_str());
1882 if (!Wipe_Data_Without_Wiping_Media())
1883 return false;
1884 } else {
1885 if (!Wipe(Restore_File_System))
1886 return false;
1887 }
Dees_Troye58d5262012-09-21 12:27:57 -04001888 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001889 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001890 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001891
1892 if (!Mount(true))
1893 return false;
1894
Dees_Troy4a2a1262012-09-18 09:33:47 -04001895 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06001896 twrpTar tar;
1897 tar.setdir(Backup_Path);
1898 tar.setfn(Full_FileName);
1899 tar.backup_name = Backup_Name;
1900#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1901 string Password;
1902 DataManager::GetValue("tw_restore_password", Password);
1903 if (!Password.empty())
1904 tar.setpassword(Password);
1905#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001906 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00001907 ret = false;
1908 else
1909 ret = true;
1910#ifdef HAVE_CAPABILITIES
1911 // Restore capabilities to the run-as binary
1912 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
1913 struct vfs_cap_data cap_data;
1914 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
1915
1916 memset(&cap_data, 0, sizeof(cap_data));
1917 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
1918 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
1919 cap_data.data[0].inheritable = 0;
1920 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
1921 cap_data.data[1].inheritable = 0;
1922 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
1923 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
1924 } else {
1925 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
1926 }
1927 }
1928#endif
1929 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001930}
1931
Ethan Yonker96af84a2015-01-05 14:58:36 -06001932bool TWPartition::Restore_Image(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size, string Restore_File_System) {
1933 string Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001934 double display_percent, progress_percent;
1935 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04001936
Dees_Troyda8b55a2012-12-12 19:18:30 +00001937 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001938 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001939
Ethan Yonker96af84a2015-01-05 14:58:36 -06001940 if (Restore_File_System == "emmc") {
1941 if (!Flash_Image_DD(Full_FileName))
1942 return false;
1943 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
1944 if (!Flash_Image_FI(Full_FileName))
1945 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08001946 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001947 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
1948 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
1949 DataManager::SetValue("tw_size_progress", size_progress);
1950 progress_percent = (display_percent / 100);
1951 DataManager::SetProgress((float)(progress_percent));
1952 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001953 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001954}
Dees_Troy5bf43922012-09-07 16:07:55 -04001955
1956bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001957 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001958
Dees_Troyab10ee22012-09-21 14:27:30 -04001959 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001960 return false;
1961
Dees_Troy0550cfb2012-10-13 11:56:13 -04001962 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001963 if (Removable || Is_Encrypted) {
1964 if (!Mount(false))
1965 return true;
1966 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001967 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001968
1969 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001970 if (!ret || Size == 0) {
1971 if (!Get_Size_Via_df(Display_Error)) {
1972 if (!Was_Already_Mounted)
1973 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001974 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001975 }
1976 }
Dees_Troy51127312012-09-08 13:08:49 -04001977
Dees_Troy5bf43922012-09-07 16:07:55 -04001978 if (Has_Data_Media) {
1979 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001980 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001981 Used = du.Get_Folder_Size("/data");
1982 Backup_Size = Used;
1983 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04001984 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001985 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001986 } else {
1987 if (!Was_Already_Mounted)
1988 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001989 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001990 }
Dees_Troye58d5262012-09-21 12:27:57 -04001991 } else if (Has_Android_Secure) {
1992 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001993 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001994 else {
1995 if (!Was_Already_Mounted)
1996 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001997 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001998 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001999 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002000 if (!Was_Already_Mounted)
2001 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002002 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002003}
Dees_Troy38bd7602012-09-14 13:33:53 -04002004
2005void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002006 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002007 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002008 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002009 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002010 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002011 Is_Present = true;
2012 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002013 return;
2014 }
2015 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002016 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002017 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002018 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002019 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002020 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002021 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002022}
2023
2024void TWPartition::Recreate_Media_Folder(void) {
2025 string Command;
2026
2027 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002028 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04002029 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002030 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00002031 LOGINFO("Recreating /data/media folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002032 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002033#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002034 // Attempt to set the correct SELinux contexts on the folder
2035 fixPermissions perms;
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05002036 perms.fixDataInternalContexts();
thata3d31fb2014-12-21 22:27:40 +01002037 // Afterwards, we will try to set the
2038 // default metadata that we were hopefully able to get during
2039 // early boot.
2040 tw_set_default_metadata("/data/media");
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002041#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002042 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002043 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002044 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002045 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002046}
Dees_Troye58d5262012-09-21 12:27:57 -04002047
2048void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002049 if (!Has_Android_Secure)
2050 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002051 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002052 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002053 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002054 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002055 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002056 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002057 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002058 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002059 }
2060}
bigbiff7cb4c332014-11-26 20:36:07 -05002061
2062uint64_t TWPartition::Get_Max_FileSize() {
2063 uint64_t maxFileSize = 0;
2064 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2065 const uint64_t constTB = (uint64_t) constGB * 1024;
2066 const uint64_t constPB = (uint64_t) constTB * 1024;
2067 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002068 if (Current_File_System == "ext4")
2069 maxFileSize = 16 * constTB; //16 TB
2070 else if (Current_File_System == "vfat")
2071 maxFileSize = 4 * constGB; //4 GB
2072 else if (Current_File_System == "ntfs")
2073 maxFileSize = 256 * constTB; //256 TB
2074 else if (Current_File_System == "exfat")
2075 maxFileSize = 16 * constPB; //16 PB
2076 else if (Current_File_System == "ext3")
2077 maxFileSize = 2 * constTB; //2 TB
2078 else if (Current_File_System == "f2fs")
2079 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002080 else
2081 maxFileSize = 100000000L;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002082 LOGINFO("Get_Max_FileSize::maxFileSize: %llu\n", maxFileSize);
bigbiff7cb4c332014-11-26 20:36:07 -05002083 return maxFileSize - 1;
2084}
2085
Ethan Yonker96af84a2015-01-05 14:58:36 -06002086bool TWPartition::Flash_Image(string Filename) {
2087 string Restore_File_System;
2088
2089 LOGINFO("Image filename is: %s\n", Filename.c_str());
2090
2091 if (Backup_Method == FILES) {
2092 LOGERR("Cannot flash images to file systems\n");
2093 return false;
2094 } else if (!Can_Flash_Img) {
2095 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2096 return false;
2097 } else {
2098 if (!Find_Partition_Size()) {
2099 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2100 return false;
2101 }
2102 unsigned long long image_size = TWFunc::Get_File_Size(Filename);
2103 if (image_size > Size) {
2104 LOGERR("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
2105 image_size, Filename.c_str(), Actual_Block_Device.c_str(), Size);
2106 return false;
2107 }
2108 if (Backup_Method == DD)
2109 return Flash_Image_DD(Filename);
2110 else if (Backup_Method == FLASH_UTILS)
2111 return Flash_Image_FI(Filename);
2112 }
2113
2114 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2115 return false;
2116}
2117
2118bool TWPartition::Flash_Image_DD(string Filename) {
2119 string Command;
2120
2121 gui_print("Flashing %s...\n", Display_Name.c_str());
codelover352b75e2015-03-29 02:44:07 +08002122 Command = "dd bs=8388608 if='" + Filename + "' of=" + Actual_Block_Device;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002123 LOGINFO("Flash command: '%s'\n", Command.c_str());
2124 TWFunc::Exec_Cmd(Command);
2125 return true;
2126}
2127
2128bool TWPartition::Flash_Image_FI(string Filename) {
2129 string Command;
2130
2131 gui_print("Flashing %s...\n", Display_Name.c_str());
2132 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2133 Command = "erase_image " + MTD_Name;
2134 LOGINFO("Erase command: '%s'\n", Command.c_str());
2135 TWFunc::Exec_Cmd(Command);
2136 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2137 LOGINFO("Flash command: '%s'\n", Command.c_str());
2138 TWFunc::Exec_Cmd(Command);
2139 return true;
2140}