blob: e154564beeecc9ac360f6060d0a13102dd6c19ac [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002 Copyright 2013 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050027#include <iostream>
28#include <sstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040029
Dees_Troy657c3092012-09-10 20:32:10 -040030#ifdef TW_INCLUDE_CRYPTO
31 #include "cutils/properties.h"
32#endif
33
bigbiff7b4c7a62015-01-01 19:44:14 -050034#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000036#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040037#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040038#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040039#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050040#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041#include "twrpTar.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050042#include "twrpDU.hpp"
bigbiff bigbiff6b600f92014-01-05 18:13:43 -050043#include "fixPermissions.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050044#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060045#include "set_metadata.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040046extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040047 #include "mtdutils/mtdutils.h"
48 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000049#ifdef USE_EXT4
50 #include "make_ext4fs.h"
51#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060052
53#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060054 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker71413f42014-02-26 13:36:08 -060055#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040056}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040057#ifdef HAVE_SELINUX
58#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060059#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040060#endif
Dees Troy4159aed2014-02-28 17:24:43 +000061#ifdef HAVE_CAPABILITIES
62#include <sys/capability.h>
63#include <sys/xattr.h>
64#include <linux/xattr.h>
65#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040066
bigbiff bigbiff9c754052013-01-09 09:09:08 -050067using namespace std;
68
Dees_Troya95f55c2013-08-17 13:14:43 +000069extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050070extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000071
Hashcode62bd9e02013-11-19 21:59:42 -080072struct flag_list {
73 const char *name;
74 unsigned flag;
75};
76
77static struct flag_list mount_flags[] = {
78 { "noatime", MS_NOATIME },
79 { "noexec", MS_NOEXEC },
80 { "nosuid", MS_NOSUID },
81 { "nodev", MS_NODEV },
82 { "nodiratime", MS_NODIRATIME },
83 { "ro", MS_RDONLY },
84 { "rw", 0 },
85 { "remount", MS_REMOUNT },
86 { "bind", MS_BIND },
87 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000088#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080089 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000090#endif
91#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080092 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000093#endif
94#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -080095 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000096#endif
97#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -080098 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +000099#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800100 { "sync", MS_SYNCHRONOUS },
101 { "defaults", 0 },
102 { 0, 0 },
103};
104
that9e0593e2014-10-08 00:01:24 +0200105TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400106 Can_Be_Mounted = false;
107 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500108 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200109 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400110 Wipe_During_Factory_Reset = false;
111 Wipe_Available_in_GUI = false;
112 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400113 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400114 SubPartition_Of = "";
115 Symlink_Path = "";
116 Symlink_Mount_Point = "";
117 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400118 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400119 Actual_Block_Device = "";
120 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400121 Alternate_Block_Device = "";
122 Removable = false;
123 Is_Present = false;
124 Length = 0;
125 Size = 0;
126 Used = 0;
127 Free = 0;
128 Backup_Size = 0;
129 Can_Be_Encrypted = false;
130 Is_Encrypted = false;
131 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600132 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400133 Decrypted_Block_Device = "";
134 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500135 Backup_Display_Name = "";
136 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400137 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400138 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400139 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400140 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000141 Can_Encrypt_Backup = false;
142 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400143 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400144 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400145 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500146 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400147 Storage_Path = "";
148 Current_File_System = "";
149 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800150 Mount_Flags = 0;
151 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400152 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000153 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000154 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600155 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600156 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600157 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500158 Mount_Read_Only = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400159}
160
161TWPartition::~TWPartition(void) {
162 // Do nothing
163}
164
Dees_Troy5bf43922012-09-07 16:07:55 -0400165bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
166 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
167 int line_len = Line.size(), index = 0, item_index = 0;
168 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400169 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400170 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500171 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400172
Dees_Troy51127312012-09-08 13:08:49 -0400173 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500174 if (full_line[index] == 34)
175 skip = !skip;
176 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400177 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400178 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400179 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000180 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400181 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500182 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000183 Display_Name = full_line + 1;
184 Backup_Display_Name = Display_Name;
185 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400186 index = Mount_Point.size();
187 while (index < line_len) {
188 while (index < line_len && full_line[index] == '\0')
189 index++;
190 if (index >= line_len)
191 continue;
192 ptr = full_line + index;
193 if (item_index == 0) {
194 // File System
195 Fstab_File_System = ptr;
196 Current_File_System = ptr;
197 item_index++;
198 } else if (item_index == 1) {
199 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400200 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400201 MTD_Name = ptr;
202 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400203 } else if (Fstab_File_System == "bml") {
204 if (Mount_Point == "/boot")
205 MTD_Name = "boot";
206 else if (Mount_Point == "/recovery")
207 MTD_Name = "recovery";
208 Primary_Block_Device = ptr;
209 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000210 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 -0400211 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400212 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000213 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400214 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000215 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400216 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400217 } else {
218 Primary_Block_Device = ptr;
219 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400220 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400221 item_index++;
222 } else if (item_index > 1) {
223 if (*ptr == '/') {
224 // Alternate Block Device
225 Alternate_Block_Device = ptr;
226 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
227 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
228 // Partition length
229 ptr += 7;
230 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400231 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
232 // Custom flags, save for later so that new values aren't overwritten by defaults
233 ptr += 6;
234 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000235 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400236 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
237 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400238 } else {
239 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000240 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400241 }
242 }
243 while (index < line_len && full_line[index] != '\0')
244 index++;
245 }
246
247 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
248 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000249 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400250 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000251 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400252 return 0;
253 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400254 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400255 Setup_File_System(Display_Error);
256 if (Mount_Point == "/system") {
257 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500258 Backup_Display_Name = Display_Name;
259 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400260 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500261 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500262 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400263 } else if (Mount_Point == "/data") {
Dees Troyc657cc02015-01-16 22:48:47 +0000264 UnMount(false); // added in case /data is mounted as tmpfs for qcom hardware decrypt
Dees_Troy5bf43922012-09-07 16:07:55 -0400265 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500266 Backup_Display_Name = Display_Name;
267 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400268 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400269 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500270 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000271 Can_Encrypt_Backup = true;
272 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500273 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200274 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400275#ifdef TW_INCLUDE_CRYPTO
276 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400277 char crypto_blkdev[255];
278 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
279 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400280 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400281 DataManager::SetValue(TW_IS_DECRYPTED, 1);
282 Is_Encrypted = true;
283 Is_Decrypted = true;
284 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000285 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400286 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600287 if (Is_Present) {
Ethan Yonker253368a2014-11-25 15:00:52 -0600288 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 -0600289 if (cryptfs_check_footer() == 0) {
290 Is_Encrypted = true;
291 Is_Decrypted = false;
292 Can_Be_Mounted = false;
293 Current_File_System = "emmc";
294 Setup_Image(Display_Error);
295 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
Vojtech Bocek7e11ac52015-03-05 23:21:49 +0100296 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
Ethan Yonker71413f42014-02-26 13:36:08 -0600297 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
298 DataManager::SetValue("tw_crypto_display", "");
299 } else {
300 LOGERR("Could not mount /data and unable to find crypto footer.\n");
301 }
302 } else {
303 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
304 }
Gary Peck82599a82012-11-21 16:23:12 -0800305 } else {
306 // Filesystem is not encrypted and the mount
307 // succeeded, so get it back to the original
308 // unmounted state
309 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400310 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500311 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400312 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400313#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500314 if (datamedia)
315 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400316#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400317 } else if (Mount_Point == "/cache") {
318 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500319 Backup_Display_Name = Display_Name;
320 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400321 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400322 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500323 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400324 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000325 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500326 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500327 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400328 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400329 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400330 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400331 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500332 Backup_Display_Name = Display_Name;
333 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400334 Is_SubPartition = true;
335 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400336 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500337 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000338 Can_Encrypt_Backup = true;
339 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400340 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400341 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400342 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500343 Backup_Display_Name = Display_Name;
344 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400345 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400346 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500347 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000348 Can_Encrypt_Backup = true;
349 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400350 } else if (Mount_Point == "/boot") {
351 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500352 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400353 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500354 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400355 }
356#ifdef TW_EXTERNAL_STORAGE_PATH
357 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
358 Is_Storage = true;
359 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400360 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500361 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400362#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000363 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400364 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400365 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500366 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400367#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000368 }
Dees_Troy8170a922012-09-18 15:40:25 -0400369#ifdef TW_INTERNAL_STORAGE_PATH
370 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
371 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500372 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400373 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500374 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400375 }
376#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000377 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400378 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500379 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500380 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400381 }
382#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400383 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400384 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400385 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500386 if (Mount_Point == "/boot") {
387 Display_Name = "Boot";
388 Backup_Display_Name = Display_Name;
389 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600390 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500391 } else if (Mount_Point == "/recovery") {
392 Display_Name = "Recovery";
393 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600394 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500395 } else if (Mount_Point == "/system_image") {
396 Display_Name = "System Image";
397 Backup_Display_Name = Display_Name;
398 Can_Flash_Img = false;
399 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500400 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400401 }
402
Dees_Troy51127312012-09-08 13:08:49 -0400403 // Process any custom flags
404 if (Flags.size() > 0)
405 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400406 return true;
407}
408
Hashcode62bd9e02013-11-19 21:59:42 -0800409bool TWPartition::Process_FS_Flags(string& Options, int Flags) {
410 int i;
411 char *p;
412 char *savep;
413 char fs_options[250];
414
415 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
416 Options = "";
417
418 p = strtok_r(fs_options, ",", &savep);
419 while (p) {
420 /* Look for the flag "p" in the flag list "fl"
421 * If not found, the loop exits with fl[i].name being null.
422 */
423 for (i = 0; mount_flags[i].name; i++) {
424 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
425 Flags |= mount_flags[i].flag;
426 break;
427 }
428 }
429
430 if (!mount_flags[i].name) {
431 if (Options.size() > 0)
432 Options += ",";
433 Options += p;
434 }
435 p = strtok_r(NULL, ",", &savep);
436 }
437
438 return true;
439}
440
Dees_Troy51127312012-09-08 13:08:49 -0400441bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
442 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500443 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400444 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500445 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400446
447 strcpy(flags, Flags.c_str());
448 flags_len = Flags.size();
449 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500450 if (flags[index] == 34)
451 skip = !skip;
452 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400453 flags[index] = '\0';
454 }
455
456 index = 0;
457 while (index < flags_len) {
458 while (index < flags_len && flags[index] == '\0')
459 index++;
460 if (index >= flags_len)
461 continue;
462 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500463 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400464 if (strcmp(ptr, "removable") == 0) {
465 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600466 } else if (strncmp(ptr, "storage", 7) == 0) {
467 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600468 Is_Storage = true;
469 } else if (ptr_len == 9) {
470 ptr += 9;
471 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
472 LOGINFO("storage set to true\n");
473 Is_Storage = true;
474 } else {
475 LOGINFO("storage set to false\n");
476 Is_Storage = false;
477 }
478 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500479 } else if (strcmp(ptr, "settingsstorage") == 0) {
480 Is_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500481 } else if (strcmp(ptr, "andsec") == 0) {
482 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400483 } else if (strcmp(ptr, "canbewiped") == 0) {
484 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700485 } else if (strcmp(ptr, "usermrf") == 0) {
486 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500487 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
488 ptr += 7;
489 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
490 Can_Be_Backed_Up = true;
491 else
492 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400493 } else if (strcmp(ptr, "wipeingui") == 0) {
494 Can_Be_Wiped = true;
495 Wipe_Available_in_GUI = true;
496 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
497 Can_Be_Wiped = true;
498 Wipe_Available_in_GUI = true;
499 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500500 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000501 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400502 Is_SubPartition = true;
503 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000504 } else if (strcmp(ptr, "ignoreblkid") == 0) {
505 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000506 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
507 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500508 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400509 ptr += 8;
510 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500511 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
512 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400513 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500514 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400515 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500516 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
517 Display_Name.resize(Display_Name.size() - 1);
518 }
519 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
520 has_storage_name = true;
521 ptr += 11;
522 if (*ptr == '\"') ptr++;
523 Storage_Name = ptr;
524 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
525 Storage_Name.resize(Storage_Name.size() - 1);
526 }
527 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
528 has_backup_name = true;
529 ptr += 10;
530 if (*ptr == '\"') ptr++;
531 Backup_Display_Name = ptr;
532 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
533 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
534 }
535 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400536 ptr += 10;
Ethan Yonkera2719152015-05-28 09:44:41 -0500537 Format_Block_Size = (unsigned long)(atol(ptr));
Dees_Troya13d74f2013-03-24 08:54:55 -0500538 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400539 ptr += 7;
540 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000541 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
542 ptr += 17;
543 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
544 Can_Encrypt_Backup = true;
545 else
546 Can_Encrypt_Backup = false;
547 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
548 ptr += 21;
549 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
550 Can_Encrypt_Backup = true;
551 Use_Userdata_Encryption = true;
552 } else {
553 Use_Userdata_Encryption = false;
554 }
Hashcode62bd9e02013-11-19 21:59:42 -0800555 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
556 ptr += 8;
557 if (*ptr == '\"') ptr++;
558
559 Mount_Options = ptr;
560 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
561 Mount_Options.resize(Mount_Options.size() - 1);
562 }
563 Process_FS_Flags(Mount_Options, Mount_Flags);
Ethan Yonker253368a2014-11-25 15:00:52 -0600564 } else if ((ptr_len > 12 && strncmp(ptr, "encryptable=", 12) == 0) || (ptr_len > 13 && strncmp(ptr, "forceencrypt=", 13) == 0)) {
565 ptr += 12;
566 if (*ptr == '=') ptr++;
567 if (*ptr == '\"') ptr++;
568
569 Crypto_Key_Location = ptr;
570 if (Crypto_Key_Location.substr(Crypto_Key_Location.size() - 1, 1) == "\"") {
571 Crypto_Key_Location.resize(Crypto_Key_Location.size() - 1);
572 }
573 } else if (ptr_len > 8 && strncmp(ptr, "mounttodecrypt", 14) == 0) {
574 Mount_To_Decrypt = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600575 } else if (strncmp(ptr, "flashimg", 8) == 0) {
576 if (ptr_len == 8) {
577 Can_Flash_Img = true;
578 } else if (ptr_len == 10) {
579 ptr += 9;
580 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
581 Can_Flash_Img = true;
582 } else {
583 Can_Flash_Img = false;
584 }
585 }
Dees_Troy51127312012-09-08 13:08:49 -0400586 } else {
587 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000588 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400589 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000590 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400591 }
592 while (index < flags_len && flags[index] != '\0')
593 index++;
594 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500595 if (has_display_name && !has_storage_name)
596 Storage_Name = Display_Name;
597 if (!has_display_name && has_storage_name)
598 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000599 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500600 Backup_Display_Name = Display_Name;
601 if (!has_display_name && has_backup_name)
602 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400603 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400604}
605
Dees_Troy5bf43922012-09-07 16:07:55 -0400606bool TWPartition::Is_File_System(string File_System) {
607 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400608 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400609 File_System == "ext4" ||
610 File_System == "vfat" ||
611 File_System == "ntfs" ||
612 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500613 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000614 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400615 File_System == "auto")
616 return true;
617 else
618 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400619}
620
Dees_Troy5bf43922012-09-07 16:07:55 -0400621bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400622 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400623 return true;
624 else
625 return false;
626}
627
Dees_Troy51127312012-09-08 13:08:49 -0400628bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400629 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400630 if (mkdir(Path.c_str(), 0777) == -1) {
631 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000632 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400633 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000634 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400635 return false;
636 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000637 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400638 return true;
639 }
640 }
641 return true;
642}
643
Dees_Troy5bf43922012-09-07 16:07:55 -0400644void TWPartition::Setup_File_System(bool Display_Error) {
645 struct statfs st;
646
647 Can_Be_Mounted = true;
648 Can_Be_Wiped = true;
649
Dees_Troy5bf43922012-09-07 16:07:55 -0400650 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400651 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400652 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
653 Backup_Name = Display_Name;
654 Backup_Method = FILES;
655}
656
657void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400658 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
659 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800660 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400661 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800662 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400663 Backup_Method = FLASH_UTILS;
664 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000665 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 -0400666 if (Find_Partition_Size()) {
667 Used = Size;
668 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400669 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400670 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000671 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400672 else
Dees Troyd932ce12013-10-18 17:12:59 +0000673 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400674 }
675}
676
Dees_Troye58d5262012-09-21 12:27:57 -0400677void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500678 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400679 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500680 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400681 Has_Android_Secure = true;
682 Symlink_Path = Mount_Point + "/.android_secure";
683 Symlink_Mount_Point = "/and-sec";
684 Backup_Path = Symlink_Mount_Point;
685 Make_Dir("/and-sec", true);
686 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600687 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400688}
689
that9e0593e2014-10-08 00:01:24 +0200690void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500691 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
692 Storage_Name = "Internal Storage";
693 Has_Data_Media = true;
694 Is_Storage = true;
695 Is_Settings_Storage = true;
696 Storage_Path = "/data/media";
697 Symlink_Path = Storage_Path;
698 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
699 Make_Dir("/emmc", false);
700 Symlink_Mount_Point = "/emmc";
701 } else {
702 Make_Dir("/sdcard", false);
703 Symlink_Mount_Point = "/sdcard";
704 }
705 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
706 Storage_Path = "/data/media/0";
707 Symlink_Path = Storage_Path;
708 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
709 UnMount(true);
710 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500711 DataManager::SetValue("tw_has_internal", 1);
712 DataManager::SetValue("tw_has_data_media", 1);
713 du.add_absolute_dir("/data/media");
714}
715
Dees_Troy5bf43922012-09-07 16:07:55 -0400716void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
717 char device[512], realDevice[512];
718
719 strcpy(device, Block.c_str());
720 memset(realDevice, 0, sizeof(realDevice));
721 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
722 {
723 strcpy(device, realDevice);
724 memset(realDevice, 0, sizeof(realDevice));
725 }
726
727 if (device[0] != '/') {
728 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000729 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400730 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000731 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400732 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400733 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400734 Block = device;
735 return;
736 }
737}
738
Dees_Troy8e337f32012-10-13 22:07:49 -0400739void TWPartition::Mount_Storage_Retry(void) {
740 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500741 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400742 int retry_count = 5;
743 while (retry_count > 0 && !Mount(false)) {
744 usleep(500000);
745 retry_count--;
746 }
747 Mount(true);
748 }
749}
750
Dees_Troy38bd7602012-09-14 13:33:53 -0400751bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
752 FILE *fp = NULL;
753 char line[255];
754
755 fp = fopen("/proc/mtd", "rt");
756 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000757 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400758 return false;
759 }
760
761 while (fgets(line, sizeof(line), fp) != NULL)
762 {
763 char device[32], label[32];
764 unsigned long size = 0;
765 char* fstype = NULL;
766 int deviceId;
767
768 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
769
770 // Skip header and blank lines
771 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
772 continue;
773
774 // Strip off the trailing " from the label
775 label[strlen(label)-1] = '\0';
776
777 if (strcmp(label, MTD_Name.c_str()) == 0) {
778 // We found our device
779 // Strip off the trailing : from the device
780 device[strlen(device)-1] = '\0';
781 if (sscanf(device,"mtd%d", &deviceId) == 1) {
782 sprintf(device, "/dev/block/mtdblock%d", deviceId);
783 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000784 fclose(fp);
785 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400786 }
787 }
788 }
789 fclose(fp);
790
791 return false;
792}
793
Dees_Troy51127312012-09-08 13:08:49 -0400794bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
795 struct statfs st;
796 string Local_Path = Mount_Point + "/.";
797
798 if (!Mount(Display_Error))
799 return false;
800
801 if (statfs(Local_Path.c_str(), &st) != 0) {
802 if (!Removable) {
803 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000804 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400805 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000806 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400807 }
808 return false;
809 }
810 Size = (st.f_blocks * st.f_bsize);
811 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
812 Free = (st.f_bfree * st.f_bsize);
813 Backup_Size = Used;
814 return true;
815}
816
817bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400818 FILE* fp;
819 char command[255], line[512];
820 int include_block = 1;
821 unsigned int min_len;
822
823 if (!Mount(Display_Error))
824 return false;
825
Dees_Troy38bd7602012-09-14 13:33:53 -0400826 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400827 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200828 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400829 fp = fopen("/tmp/dfoutput.txt", "rt");
830 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000831 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400832 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400833 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400834
835 while (fgets(line, sizeof(line), fp) != NULL)
836 {
837 unsigned long blocks, used, available;
838 char device[64];
839 char tmpString[64];
840
841 if (strncmp(line, "Filesystem", 10) == 0)
842 continue;
843 if (strlen(line) < min_len) {
844 include_block = 0;
845 continue;
846 }
847 if (include_block) {
848 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
849 } else {
850 // The device block string is so long that the df information is on the next line
851 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400852 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400853 while (tmpString[space_count] == 32)
854 space_count++;
855 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
856 }
857
858 // Adjust block size to byte size
859 Size = blocks * 1024ULL;
860 Used = used * 1024ULL;
861 Free = available * 1024ULL;
862 Backup_Size = Used;
863 }
864 fclose(fp);
865 return true;
866}
867
Dees_Troy5bf43922012-09-07 16:07:55 -0400868bool TWPartition::Find_Partition_Size(void) {
869 FILE* fp;
870 char line[512];
871 string tmpdevice;
872
igoriok87e3d932013-01-31 21:03:53 +0200873 fp = fopen("/proc/dumchar_info", "rt");
874 if (fp != NULL) {
875 while (fgets(line, sizeof(line), fp) != NULL)
876 {
877 char label[32], device[32];
878 unsigned long size = 0;
879
thatd43bf2d2014-09-21 23:13:02 +0200880 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200881
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500882 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200883 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
884 continue;
885
886 tmpdevice = "/dev/";
887 tmpdevice += label;
888 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
889 Size = size;
890 fclose(fp);
891 return true;
892 }
893 }
894 }
895
Dees_Troy5bf43922012-09-07 16:07:55 -0400896 // In this case, we'll first get the partitions we care about (with labels)
897 fp = fopen("/proc/partitions", "rt");
898 if (fp == NULL)
899 return false;
900
901 while (fgets(line, sizeof(line), fp) != NULL)
902 {
903 unsigned long major, minor, blocks;
904 char device[512];
905 char tmpString[64];
906
Dees_Troy63c8df72012-09-10 14:02:05 -0400907 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400908 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
909
910 tmpdevice = "/dev/block/";
911 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400912 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400913 // Adjust block size to byte size
914 Size = blocks * 1024ULL;
915 fclose(fp);
916 return true;
917 }
918 }
919 fclose(fp);
920 return false;
921}
922
Dees_Troy5bf43922012-09-07 16:07:55 -0400923bool TWPartition::Is_Mounted(void) {
924 if (!Can_Be_Mounted)
925 return false;
926
927 struct stat st1, st2;
928 string test_path;
929
930 // Check to see if the mount point directory exists
931 test_path = Mount_Point + "/.";
932 if (stat(test_path.c_str(), &st1) != 0) return false;
933
934 // Check to see if the directory above the mount point exists
935 test_path = Mount_Point + "/../.";
936 if (stat(test_path.c_str(), &st2) != 0) return false;
937
938 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
939 int ret = (st1.st_dev != st2.st_dev) ? true : false;
940
941 return ret;
942}
943
944bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000945 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500946 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000947
Dees_Troy5bf43922012-09-07 16:07:55 -0400948 if (Is_Mounted()) {
949 return true;
950 } else if (!Can_Be_Mounted) {
951 return false;
952 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400953
954 Find_Actual_Block_Device();
955
956 // Check the current file system before mounting
957 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000958 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000959 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 +0000960 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000961 string result;
962 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000963 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000964 Current_File_System = "vfat";
965 } else {
966#ifdef TW_NO_EXFAT_FUSE
967 UnMount(false);
968 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
969 // Some kernels let us mount vfat as exfat which doesn't work out too well
970#else
971 exfat_mounted = 1;
972#endif
973 }
974 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500975
976 if (Mount_Read_Only)
977 flags |= MS_RDONLY;
978
Dees_Troy22042032012-12-18 21:23:08 +0000979 if (Fstab_File_System == "yaffs2") {
980 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500981 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
982 if (Mount_Read_Only)
983 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +0000984 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
985 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
986 if (Display_Error)
987 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
988 else
989 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
990 return false;
991 } else {
992 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
993 return true;
994 }
995 } else {
996 struct stat st;
997 string test_path = Mount_Point;
998 if (stat(test_path.c_str(), &st) < 0) {
999 if (Display_Error)
1000 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1001 else
1002 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1003 return false;
1004 }
1005 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1006 if (new_mode != st.st_mode) {
1007 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1008 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1009 if (Display_Error)
1010 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1011 else
1012 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1013 return false;
1014 }
1015 }
Dees_Troy22042032012-12-18 21:23:08 +00001016 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001017 }
thatc7572eb2015-03-31 18:55:30 +02001018 }
1019
1020 string mount_fs = Current_File_System;
1021 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1022 mount_fs = "texfat";
1023
1024 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001025 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1026 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001027#ifdef TW_NO_EXFAT_FUSE
1028 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001029 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001030 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001031 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001032 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001033 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001034 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001035 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(), flags, Mount_Options.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001036 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001037 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001038 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001039#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001040 if (!Removable && Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001041 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001042 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001043 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1044 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 +00001045 return false;
1046#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001047 }
1048#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001049 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001050
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001051 if (Removable)
1052 Update_Size(Display_Error);
1053
xiaolu9416f4f2015-06-04 08:22:23 +08001054 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001055 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001056 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001057 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001058 return true;
1059}
1060
1061bool TWPartition::UnMount(bool Display_Error) {
1062 if (Is_Mounted()) {
1063 int never_unmount_system;
1064
1065 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1066 if (never_unmount_system == 1 && Mount_Point == "/system")
1067 return true; // Never unmount system if you're not supposed to unmount it
1068
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001069 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001070 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001071
Dees_Troy38bd7602012-09-14 13:33:53 -04001072 if (!Symlink_Mount_Point.empty())
1073 umount(Symlink_Mount_Point.c_str());
1074
Dees_Troyb05ddee2013-01-28 20:24:50 +00001075 umount(Mount_Point.c_str());
1076 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001077 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001078 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001079 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001080 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001081 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001082 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001083 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001084 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001085 } else {
1086 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001087 }
1088}
1089
Gary Peck43acadf2012-11-21 21:19:01 -08001090bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001091 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001092 int check;
1093 string Layout_Filename = Mount_Point + "/.layout_version";
1094
Dees_Troy38bd7602012-09-14 13:33:53 -04001095 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001096 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001097 return false;
1098 }
1099
Dees_Troyc51f1f92012-09-20 15:32:13 -04001100 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001101 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001102
Dees_Troy16c2b312013-01-15 16:51:18 +00001103 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1104 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1105 else
1106 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001107
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001108 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001109 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001110 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001111 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001112 DataManager::GetValue(TW_RM_RF_VAR, check);
1113
Hashcodedabfd492013-08-29 22:45:30 -07001114 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001115 wiped = Wipe_RMRF();
1116 else if (New_File_System == "ext4")
1117 wiped = Wipe_EXT4();
1118 else if (New_File_System == "ext2" || New_File_System == "ext3")
1119 wiped = Wipe_EXT23(New_File_System);
1120 else if (New_File_System == "vfat")
1121 wiped = Wipe_FAT();
1122 else if (New_File_System == "exfat")
1123 wiped = Wipe_EXFAT();
1124 else if (New_File_System == "yaffs2")
1125 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001126 else if (New_File_System == "f2fs")
1127 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001128 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001129 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 +00001130 unlink("/.layout_version");
1131 return false;
1132 }
1133 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001134 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001135
Gary Pecke8bc5d72012-12-21 06:45:25 -08001136 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001137 if (Mount_Point == "/cache")
1138 DataManager::Output_Version();
1139
Dees_Troy16c2b312013-01-15 16:51:18 +00001140 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1141 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1142
1143 if (update_crypt) {
1144 Setup_File_System(false);
1145 if (Is_Encrypted && !Is_Decrypted) {
1146 // just wiped an encrypted partition back to its unencrypted state
1147 Is_Encrypted = false;
1148 Is_Decrypted = false;
1149 Decrypted_Block_Device = "";
1150 if (Mount_Point == "/data") {
1151 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1152 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1153 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001154 }
1155 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001156
1157 if (Mount_Point == "/data" && Has_Data_Media && recreate_media) {
1158 Recreate_Media_Folder();
1159 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001160 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001161 if (Is_Storage) {
1162 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001163 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001164 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001165}
1166
Gary Peck43acadf2012-11-21 21:19:01 -08001167bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001168 if (Is_File_System(Current_File_System))
1169 return Wipe(Current_File_System);
1170 else
1171 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001172}
1173
Dees_Troye58d5262012-09-21 12:27:57 -04001174bool TWPartition::Wipe_AndSec(void) {
1175 if (!Has_Android_Secure)
1176 return false;
1177
Dees_Troye58d5262012-09-21 12:27:57 -04001178 if (!Mount(true))
1179 return false;
1180
Dees_Troy2673cec2013-04-02 20:22:16 +00001181 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001182 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001183 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001184}
1185
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001186bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001187 if (Mount_Read_Only)
1188 return false;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001189 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/dosfsck"))
1190 return true;
1191 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1192 return true;
1193 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1194 return true;
1195 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1196 return true;
1197 return false;
1198}
1199
1200bool TWPartition::Repair() {
1201 string command;
1202
1203 if (Current_File_System == "vfat") {
1204 if (!TWFunc::Path_Exists("/sbin/dosfsck")) {
1205 gui_print("dosfsck does not exist! Cannot repair!\n");
1206 return false;
1207 }
1208 if (!UnMount(true))
1209 return false;
1210 gui_print("Repairing %s using dosfsck...\n", Display_Name.c_str());
1211 Find_Actual_Block_Device();
1212 command = "/sbin/dosfsck -y " + 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 == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1223 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
1224 gui_print("e2fsck does not exist! Cannot repair!\n");
1225 return false;
1226 }
1227 if (!UnMount(true))
1228 return false;
1229 gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str());
1230 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001231 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001232 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 if (Current_File_System == "exfat") {
1242 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
1243 gui_print("fsck.exfat does not exist! Cannot repair!\n");
1244 return false;
1245 }
1246 if (!UnMount(true))
1247 return false;
1248 gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str());
1249 Find_Actual_Block_Device();
1250 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1251 LOGINFO("Repair command: %s\n", command.c_str());
1252 if (TWFunc::Exec_Cmd(command) == 0) {
1253 gui_print("Done.\n");
1254 return true;
1255 } else {
1256 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1257 return false;
1258 }
1259 }
1260 if (Current_File_System == "f2fs") {
1261 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
1262 gui_print("fsck.f2fs does not exist! Cannot repair!\n");
1263 return false;
1264 }
1265 if (!UnMount(true))
1266 return false;
1267 gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str());
1268 Find_Actual_Block_Device();
1269 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1270 LOGINFO("Repair command: %s\n", command.c_str());
1271 if (TWFunc::Exec_Cmd(command) == 0) {
1272 gui_print("Done.\n");
1273 return true;
1274 } else {
1275 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1276 return false;
1277 }
1278 }
1279 return false;
1280}
1281
Ethan Yonkera2719152015-05-28 09:44:41 -05001282bool TWPartition::Can_Resize() {
1283 if (Mount_Read_Only)
1284 return false;
1285 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1286 return true;
1287 return false;
1288}
1289
1290bool TWPartition::Resize() {
1291 string command;
1292
1293 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1294 if (!Can_Repair()) {
1295 LOGERR("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1296 return false;
1297 }
1298 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
1299 gui_print("resize2fs does not exist! Cannot resize!\n");
1300 return false;
1301 }
1302 // Repair will unmount so no need to do it twice
1303 gui_print("Repairing %s before resizing.\n", Display_Name.c_str());
1304 if (!Repair())
1305 return false;
1306 gui_print("Resizing %s using resize2fs...\n", Display_Name.c_str());
1307 Find_Actual_Block_Device();
1308 command = "/sbin/resize2fs " + Actual_Block_Device;
1309 if (Length != 0) {
1310 unsigned int block_device_size;
1311 int fd, ret;
1312
1313 fd = open(Actual_Block_Device.c_str(), O_RDONLY);
1314 if (fd < 0) {
1315 LOGERR("Resize: Failed to open '%s'\n", Actual_Block_Device.c_str());
1316 return false;
1317 }
1318 ret = ioctl(fd, BLKGETSIZE, &block_device_size);
1319 close(fd);
1320 if (ret) {
1321 LOGERR("Resize: ioctl error\n");
1322 return false;
1323 }
1324 unsigned long long Actual_Size = (unsigned long long)(block_device_size) * 512LLU;
1325 unsigned long long Block_Count;
1326 if (Length < 0) {
1327 // Reduce overall size by this length
1328 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1329 } else {
1330 // This is the size, not a size reduction
1331 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1332 }
1333 char temp[256];
1334 sprintf(temp, "%llu", Block_Count);
1335 command += " ";
1336 command += temp;
1337 command += "K";
1338 }
1339 LOGINFO("Resize command: %s\n", command.c_str());
1340 if (TWFunc::Exec_Cmd(command) == 0) {
1341 Update_Size(true);
1342 gui_print("Done.\n");
1343 return true;
1344 } else {
1345 Update_Size(true);
1346 LOGERR("Unable to resize '%s'.\n", Mount_Point.c_str());
1347 return false;
1348 }
1349 }
1350 return false;
1351}
1352
bigbiff7abc5fe2015-01-17 16:53:12 -05001353bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
1354 if (Backup_Method == FILES) {
1355 return Backup_Tar(backup_folder, overall_size, other_backups_size, tar_fork_pid);
1356 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001357 else if (Backup_Method == DD)
1358 return Backup_DD(backup_folder);
1359 else if (Backup_Method == FLASH_UTILS)
1360 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001361 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001362 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001363}
1364
Dees_Troy43d8b002012-09-17 16:00:01 -04001365bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001366 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001367 char split_filename[512];
1368 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001369 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001370
Captain Throwbackff5935f2014-09-23 16:08:34 -04001371 sync();
1372
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001373 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001374 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001375 if (!TWFunc::Path_Exists(Full_Filename)) {
1376 // This is a split archive, we presume
1377 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001378 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001379 md5file = split_filename;
1380 md5file += ".md5";
1381 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001382 LOGERR("No md5 file found for '%s'.\n", split_filename);
1383 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001384 return false;
1385 }
1386 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001387 while (index < 1000) {
1388 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001389 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001390 return false;
1391 }
1392 index++;
1393 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001394 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001395 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001396 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001397 } else {
1398 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001399 md5file = Full_Filename + ".md5";
1400 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001401 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1402 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001403 return false;
1404 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001405 md5sum.setfn(Full_Filename);
1406 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001407 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001408 return false;
1409 } else
1410 return true;
1411 }
1412 return false;
1413}
1414
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001415bool 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 -08001416 string Restore_File_System;
1417
1418 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001419 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001420
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001421 Restore_File_System = Get_Restore_File_System(restore_folder);
1422
1423 if (Is_File_System(Restore_File_System))
1424 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1425 else if (Is_Image(Restore_File_System)) {
Ethan Yonkerf66324f2015-01-09 16:46:57 -06001426 return Restore_Image(restore_folder, total_restore_size, already_restored_size, Restore_File_System);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001427 }
1428
1429 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1430 return false;
1431}
1432
1433string TWPartition::Get_Restore_File_System(string restore_folder) {
1434 size_t first_period, second_period;
1435 string Restore_File_System;
1436
Gary Peck43acadf2012-11-21 21:19:01 -08001437 // Parse backup filename to extract the file system before wiping
1438 first_period = Backup_FileName.find(".");
1439 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001440 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001441 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001442 }
1443 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1444 second_period = Restore_File_System.find(".");
1445 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001446 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001447 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001448 }
1449 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001450 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001451 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001452}
1453
1454string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001455 if (Backup_Method == NONE)
1456 return "none";
1457 else if (Backup_Method == FILES)
1458 return "files";
1459 else if (Backup_Method == DD)
1460 return "dd";
1461 else if (Backup_Method == FLASH_UTILS)
1462 return "flash_utils";
1463 else
1464 return "undefined";
1465 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001466}
1467
1468bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001469 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001470 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001471 return 1;
1472}
1473
1474bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001475 bool Save_Data_Media = Has_Data_Media;
1476
1477 if (!UnMount(true))
1478 return false;
1479
Dees_Troy38bd7602012-09-14 13:33:53 -04001480 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001481 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001482#ifdef TW_INCLUDE_CRYPTO
1483 if (Is_Decrypted) {
1484 if (!UnMount(true))
1485 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001486 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001487 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1488 }
1489 }
1490#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001491 Is_Decrypted = false;
1492 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001493 Find_Actual_Block_Device();
Gary Pecke8bc5d72012-12-21 06:45:25 -08001494 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001495 Has_Data_Media = Save_Data_Media;
1496 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1497 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001498 if (Mount(false))
1499 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001500 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001501 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001502#ifndef TW_OEM_BUILD
Dees_Troy2673cec2013-04-02 20:22:16 +00001503 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Ethan Yonker83e82572014-04-04 10:59:28 -05001504#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001505 return true;
1506 } else {
1507 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001508 LOGERR("Unable to format to remove encryption.\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06001509 if (Has_Data_Media && Mount(false))
1510 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001511 return false;
1512 }
1513 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001514}
1515
1516void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001517 const char* type;
1518 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001519
Dees_Troy68cab492012-12-12 19:29:35 +00001520 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1521 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001522
Dees_Troy38bd7602012-09-14 13:33:53 -04001523 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001524 if (!Is_Present)
1525 return;
Dees_Troy51127312012-09-08 13:08:49 -04001526
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001527 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1528 if (blkid_do_fullprobe(pr)) {
1529 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001530 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001531 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001532 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001533
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001534 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001535 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001536 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001537 return;
1538 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001539
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001540 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001541 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001542}
1543
Gary Peck43acadf2012-11-21 21:19:01 -08001544bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001545 if (!UnMount(true))
1546 return false;
1547
Dees_Troy43d8b002012-09-17 16:00:01 -04001548 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001549 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001550
Dees_Troy2673cec2013-04-02 20:22:16 +00001551 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001552 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001553 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001554 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001555 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001556 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001557 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001558 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001559 return true;
1560 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001561 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001562 return false;
1563 }
1564 } else
1565 return Wipe_RMRF();
1566
1567 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001568}
1569
1570bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001571 Find_Actual_Block_Device();
1572 if (!Is_Present) {
1573 LOGERR("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1574 return false;
1575 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001576 if (!UnMount(true))
1577 return false;
1578
Dees_Troyb3265ab2013-08-30 02:59:14 +00001579#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001580 int ret;
1581 char *secontext = NULL;
1582
Dees_Troya95f55c2013-08-17 13:14:43 +00001583 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001584
Dees Troy99c8dbf2014-03-10 16:53:58 +00001585 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001586 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1587 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1588 } else {
1589 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1590 }
1591 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001592 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1593 return false;
1594 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001595 string sedir = Mount_Point + "/lost+found";
1596 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1597 rmdir(sedir.c_str());
1598 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001599 return true;
1600 }
1601#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001602 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001603 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001604
Dees_Troy2673cec2013-04-02 20:22:16 +00001605 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001606 Find_Actual_Block_Device();
1607 Command = "make_ext4fs";
1608 if (!Is_Decrypted && Length != 0) {
1609 // Only use length if we're not decrypted
1610 char len[32];
1611 sprintf(len, "%i", Length);
1612 Command += " -l ";
1613 Command += len;
1614 }
Dees_Troy5295d582013-09-06 15:51:08 +00001615 if (TWFunc::Path_Exists("/file_contexts")) {
1616 Command += " -S /file_contexts";
1617 }
1618 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001619 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001620 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001621 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001622 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001623 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001624 return true;
1625 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001626 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001627 return false;
1628 }
1629 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001630 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001631#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001632 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001633}
1634
1635bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001636 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001637
Dees_Troy43d8b002012-09-17 16:00:01 -04001638 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001639 if (!UnMount(true))
1640 return false;
1641
Dees_Troy2673cec2013-04-02 20:22:16 +00001642 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001643 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001644 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001645 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001646 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001647 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001648 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001649 return true;
1650 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001651 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001652 return false;
1653 }
1654 return true;
1655 }
1656 else
1657 return Wipe_RMRF();
1658
1659 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001660}
1661
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001662bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001663 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001664
1665 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1666 if (!UnMount(true))
1667 return false;
1668
Dees_Troy2673cec2013-04-02 20:22:16 +00001669 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001670 Find_Actual_Block_Device();
1671 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001672 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001673 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001674 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001675 return true;
1676 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001677 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001678 return false;
1679 }
1680 return true;
1681 }
1682 return false;
1683}
1684
Dees_Troy38bd7602012-09-14 13:33:53 -04001685bool TWPartition::Wipe_MTD() {
1686 if (!UnMount(true))
1687 return false;
1688
Dees_Troy2673cec2013-04-02 20:22:16 +00001689 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001690
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001691 mtd_scan_partitions();
1692 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1693 if (mtd == NULL) {
1694 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1695 return false;
1696 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001697
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001698 MtdWriteContext* ctx = mtd_write_partition(mtd);
1699 if (ctx == NULL) {
1700 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1701 return false;
1702 }
1703 if (mtd_erase_blocks(ctx, -1) == -1) {
1704 mtd_write_close(ctx);
1705 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1706 return false;
1707 }
1708 if (mtd_write_close(ctx) != 0) {
1709 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1710 return false;
1711 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001712 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001713 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001714 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001715 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001716}
1717
1718bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001719 if (!Mount(true))
1720 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06001721 // This is the only wipe that leaves the partition mounted, so we
1722 // must manually remove the partition from MTP if it is a storage
1723 // partition.
1724 if (Is_Storage)
1725 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001726
Dees_Troy2673cec2013-04-02 20:22:16 +00001727 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001728 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001729 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001730 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001731}
1732
Dees_Troye5017042013-08-29 16:38:55 +00001733bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001734 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001735
1736 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1737 if (!UnMount(true))
1738 return false;
1739
1740 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1741 Find_Actual_Block_Device();
dhacker29a3fa75f2015-01-17 19:42:33 -05001742 command = "mkfs.f2fs -t 1";
1743 if (!Is_Decrypted && Length != 0) {
1744 // Only use length if we're not decrypted
1745 char len[32];
1746 int mod_length = Length;
1747 if (Length < 0)
1748 mod_length *= -1;
1749 sprintf(len, "%i", mod_length);
1750 command += " -r ";
1751 command += len;
1752 }
1753 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001754 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001755 Recreate_AndSec_Folder();
1756 gui_print("Done.\n");
1757 return true;
1758 } else {
1759 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1760 return false;
1761 }
1762 return true;
1763 } else {
1764 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1765 return Wipe_RMRF();
1766 }
1767 return false;
1768}
1769
Dees_Troy51a0e822012-09-05 15:24:24 -04001770bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001771#ifdef TW_OEM_BUILD
1772 // In an OEM Build we want to do a full format
1773 return Wipe_Encryption();
1774#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001775 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001776
1777 // This handles wiping data on devices with "sdcard" in /data/media
1778 if (!Mount(true))
1779 return false;
1780
Dees_Troy2673cec2013-04-02 20:22:16 +00001781 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001782
1783 DIR* d;
1784 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001785 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001786 struct dirent* de;
1787 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001788 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001789 // The media folder is the "internal sdcard"
1790 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001791 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001792 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001793 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001794
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001795 dir = "/data/";
1796 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001797 if (de->d_type == DT_DIR) {
1798 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001799 } 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 +00001800 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001801 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001802 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001803 }
1804 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001805
Dees_Troy2673cec2013-04-02 20:22:16 +00001806 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001807 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001808 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001809 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001810 return false;
Ethan Yonker83e82572014-04-04 10:59:28 -05001811#endif // ifdef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001812}
1813
bigbiff7abc5fe2015-01-17 16:53:12 -05001814bool TWPartition::Backup_Tar(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001815 char back_name[255], split_index[5];
1816 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001817 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001818 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001819 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001820 twrpTar tar;
1821 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001822
Dees_Troy43d8b002012-09-17 16:00:01 -04001823 if (!Mount(true))
1824 return false;
1825
Dees_Troya13d74f2013-03-24 08:54:55 -05001826 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001827 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001828
1829 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001830 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001831
Dees_Troy83bd4832013-05-04 12:39:56 +00001832#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1833 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1834 if (use_encryption && Can_Encrypt_Backup) {
1835 tar.use_encryption = use_encryption;
1836 if (Use_Userdata_Encryption)
1837 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001838 string Password;
1839 DataManager::GetValue("tw_backup_password", Password);
1840 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001841 } else {
1842 use_encryption = false;
1843 }
1844#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001845
1846 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1847 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001848 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001849 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001850 Full_FileName = backup_folder + "/" + Backup_FileName;
1851 tar.setdir(Backup_Path);
1852 tar.setfn(Full_FileName);
1853 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001854 tar.partition_name = Backup_Name;
1855 tar.backup_folder = backup_folder;
bigbiff7abc5fe2015-01-17 16:53:12 -05001856 if (tar.createTarFork(overall_size, other_backups_size, tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00001857 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001858 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001859}
1860
1861bool TWPartition::Backup_DD(string backup_folder) {
codelover352b75e2015-03-29 02:44:07 +08001862 char back_name[255], block_size[32], dd_count[32];
1863 string Full_FileName, Command, DD_BS, DD_COUNT;
Dees_Troy43d8b002012-09-17 16:00:01 -04001864 int use_compression;
codelover352b75e2015-03-29 02:44:07 +08001865 unsigned long long DD_Block_Size, DD_Count;
Dees_Troy43d8b002012-09-17 16:00:01 -04001866
codelover352b75e2015-03-29 02:44:07 +08001867 DD_Block_Size = 16 * 1024 * 1024;
1868 while (Backup_Size % DD_Block_Size != 0) DD_Block_Size >>= 1;
1869
1870 DD_Count = Backup_Size / DD_Block_Size;
1871
1872 sprintf(dd_count, "%llu", DD_Count);
1873 DD_COUNT = dd_count;
1874
1875 sprintf(block_size, "%llu", DD_Block_Size);
1876 DD_BS = block_size;
igoriok87e3d932013-01-31 21:03:53 +02001877
Dees_Troyb46a6842012-09-25 11:06:46 -04001878 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001879 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001880
1881 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1882 Backup_FileName = back_name;
1883
1884 Full_FileName = backup_folder + "/" + Backup_FileName;
1885
codelover352b75e2015-03-29 02:44:07 +08001886 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + " count=" + DD_COUNT;
Dees_Troy2673cec2013-04-02 20:22:16 +00001887 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001888 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001889 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04001890 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001891 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001892 return false;
1893 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001894 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001895}
1896
1897bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001898 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001899 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001900 int use_compression;
1901
Dees_Troyb46a6842012-09-25 11:06:46 -04001902 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001903 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001904
1905 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1906 Backup_FileName = back_name;
1907
1908 Full_FileName = backup_folder + "/" + Backup_FileName;
1909
1910 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001911 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001912 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001913 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001914 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1915 // 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 +00001916 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001917 return false;
1918 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001919 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001920}
1921
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001922unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
1923 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
1924 if (restore_info.LoadValues() == 0) {
1925 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
1926 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
1927 return Restore_Size;
1928 }
1929 }
1930 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
1931
1932 Full_FileName = restore_folder + "/" + Backup_FileName;
1933
1934 if (Is_Image(Restore_File_System)) {
1935 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
1936 return Restore_Size;
1937 }
1938
1939 twrpTar tar;
1940 tar.setdir(Backup_Path);
1941 tar.setfn(Full_FileName);
1942 tar.backup_name = Backup_Name;
1943#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1944 string Password;
1945 DataManager::GetValue("tw_restore_password", Password);
1946 if (!Password.empty())
1947 tar.setpassword(Password);
1948#endif
1949 tar.partition_name = Backup_Name;
1950 tar.backup_folder = restore_folder;
1951 Restore_Size = tar.get_size();
1952 return Restore_Size;
1953}
1954
1955bool 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 -08001956 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001957 int index = 0;
1958 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00001959 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001960
Dees_Troye58d5262012-09-21 12:27:57 -04001961 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001962 if (!Wipe_AndSec())
1963 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001964 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001965 gui_print("Wiping %s...\n", Display_Name.c_str());
Ethan Yonker5eac2222014-06-11 12:22:55 -05001966 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
1967 gui_print("WARNING: This /data backup was made with %s file system!\n", Restore_File_System.c_str());
1968 gui_print("The backup may not boot unless you change back to %s.\n", Restore_File_System.c_str());
1969 if (!Wipe_Data_Without_Wiping_Media())
1970 return false;
1971 } else {
1972 if (!Wipe(Restore_File_System))
1973 return false;
1974 }
Dees_Troye58d5262012-09-21 12:27:57 -04001975 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001976 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001977 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001978
1979 if (!Mount(true))
1980 return false;
1981
Dees_Troy4a2a1262012-09-18 09:33:47 -04001982 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06001983 twrpTar tar;
1984 tar.setdir(Backup_Path);
1985 tar.setfn(Full_FileName);
1986 tar.backup_name = Backup_Name;
1987#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1988 string Password;
1989 DataManager::GetValue("tw_restore_password", Password);
1990 if (!Password.empty())
1991 tar.setpassword(Password);
1992#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001993 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00001994 ret = false;
1995 else
1996 ret = true;
1997#ifdef HAVE_CAPABILITIES
1998 // Restore capabilities to the run-as binary
1999 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2000 struct vfs_cap_data cap_data;
2001 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2002
2003 memset(&cap_data, 0, sizeof(cap_data));
2004 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2005 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2006 cap_data.data[0].inheritable = 0;
2007 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2008 cap_data.data[1].inheritable = 0;
2009 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2010 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2011 } else {
2012 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2013 }
2014 }
2015#endif
2016 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002017}
2018
Ethan Yonker96af84a2015-01-05 14:58:36 -06002019bool TWPartition::Restore_Image(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size, string Restore_File_System) {
2020 string Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002021 double display_percent, progress_percent;
2022 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04002023
Dees_Troyda8b55a2012-12-12 19:18:30 +00002024 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04002025 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002026
Ethan Yonker96af84a2015-01-05 14:58:36 -06002027 if (Restore_File_System == "emmc") {
2028 if (!Flash_Image_DD(Full_FileName))
2029 return false;
2030 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
2031 if (!Flash_Image_FI(Full_FileName))
2032 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002033 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002034 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
2035 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
2036 DataManager::SetValue("tw_size_progress", size_progress);
2037 progress_percent = (display_percent / 100);
2038 DataManager::SetProgress((float)(progress_percent));
2039 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04002040 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002041}
Dees_Troy5bf43922012-09-07 16:07:55 -04002042
2043bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002044 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002045
Dees_Troyab10ee22012-09-21 14:27:30 -04002046 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002047 return false;
2048
Dees_Troy0550cfb2012-10-13 11:56:13 -04002049 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002050 if (Removable || Is_Encrypted) {
2051 if (!Mount(false))
2052 return true;
2053 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002054 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002055
2056 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002057 if (!ret || Size == 0) {
2058 if (!Get_Size_Via_df(Display_Error)) {
2059 if (!Was_Already_Mounted)
2060 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002061 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002062 }
2063 }
Dees_Troy51127312012-09-08 13:08:49 -04002064
Dees_Troy5bf43922012-09-07 16:07:55 -04002065 if (Has_Data_Media) {
2066 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002067 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002068 Used = du.Get_Folder_Size("/data");
2069 Backup_Size = Used;
2070 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002071 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002072 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002073 } else {
2074 if (!Was_Already_Mounted)
2075 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002076 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002077 }
Dees_Troye58d5262012-09-21 12:27:57 -04002078 } else if (Has_Android_Secure) {
2079 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002080 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002081 else {
2082 if (!Was_Already_Mounted)
2083 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002084 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002085 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002086 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002087 if (!Was_Already_Mounted)
2088 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002089 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002090}
Dees_Troy38bd7602012-09-14 13:33:53 -04002091
2092void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002093 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002094 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002095 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002096 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002097 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002098 Is_Present = true;
2099 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002100 return;
2101 }
2102 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002103 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002104 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002105 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002106 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002107 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002108 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002109}
2110
2111void TWPartition::Recreate_Media_Folder(void) {
2112 string Command;
2113
2114 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002115 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04002116 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002117 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00002118 LOGINFO("Recreating /data/media folder.\n");
xiaolu9416f4f2015-06-04 08:22:23 +08002119 mkdir("/data/media", 0770);
2120 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2121 if (!Internal_path.empty()) {
2122 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2123 mkdir(Internal_path.c_str(), 0770);
2124 }
2125#ifdef TW_INTERNAL_STORAGE_PATH
2126 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2127#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002128#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002129 // Afterwards, we will try to set the
2130 // default metadata that we were hopefully able to get during
2131 // early boot.
2132 tw_set_default_metadata("/data/media");
xiaolu9416f4f2015-06-04 08:22:23 +08002133 if (!Internal_path.empty())
2134 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002135#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002136 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002137 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002138 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002139 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002140}
Dees_Troye58d5262012-09-21 12:27:57 -04002141
2142void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002143 if (!Has_Android_Secure)
2144 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002145 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002146 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002147 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002148 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002149 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002150 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002151 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002152 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002153 }
2154}
bigbiff7cb4c332014-11-26 20:36:07 -05002155
2156uint64_t TWPartition::Get_Max_FileSize() {
2157 uint64_t maxFileSize = 0;
2158 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2159 const uint64_t constTB = (uint64_t) constGB * 1024;
2160 const uint64_t constPB = (uint64_t) constTB * 1024;
2161 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002162 if (Current_File_System == "ext4")
2163 maxFileSize = 16 * constTB; //16 TB
2164 else if (Current_File_System == "vfat")
2165 maxFileSize = 4 * constGB; //4 GB
2166 else if (Current_File_System == "ntfs")
2167 maxFileSize = 256 * constTB; //256 TB
2168 else if (Current_File_System == "exfat")
2169 maxFileSize = 16 * constPB; //16 PB
2170 else if (Current_File_System == "ext3")
2171 maxFileSize = 2 * constTB; //2 TB
2172 else if (Current_File_System == "f2fs")
2173 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002174 else
2175 maxFileSize = 100000000L;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002176 LOGINFO("Get_Max_FileSize::maxFileSize: %llu\n", maxFileSize);
bigbiff7cb4c332014-11-26 20:36:07 -05002177 return maxFileSize - 1;
2178}
2179
Ethan Yonker96af84a2015-01-05 14:58:36 -06002180bool TWPartition::Flash_Image(string Filename) {
2181 string Restore_File_System;
2182
2183 LOGINFO("Image filename is: %s\n", Filename.c_str());
2184
2185 if (Backup_Method == FILES) {
2186 LOGERR("Cannot flash images to file systems\n");
2187 return false;
2188 } else if (!Can_Flash_Img) {
2189 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2190 return false;
2191 } else {
2192 if (!Find_Partition_Size()) {
2193 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2194 return false;
2195 }
2196 unsigned long long image_size = TWFunc::Get_File_Size(Filename);
2197 if (image_size > Size) {
2198 LOGERR("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
2199 image_size, Filename.c_str(), Actual_Block_Device.c_str(), Size);
2200 return false;
2201 }
2202 if (Backup_Method == DD)
2203 return Flash_Image_DD(Filename);
2204 else if (Backup_Method == FLASH_UTILS)
2205 return Flash_Image_FI(Filename);
2206 }
2207
2208 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2209 return false;
2210}
2211
2212bool TWPartition::Flash_Image_DD(string Filename) {
2213 string Command;
2214
2215 gui_print("Flashing %s...\n", Display_Name.c_str());
codelover352b75e2015-03-29 02:44:07 +08002216 Command = "dd bs=8388608 if='" + Filename + "' of=" + Actual_Block_Device;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002217 LOGINFO("Flash command: '%s'\n", Command.c_str());
2218 TWFunc::Exec_Cmd(Command);
2219 return true;
2220}
2221
2222bool TWPartition::Flash_Image_FI(string Filename) {
2223 string Command;
2224
2225 gui_print("Flashing %s...\n", Display_Name.c_str());
2226 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2227 Command = "erase_image " + MTD_Name;
2228 LOGINFO("Erase command: '%s'\n", Command.c_str());
2229 TWFunc::Exec_Cmd(Command);
2230 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2231 LOGINFO("Flash command: '%s'\n", Command.c_str());
2232 TWFunc::Exec_Cmd(Command);
2233 return true;
2234}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002235
2236void TWPartition::Change_Mount_Read_Only(bool new_value) {
2237 Mount_Read_Only = new_value;
2238}
2239
2240int TWPartition::Check_Lifetime_Writes() {
2241 bool original_read_only = Mount_Read_Only;
2242 int ret = 1;
2243
2244 Mount_Read_Only = true;
2245 if (Mount(false)) {
2246 Find_Actual_Block_Device();
2247 string block = basename(Actual_Block_Device.c_str());
2248 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2249 string result;
2250 if (TWFunc::Path_Exists(file)) {
2251 if (TWFunc::read_file(file, result) != 0) {
2252 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2253 } else {
2254 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2255 if (result == "0") {
2256 ret = 0;
2257 }
2258 }
2259 } else {
2260 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2261 }
2262 UnMount(true);
2263 } else {
2264 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2265 }
2266 Mount_Read_Only = original_read_only;
2267 return ret;
2268}