blob: e612be34fe9ad1952f56a38ec09386e2c4c41b3b [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;
Xueferbfce5042015-04-14 02:02:05 +0800481 Is_Settings_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500482 } else if (strcmp(ptr, "andsec") == 0) {
483 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400484 } else if (strcmp(ptr, "canbewiped") == 0) {
485 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700486 } else if (strcmp(ptr, "usermrf") == 0) {
487 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500488 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
489 ptr += 7;
490 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
491 Can_Be_Backed_Up = true;
492 else
493 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400494 } else if (strcmp(ptr, "wipeingui") == 0) {
495 Can_Be_Wiped = true;
496 Wipe_Available_in_GUI = true;
497 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
498 Can_Be_Wiped = true;
499 Wipe_Available_in_GUI = true;
500 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500501 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000502 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400503 Is_SubPartition = true;
504 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000505 } else if (strcmp(ptr, "ignoreblkid") == 0) {
506 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000507 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
508 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500509 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400510 ptr += 8;
511 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500512 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
513 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400514 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500515 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400516 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500517 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
518 Display_Name.resize(Display_Name.size() - 1);
519 }
520 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
521 has_storage_name = true;
522 ptr += 11;
523 if (*ptr == '\"') ptr++;
524 Storage_Name = ptr;
525 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
526 Storage_Name.resize(Storage_Name.size() - 1);
527 }
528 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
529 has_backup_name = true;
530 ptr += 10;
531 if (*ptr == '\"') ptr++;
532 Backup_Display_Name = ptr;
533 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
534 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
535 }
536 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400537 ptr += 10;
Ethan Yonkera2719152015-05-28 09:44:41 -0500538 Format_Block_Size = (unsigned long)(atol(ptr));
Dees_Troya13d74f2013-03-24 08:54:55 -0500539 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400540 ptr += 7;
541 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000542 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
543 ptr += 17;
544 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
545 Can_Encrypt_Backup = true;
546 else
547 Can_Encrypt_Backup = false;
548 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
549 ptr += 21;
550 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
551 Can_Encrypt_Backup = true;
552 Use_Userdata_Encryption = true;
553 } else {
554 Use_Userdata_Encryption = false;
555 }
Hashcode62bd9e02013-11-19 21:59:42 -0800556 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
557 ptr += 8;
558 if (*ptr == '\"') ptr++;
559
560 Mount_Options = ptr;
561 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
562 Mount_Options.resize(Mount_Options.size() - 1);
563 }
564 Process_FS_Flags(Mount_Options, Mount_Flags);
Ethan Yonker253368a2014-11-25 15:00:52 -0600565 } else if ((ptr_len > 12 && strncmp(ptr, "encryptable=", 12) == 0) || (ptr_len > 13 && strncmp(ptr, "forceencrypt=", 13) == 0)) {
566 ptr += 12;
567 if (*ptr == '=') ptr++;
568 if (*ptr == '\"') ptr++;
569
570 Crypto_Key_Location = ptr;
571 if (Crypto_Key_Location.substr(Crypto_Key_Location.size() - 1, 1) == "\"") {
572 Crypto_Key_Location.resize(Crypto_Key_Location.size() - 1);
573 }
574 } else if (ptr_len > 8 && strncmp(ptr, "mounttodecrypt", 14) == 0) {
575 Mount_To_Decrypt = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600576 } else if (strncmp(ptr, "flashimg", 8) == 0) {
577 if (ptr_len == 8) {
578 Can_Flash_Img = true;
579 } else if (ptr_len == 10) {
580 ptr += 9;
581 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
582 Can_Flash_Img = true;
583 } else {
584 Can_Flash_Img = false;
585 }
586 }
Dees_Troy51127312012-09-08 13:08:49 -0400587 } else {
588 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000589 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400590 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000591 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400592 }
593 while (index < flags_len && flags[index] != '\0')
594 index++;
595 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500596 if (has_display_name && !has_storage_name)
597 Storage_Name = Display_Name;
598 if (!has_display_name && has_storage_name)
599 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000600 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500601 Backup_Display_Name = Display_Name;
602 if (!has_display_name && has_backup_name)
603 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400604 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400605}
606
Dees_Troy5bf43922012-09-07 16:07:55 -0400607bool TWPartition::Is_File_System(string File_System) {
608 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400609 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400610 File_System == "ext4" ||
611 File_System == "vfat" ||
612 File_System == "ntfs" ||
613 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500614 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000615 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400616 File_System == "auto")
617 return true;
618 else
619 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400620}
621
Dees_Troy5bf43922012-09-07 16:07:55 -0400622bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400623 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400624 return true;
625 else
626 return false;
627}
628
Dees_Troy51127312012-09-08 13:08:49 -0400629bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400630 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400631 if (mkdir(Path.c_str(), 0777) == -1) {
632 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000633 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400634 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000635 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400636 return false;
637 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000638 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400639 return true;
640 }
641 }
642 return true;
643}
644
Dees_Troy5bf43922012-09-07 16:07:55 -0400645void TWPartition::Setup_File_System(bool Display_Error) {
646 struct statfs st;
647
648 Can_Be_Mounted = true;
649 Can_Be_Wiped = true;
650
Dees_Troy5bf43922012-09-07 16:07:55 -0400651 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400652 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400653 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
654 Backup_Name = Display_Name;
655 Backup_Method = FILES;
656}
657
658void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400659 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
660 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800661 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400662 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800663 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400664 Backup_Method = FLASH_UTILS;
665 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000666 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 -0400667 if (Find_Partition_Size()) {
668 Used = Size;
669 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400670 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400671 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000672 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400673 else
Dees Troyd932ce12013-10-18 17:12:59 +0000674 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400675 }
676}
677
Dees_Troye58d5262012-09-21 12:27:57 -0400678void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500679 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400680 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500681 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400682 Has_Android_Secure = true;
683 Symlink_Path = Mount_Point + "/.android_secure";
684 Symlink_Mount_Point = "/and-sec";
685 Backup_Path = Symlink_Mount_Point;
686 Make_Dir("/and-sec", true);
687 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600688 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400689}
690
that9e0593e2014-10-08 00:01:24 +0200691void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500692 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
693 Storage_Name = "Internal Storage";
694 Has_Data_Media = true;
695 Is_Storage = true;
696 Is_Settings_Storage = true;
697 Storage_Path = "/data/media";
698 Symlink_Path = Storage_Path;
699 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
700 Make_Dir("/emmc", false);
701 Symlink_Mount_Point = "/emmc";
702 } else {
703 Make_Dir("/sdcard", false);
704 Symlink_Mount_Point = "/sdcard";
705 }
706 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
707 Storage_Path = "/data/media/0";
708 Symlink_Path = Storage_Path;
709 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
710 UnMount(true);
711 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500712 DataManager::SetValue("tw_has_internal", 1);
713 DataManager::SetValue("tw_has_data_media", 1);
714 du.add_absolute_dir("/data/media");
715}
716
Dees_Troy5bf43922012-09-07 16:07:55 -0400717void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
718 char device[512], realDevice[512];
719
720 strcpy(device, Block.c_str());
721 memset(realDevice, 0, sizeof(realDevice));
722 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
723 {
724 strcpy(device, realDevice);
725 memset(realDevice, 0, sizeof(realDevice));
726 }
727
728 if (device[0] != '/') {
729 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000730 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400731 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000732 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400733 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400734 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400735 Block = device;
736 return;
737 }
738}
739
Dees_Troy8e337f32012-10-13 22:07:49 -0400740void TWPartition::Mount_Storage_Retry(void) {
741 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500742 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400743 int retry_count = 5;
744 while (retry_count > 0 && !Mount(false)) {
745 usleep(500000);
746 retry_count--;
747 }
748 Mount(true);
749 }
750}
751
Dees_Troy38bd7602012-09-14 13:33:53 -0400752bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
753 FILE *fp = NULL;
754 char line[255];
755
756 fp = fopen("/proc/mtd", "rt");
757 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000758 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400759 return false;
760 }
761
762 while (fgets(line, sizeof(line), fp) != NULL)
763 {
764 char device[32], label[32];
765 unsigned long size = 0;
766 char* fstype = NULL;
767 int deviceId;
768
769 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
770
771 // Skip header and blank lines
772 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
773 continue;
774
775 // Strip off the trailing " from the label
776 label[strlen(label)-1] = '\0';
777
778 if (strcmp(label, MTD_Name.c_str()) == 0) {
779 // We found our device
780 // Strip off the trailing : from the device
781 device[strlen(device)-1] = '\0';
782 if (sscanf(device,"mtd%d", &deviceId) == 1) {
783 sprintf(device, "/dev/block/mtdblock%d", deviceId);
784 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000785 fclose(fp);
786 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400787 }
788 }
789 }
790 fclose(fp);
791
792 return false;
793}
794
Dees_Troy51127312012-09-08 13:08:49 -0400795bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
796 struct statfs st;
797 string Local_Path = Mount_Point + "/.";
798
799 if (!Mount(Display_Error))
800 return false;
801
802 if (statfs(Local_Path.c_str(), &st) != 0) {
803 if (!Removable) {
804 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000805 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400806 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000807 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400808 }
809 return false;
810 }
811 Size = (st.f_blocks * st.f_bsize);
812 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
813 Free = (st.f_bfree * st.f_bsize);
814 Backup_Size = Used;
815 return true;
816}
817
818bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400819 FILE* fp;
820 char command[255], line[512];
821 int include_block = 1;
822 unsigned int min_len;
823
824 if (!Mount(Display_Error))
825 return false;
826
Dees_Troy38bd7602012-09-14 13:33:53 -0400827 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400828 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200829 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400830 fp = fopen("/tmp/dfoutput.txt", "rt");
831 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000832 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400833 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400834 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400835
836 while (fgets(line, sizeof(line), fp) != NULL)
837 {
838 unsigned long blocks, used, available;
839 char device[64];
840 char tmpString[64];
841
842 if (strncmp(line, "Filesystem", 10) == 0)
843 continue;
844 if (strlen(line) < min_len) {
845 include_block = 0;
846 continue;
847 }
848 if (include_block) {
849 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
850 } else {
851 // The device block string is so long that the df information is on the next line
852 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400853 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400854 while (tmpString[space_count] == 32)
855 space_count++;
856 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
857 }
858
859 // Adjust block size to byte size
860 Size = blocks * 1024ULL;
861 Used = used * 1024ULL;
862 Free = available * 1024ULL;
863 Backup_Size = Used;
864 }
865 fclose(fp);
866 return true;
867}
868
Dees_Troy5bf43922012-09-07 16:07:55 -0400869bool TWPartition::Find_Partition_Size(void) {
870 FILE* fp;
871 char line[512];
872 string tmpdevice;
873
igoriok87e3d932013-01-31 21:03:53 +0200874 fp = fopen("/proc/dumchar_info", "rt");
875 if (fp != NULL) {
876 while (fgets(line, sizeof(line), fp) != NULL)
877 {
878 char label[32], device[32];
879 unsigned long size = 0;
880
thatd43bf2d2014-09-21 23:13:02 +0200881 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200882
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500883 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200884 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
885 continue;
886
887 tmpdevice = "/dev/";
888 tmpdevice += label;
889 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
890 Size = size;
891 fclose(fp);
892 return true;
893 }
894 }
895 }
896
Dees_Troy5bf43922012-09-07 16:07:55 -0400897 // In this case, we'll first get the partitions we care about (with labels)
898 fp = fopen("/proc/partitions", "rt");
899 if (fp == NULL)
900 return false;
901
902 while (fgets(line, sizeof(line), fp) != NULL)
903 {
904 unsigned long major, minor, blocks;
905 char device[512];
906 char tmpString[64];
907
Dees_Troy63c8df72012-09-10 14:02:05 -0400908 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400909 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
910
911 tmpdevice = "/dev/block/";
912 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400913 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400914 // Adjust block size to byte size
915 Size = blocks * 1024ULL;
916 fclose(fp);
917 return true;
918 }
919 }
920 fclose(fp);
921 return false;
922}
923
Dees_Troy5bf43922012-09-07 16:07:55 -0400924bool TWPartition::Is_Mounted(void) {
925 if (!Can_Be_Mounted)
926 return false;
927
928 struct stat st1, st2;
929 string test_path;
930
931 // Check to see if the mount point directory exists
932 test_path = Mount_Point + "/.";
933 if (stat(test_path.c_str(), &st1) != 0) return false;
934
935 // Check to see if the directory above the mount point exists
936 test_path = Mount_Point + "/../.";
937 if (stat(test_path.c_str(), &st2) != 0) return false;
938
939 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
940 int ret = (st1.st_dev != st2.st_dev) ? true : false;
941
942 return ret;
943}
944
945bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000946 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500947 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000948
Dees_Troy5bf43922012-09-07 16:07:55 -0400949 if (Is_Mounted()) {
950 return true;
951 } else if (!Can_Be_Mounted) {
952 return false;
953 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400954
955 Find_Actual_Block_Device();
956
957 // Check the current file system before mounting
958 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000959 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000960 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 +0000961 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000962 string result;
963 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000964 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000965 Current_File_System = "vfat";
966 } else {
967#ifdef TW_NO_EXFAT_FUSE
968 UnMount(false);
969 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
970 // Some kernels let us mount vfat as exfat which doesn't work out too well
971#else
972 exfat_mounted = 1;
973#endif
974 }
975 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500976
Ethan Yonkerb81d9052015-07-09 13:20:53 -0500977 if (Current_File_System == "ntfs" && TWFunc::Path_Exists("/sbin/ntfs-3g")) {
978 string cmd;
979 if (Mount_Read_Only)
980 cmd = "/sbin/ntfs-3g -o ro " + Actual_Block_Device + " " + Mount_Point;
981 else
982 cmd = "/sbin/ntfs-3g " + Actual_Block_Device + " " + Mount_Point;
983 LOGINFO("cmd: '%s'\n", cmd.c_str());
984 if (TWFunc::Exec_Cmd(cmd) == 0) {
985 return true;
986 } else {
987 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
988 }
989 }
990
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500991 if (Mount_Read_Only)
992 flags |= MS_RDONLY;
993
Dees_Troy22042032012-12-18 21:23:08 +0000994 if (Fstab_File_System == "yaffs2") {
995 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500996 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
997 if (Mount_Read_Only)
998 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +0000999 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1000 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1001 if (Display_Error)
1002 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1003 else
1004 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1005 return false;
1006 } else {
1007 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1008 return true;
1009 }
1010 } else {
1011 struct stat st;
1012 string test_path = Mount_Point;
1013 if (stat(test_path.c_str(), &st) < 0) {
1014 if (Display_Error)
1015 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1016 else
1017 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1018 return false;
1019 }
1020 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1021 if (new_mode != st.st_mode) {
1022 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1023 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1024 if (Display_Error)
1025 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1026 else
1027 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1028 return false;
1029 }
1030 }
Dees_Troy22042032012-12-18 21:23:08 +00001031 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001032 }
thatc7572eb2015-03-31 18:55:30 +02001033 }
1034
1035 string mount_fs = Current_File_System;
1036 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1037 mount_fs = "texfat";
1038
1039 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001040 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1041 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001042#ifdef TW_NO_EXFAT_FUSE
1043 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001044 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001045 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001046 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001047 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001048 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001049 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001050 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 +00001051 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001052 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001053 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001054#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001055 if (!Removable && Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001056 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001057 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001058 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1059 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 +00001060 return false;
1061#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001062 }
1063#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001064 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001065
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001066 if (Removable)
1067 Update_Size(Display_Error);
1068
xiaolu9416f4f2015-06-04 08:22:23 +08001069 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001070 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001071 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001072 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001073 return true;
1074}
1075
1076bool TWPartition::UnMount(bool Display_Error) {
1077 if (Is_Mounted()) {
1078 int never_unmount_system;
1079
1080 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1081 if (never_unmount_system == 1 && Mount_Point == "/system")
1082 return true; // Never unmount system if you're not supposed to unmount it
1083
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001084 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001085 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001086
Dees_Troy38bd7602012-09-14 13:33:53 -04001087 if (!Symlink_Mount_Point.empty())
1088 umount(Symlink_Mount_Point.c_str());
1089
Dees_Troyb05ddee2013-01-28 20:24:50 +00001090 umount(Mount_Point.c_str());
1091 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001092 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001093 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001094 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001095 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001096 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001097 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001098 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001099 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001100 } else {
1101 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001102 }
1103}
1104
Gary Peck43acadf2012-11-21 21:19:01 -08001105bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001106 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001107 int check;
1108 string Layout_Filename = Mount_Point + "/.layout_version";
1109
Dees_Troy38bd7602012-09-14 13:33:53 -04001110 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001111 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001112 return false;
1113 }
1114
Dees_Troyc51f1f92012-09-20 15:32:13 -04001115 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001116 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001117
Dees_Troy16c2b312013-01-15 16:51:18 +00001118 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1119 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1120 else
1121 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001122
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001123 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001124 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001125 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001126 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001127 DataManager::GetValue(TW_RM_RF_VAR, check);
1128
Hashcodedabfd492013-08-29 22:45:30 -07001129 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001130 wiped = Wipe_RMRF();
1131 else if (New_File_System == "ext4")
1132 wiped = Wipe_EXT4();
1133 else if (New_File_System == "ext2" || New_File_System == "ext3")
1134 wiped = Wipe_EXT23(New_File_System);
1135 else if (New_File_System == "vfat")
1136 wiped = Wipe_FAT();
1137 else if (New_File_System == "exfat")
1138 wiped = Wipe_EXFAT();
1139 else if (New_File_System == "yaffs2")
1140 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001141 else if (New_File_System == "f2fs")
1142 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001143 else if (New_File_System == "ntfs")
1144 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001145 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001146 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 +00001147 unlink("/.layout_version");
1148 return false;
1149 }
1150 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001151 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001152
Gary Pecke8bc5d72012-12-21 06:45:25 -08001153 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001154 if (Mount_Point == "/cache")
1155 DataManager::Output_Version();
1156
Dees_Troy16c2b312013-01-15 16:51:18 +00001157 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1158 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1159
1160 if (update_crypt) {
1161 Setup_File_System(false);
1162 if (Is_Encrypted && !Is_Decrypted) {
1163 // just wiped an encrypted partition back to its unencrypted state
1164 Is_Encrypted = false;
1165 Is_Decrypted = false;
1166 Decrypted_Block_Device = "";
1167 if (Mount_Point == "/data") {
1168 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1169 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1170 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001171 }
1172 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001173
1174 if (Mount_Point == "/data" && Has_Data_Media && recreate_media) {
1175 Recreate_Media_Folder();
1176 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001177 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001178 if (Is_Storage) {
1179 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001180 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001181 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001182}
1183
Gary Peck43acadf2012-11-21 21:19:01 -08001184bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001185 if (Is_File_System(Current_File_System))
1186 return Wipe(Current_File_System);
1187 else
1188 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001189}
1190
Dees_Troye58d5262012-09-21 12:27:57 -04001191bool TWPartition::Wipe_AndSec(void) {
1192 if (!Has_Android_Secure)
1193 return false;
1194
Dees_Troye58d5262012-09-21 12:27:57 -04001195 if (!Mount(true))
1196 return false;
1197
Dees_Troy2673cec2013-04-02 20:22:16 +00001198 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001199 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001200 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001201}
1202
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001203bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001204 if (Mount_Read_Only)
1205 return false;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001206 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/dosfsck"))
1207 return true;
1208 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1209 return true;
1210 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1211 return true;
1212 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1213 return true;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001214 else if (Current_File_System == "ntfs" && TWFunc::Path_Exists("/sbin/ntfsfix"))
1215 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001216 return false;
1217}
1218
1219bool TWPartition::Repair() {
1220 string command;
1221
1222 if (Current_File_System == "vfat") {
1223 if (!TWFunc::Path_Exists("/sbin/dosfsck")) {
1224 gui_print("dosfsck does not exist! Cannot repair!\n");
1225 return false;
1226 }
1227 if (!UnMount(true))
1228 return false;
1229 gui_print("Repairing %s using dosfsck...\n", Display_Name.c_str());
1230 Find_Actual_Block_Device();
1231 command = "/sbin/dosfsck -y " + Actual_Block_Device;
1232 LOGINFO("Repair command: %s\n", command.c_str());
1233 if (TWFunc::Exec_Cmd(command) == 0) {
1234 gui_print("Done.\n");
1235 return true;
1236 } else {
1237 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1238 return false;
1239 }
1240 }
1241 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1242 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
1243 gui_print("e2fsck does not exist! Cannot repair!\n");
1244 return false;
1245 }
1246 if (!UnMount(true))
1247 return false;
1248 gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str());
1249 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001250 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001251 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 == "exfat") {
1261 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
1262 gui_print("fsck.exfat does not exist! Cannot repair!\n");
1263 return false;
1264 }
1265 if (!UnMount(true))
1266 return false;
1267 gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str());
1268 Find_Actual_Block_Device();
1269 command = "/sbin/fsck.exfat " + 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 if (Current_File_System == "f2fs") {
1280 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
1281 gui_print("fsck.f2fs does not exist! Cannot repair!\n");
1282 return false;
1283 }
1284 if (!UnMount(true))
1285 return false;
1286 gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str());
1287 Find_Actual_Block_Device();
1288 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1289 LOGINFO("Repair command: %s\n", command.c_str());
1290 if (TWFunc::Exec_Cmd(command) == 0) {
1291 gui_print("Done.\n");
1292 return true;
1293 } else {
1294 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1295 return false;
1296 }
1297 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001298 if (Current_File_System == "ntfs") {
1299 if (!TWFunc::Path_Exists("/sbin/ntfsfix")) {
1300 gui_print("ntfsfix does not exist! Cannot repair!\n");
1301 return false;
1302 }
1303 if (!UnMount(true))
1304 return false;
1305 gui_print("Repairing %s using ntfsfix...\n", Display_Name.c_str());
1306 Find_Actual_Block_Device();
1307 command = "/sbin/ntfsfix " + Actual_Block_Device;
1308 LOGINFO("Repair command: %s\n", command.c_str());
1309 if (TWFunc::Exec_Cmd(command) == 0) {
1310 gui_print("Done.\n");
1311 return true;
1312 } else {
1313 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1314 return false;
1315 }
1316 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001317 return false;
1318}
1319
Ethan Yonkera2719152015-05-28 09:44:41 -05001320bool TWPartition::Can_Resize() {
1321 if (Mount_Read_Only)
1322 return false;
1323 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1324 return true;
1325 return false;
1326}
1327
1328bool TWPartition::Resize() {
1329 string command;
1330
1331 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1332 if (!Can_Repair()) {
1333 LOGERR("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1334 return false;
1335 }
1336 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
1337 gui_print("resize2fs does not exist! Cannot resize!\n");
1338 return false;
1339 }
1340 // Repair will unmount so no need to do it twice
1341 gui_print("Repairing %s before resizing.\n", Display_Name.c_str());
1342 if (!Repair())
1343 return false;
1344 gui_print("Resizing %s using resize2fs...\n", Display_Name.c_str());
1345 Find_Actual_Block_Device();
1346 command = "/sbin/resize2fs " + Actual_Block_Device;
1347 if (Length != 0) {
1348 unsigned int block_device_size;
1349 int fd, ret;
1350
1351 fd = open(Actual_Block_Device.c_str(), O_RDONLY);
1352 if (fd < 0) {
1353 LOGERR("Resize: Failed to open '%s'\n", Actual_Block_Device.c_str());
1354 return false;
1355 }
1356 ret = ioctl(fd, BLKGETSIZE, &block_device_size);
1357 close(fd);
1358 if (ret) {
1359 LOGERR("Resize: ioctl error\n");
1360 return false;
1361 }
1362 unsigned long long Actual_Size = (unsigned long long)(block_device_size) * 512LLU;
1363 unsigned long long Block_Count;
1364 if (Length < 0) {
1365 // Reduce overall size by this length
1366 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1367 } else {
1368 // This is the size, not a size reduction
1369 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1370 }
1371 char temp[256];
1372 sprintf(temp, "%llu", Block_Count);
1373 command += " ";
1374 command += temp;
1375 command += "K";
1376 }
1377 LOGINFO("Resize command: %s\n", command.c_str());
1378 if (TWFunc::Exec_Cmd(command) == 0) {
1379 Update_Size(true);
1380 gui_print("Done.\n");
1381 return true;
1382 } else {
1383 Update_Size(true);
1384 LOGERR("Unable to resize '%s'.\n", Mount_Point.c_str());
1385 return false;
1386 }
1387 }
1388 return false;
1389}
1390
bigbiff7abc5fe2015-01-17 16:53:12 -05001391bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
1392 if (Backup_Method == FILES) {
1393 return Backup_Tar(backup_folder, overall_size, other_backups_size, tar_fork_pid);
1394 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001395 else if (Backup_Method == DD)
1396 return Backup_DD(backup_folder);
1397 else if (Backup_Method == FLASH_UTILS)
1398 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001399 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001400 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001401}
1402
Dees_Troy43d8b002012-09-17 16:00:01 -04001403bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001404 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001405 char split_filename[512];
1406 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001407 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001408
Captain Throwbackff5935f2014-09-23 16:08:34 -04001409 sync();
1410
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001411 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001412 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001413 if (!TWFunc::Path_Exists(Full_Filename)) {
1414 // This is a split archive, we presume
1415 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001416 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001417 md5file = split_filename;
1418 md5file += ".md5";
1419 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001420 LOGERR("No md5 file found for '%s'.\n", split_filename);
1421 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001422 return false;
1423 }
1424 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001425 while (index < 1000) {
1426 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001427 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001428 return false;
1429 }
1430 index++;
1431 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001432 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001433 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001434 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001435 } else {
1436 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001437 md5file = Full_Filename + ".md5";
1438 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001439 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1440 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001441 return false;
1442 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001443 md5sum.setfn(Full_Filename);
1444 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001445 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001446 return false;
1447 } else
1448 return true;
1449 }
1450 return false;
1451}
1452
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001453bool 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 -08001454 string Restore_File_System;
1455
1456 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001457 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001458
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001459 Restore_File_System = Get_Restore_File_System(restore_folder);
1460
1461 if (Is_File_System(Restore_File_System))
1462 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1463 else if (Is_Image(Restore_File_System)) {
Ethan Yonkerf66324f2015-01-09 16:46:57 -06001464 return Restore_Image(restore_folder, total_restore_size, already_restored_size, Restore_File_System);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001465 }
1466
1467 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1468 return false;
1469}
1470
1471string TWPartition::Get_Restore_File_System(string restore_folder) {
1472 size_t first_period, second_period;
1473 string Restore_File_System;
1474
Gary Peck43acadf2012-11-21 21:19:01 -08001475 // Parse backup filename to extract the file system before wiping
1476 first_period = Backup_FileName.find(".");
1477 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001478 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001479 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001480 }
1481 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1482 second_period = Restore_File_System.find(".");
1483 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001484 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001485 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001486 }
1487 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001488 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001489 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001490}
1491
1492string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001493 if (Backup_Method == NONE)
1494 return "none";
1495 else if (Backup_Method == FILES)
1496 return "files";
1497 else if (Backup_Method == DD)
1498 return "dd";
1499 else if (Backup_Method == FLASH_UTILS)
1500 return "flash_utils";
1501 else
1502 return "undefined";
1503 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001504}
1505
1506bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001507 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001508 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001509 return 1;
1510}
1511
1512bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001513 bool Save_Data_Media = Has_Data_Media;
1514
1515 if (!UnMount(true))
1516 return false;
1517
Dees_Troy38bd7602012-09-14 13:33:53 -04001518 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001519 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001520#ifdef TW_INCLUDE_CRYPTO
1521 if (Is_Decrypted) {
1522 if (!UnMount(true))
1523 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001524 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001525 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1526 }
1527 }
1528#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001529 Is_Decrypted = false;
1530 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001531 Find_Actual_Block_Device();
Gary Pecke8bc5d72012-12-21 06:45:25 -08001532 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001533 Has_Data_Media = Save_Data_Media;
1534 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1535 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001536 if (Mount(false))
1537 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001538 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001539 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001540#ifndef TW_OEM_BUILD
Dees_Troy2673cec2013-04-02 20:22:16 +00001541 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Ethan Yonker83e82572014-04-04 10:59:28 -05001542#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001543 return true;
1544 } else {
1545 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001546 LOGERR("Unable to format to remove encryption.\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06001547 if (Has_Data_Media && Mount(false))
1548 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001549 return false;
1550 }
1551 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001552}
1553
1554void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001555 const char* type;
1556 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001557
Dees_Troy68cab492012-12-12 19:29:35 +00001558 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1559 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001560
Dees_Troy38bd7602012-09-14 13:33:53 -04001561 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001562 if (!Is_Present)
1563 return;
Dees_Troy51127312012-09-08 13:08:49 -04001564
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001565 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1566 if (blkid_do_fullprobe(pr)) {
1567 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001568 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001569 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001570 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001571
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001572 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001573 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001574 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001575 return;
1576 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001577
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001578 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001579 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001580}
1581
Gary Peck43acadf2012-11-21 21:19:01 -08001582bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001583 if (!UnMount(true))
1584 return false;
1585
Dees_Troy43d8b002012-09-17 16:00:01 -04001586 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001587 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001588
Dees_Troy2673cec2013-04-02 20:22:16 +00001589 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001590 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001591 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001592 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001593 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001594 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001595 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001596 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001597 return true;
1598 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001599 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001600 return false;
1601 }
1602 } else
1603 return Wipe_RMRF();
1604
1605 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001606}
1607
1608bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001609 Find_Actual_Block_Device();
1610 if (!Is_Present) {
1611 LOGERR("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1612 return false;
1613 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001614 if (!UnMount(true))
1615 return false;
1616
Dees_Troyb3265ab2013-08-30 02:59:14 +00001617#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001618 int ret;
1619 char *secontext = NULL;
1620
Dees_Troya95f55c2013-08-17 13:14:43 +00001621 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001622
Dees Troy99c8dbf2014-03-10 16:53:58 +00001623 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001624 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1625 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1626 } else {
1627 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1628 }
1629 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001630 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1631 return false;
1632 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001633 string sedir = Mount_Point + "/lost+found";
1634 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1635 rmdir(sedir.c_str());
1636 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001637 return true;
1638 }
1639#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001640 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001641 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001642
Dees_Troy2673cec2013-04-02 20:22:16 +00001643 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001644 Find_Actual_Block_Device();
1645 Command = "make_ext4fs";
1646 if (!Is_Decrypted && Length != 0) {
1647 // Only use length if we're not decrypted
1648 char len[32];
1649 sprintf(len, "%i", Length);
1650 Command += " -l ";
1651 Command += len;
1652 }
Dees_Troy5295d582013-09-06 15:51:08 +00001653 if (TWFunc::Path_Exists("/file_contexts")) {
1654 Command += " -S /file_contexts";
1655 }
1656 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001657 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001658 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001659 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001660 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001661 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001662 return true;
1663 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001664 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001665 return false;
1666 }
1667 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001668 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001669#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001670 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001671}
1672
1673bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001674 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001675
Dees_Troy43d8b002012-09-17 16:00:01 -04001676 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001677 if (!UnMount(true))
1678 return false;
1679
Dees_Troy2673cec2013-04-02 20:22:16 +00001680 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001681 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001682 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001683 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001684 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001685 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001686 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001687 return true;
1688 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001689 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001690 return false;
1691 }
1692 return true;
1693 }
1694 else
1695 return Wipe_RMRF();
1696
1697 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001698}
1699
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001700bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001701 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001702
1703 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1704 if (!UnMount(true))
1705 return false;
1706
Dees_Troy2673cec2013-04-02 20:22:16 +00001707 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001708 Find_Actual_Block_Device();
1709 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001710 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001711 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001712 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001713 return true;
1714 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001715 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001716 return false;
1717 }
1718 return true;
1719 }
1720 return false;
1721}
1722
Dees_Troy38bd7602012-09-14 13:33:53 -04001723bool TWPartition::Wipe_MTD() {
1724 if (!UnMount(true))
1725 return false;
1726
Dees_Troy2673cec2013-04-02 20:22:16 +00001727 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001728
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001729 mtd_scan_partitions();
1730 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1731 if (mtd == NULL) {
1732 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1733 return false;
1734 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001735
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001736 MtdWriteContext* ctx = mtd_write_partition(mtd);
1737 if (ctx == NULL) {
1738 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1739 return false;
1740 }
1741 if (mtd_erase_blocks(ctx, -1) == -1) {
1742 mtd_write_close(ctx);
1743 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1744 return false;
1745 }
1746 if (mtd_write_close(ctx) != 0) {
1747 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1748 return false;
1749 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001750 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001751 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001752 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001753 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001754}
1755
1756bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001757 if (!Mount(true))
1758 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06001759 // This is the only wipe that leaves the partition mounted, so we
1760 // must manually remove the partition from MTP if it is a storage
1761 // partition.
1762 if (Is_Storage)
1763 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001764
Dees_Troy2673cec2013-04-02 20:22:16 +00001765 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001766 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001767 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001768 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001769}
1770
Dees_Troye5017042013-08-29 16:38:55 +00001771bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001772 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001773
1774 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1775 if (!UnMount(true))
1776 return false;
1777
1778 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1779 Find_Actual_Block_Device();
dhacker29a3fa75f2015-01-17 19:42:33 -05001780 command = "mkfs.f2fs -t 1";
1781 if (!Is_Decrypted && Length != 0) {
1782 // Only use length if we're not decrypted
1783 char len[32];
1784 int mod_length = Length;
1785 if (Length < 0)
1786 mod_length *= -1;
1787 sprintf(len, "%i", mod_length);
1788 command += " -r ";
1789 command += len;
1790 }
1791 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001792 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001793 Recreate_AndSec_Folder();
1794 gui_print("Done.\n");
1795 return true;
1796 } else {
1797 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1798 return false;
1799 }
1800 return true;
1801 } else {
1802 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1803 return Wipe_RMRF();
1804 }
1805 return false;
1806}
1807
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001808bool TWPartition::Wipe_NTFS() {
1809 string command;
1810
1811 if (TWFunc::Path_Exists("/sbin/mkntfs")) {
1812 if (!UnMount(true))
1813 return false;
1814
1815 gui_print("Formatting %s using mkntfs...\n", Display_Name.c_str());
1816 Find_Actual_Block_Device();
1817 command = "mkntfs " + Actual_Block_Device;
1818 if (TWFunc::Exec_Cmd(command) == 0) {
1819 Recreate_AndSec_Folder();
1820 gui_print("Done.\n");
1821 return true;
1822 } else {
1823 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1824 return false;
1825 }
1826 return true;
1827 }
1828 return false;
1829}
1830
Dees_Troy51a0e822012-09-05 15:24:24 -04001831bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001832#ifdef TW_OEM_BUILD
1833 // In an OEM Build we want to do a full format
1834 return Wipe_Encryption();
1835#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001836 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001837
1838 // This handles wiping data on devices with "sdcard" in /data/media
1839 if (!Mount(true))
1840 return false;
1841
Dees_Troy2673cec2013-04-02 20:22:16 +00001842 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001843
1844 DIR* d;
1845 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001846 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001847 struct dirent* de;
1848 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001849 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001850 // The media folder is the "internal sdcard"
1851 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001852 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001853 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001854 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001855
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001856 dir = "/data/";
1857 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001858 if (de->d_type == DT_DIR) {
1859 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001860 } 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 +00001861 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001862 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001863 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001864 }
1865 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001866
Dees_Troy2673cec2013-04-02 20:22:16 +00001867 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001868 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001869 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001870 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001871 return false;
Ethan Yonker83e82572014-04-04 10:59:28 -05001872#endif // ifdef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001873}
1874
bigbiff7abc5fe2015-01-17 16:53:12 -05001875bool 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 -04001876 char back_name[255], split_index[5];
1877 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001878 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001879 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001880 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001881 twrpTar tar;
1882 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001883
Dees_Troy43d8b002012-09-17 16:00:01 -04001884 if (!Mount(true))
1885 return false;
1886
Dees_Troya13d74f2013-03-24 08:54:55 -05001887 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001888 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001889
1890 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001891 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001892
Dees_Troy83bd4832013-05-04 12:39:56 +00001893#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1894 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1895 if (use_encryption && Can_Encrypt_Backup) {
1896 tar.use_encryption = use_encryption;
1897 if (Use_Userdata_Encryption)
1898 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001899 string Password;
1900 DataManager::GetValue("tw_backup_password", Password);
1901 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001902 } else {
1903 use_encryption = false;
1904 }
1905#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001906
1907 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1908 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001909 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001910 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001911 Full_FileName = backup_folder + "/" + Backup_FileName;
1912 tar.setdir(Backup_Path);
1913 tar.setfn(Full_FileName);
1914 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001915 tar.partition_name = Backup_Name;
1916 tar.backup_folder = backup_folder;
bigbiff7abc5fe2015-01-17 16:53:12 -05001917 if (tar.createTarFork(overall_size, other_backups_size, tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00001918 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001919 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001920}
1921
1922bool TWPartition::Backup_DD(string backup_folder) {
codelover352b75e2015-03-29 02:44:07 +08001923 char back_name[255], block_size[32], dd_count[32];
1924 string Full_FileName, Command, DD_BS, DD_COUNT;
Dees_Troy43d8b002012-09-17 16:00:01 -04001925 int use_compression;
codelover352b75e2015-03-29 02:44:07 +08001926 unsigned long long DD_Block_Size, DD_Count;
Dees_Troy43d8b002012-09-17 16:00:01 -04001927
codelover352b75e2015-03-29 02:44:07 +08001928 DD_Block_Size = 16 * 1024 * 1024;
1929 while (Backup_Size % DD_Block_Size != 0) DD_Block_Size >>= 1;
1930
1931 DD_Count = Backup_Size / DD_Block_Size;
1932
1933 sprintf(dd_count, "%llu", DD_Count);
1934 DD_COUNT = dd_count;
1935
1936 sprintf(block_size, "%llu", DD_Block_Size);
1937 DD_BS = block_size;
igoriok87e3d932013-01-31 21:03:53 +02001938
Dees_Troyb46a6842012-09-25 11:06:46 -04001939 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001940 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001941
1942 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1943 Backup_FileName = back_name;
1944
1945 Full_FileName = backup_folder + "/" + Backup_FileName;
1946
codelover352b75e2015-03-29 02:44:07 +08001947 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + " count=" + DD_COUNT;
Dees_Troy2673cec2013-04-02 20:22:16 +00001948 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001949 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001950 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04001951 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001952 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001953 return false;
1954 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001955 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001956}
1957
1958bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001959 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001960 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001961 int use_compression;
1962
Dees_Troyb46a6842012-09-25 11:06:46 -04001963 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001964 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001965
1966 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1967 Backup_FileName = back_name;
1968
1969 Full_FileName = backup_folder + "/" + Backup_FileName;
1970
1971 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001972 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001973 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001974 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001975 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1976 // 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 +00001977 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001978 return false;
1979 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001980 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001981}
1982
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001983unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
1984 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
1985 if (restore_info.LoadValues() == 0) {
1986 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
1987 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
1988 return Restore_Size;
1989 }
1990 }
1991 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
1992
1993 Full_FileName = restore_folder + "/" + Backup_FileName;
1994
1995 if (Is_Image(Restore_File_System)) {
1996 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
1997 return Restore_Size;
1998 }
1999
2000 twrpTar tar;
2001 tar.setdir(Backup_Path);
2002 tar.setfn(Full_FileName);
2003 tar.backup_name = Backup_Name;
2004#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2005 string Password;
2006 DataManager::GetValue("tw_restore_password", Password);
2007 if (!Password.empty())
2008 tar.setpassword(Password);
2009#endif
2010 tar.partition_name = Backup_Name;
2011 tar.backup_folder = restore_folder;
2012 Restore_Size = tar.get_size();
2013 return Restore_Size;
2014}
2015
2016bool 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 -08002017 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04002018 int index = 0;
2019 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00002020 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002021
Dees_Troye58d5262012-09-21 12:27:57 -04002022 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002023 if (!Wipe_AndSec())
2024 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002025 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00002026 gui_print("Wiping %s...\n", Display_Name.c_str());
Ethan Yonker5eac2222014-06-11 12:22:55 -05002027 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
2028 gui_print("WARNING: This /data backup was made with %s file system!\n", Restore_File_System.c_str());
2029 gui_print("The backup may not boot unless you change back to %s.\n", Restore_File_System.c_str());
2030 if (!Wipe_Data_Without_Wiping_Media())
2031 return false;
2032 } else {
2033 if (!Wipe(Restore_File_System))
2034 return false;
2035 }
Dees_Troye58d5262012-09-21 12:27:57 -04002036 }
Dees_Troya13d74f2013-03-24 08:54:55 -05002037 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00002038 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04002039
2040 if (!Mount(true))
2041 return false;
2042
Dees_Troy4a2a1262012-09-18 09:33:47 -04002043 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002044 twrpTar tar;
2045 tar.setdir(Backup_Path);
2046 tar.setfn(Full_FileName);
2047 tar.backup_name = Backup_Name;
2048#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2049 string Password;
2050 DataManager::GetValue("tw_restore_password", Password);
2051 if (!Password.empty())
2052 tar.setpassword(Password);
2053#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002054 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002055 ret = false;
2056 else
2057 ret = true;
2058#ifdef HAVE_CAPABILITIES
2059 // Restore capabilities to the run-as binary
2060 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2061 struct vfs_cap_data cap_data;
2062 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2063
2064 memset(&cap_data, 0, sizeof(cap_data));
2065 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2066 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2067 cap_data.data[0].inheritable = 0;
2068 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2069 cap_data.data[1].inheritable = 0;
2070 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2071 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2072 } else {
2073 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2074 }
2075 }
2076#endif
2077 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002078}
2079
Ethan Yonker96af84a2015-01-05 14:58:36 -06002080bool TWPartition::Restore_Image(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size, string Restore_File_System) {
2081 string Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002082 double display_percent, progress_percent;
2083 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04002084
Dees_Troyda8b55a2012-12-12 19:18:30 +00002085 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04002086 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002087
Ethan Yonker96af84a2015-01-05 14:58:36 -06002088 if (Restore_File_System == "emmc") {
2089 if (!Flash_Image_DD(Full_FileName))
2090 return false;
2091 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
2092 if (!Flash_Image_FI(Full_FileName))
2093 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002094 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002095 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
2096 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
2097 DataManager::SetValue("tw_size_progress", size_progress);
2098 progress_percent = (display_percent / 100);
2099 DataManager::SetProgress((float)(progress_percent));
2100 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04002101 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002102}
Dees_Troy5bf43922012-09-07 16:07:55 -04002103
2104bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002105 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002106
Dees_Troyab10ee22012-09-21 14:27:30 -04002107 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002108 return false;
2109
Dees_Troy0550cfb2012-10-13 11:56:13 -04002110 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002111 if (Removable || Is_Encrypted) {
2112 if (!Mount(false))
2113 return true;
2114 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002115 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002116
2117 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002118 if (!ret || Size == 0) {
2119 if (!Get_Size_Via_df(Display_Error)) {
2120 if (!Was_Already_Mounted)
2121 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002122 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002123 }
2124 }
Dees_Troy51127312012-09-08 13:08:49 -04002125
Dees_Troy5bf43922012-09-07 16:07:55 -04002126 if (Has_Data_Media) {
2127 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002128 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002129 Used = du.Get_Folder_Size("/data");
2130 Backup_Size = Used;
2131 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002132 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002133 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002134 } else {
2135 if (!Was_Already_Mounted)
2136 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002137 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002138 }
Dees_Troye58d5262012-09-21 12:27:57 -04002139 } else if (Has_Android_Secure) {
2140 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002141 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002142 else {
2143 if (!Was_Already_Mounted)
2144 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002145 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002146 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002147 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002148 if (!Was_Already_Mounted)
2149 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002150 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002151}
Dees_Troy38bd7602012-09-14 13:33:53 -04002152
2153void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002154 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002155 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002156 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002157 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002158 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002159 Is_Present = true;
2160 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002161 return;
2162 }
2163 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002164 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002165 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002166 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002167 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002168 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002169 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002170}
2171
2172void TWPartition::Recreate_Media_Folder(void) {
2173 string Command;
2174
2175 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002176 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04002177 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002178 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00002179 LOGINFO("Recreating /data/media folder.\n");
xiaolu9416f4f2015-06-04 08:22:23 +08002180 mkdir("/data/media", 0770);
2181 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2182 if (!Internal_path.empty()) {
2183 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2184 mkdir(Internal_path.c_str(), 0770);
2185 }
2186#ifdef TW_INTERNAL_STORAGE_PATH
2187 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2188#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002189#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002190 // Afterwards, we will try to set the
2191 // default metadata that we were hopefully able to get during
2192 // early boot.
2193 tw_set_default_metadata("/data/media");
xiaolu9416f4f2015-06-04 08:22:23 +08002194 if (!Internal_path.empty())
2195 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002196#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002197 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002198 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002199 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002200 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002201}
Dees_Troye58d5262012-09-21 12:27:57 -04002202
2203void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002204 if (!Has_Android_Secure)
2205 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002206 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002207 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002208 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002209 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002210 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002211 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002212 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002213 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002214 }
2215}
bigbiff7cb4c332014-11-26 20:36:07 -05002216
2217uint64_t TWPartition::Get_Max_FileSize() {
2218 uint64_t maxFileSize = 0;
2219 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2220 const uint64_t constTB = (uint64_t) constGB * 1024;
2221 const uint64_t constPB = (uint64_t) constTB * 1024;
2222 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002223 if (Current_File_System == "ext4")
2224 maxFileSize = 16 * constTB; //16 TB
2225 else if (Current_File_System == "vfat")
2226 maxFileSize = 4 * constGB; //4 GB
2227 else if (Current_File_System == "ntfs")
2228 maxFileSize = 256 * constTB; //256 TB
2229 else if (Current_File_System == "exfat")
2230 maxFileSize = 16 * constPB; //16 PB
2231 else if (Current_File_System == "ext3")
2232 maxFileSize = 2 * constTB; //2 TB
2233 else if (Current_File_System == "f2fs")
2234 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002235 else
2236 maxFileSize = 100000000L;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002237 LOGINFO("Get_Max_FileSize::maxFileSize: %llu\n", maxFileSize);
bigbiff7cb4c332014-11-26 20:36:07 -05002238 return maxFileSize - 1;
2239}
2240
Ethan Yonker96af84a2015-01-05 14:58:36 -06002241bool TWPartition::Flash_Image(string Filename) {
2242 string Restore_File_System;
2243
2244 LOGINFO("Image filename is: %s\n", Filename.c_str());
2245
2246 if (Backup_Method == FILES) {
2247 LOGERR("Cannot flash images to file systems\n");
2248 return false;
2249 } else if (!Can_Flash_Img) {
2250 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2251 return false;
2252 } else {
2253 if (!Find_Partition_Size()) {
2254 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2255 return false;
2256 }
2257 unsigned long long image_size = TWFunc::Get_File_Size(Filename);
2258 if (image_size > Size) {
2259 LOGERR("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
2260 image_size, Filename.c_str(), Actual_Block_Device.c_str(), Size);
2261 return false;
2262 }
2263 if (Backup_Method == DD)
2264 return Flash_Image_DD(Filename);
2265 else if (Backup_Method == FLASH_UTILS)
2266 return Flash_Image_FI(Filename);
2267 }
2268
2269 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2270 return false;
2271}
2272
2273bool TWPartition::Flash_Image_DD(string Filename) {
2274 string Command;
2275
2276 gui_print("Flashing %s...\n", Display_Name.c_str());
codelover352b75e2015-03-29 02:44:07 +08002277 Command = "dd bs=8388608 if='" + Filename + "' of=" + Actual_Block_Device;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002278 LOGINFO("Flash command: '%s'\n", Command.c_str());
2279 TWFunc::Exec_Cmd(Command);
2280 return true;
2281}
2282
2283bool TWPartition::Flash_Image_FI(string Filename) {
2284 string Command;
2285
2286 gui_print("Flashing %s...\n", Display_Name.c_str());
2287 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2288 Command = "erase_image " + MTD_Name;
2289 LOGINFO("Erase command: '%s'\n", Command.c_str());
2290 TWFunc::Exec_Cmd(Command);
2291 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2292 LOGINFO("Flash command: '%s'\n", Command.c_str());
2293 TWFunc::Exec_Cmd(Command);
2294 return true;
2295}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002296
2297void TWPartition::Change_Mount_Read_Only(bool new_value) {
2298 Mount_Read_Only = new_value;
2299}
2300
2301int TWPartition::Check_Lifetime_Writes() {
2302 bool original_read_only = Mount_Read_Only;
2303 int ret = 1;
2304
2305 Mount_Read_Only = true;
2306 if (Mount(false)) {
2307 Find_Actual_Block_Device();
2308 string block = basename(Actual_Block_Device.c_str());
2309 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2310 string result;
2311 if (TWFunc::Path_Exists(file)) {
2312 if (TWFunc::read_file(file, result) != 0) {
2313 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2314 } else {
2315 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2316 if (result == "0") {
2317 ret = 0;
2318 }
2319 }
2320 } else {
2321 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2322 }
2323 UnMount(true);
2324 } else {
2325 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2326 }
2327 Mount_Read_Only = original_read_only;
2328 return ret;
2329}