blob: d84e48fae25a7b74064e302b232ebef1af08d9d6 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002 Copyright 2013 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050027#include <iostream>
28#include <sstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040029
Dees_Troy657c3092012-09-10 20:32:10 -040030#ifdef TW_INCLUDE_CRYPTO
31 #include "cutils/properties.h"
32#endif
33
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050034#include "libblkid/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000036#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040037#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040038#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040039#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050040#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041#include "twrpTar.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050042#include "twrpDU.hpp"
bigbiff bigbiff6b600f92014-01-05 18:13:43 -050043#include "fixPermissions.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050044#include "infomanager.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040045extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040046 #include "mtdutils/mtdutils.h"
47 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000048#ifdef USE_EXT4
49 #include "make_ext4fs.h"
50#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060051
52#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060053 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker71413f42014-02-26 13:36:08 -060054#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040055}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040056#ifdef HAVE_SELINUX
57#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060058#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040059#endif
Dees Troy4159aed2014-02-28 17:24:43 +000060#ifdef HAVE_CAPABILITIES
61#include <sys/capability.h>
62#include <sys/xattr.h>
63#include <linux/xattr.h>
64#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040065
bigbiff bigbiff9c754052013-01-09 09:09:08 -050066using namespace std;
67
Dees_Troya95f55c2013-08-17 13:14:43 +000068extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050069extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000070
Hashcode62bd9e02013-11-19 21:59:42 -080071struct flag_list {
72 const char *name;
73 unsigned flag;
74};
75
76static struct flag_list mount_flags[] = {
77 { "noatime", MS_NOATIME },
78 { "noexec", MS_NOEXEC },
79 { "nosuid", MS_NOSUID },
80 { "nodev", MS_NODEV },
81 { "nodiratime", MS_NODIRATIME },
82 { "ro", MS_RDONLY },
83 { "rw", 0 },
84 { "remount", MS_REMOUNT },
85 { "bind", MS_BIND },
86 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000087#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080088 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000089#endif
90#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080091 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000092#endif
93#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -080094 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000095#endif
96#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -080097 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +000098#endif
Hashcode62bd9e02013-11-19 21:59:42 -080099 { "sync", MS_SYNCHRONOUS },
100 { "defaults", 0 },
101 { 0, 0 },
102};
103
that9e0593e2014-10-08 00:01:24 +0200104TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400105 Can_Be_Mounted = false;
106 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500107 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200108 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400109 Wipe_During_Factory_Reset = false;
110 Wipe_Available_in_GUI = false;
111 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400112 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400113 SubPartition_Of = "";
114 Symlink_Path = "";
115 Symlink_Mount_Point = "";
116 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400117 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400118 Actual_Block_Device = "";
119 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400120 Alternate_Block_Device = "";
121 Removable = false;
122 Is_Present = false;
123 Length = 0;
124 Size = 0;
125 Used = 0;
126 Free = 0;
127 Backup_Size = 0;
128 Can_Be_Encrypted = false;
129 Is_Encrypted = false;
130 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600131 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400132 Decrypted_Block_Device = "";
133 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500134 Backup_Display_Name = "";
135 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400136 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400137 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400138 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400139 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000140 Can_Encrypt_Backup = false;
141 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400142 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400143 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400144 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500145 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400146 Storage_Path = "";
147 Current_File_System = "";
148 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800149 Mount_Flags = 0;
150 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400151 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000152 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000153 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600154 Crypto_Key_Location = "footer";
Dees_Troy51a0e822012-09-05 15:24:24 -0400155}
156
157TWPartition::~TWPartition(void) {
158 // Do nothing
159}
160
Dees_Troy5bf43922012-09-07 16:07:55 -0400161bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
162 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
163 int line_len = Line.size(), index = 0, item_index = 0;
164 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400165 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400166 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500167 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400168
Dees_Troy51127312012-09-08 13:08:49 -0400169 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500170 if (full_line[index] == 34)
171 skip = !skip;
172 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400173 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400174 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400175 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000176 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400177 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500178 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000179 Display_Name = full_line + 1;
180 Backup_Display_Name = Display_Name;
181 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400182 index = Mount_Point.size();
183 while (index < line_len) {
184 while (index < line_len && full_line[index] == '\0')
185 index++;
186 if (index >= line_len)
187 continue;
188 ptr = full_line + index;
189 if (item_index == 0) {
190 // File System
191 Fstab_File_System = ptr;
192 Current_File_System = ptr;
193 item_index++;
194 } else if (item_index == 1) {
195 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400196 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400197 MTD_Name = ptr;
198 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400199 } else if (Fstab_File_System == "bml") {
200 if (Mount_Point == "/boot")
201 MTD_Name = "boot";
202 else if (Mount_Point == "/recovery")
203 MTD_Name = "recovery";
204 Primary_Block_Device = ptr;
205 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000206 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 -0400207 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400208 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000209 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400210 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000211 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400212 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400213 } else {
214 Primary_Block_Device = ptr;
215 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400216 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400217 item_index++;
218 } else if (item_index > 1) {
219 if (*ptr == '/') {
220 // Alternate Block Device
221 Alternate_Block_Device = ptr;
222 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
223 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
224 // Partition length
225 ptr += 7;
226 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400227 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
228 // Custom flags, save for later so that new values aren't overwritten by defaults
229 ptr += 6;
230 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000231 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400232 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
233 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400234 } else {
235 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000236 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400237 }
238 }
239 while (index < line_len && full_line[index] != '\0')
240 index++;
241 }
242
243 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
244 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000245 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400246 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000247 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400248 return 0;
249 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400250 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400251 Setup_File_System(Display_Error);
252 if (Mount_Point == "/system") {
253 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500254 Backup_Display_Name = Display_Name;
255 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400256 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500257 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400258 } else if (Mount_Point == "/data") {
259 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500260 Backup_Display_Name = Display_Name;
261 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400262 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400263 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500264 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000265 Can_Encrypt_Backup = true;
266 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500267 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200268 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400269#ifdef TW_INCLUDE_CRYPTO
270 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400271 char crypto_blkdev[255];
272 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
273 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400274 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400275 DataManager::SetValue(TW_IS_DECRYPTED, 1);
276 Is_Encrypted = true;
277 Is_Decrypted = true;
278 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000279 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400280 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600281 if (Is_Present) {
Ethan Yonker253368a2014-11-25 15:00:52 -0600282 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 -0600283 if (cryptfs_check_footer() == 0) {
284 Is_Encrypted = true;
285 Is_Decrypted = false;
286 Can_Be_Mounted = false;
287 Current_File_System = "emmc";
288 Setup_Image(Display_Error);
289 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
290 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
291 DataManager::SetValue("tw_crypto_display", "");
292 } else {
293 LOGERR("Could not mount /data and unable to find crypto footer.\n");
294 }
295 } else {
296 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
297 }
Gary Peck82599a82012-11-21 16:23:12 -0800298 } else {
299 // Filesystem is not encrypted and the mount
300 // succeeded, so get it back to the original
301 // unmounted state
302 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400303 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500304 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400305 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400306#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500307 if (datamedia)
308 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400309#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400310 } else if (Mount_Point == "/cache") {
311 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500312 Backup_Display_Name = Display_Name;
313 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400314 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400315 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500316 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400317 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000318 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500319 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500320 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400321 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400322 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400323 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400324 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500325 Backup_Display_Name = Display_Name;
326 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400327 Is_SubPartition = true;
328 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400329 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500330 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000331 Can_Encrypt_Backup = true;
332 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400333 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400334 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400335 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500336 Backup_Display_Name = Display_Name;
337 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400338 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400339 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500340 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000341 Can_Encrypt_Backup = true;
342 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400343 } else if (Mount_Point == "/boot") {
344 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500345 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400346 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500347 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400348 }
349#ifdef TW_EXTERNAL_STORAGE_PATH
350 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
351 Is_Storage = true;
352 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400353 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500354 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400355#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000356 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400357 Is_Storage = true;
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#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000361 }
Dees_Troy8170a922012-09-18 15:40:25 -0400362#ifdef TW_INTERNAL_STORAGE_PATH
363 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
364 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500365 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400366 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500367 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400368 }
369#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000370 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400371 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500372 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500373 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400374 }
375#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400376 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400377 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400378 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500379 if (Mount_Point == "/boot") {
380 Display_Name = "Boot";
381 Backup_Display_Name = Display_Name;
382 Can_Be_Backed_Up = true;
383 } else if (Mount_Point == "/recovery") {
384 Display_Name = "Recovery";
385 Backup_Display_Name = Display_Name;
Dees_Troya13d74f2013-03-24 08:54:55 -0500386 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400387 }
388
Dees_Troy51127312012-09-08 13:08:49 -0400389 // Process any custom flags
390 if (Flags.size() > 0)
391 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400392 return true;
393}
394
Hashcode62bd9e02013-11-19 21:59:42 -0800395bool TWPartition::Process_FS_Flags(string& Options, int Flags) {
396 int i;
397 char *p;
398 char *savep;
399 char fs_options[250];
400
401 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
402 Options = "";
403
404 p = strtok_r(fs_options, ",", &savep);
405 while (p) {
406 /* Look for the flag "p" in the flag list "fl"
407 * If not found, the loop exits with fl[i].name being null.
408 */
409 for (i = 0; mount_flags[i].name; i++) {
410 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
411 Flags |= mount_flags[i].flag;
412 break;
413 }
414 }
415
416 if (!mount_flags[i].name) {
417 if (Options.size() > 0)
418 Options += ",";
419 Options += p;
420 }
421 p = strtok_r(NULL, ",", &savep);
422 }
423
424 return true;
425}
426
Dees_Troy51127312012-09-08 13:08:49 -0400427bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
428 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500429 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400430 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500431 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400432
433 strcpy(flags, Flags.c_str());
434 flags_len = Flags.size();
435 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500436 if (flags[index] == 34)
437 skip = !skip;
438 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400439 flags[index] = '\0';
440 }
441
442 index = 0;
443 while (index < flags_len) {
444 while (index < flags_len && flags[index] == '\0')
445 index++;
446 if (index >= flags_len)
447 continue;
448 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500449 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400450 if (strcmp(ptr, "removable") == 0) {
451 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600452 } else if (strncmp(ptr, "storage", 7) == 0) {
453 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600454 Is_Storage = true;
455 } else if (ptr_len == 9) {
456 ptr += 9;
457 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
458 LOGINFO("storage set to true\n");
459 Is_Storage = true;
460 } else {
461 LOGINFO("storage set to false\n");
462 Is_Storage = false;
463 }
464 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500465 } else if (strcmp(ptr, "settingsstorage") == 0) {
466 Is_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500467 } else if (strcmp(ptr, "andsec") == 0) {
468 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400469 } else if (strcmp(ptr, "canbewiped") == 0) {
470 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700471 } else if (strcmp(ptr, "usermrf") == 0) {
472 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500473 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
474 ptr += 7;
475 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
476 Can_Be_Backed_Up = true;
477 else
478 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400479 } else if (strcmp(ptr, "wipeingui") == 0) {
480 Can_Be_Wiped = true;
481 Wipe_Available_in_GUI = true;
482 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
483 Can_Be_Wiped = true;
484 Wipe_Available_in_GUI = true;
485 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500486 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000487 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400488 Is_SubPartition = true;
489 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000490 } else if (strcmp(ptr, "ignoreblkid") == 0) {
491 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000492 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
493 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500494 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400495 ptr += 8;
496 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500497 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
498 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400499 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500500 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400501 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500502 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
503 Display_Name.resize(Display_Name.size() - 1);
504 }
505 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
506 has_storage_name = true;
507 ptr += 11;
508 if (*ptr == '\"') ptr++;
509 Storage_Name = ptr;
510 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
511 Storage_Name.resize(Storage_Name.size() - 1);
512 }
513 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
514 has_backup_name = true;
515 ptr += 10;
516 if (*ptr == '\"') ptr++;
517 Backup_Display_Name = ptr;
518 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
519 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
520 }
521 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400522 ptr += 10;
523 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500524 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400525 ptr += 7;
526 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000527 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
528 ptr += 17;
529 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
530 Can_Encrypt_Backup = true;
531 else
532 Can_Encrypt_Backup = false;
533 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
534 ptr += 21;
535 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
536 Can_Encrypt_Backup = true;
537 Use_Userdata_Encryption = true;
538 } else {
539 Use_Userdata_Encryption = false;
540 }
Hashcode62bd9e02013-11-19 21:59:42 -0800541 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
542 ptr += 8;
543 if (*ptr == '\"') ptr++;
544
545 Mount_Options = ptr;
546 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
547 Mount_Options.resize(Mount_Options.size() - 1);
548 }
549 Process_FS_Flags(Mount_Options, Mount_Flags);
Ethan Yonker253368a2014-11-25 15:00:52 -0600550 } else if ((ptr_len > 12 && strncmp(ptr, "encryptable=", 12) == 0) || (ptr_len > 13 && strncmp(ptr, "forceencrypt=", 13) == 0)) {
551 ptr += 12;
552 if (*ptr == '=') ptr++;
553 if (*ptr == '\"') ptr++;
554
555 Crypto_Key_Location = ptr;
556 if (Crypto_Key_Location.substr(Crypto_Key_Location.size() - 1, 1) == "\"") {
557 Crypto_Key_Location.resize(Crypto_Key_Location.size() - 1);
558 }
559 } else if (ptr_len > 8 && strncmp(ptr, "mounttodecrypt", 14) == 0) {
560 Mount_To_Decrypt = true;
Dees_Troy51127312012-09-08 13:08:49 -0400561 } else {
562 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000563 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400564 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000565 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400566 }
567 while (index < flags_len && flags[index] != '\0')
568 index++;
569 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500570 if (has_display_name && !has_storage_name)
571 Storage_Name = Display_Name;
572 if (!has_display_name && has_storage_name)
573 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000574 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500575 Backup_Display_Name = Display_Name;
576 if (!has_display_name && has_backup_name)
577 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400578 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400579}
580
Dees_Troy5bf43922012-09-07 16:07:55 -0400581bool TWPartition::Is_File_System(string File_System) {
582 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400583 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400584 File_System == "ext4" ||
585 File_System == "vfat" ||
586 File_System == "ntfs" ||
587 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500588 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000589 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400590 File_System == "auto")
591 return true;
592 else
593 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400594}
595
Dees_Troy5bf43922012-09-07 16:07:55 -0400596bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400597 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400598 return true;
599 else
600 return false;
601}
602
Dees_Troy51127312012-09-08 13:08:49 -0400603bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400604 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400605 if (mkdir(Path.c_str(), 0777) == -1) {
606 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000607 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400608 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000609 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400610 return false;
611 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000612 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400613 return true;
614 }
615 }
616 return true;
617}
618
Dees_Troy5bf43922012-09-07 16:07:55 -0400619void TWPartition::Setup_File_System(bool Display_Error) {
620 struct statfs st;
621
622 Can_Be_Mounted = true;
623 Can_Be_Wiped = true;
624
Dees_Troy5bf43922012-09-07 16:07:55 -0400625 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400626 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400627 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
628 Backup_Name = Display_Name;
629 Backup_Method = FILES;
630}
631
632void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400633 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
634 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800635 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400636 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800637 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400638 Backup_Method = FLASH_UTILS;
639 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000640 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 -0400641 if (Find_Partition_Size()) {
642 Used = Size;
643 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400644 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400645 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000646 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400647 else
Dees Troyd932ce12013-10-18 17:12:59 +0000648 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400649 }
650}
651
Dees_Troye58d5262012-09-21 12:27:57 -0400652void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500653 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400654 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500655 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400656 Has_Android_Secure = true;
657 Symlink_Path = Mount_Point + "/.android_secure";
658 Symlink_Mount_Point = "/and-sec";
659 Backup_Path = Symlink_Mount_Point;
660 Make_Dir("/and-sec", true);
661 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600662 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400663}
664
that9e0593e2014-10-08 00:01:24 +0200665void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500666 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
667 Storage_Name = "Internal Storage";
668 Has_Data_Media = true;
669 Is_Storage = true;
670 Is_Settings_Storage = true;
671 Storage_Path = "/data/media";
672 Symlink_Path = Storage_Path;
673 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
674 Make_Dir("/emmc", false);
675 Symlink_Mount_Point = "/emmc";
676 } else {
677 Make_Dir("/sdcard", false);
678 Symlink_Mount_Point = "/sdcard";
679 }
680 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
681 Storage_Path = "/data/media/0";
682 Symlink_Path = Storage_Path;
683 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
684 UnMount(true);
685 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500686 DataManager::SetValue("tw_has_internal", 1);
687 DataManager::SetValue("tw_has_data_media", 1);
688 du.add_absolute_dir("/data/media");
689}
690
Dees_Troy5bf43922012-09-07 16:07:55 -0400691void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
692 char device[512], realDevice[512];
693
694 strcpy(device, Block.c_str());
695 memset(realDevice, 0, sizeof(realDevice));
696 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
697 {
698 strcpy(device, realDevice);
699 memset(realDevice, 0, sizeof(realDevice));
700 }
701
702 if (device[0] != '/') {
703 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000704 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400705 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000706 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400707 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400708 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400709 Block = device;
710 return;
711 }
712}
713
Dees_Troy8e337f32012-10-13 22:07:49 -0400714void TWPartition::Mount_Storage_Retry(void) {
715 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500716 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400717 int retry_count = 5;
718 while (retry_count > 0 && !Mount(false)) {
719 usleep(500000);
720 retry_count--;
721 }
722 Mount(true);
723 }
724}
725
Dees_Troy38bd7602012-09-14 13:33:53 -0400726bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
727 FILE *fp = NULL;
728 char line[255];
729
730 fp = fopen("/proc/mtd", "rt");
731 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000732 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400733 return false;
734 }
735
736 while (fgets(line, sizeof(line), fp) != NULL)
737 {
738 char device[32], label[32];
739 unsigned long size = 0;
740 char* fstype = NULL;
741 int deviceId;
742
743 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
744
745 // Skip header and blank lines
746 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
747 continue;
748
749 // Strip off the trailing " from the label
750 label[strlen(label)-1] = '\0';
751
752 if (strcmp(label, MTD_Name.c_str()) == 0) {
753 // We found our device
754 // Strip off the trailing : from the device
755 device[strlen(device)-1] = '\0';
756 if (sscanf(device,"mtd%d", &deviceId) == 1) {
757 sprintf(device, "/dev/block/mtdblock%d", deviceId);
758 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000759 fclose(fp);
760 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400761 }
762 }
763 }
764 fclose(fp);
765
766 return false;
767}
768
Dees_Troy51127312012-09-08 13:08:49 -0400769bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
770 struct statfs st;
771 string Local_Path = Mount_Point + "/.";
772
773 if (!Mount(Display_Error))
774 return false;
775
776 if (statfs(Local_Path.c_str(), &st) != 0) {
777 if (!Removable) {
778 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000779 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400780 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000781 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400782 }
783 return false;
784 }
785 Size = (st.f_blocks * st.f_bsize);
786 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
787 Free = (st.f_bfree * st.f_bsize);
788 Backup_Size = Used;
789 return true;
790}
791
792bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400793 FILE* fp;
794 char command[255], line[512];
795 int include_block = 1;
796 unsigned int min_len;
797
798 if (!Mount(Display_Error))
799 return false;
800
Dees_Troy38bd7602012-09-14 13:33:53 -0400801 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400802 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200803 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400804 fp = fopen("/tmp/dfoutput.txt", "rt");
805 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000806 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400807 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400808 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400809
810 while (fgets(line, sizeof(line), fp) != NULL)
811 {
812 unsigned long blocks, used, available;
813 char device[64];
814 char tmpString[64];
815
816 if (strncmp(line, "Filesystem", 10) == 0)
817 continue;
818 if (strlen(line) < min_len) {
819 include_block = 0;
820 continue;
821 }
822 if (include_block) {
823 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
824 } else {
825 // The device block string is so long that the df information is on the next line
826 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400827 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400828 while (tmpString[space_count] == 32)
829 space_count++;
830 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
831 }
832
833 // Adjust block size to byte size
834 Size = blocks * 1024ULL;
835 Used = used * 1024ULL;
836 Free = available * 1024ULL;
837 Backup_Size = Used;
838 }
839 fclose(fp);
840 return true;
841}
842
Dees_Troy5bf43922012-09-07 16:07:55 -0400843bool TWPartition::Find_Partition_Size(void) {
844 FILE* fp;
845 char line[512];
846 string tmpdevice;
847
igoriok87e3d932013-01-31 21:03:53 +0200848 fp = fopen("/proc/dumchar_info", "rt");
849 if (fp != NULL) {
850 while (fgets(line, sizeof(line), fp) != NULL)
851 {
852 char label[32], device[32];
853 unsigned long size = 0;
854
thatd43bf2d2014-09-21 23:13:02 +0200855 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200856
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500857 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200858 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
859 continue;
860
861 tmpdevice = "/dev/";
862 tmpdevice += label;
863 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
864 Size = size;
865 fclose(fp);
866 return true;
867 }
868 }
869 }
870
Dees_Troy5bf43922012-09-07 16:07:55 -0400871 // In this case, we'll first get the partitions we care about (with labels)
872 fp = fopen("/proc/partitions", "rt");
873 if (fp == NULL)
874 return false;
875
876 while (fgets(line, sizeof(line), fp) != NULL)
877 {
878 unsigned long major, minor, blocks;
879 char device[512];
880 char tmpString[64];
881
Dees_Troy63c8df72012-09-10 14:02:05 -0400882 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400883 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
884
885 tmpdevice = "/dev/block/";
886 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400887 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400888 // Adjust block size to byte size
889 Size = blocks * 1024ULL;
890 fclose(fp);
891 return true;
892 }
893 }
894 fclose(fp);
895 return false;
896}
897
Dees_Troy5bf43922012-09-07 16:07:55 -0400898bool TWPartition::Is_Mounted(void) {
899 if (!Can_Be_Mounted)
900 return false;
901
902 struct stat st1, st2;
903 string test_path;
904
905 // Check to see if the mount point directory exists
906 test_path = Mount_Point + "/.";
907 if (stat(test_path.c_str(), &st1) != 0) return false;
908
909 // Check to see if the directory above the mount point exists
910 test_path = Mount_Point + "/../.";
911 if (stat(test_path.c_str(), &st2) != 0) return false;
912
913 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
914 int ret = (st1.st_dev != st2.st_dev) ? true : false;
915
916 return ret;
917}
918
919bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000920 int exfat_mounted = 0;
921
Dees_Troy5bf43922012-09-07 16:07:55 -0400922 if (Is_Mounted()) {
923 return true;
924 } else if (!Can_Be_Mounted) {
925 return false;
926 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400927
928 Find_Actual_Block_Device();
929
930 // Check the current file system before mounting
931 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000932 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000933 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 +0000934 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000935 string result;
936 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000937 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000938 Current_File_System = "vfat";
939 } else {
940#ifdef TW_NO_EXFAT_FUSE
941 UnMount(false);
942 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
943 // Some kernels let us mount vfat as exfat which doesn't work out too well
944#else
945 exfat_mounted = 1;
946#endif
947 }
948 }
Dees_Troy22042032012-12-18 21:23:08 +0000949 if (Fstab_File_System == "yaffs2") {
950 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000951 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
952 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
953 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
954 if (Display_Error)
955 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
956 else
957 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
958 return false;
959 } else {
960 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
961 return true;
962 }
963 } else {
964 struct stat st;
965 string test_path = Mount_Point;
966 if (stat(test_path.c_str(), &st) < 0) {
967 if (Display_Error)
968 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
969 else
970 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
971 return false;
972 }
973 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
974 if (new_mode != st.st_mode) {
975 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
976 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
977 if (Display_Error)
978 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
979 else
980 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
981 return false;
982 }
983 }
Dees_Troy22042032012-12-18 21:23:08 +0000984 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000985 }
Dees Troy216e0422014-02-07 03:46:42 +0000986 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), Mount_Flags, Mount_Options.c_str()) != 0 && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), Mount_Flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000987#ifdef TW_NO_EXFAT_FUSE
988 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000989 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000990 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000991 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000992 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000993 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000994 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800995 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 +0000996 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000997 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000998 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000999#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001000 if (!Removable && Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001001 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001002 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001003 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1004 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 +00001005 return false;
1006#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001007 }
1008#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001009 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001010
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001011 if (Removable)
1012 Update_Size(Display_Error);
1013
1014 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001015 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
1016 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001017 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001018 return true;
1019}
1020
1021bool TWPartition::UnMount(bool Display_Error) {
1022 if (Is_Mounted()) {
1023 int never_unmount_system;
1024
1025 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1026 if (never_unmount_system == 1 && Mount_Point == "/system")
1027 return true; // Never unmount system if you're not supposed to unmount it
1028
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001029 if (Is_Storage)
1030 TWFunc::Toggle_MTP(false);
1031
Dees_Troy38bd7602012-09-14 13:33:53 -04001032 if (!Symlink_Mount_Point.empty())
1033 umount(Symlink_Mount_Point.c_str());
1034
Dees_Troyb05ddee2013-01-28 20:24:50 +00001035 umount(Mount_Point.c_str());
1036 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001037 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001038 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001039 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001040 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001041 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001042 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001043 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001044 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001045 } else {
1046 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001047 }
1048}
1049
Gary Peck43acadf2012-11-21 21:19:01 -08001050bool TWPartition::Wipe(string New_File_System) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001051 bool wiped = false, update_crypt = false, recreate_media = true, mtp_toggle = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001052 int check;
1053 string Layout_Filename = Mount_Point + "/.layout_version";
1054
Dees_Troy38bd7602012-09-14 13:33:53 -04001055 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001056 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001057 return false;
1058 }
1059
Dees_Troyc51f1f92012-09-20 15:32:13 -04001060 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001061 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001062
Dees_Troy16c2b312013-01-15 16:51:18 +00001063 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1064 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1065 else
1066 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001067
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001068 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001069 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001070 recreate_media = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001071 mtp_toggle = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001072 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001073 DataManager::GetValue(TW_RM_RF_VAR, check);
1074
Hashcodedabfd492013-08-29 22:45:30 -07001075 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001076 wiped = Wipe_RMRF();
1077 else if (New_File_System == "ext4")
1078 wiped = Wipe_EXT4();
1079 else if (New_File_System == "ext2" || New_File_System == "ext3")
1080 wiped = Wipe_EXT23(New_File_System);
1081 else if (New_File_System == "vfat")
1082 wiped = Wipe_FAT();
1083 else if (New_File_System == "exfat")
1084 wiped = Wipe_EXFAT();
1085 else if (New_File_System == "yaffs2")
1086 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001087 else if (New_File_System == "f2fs")
1088 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001089 else {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001090 if (Is_Storage) {
1091 TWFunc::Toggle_MTP(true);
1092 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001093 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 +00001094 unlink("/.layout_version");
1095 return false;
1096 }
1097 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001098 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001099
Gary Pecke8bc5d72012-12-21 06:45:25 -08001100 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001101 if (Mount_Point == "/cache")
1102 DataManager::Output_Version();
1103
Dees_Troy16c2b312013-01-15 16:51:18 +00001104 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1105 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1106
1107 if (update_crypt) {
1108 Setup_File_System(false);
1109 if (Is_Encrypted && !Is_Decrypted) {
1110 // just wiped an encrypted partition back to its unencrypted state
1111 Is_Encrypted = false;
1112 Is_Decrypted = false;
1113 Decrypted_Block_Device = "";
1114 if (Mount_Point == "/data") {
1115 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1116 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1117 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001118 }
1119 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001120
1121 if (Mount_Point == "/data" && Has_Data_Media && recreate_media) {
1122 Recreate_Media_Folder();
1123 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001124 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001125 if (Is_Storage && mtp_toggle) {
1126 TWFunc::Toggle_MTP(true);
1127 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001128 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001129}
1130
Gary Peck43acadf2012-11-21 21:19:01 -08001131bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001132 if (Is_File_System(Current_File_System))
1133 return Wipe(Current_File_System);
1134 else
1135 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001136}
1137
Dees_Troye58d5262012-09-21 12:27:57 -04001138bool TWPartition::Wipe_AndSec(void) {
1139 if (!Has_Android_Secure)
1140 return false;
1141
Dees_Troye58d5262012-09-21 12:27:57 -04001142 if (!Mount(true))
1143 return false;
1144
Dees_Troy2673cec2013-04-02 20:22:16 +00001145 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001146 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001147 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001148}
1149
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001150bool TWPartition::Can_Repair() {
1151 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/dosfsck"))
1152 return true;
1153 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1154 return true;
1155 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1156 return true;
1157 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1158 return true;
1159 return false;
1160}
1161
1162bool TWPartition::Repair() {
1163 string command;
1164
1165 if (Current_File_System == "vfat") {
1166 if (!TWFunc::Path_Exists("/sbin/dosfsck")) {
1167 gui_print("dosfsck does not exist! Cannot repair!\n");
1168 return false;
1169 }
1170 if (!UnMount(true))
1171 return false;
1172 gui_print("Repairing %s using dosfsck...\n", Display_Name.c_str());
1173 Find_Actual_Block_Device();
1174 command = "/sbin/dosfsck -y " + Actual_Block_Device;
1175 LOGINFO("Repair command: %s\n", command.c_str());
1176 if (TWFunc::Exec_Cmd(command) == 0) {
1177 gui_print("Done.\n");
1178 return true;
1179 } else {
1180 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1181 return false;
1182 }
1183 }
1184 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1185 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
1186 gui_print("e2fsck does not exist! Cannot repair!\n");
1187 return false;
1188 }
1189 if (!UnMount(true))
1190 return false;
1191 gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str());
1192 Find_Actual_Block_Device();
1193 command = "/sbin/e2fsck -p " + Actual_Block_Device;
1194 LOGINFO("Repair command: %s\n", command.c_str());
1195 if (TWFunc::Exec_Cmd(command) == 0) {
1196 gui_print("Done.\n");
1197 return true;
1198 } else {
1199 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1200 return false;
1201 }
1202 }
1203 if (Current_File_System == "exfat") {
1204 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
1205 gui_print("fsck.exfat does not exist! Cannot repair!\n");
1206 return false;
1207 }
1208 if (!UnMount(true))
1209 return false;
1210 gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str());
1211 Find_Actual_Block_Device();
1212 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1213 LOGINFO("Repair command: %s\n", command.c_str());
1214 if (TWFunc::Exec_Cmd(command) == 0) {
1215 gui_print("Done.\n");
1216 return true;
1217 } else {
1218 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1219 return false;
1220 }
1221 }
1222 if (Current_File_System == "f2fs") {
1223 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
1224 gui_print("fsck.f2fs does not exist! Cannot repair!\n");
1225 return false;
1226 }
1227 if (!UnMount(true))
1228 return false;
1229 gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str());
1230 Find_Actual_Block_Device();
1231 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1232 LOGINFO("Repair command: %s\n", command.c_str());
1233 if (TWFunc::Exec_Cmd(command) == 0) {
1234 gui_print("Done.\n");
1235 return true;
1236 } else {
1237 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1238 return false;
1239 }
1240 }
1241 return false;
1242}
1243
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001244bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001245 if (Backup_Method == FILES)
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001246 return Backup_Tar(backup_folder, overall_size, other_backups_size);
Dees_Troy38bd7602012-09-14 13:33:53 -04001247 else if (Backup_Method == DD)
1248 return Backup_DD(backup_folder);
1249 else if (Backup_Method == FLASH_UTILS)
1250 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001251 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001252 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001253}
1254
Dees_Troy43d8b002012-09-17 16:00:01 -04001255bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001256 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001257 char split_filename[512];
1258 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001259 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001260
Captain Throwbackff5935f2014-09-23 16:08:34 -04001261 sync();
1262
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001263 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001264 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001265 if (!TWFunc::Path_Exists(Full_Filename)) {
1266 // This is a split archive, we presume
1267 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001268 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001269 md5file = split_filename;
1270 md5file += ".md5";
1271 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001272 LOGERR("No md5 file found for '%s'.\n", split_filename);
1273 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001274 return false;
1275 }
1276 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001277 while (index < 1000) {
1278 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001279 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001280 return false;
1281 }
1282 index++;
1283 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001284 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001285 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001286 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001287 } else {
1288 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001289 md5file = Full_Filename + ".md5";
1290 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001291 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1292 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001293 return false;
1294 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001295 md5sum.setfn(Full_Filename);
1296 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001297 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001298 return false;
1299 } else
1300 return true;
1301 }
1302 return false;
1303}
1304
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001305bool 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 -08001306 string Restore_File_System;
1307
1308 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001309 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001310
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001311 Restore_File_System = Get_Restore_File_System(restore_folder);
1312
1313 if (Is_File_System(Restore_File_System))
1314 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1315 else if (Is_Image(Restore_File_System)) {
1316 *already_restored_size += TWFunc::Get_File_Size(Backup_Name);
1317 if (Restore_File_System == "emmc")
1318 return Restore_DD(restore_folder, total_restore_size, already_restored_size);
1319 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1320 return Restore_Flash_Image(restore_folder, total_restore_size, already_restored_size);
1321 }
1322
1323 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1324 return false;
1325}
1326
1327string TWPartition::Get_Restore_File_System(string restore_folder) {
1328 size_t first_period, second_period;
1329 string Restore_File_System;
1330
Gary Peck43acadf2012-11-21 21:19:01 -08001331 // Parse backup filename to extract the file system before wiping
1332 first_period = Backup_FileName.find(".");
1333 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001334 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001335 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001336 }
1337 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1338 second_period = Restore_File_System.find(".");
1339 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001340 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001341 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001342 }
1343 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001344 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001345 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001346}
1347
1348string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001349 if (Backup_Method == NONE)
1350 return "none";
1351 else if (Backup_Method == FILES)
1352 return "files";
1353 else if (Backup_Method == DD)
1354 return "dd";
1355 else if (Backup_Method == FLASH_UTILS)
1356 return "flash_utils";
1357 else
1358 return "undefined";
1359 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001360}
1361
1362bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001363 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001364 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001365 return 1;
1366}
1367
1368bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001369 bool Save_Data_Media = Has_Data_Media;
1370
1371 if (!UnMount(true))
1372 return false;
1373
Dees_Troy38bd7602012-09-14 13:33:53 -04001374 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001375 Decrypted_Block_Device = "";
1376 Is_Decrypted = false;
1377 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001378 Find_Actual_Block_Device();
1379 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001380 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001381 Has_Data_Media = Save_Data_Media;
1382 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1383 Recreate_Media_Folder();
1384 }
Ethan Yonker83e82572014-04-04 10:59:28 -05001385#ifndef TW_OEM_BUILD
Dees_Troy2673cec2013-04-02 20:22:16 +00001386 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Ethan Yonker83e82572014-04-04 10:59:28 -05001387#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001388 TWFunc::Toggle_MTP(mtp_was_enabled);
Dees_Troy38bd7602012-09-14 13:33:53 -04001389 return true;
1390 } else {
1391 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001392 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001393 return false;
1394 }
1395 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001396}
1397
1398void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001399 const char* type;
1400 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001401
Dees_Troy68cab492012-12-12 19:29:35 +00001402 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1403 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001404
Dees_Troy38bd7602012-09-14 13:33:53 -04001405 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001406 if (!Is_Present)
1407 return;
Dees_Troy51127312012-09-08 13:08:49 -04001408
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001409 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1410 if (blkid_do_fullprobe(pr)) {
1411 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001412 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001413 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001414 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001415
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001416 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001417 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001418 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001419 return;
1420 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001421
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001422 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001423 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001424}
1425
Gary Peck43acadf2012-11-21 21:19:01 -08001426bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001427 if (!UnMount(true))
1428 return false;
1429
Dees_Troy43d8b002012-09-17 16:00:01 -04001430 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001431 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001432
Dees_Troy2673cec2013-04-02 20:22:16 +00001433 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001434 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001435 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001436 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001437 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001438 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001439 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001440 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001441 return true;
1442 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001443 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001444 return false;
1445 }
1446 } else
1447 return Wipe_RMRF();
1448
1449 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001450}
1451
1452bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001453 Find_Actual_Block_Device();
1454 if (!Is_Present) {
1455 LOGERR("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1456 return false;
1457 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001458 if (!UnMount(true))
1459 return false;
1460
Dees_Troyb3265ab2013-08-30 02:59:14 +00001461#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001462 int ret;
1463 char *secontext = NULL;
1464
Dees_Troya95f55c2013-08-17 13:14:43 +00001465 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001466
Dees Troy99c8dbf2014-03-10 16:53:58 +00001467 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001468 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1469 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1470 } else {
1471 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1472 }
1473 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001474 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1475 return false;
1476 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001477 string sedir = Mount_Point + "/lost+found";
1478 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1479 rmdir(sedir.c_str());
1480 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001481 return true;
1482 }
1483#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001484 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001485 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001486
Dees_Troy2673cec2013-04-02 20:22:16 +00001487 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001488 Find_Actual_Block_Device();
1489 Command = "make_ext4fs";
1490 if (!Is_Decrypted && Length != 0) {
1491 // Only use length if we're not decrypted
1492 char len[32];
1493 sprintf(len, "%i", Length);
1494 Command += " -l ";
1495 Command += len;
1496 }
Dees_Troy5295d582013-09-06 15:51:08 +00001497 if (TWFunc::Path_Exists("/file_contexts")) {
1498 Command += " -S /file_contexts";
1499 }
1500 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001501 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001502 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001503 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001504 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001505 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001506 return true;
1507 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001508 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001509 return false;
1510 }
1511 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001512 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001513#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001514 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001515}
1516
1517bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001518 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001519
Dees_Troy43d8b002012-09-17 16:00:01 -04001520 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001521 if (!UnMount(true))
1522 return false;
1523
Dees_Troy2673cec2013-04-02 20:22:16 +00001524 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001525 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001526 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001527 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001528 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001529 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001530 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001531 return true;
1532 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001533 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001534 return false;
1535 }
1536 return true;
1537 }
1538 else
1539 return Wipe_RMRF();
1540
1541 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001542}
1543
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001544bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001545 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001546
1547 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1548 if (!UnMount(true))
1549 return false;
1550
Dees_Troy2673cec2013-04-02 20:22:16 +00001551 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001552 Find_Actual_Block_Device();
1553 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001554 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001555 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001556 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001557 return true;
1558 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001559 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001560 return false;
1561 }
1562 return true;
1563 }
1564 return false;
1565}
1566
Dees_Troy38bd7602012-09-14 13:33:53 -04001567bool TWPartition::Wipe_MTD() {
1568 if (!UnMount(true))
1569 return false;
1570
Dees_Troy2673cec2013-04-02 20:22:16 +00001571 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001572
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001573 mtd_scan_partitions();
1574 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1575 if (mtd == NULL) {
1576 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1577 return false;
1578 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001579
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001580 MtdWriteContext* ctx = mtd_write_partition(mtd);
1581 if (ctx == NULL) {
1582 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1583 return false;
1584 }
1585 if (mtd_erase_blocks(ctx, -1) == -1) {
1586 mtd_write_close(ctx);
1587 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1588 return false;
1589 }
1590 if (mtd_write_close(ctx) != 0) {
1591 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1592 return false;
1593 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001594 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001595 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001596 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001597 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001598}
1599
1600bool TWPartition::Wipe_RMRF() {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001601 if (Is_Storage)
1602 TWFunc::Toggle_MTP(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001603 if (!Mount(true))
1604 return false;
1605
Dees_Troy2673cec2013-04-02 20:22:16 +00001606 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001607 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001608 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001609 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001610}
1611
Dees_Troye5017042013-08-29 16:38:55 +00001612bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001613 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001614
1615 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1616 if (!UnMount(true))
1617 return false;
1618
1619 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1620 Find_Actual_Block_Device();
1621 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001622 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001623 Recreate_AndSec_Folder();
1624 gui_print("Done.\n");
1625 return true;
1626 } else {
1627 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1628 return false;
1629 }
1630 return true;
1631 } else {
1632 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1633 return Wipe_RMRF();
1634 }
1635 return false;
1636}
1637
Dees_Troy51a0e822012-09-05 15:24:24 -04001638bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001639#ifdef TW_OEM_BUILD
1640 // In an OEM Build we want to do a full format
1641 return Wipe_Encryption();
1642#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001643 string dir;
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001644 #ifdef HAVE_SELINUX
1645 fixPermissions perms;
1646 #endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001647
1648 // This handles wiping data on devices with "sdcard" in /data/media
1649 if (!Mount(true))
1650 return false;
1651
Dees_Troy2673cec2013-04-02 20:22:16 +00001652 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001653
1654 DIR* d;
1655 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001656 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001657 struct dirent* de;
1658 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001659 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001660 // The media folder is the "internal sdcard"
1661 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001662 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001663 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001664 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001665
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001666 dir = "/data/";
1667 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001668 if (de->d_type == DT_DIR) {
1669 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001670 } 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 +00001671 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001672 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001673 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001674 }
1675 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001676
Dees_Troy2673cec2013-04-02 20:22:16 +00001677 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001678 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001679 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001680 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001681 return false;
Ethan Yonker83e82572014-04-04 10:59:28 -05001682#endif // ifdef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001683}
1684
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001685bool TWPartition::Backup_Tar(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001686 char back_name[255], split_index[5];
1687 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001688 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001689 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001690 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001691 twrpTar tar;
1692 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001693
Dees_Troy43d8b002012-09-17 16:00:01 -04001694 if (!Mount(true))
1695 return false;
1696
Dees_Troya13d74f2013-03-24 08:54:55 -05001697 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001698 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001699
1700 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001701 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001702
Dees_Troy83bd4832013-05-04 12:39:56 +00001703#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1704 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1705 if (use_encryption && Can_Encrypt_Backup) {
1706 tar.use_encryption = use_encryption;
1707 if (Use_Userdata_Encryption)
1708 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001709 string Password;
1710 DataManager::GetValue("tw_backup_password", Password);
1711 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001712 } else {
1713 use_encryption = false;
1714 }
1715#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001716
1717 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1718 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001719 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001720 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001721 Full_FileName = backup_folder + "/" + Backup_FileName;
1722 tar.setdir(Backup_Path);
1723 tar.setfn(Full_FileName);
1724 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001725 tar.partition_name = Backup_Name;
1726 tar.backup_folder = backup_folder;
1727 if (tar.createTarFork(overall_size, other_backups_size) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00001728 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001729 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001730}
1731
1732bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001733 char back_name[255], backup_size[32];
Vojtech Bocek05534202013-09-11 08:11:56 +02001734 string Full_FileName, Command, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001735 int use_compression;
1736
igoriok87e3d932013-01-31 21:03:53 +02001737 sprintf(backup_size, "%llu", Backup_Size);
1738 DD_BS = backup_size;
1739
Dees_Troyb46a6842012-09-25 11:06:46 -04001740 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001741 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001742
1743 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1744 Backup_FileName = back_name;
1745
1746 Full_FileName = backup_folder + "/" + Backup_FileName;
1747
Ethan Yonker448c8dc2014-12-08 15:34:34 -06001748 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + " count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001749 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001750 TWFunc::Exec_Cmd(Command);
Dees_Troyc154ac22012-10-12 15:36:47 -04001751 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001752 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001753 return false;
1754 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001755 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001756}
1757
1758bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001759 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001760 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001761 int use_compression;
1762
Dees_Troyb46a6842012-09-25 11:06:46 -04001763 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001764 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001765
1766 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1767 Backup_FileName = back_name;
1768
1769 Full_FileName = backup_folder + "/" + Backup_FileName;
1770
1771 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001772 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001773 TWFunc::Exec_Cmd(Command);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001774 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1775 // 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 +00001776 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001777 return false;
1778 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001779 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001780}
1781
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001782unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
1783 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
1784 if (restore_info.LoadValues() == 0) {
1785 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
1786 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
1787 return Restore_Size;
1788 }
1789 }
1790 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
1791
1792 Full_FileName = restore_folder + "/" + Backup_FileName;
1793
1794 if (Is_Image(Restore_File_System)) {
1795 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
1796 return Restore_Size;
1797 }
1798
1799 twrpTar tar;
1800 tar.setdir(Backup_Path);
1801 tar.setfn(Full_FileName);
1802 tar.backup_name = Backup_Name;
1803#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1804 string Password;
1805 DataManager::GetValue("tw_restore_password", Password);
1806 if (!Password.empty())
1807 tar.setpassword(Password);
1808#endif
1809 tar.partition_name = Backup_Name;
1810 tar.backup_folder = restore_folder;
1811 Restore_Size = tar.get_size();
1812 return Restore_Size;
1813}
1814
1815bool 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 -08001816 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001817 int index = 0;
1818 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00001819 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001820
Dees_Troye58d5262012-09-21 12:27:57 -04001821 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001822 if (!Wipe_AndSec())
1823 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001824 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001825 gui_print("Wiping %s...\n", Display_Name.c_str());
Ethan Yonker5eac2222014-06-11 12:22:55 -05001826 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
1827 gui_print("WARNING: This /data backup was made with %s file system!\n", Restore_File_System.c_str());
1828 gui_print("The backup may not boot unless you change back to %s.\n", Restore_File_System.c_str());
1829 if (!Wipe_Data_Without_Wiping_Media())
1830 return false;
1831 } else {
1832 if (!Wipe(Restore_File_System))
1833 return false;
1834 }
Dees_Troye58d5262012-09-21 12:27:57 -04001835 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001836 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001837 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001838
1839 if (!Mount(true))
1840 return false;
1841
Dees_Troy4a2a1262012-09-18 09:33:47 -04001842 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06001843 twrpTar tar;
1844 tar.setdir(Backup_Path);
1845 tar.setfn(Full_FileName);
1846 tar.backup_name = Backup_Name;
1847#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1848 string Password;
1849 DataManager::GetValue("tw_restore_password", Password);
1850 if (!Password.empty())
1851 tar.setpassword(Password);
1852#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001853 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00001854 ret = false;
1855 else
1856 ret = true;
1857#ifdef HAVE_CAPABILITIES
1858 // Restore capabilities to the run-as binary
1859 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
1860 struct vfs_cap_data cap_data;
1861 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
1862
1863 memset(&cap_data, 0, sizeof(cap_data));
1864 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
1865 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
1866 cap_data.data[0].inheritable = 0;
1867 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
1868 cap_data.data[1].inheritable = 0;
1869 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
1870 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
1871 } else {
1872 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
1873 }
1874 }
1875#endif
1876 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001877}
1878
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001879bool TWPartition::Restore_DD(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001880 string Full_FileName, Command;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001881 double display_percent, progress_percent;
1882 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04001883
Dees_Troyda8b55a2012-12-12 19:18:30 +00001884 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001885 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001886
1887 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001888 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001889 return false;
1890 }
1891 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1892 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001893 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001894 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1895 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1896 return false;
1897 }
1898
Dees_Troy2673cec2013-04-02 20:22:16 +00001899 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001900 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001901 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001902 TWFunc::Exec_Cmd(Command);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001903 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
1904 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
1905 DataManager::SetValue("tw_size_progress", size_progress);
1906 progress_percent = (display_percent / 100);
1907 DataManager::SetProgress((float)(progress_percent));
1908 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001909 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001910}
1911
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001912bool TWPartition::Restore_Flash_Image(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001913 string Full_FileName, Command;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001914 double display_percent, progress_percent;
1915 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04001916
Dees_Troy2673cec2013-04-02 20:22:16 +00001917 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001918 Full_FileName = restore_folder + "/" + Backup_FileName;
1919 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1920 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001921 LOGINFO("Erase command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001922 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001923 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001924 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001925 TWFunc::Exec_Cmd(Command);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001926 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
1927 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
1928 DataManager::SetValue("tw_size_progress", size_progress);
1929 progress_percent = (display_percent / 100);
1930 DataManager::SetProgress((float)(progress_percent));
1931 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001932 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001933}
Dees_Troy5bf43922012-09-07 16:07:55 -04001934
1935bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001936 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001937
Dees_Troyab10ee22012-09-21 14:27:30 -04001938 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001939 return false;
1940
Dees_Troy0550cfb2012-10-13 11:56:13 -04001941 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001942 if (Removable || Is_Encrypted) {
1943 if (!Mount(false))
1944 return true;
1945 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001946 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001947
1948 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001949 if (!ret || Size == 0) {
1950 if (!Get_Size_Via_df(Display_Error)) {
1951 if (!Was_Already_Mounted)
1952 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001953 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001954 }
1955 }
Dees_Troy51127312012-09-08 13:08:49 -04001956
Dees_Troy5bf43922012-09-07 16:07:55 -04001957 if (Has_Data_Media) {
1958 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001959 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001960 Used = du.Get_Folder_Size("/data");
1961 Backup_Size = Used;
1962 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04001963 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001964 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001965 } else {
1966 if (!Was_Already_Mounted)
1967 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001968 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001969 }
Dees_Troye58d5262012-09-21 12:27:57 -04001970 } else if (Has_Android_Secure) {
1971 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001972 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001973 else {
1974 if (!Was_Already_Mounted)
1975 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001976 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001977 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001978 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001979 if (!Was_Already_Mounted)
1980 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001981 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001982}
Dees_Troy38bd7602012-09-14 13:33:53 -04001983
1984void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001985 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001986 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001987 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001988 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001989 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001990 Is_Present = true;
1991 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001992 return;
1993 }
1994 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001995 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001996 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001997 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001998 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001999 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002000 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002001}
2002
2003void TWPartition::Recreate_Media_Folder(void) {
2004 string Command;
2005
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05002006 #ifdef HAVE_SELINUX
2007 fixPermissions perms;
2008 #endif
2009
Dees_Troy38bd7602012-09-14 13:33:53 -04002010 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002011 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04002012 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002013 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00002014 LOGINFO("Recreating /data/media folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002015 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002016#ifdef HAVE_SELINUX
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05002017 perms.fixDataInternalContexts();
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002018#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002019 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002020 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002021 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002022 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002023}
Dees_Troye58d5262012-09-21 12:27:57 -04002024
2025void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002026 if (!Has_Android_Secure)
2027 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002028 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002029 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002030 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002031 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002032 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002033 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002034 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002035 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002036 }
2037}
bigbiff7cb4c332014-11-26 20:36:07 -05002038
2039uint64_t TWPartition::Get_Max_FileSize() {
2040 uint64_t maxFileSize = 0;
2041 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2042 const uint64_t constTB = (uint64_t) constGB * 1024;
2043 const uint64_t constPB = (uint64_t) constTB * 1024;
2044 const uint64_t constEB = (uint64_t) constPB * 1024;
2045
2046 if (Current_File_System == "ext4")
2047 maxFileSize = 16 * constTB; //16 TB
2048 else if (Current_File_System == "vfat")
2049 maxFileSize = 4 * constGB; //4 GB
2050 else if (Current_File_System == "ntfs")
2051 maxFileSize = 256 * constTB; //256 TB
2052 if (Current_File_System == "exfat")
2053 maxFileSize = 16 * constPB; //16 PB
2054 else if (Current_File_System == "ext3")
2055 maxFileSize = 2 * constTB; //2 TB
2056 else if (Current_File_System == "f2fs")
2057 maxFileSize = 3.94 * constTB; //3.94 TB
2058 else
2059 maxFileSize = 100000000L;
2060 return maxFileSize - 1;
2061}
2062