blob: 6c5cf532ed642d177d116c230b5daea95318952c [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
977 if (Mount_Read_Only)
978 flags |= MS_RDONLY;
979
Dees_Troy22042032012-12-18 21:23:08 +0000980 if (Fstab_File_System == "yaffs2") {
981 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500982 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
983 if (Mount_Read_Only)
984 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +0000985 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
986 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
987 if (Display_Error)
988 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
989 else
990 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
991 return false;
992 } else {
993 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
994 return true;
995 }
996 } else {
997 struct stat st;
998 string test_path = Mount_Point;
999 if (stat(test_path.c_str(), &st) < 0) {
1000 if (Display_Error)
1001 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1002 else
1003 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1004 return false;
1005 }
1006 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1007 if (new_mode != st.st_mode) {
1008 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1009 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1010 if (Display_Error)
1011 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1012 else
1013 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1014 return false;
1015 }
1016 }
Dees_Troy22042032012-12-18 21:23:08 +00001017 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001018 }
thatc7572eb2015-03-31 18:55:30 +02001019 }
1020
1021 string mount_fs = Current_File_System;
1022 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1023 mount_fs = "texfat";
1024
1025 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001026 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1027 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001028#ifdef TW_NO_EXFAT_FUSE
1029 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001030 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001031 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001032 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001033 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001034 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001035 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001036 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 +00001037 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001038 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001039 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001040#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001041 if (!Removable && Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001042 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001043 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001044 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1045 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 +00001046 return false;
1047#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001048 }
1049#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001050 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001051
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001052 if (Removable)
1053 Update_Size(Display_Error);
1054
xiaolu9416f4f2015-06-04 08:22:23 +08001055 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001056 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001057 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001058 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001059 return true;
1060}
1061
1062bool TWPartition::UnMount(bool Display_Error) {
1063 if (Is_Mounted()) {
1064 int never_unmount_system;
1065
1066 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1067 if (never_unmount_system == 1 && Mount_Point == "/system")
1068 return true; // Never unmount system if you're not supposed to unmount it
1069
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001070 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001071 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001072
Dees_Troy38bd7602012-09-14 13:33:53 -04001073 if (!Symlink_Mount_Point.empty())
1074 umount(Symlink_Mount_Point.c_str());
1075
Dees_Troyb05ddee2013-01-28 20:24:50 +00001076 umount(Mount_Point.c_str());
1077 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001078 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001079 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001080 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001081 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001082 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001083 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001084 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001085 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001086 } else {
1087 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001088 }
1089}
1090
Gary Peck43acadf2012-11-21 21:19:01 -08001091bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001092 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001093 int check;
1094 string Layout_Filename = Mount_Point + "/.layout_version";
1095
Dees_Troy38bd7602012-09-14 13:33:53 -04001096 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001097 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001098 return false;
1099 }
1100
Dees_Troyc51f1f92012-09-20 15:32:13 -04001101 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001102 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001103
Dees_Troy16c2b312013-01-15 16:51:18 +00001104 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1105 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1106 else
1107 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001108
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001109 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001110 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001111 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001112 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001113 DataManager::GetValue(TW_RM_RF_VAR, check);
1114
Hashcodedabfd492013-08-29 22:45:30 -07001115 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001116 wiped = Wipe_RMRF();
1117 else if (New_File_System == "ext4")
1118 wiped = Wipe_EXT4();
1119 else if (New_File_System == "ext2" || New_File_System == "ext3")
1120 wiped = Wipe_EXT23(New_File_System);
1121 else if (New_File_System == "vfat")
1122 wiped = Wipe_FAT();
1123 else if (New_File_System == "exfat")
1124 wiped = Wipe_EXFAT();
1125 else if (New_File_System == "yaffs2")
1126 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001127 else if (New_File_System == "f2fs")
1128 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001129 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001130 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 +00001131 unlink("/.layout_version");
1132 return false;
1133 }
1134 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001135 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001136
Gary Pecke8bc5d72012-12-21 06:45:25 -08001137 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001138 if (Mount_Point == "/cache")
1139 DataManager::Output_Version();
1140
Dees_Troy16c2b312013-01-15 16:51:18 +00001141 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1142 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1143
1144 if (update_crypt) {
1145 Setup_File_System(false);
1146 if (Is_Encrypted && !Is_Decrypted) {
1147 // just wiped an encrypted partition back to its unencrypted state
1148 Is_Encrypted = false;
1149 Is_Decrypted = false;
1150 Decrypted_Block_Device = "";
1151 if (Mount_Point == "/data") {
1152 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1153 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1154 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001155 }
1156 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001157
1158 if (Mount_Point == "/data" && Has_Data_Media && recreate_media) {
1159 Recreate_Media_Folder();
1160 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001161 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001162 if (Is_Storage) {
1163 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001164 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001165 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001166}
1167
Gary Peck43acadf2012-11-21 21:19:01 -08001168bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001169 if (Is_File_System(Current_File_System))
1170 return Wipe(Current_File_System);
1171 else
1172 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001173}
1174
Dees_Troye58d5262012-09-21 12:27:57 -04001175bool TWPartition::Wipe_AndSec(void) {
1176 if (!Has_Android_Secure)
1177 return false;
1178
Dees_Troye58d5262012-09-21 12:27:57 -04001179 if (!Mount(true))
1180 return false;
1181
Dees_Troy2673cec2013-04-02 20:22:16 +00001182 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001183 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001184 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001185}
1186
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001187bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001188 if (Mount_Read_Only)
1189 return false;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001190 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/dosfsck"))
1191 return true;
1192 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1193 return true;
1194 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1195 return true;
1196 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1197 return true;
1198 return false;
1199}
1200
1201bool TWPartition::Repair() {
1202 string command;
1203
1204 if (Current_File_System == "vfat") {
1205 if (!TWFunc::Path_Exists("/sbin/dosfsck")) {
1206 gui_print("dosfsck does not exist! Cannot repair!\n");
1207 return false;
1208 }
1209 if (!UnMount(true))
1210 return false;
1211 gui_print("Repairing %s using dosfsck...\n", Display_Name.c_str());
1212 Find_Actual_Block_Device();
1213 command = "/sbin/dosfsck -y " + Actual_Block_Device;
1214 LOGINFO("Repair command: %s\n", command.c_str());
1215 if (TWFunc::Exec_Cmd(command) == 0) {
1216 gui_print("Done.\n");
1217 return true;
1218 } else {
1219 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1220 return false;
1221 }
1222 }
1223 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1224 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
1225 gui_print("e2fsck does not exist! Cannot repair!\n");
1226 return false;
1227 }
1228 if (!UnMount(true))
1229 return false;
1230 gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str());
1231 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001232 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001233 LOGINFO("Repair command: %s\n", command.c_str());
1234 if (TWFunc::Exec_Cmd(command) == 0) {
1235 gui_print("Done.\n");
1236 return true;
1237 } else {
1238 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1239 return false;
1240 }
1241 }
1242 if (Current_File_System == "exfat") {
1243 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
1244 gui_print("fsck.exfat does not exist! Cannot repair!\n");
1245 return false;
1246 }
1247 if (!UnMount(true))
1248 return false;
1249 gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str());
1250 Find_Actual_Block_Device();
1251 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1252 LOGINFO("Repair command: %s\n", command.c_str());
1253 if (TWFunc::Exec_Cmd(command) == 0) {
1254 gui_print("Done.\n");
1255 return true;
1256 } else {
1257 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1258 return false;
1259 }
1260 }
1261 if (Current_File_System == "f2fs") {
1262 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
1263 gui_print("fsck.f2fs does not exist! Cannot repair!\n");
1264 return false;
1265 }
1266 if (!UnMount(true))
1267 return false;
1268 gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str());
1269 Find_Actual_Block_Device();
1270 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1271 LOGINFO("Repair command: %s\n", command.c_str());
1272 if (TWFunc::Exec_Cmd(command) == 0) {
1273 gui_print("Done.\n");
1274 return true;
1275 } else {
1276 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1277 return false;
1278 }
1279 }
1280 return false;
1281}
1282
Ethan Yonkera2719152015-05-28 09:44:41 -05001283bool TWPartition::Can_Resize() {
1284 if (Mount_Read_Only)
1285 return false;
1286 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1287 return true;
1288 return false;
1289}
1290
1291bool TWPartition::Resize() {
1292 string command;
1293
1294 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1295 if (!Can_Repair()) {
1296 LOGERR("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1297 return false;
1298 }
1299 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
1300 gui_print("resize2fs does not exist! Cannot resize!\n");
1301 return false;
1302 }
1303 // Repair will unmount so no need to do it twice
1304 gui_print("Repairing %s before resizing.\n", Display_Name.c_str());
1305 if (!Repair())
1306 return false;
1307 gui_print("Resizing %s using resize2fs...\n", Display_Name.c_str());
1308 Find_Actual_Block_Device();
1309 command = "/sbin/resize2fs " + Actual_Block_Device;
1310 if (Length != 0) {
1311 unsigned int block_device_size;
1312 int fd, ret;
1313
1314 fd = open(Actual_Block_Device.c_str(), O_RDONLY);
1315 if (fd < 0) {
1316 LOGERR("Resize: Failed to open '%s'\n", Actual_Block_Device.c_str());
1317 return false;
1318 }
1319 ret = ioctl(fd, BLKGETSIZE, &block_device_size);
1320 close(fd);
1321 if (ret) {
1322 LOGERR("Resize: ioctl error\n");
1323 return false;
1324 }
1325 unsigned long long Actual_Size = (unsigned long long)(block_device_size) * 512LLU;
1326 unsigned long long Block_Count;
1327 if (Length < 0) {
1328 // Reduce overall size by this length
1329 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1330 } else {
1331 // This is the size, not a size reduction
1332 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1333 }
1334 char temp[256];
1335 sprintf(temp, "%llu", Block_Count);
1336 command += " ";
1337 command += temp;
1338 command += "K";
1339 }
1340 LOGINFO("Resize command: %s\n", command.c_str());
1341 if (TWFunc::Exec_Cmd(command) == 0) {
1342 Update_Size(true);
1343 gui_print("Done.\n");
1344 return true;
1345 } else {
1346 Update_Size(true);
1347 LOGERR("Unable to resize '%s'.\n", Mount_Point.c_str());
1348 return false;
1349 }
1350 }
1351 return false;
1352}
1353
bigbiff7abc5fe2015-01-17 16:53:12 -05001354bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
1355 if (Backup_Method == FILES) {
1356 return Backup_Tar(backup_folder, overall_size, other_backups_size, tar_fork_pid);
1357 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001358 else if (Backup_Method == DD)
1359 return Backup_DD(backup_folder);
1360 else if (Backup_Method == FLASH_UTILS)
1361 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001362 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001363 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001364}
1365
Dees_Troy43d8b002012-09-17 16:00:01 -04001366bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001367 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001368 char split_filename[512];
1369 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001370 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001371
Captain Throwbackff5935f2014-09-23 16:08:34 -04001372 sync();
1373
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001374 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001375 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001376 if (!TWFunc::Path_Exists(Full_Filename)) {
1377 // This is a split archive, we presume
1378 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001379 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001380 md5file = split_filename;
1381 md5file += ".md5";
1382 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001383 LOGERR("No md5 file found for '%s'.\n", split_filename);
1384 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001385 return false;
1386 }
1387 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001388 while (index < 1000) {
1389 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001390 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001391 return false;
1392 }
1393 index++;
1394 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001395 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001396 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001397 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001398 } else {
1399 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001400 md5file = Full_Filename + ".md5";
1401 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001402 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1403 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001404 return false;
1405 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001406 md5sum.setfn(Full_Filename);
1407 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001408 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001409 return false;
1410 } else
1411 return true;
1412 }
1413 return false;
1414}
1415
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001416bool 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 -08001417 string Restore_File_System;
1418
1419 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001420 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001421
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001422 Restore_File_System = Get_Restore_File_System(restore_folder);
1423
1424 if (Is_File_System(Restore_File_System))
1425 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1426 else if (Is_Image(Restore_File_System)) {
Ethan Yonkerf66324f2015-01-09 16:46:57 -06001427 return Restore_Image(restore_folder, total_restore_size, already_restored_size, Restore_File_System);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001428 }
1429
1430 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1431 return false;
1432}
1433
1434string TWPartition::Get_Restore_File_System(string restore_folder) {
1435 size_t first_period, second_period;
1436 string Restore_File_System;
1437
Gary Peck43acadf2012-11-21 21:19:01 -08001438 // Parse backup filename to extract the file system before wiping
1439 first_period = Backup_FileName.find(".");
1440 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001441 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001442 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001443 }
1444 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1445 second_period = Restore_File_System.find(".");
1446 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001447 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001448 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001449 }
1450 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001451 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001452 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001453}
1454
1455string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001456 if (Backup_Method == NONE)
1457 return "none";
1458 else if (Backup_Method == FILES)
1459 return "files";
1460 else if (Backup_Method == DD)
1461 return "dd";
1462 else if (Backup_Method == FLASH_UTILS)
1463 return "flash_utils";
1464 else
1465 return "undefined";
1466 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001467}
1468
1469bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001470 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001471 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001472 return 1;
1473}
1474
1475bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001476 bool Save_Data_Media = Has_Data_Media;
1477
1478 if (!UnMount(true))
1479 return false;
1480
Dees_Troy38bd7602012-09-14 13:33:53 -04001481 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001482 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001483#ifdef TW_INCLUDE_CRYPTO
1484 if (Is_Decrypted) {
1485 if (!UnMount(true))
1486 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001487 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001488 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1489 }
1490 }
1491#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001492 Is_Decrypted = false;
1493 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001494 Find_Actual_Block_Device();
Gary Pecke8bc5d72012-12-21 06:45:25 -08001495 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001496 Has_Data_Media = Save_Data_Media;
1497 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1498 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001499 if (Mount(false))
1500 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001501 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001502 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001503#ifndef TW_OEM_BUILD
Dees_Troy2673cec2013-04-02 20:22:16 +00001504 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Ethan Yonker83e82572014-04-04 10:59:28 -05001505#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001506 return true;
1507 } else {
1508 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001509 LOGERR("Unable to format to remove encryption.\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06001510 if (Has_Data_Media && Mount(false))
1511 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001512 return false;
1513 }
1514 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001515}
1516
1517void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001518 const char* type;
1519 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001520
Dees_Troy68cab492012-12-12 19:29:35 +00001521 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1522 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001523
Dees_Troy38bd7602012-09-14 13:33:53 -04001524 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001525 if (!Is_Present)
1526 return;
Dees_Troy51127312012-09-08 13:08:49 -04001527
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001528 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1529 if (blkid_do_fullprobe(pr)) {
1530 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001531 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001532 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001533 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001534
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001535 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001536 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001537 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001538 return;
1539 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001540
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001541 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001542 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001543}
1544
Gary Peck43acadf2012-11-21 21:19:01 -08001545bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001546 if (!UnMount(true))
1547 return false;
1548
Dees_Troy43d8b002012-09-17 16:00:01 -04001549 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001550 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001551
Dees_Troy2673cec2013-04-02 20:22:16 +00001552 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001553 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001554 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001555 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001556 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001557 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001558 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001559 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001560 return true;
1561 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001562 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001563 return false;
1564 }
1565 } else
1566 return Wipe_RMRF();
1567
1568 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001569}
1570
1571bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001572 Find_Actual_Block_Device();
1573 if (!Is_Present) {
1574 LOGERR("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1575 return false;
1576 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001577 if (!UnMount(true))
1578 return false;
1579
Dees_Troyb3265ab2013-08-30 02:59:14 +00001580#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001581 int ret;
1582 char *secontext = NULL;
1583
Dees_Troya95f55c2013-08-17 13:14:43 +00001584 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001585
Dees Troy99c8dbf2014-03-10 16:53:58 +00001586 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001587 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1588 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1589 } else {
1590 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1591 }
1592 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001593 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1594 return false;
1595 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001596 string sedir = Mount_Point + "/lost+found";
1597 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1598 rmdir(sedir.c_str());
1599 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001600 return true;
1601 }
1602#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001603 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001604 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001605
Dees_Troy2673cec2013-04-02 20:22:16 +00001606 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001607 Find_Actual_Block_Device();
1608 Command = "make_ext4fs";
1609 if (!Is_Decrypted && Length != 0) {
1610 // Only use length if we're not decrypted
1611 char len[32];
1612 sprintf(len, "%i", Length);
1613 Command += " -l ";
1614 Command += len;
1615 }
Dees_Troy5295d582013-09-06 15:51:08 +00001616 if (TWFunc::Path_Exists("/file_contexts")) {
1617 Command += " -S /file_contexts";
1618 }
1619 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001620 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001621 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001622 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001623 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001624 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001625 return true;
1626 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001627 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001628 return false;
1629 }
1630 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001631 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001632#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001633 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001634}
1635
1636bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001637 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001638
Dees_Troy43d8b002012-09-17 16:00:01 -04001639 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001640 if (!UnMount(true))
1641 return false;
1642
Dees_Troy2673cec2013-04-02 20:22:16 +00001643 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001644 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001645 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001646 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001647 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001648 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001649 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001650 return true;
1651 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001652 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001653 return false;
1654 }
1655 return true;
1656 }
1657 else
1658 return Wipe_RMRF();
1659
1660 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001661}
1662
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001663bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001664 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001665
1666 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1667 if (!UnMount(true))
1668 return false;
1669
Dees_Troy2673cec2013-04-02 20:22:16 +00001670 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001671 Find_Actual_Block_Device();
1672 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001673 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001674 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001675 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001676 return true;
1677 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001678 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001679 return false;
1680 }
1681 return true;
1682 }
1683 return false;
1684}
1685
Dees_Troy38bd7602012-09-14 13:33:53 -04001686bool TWPartition::Wipe_MTD() {
1687 if (!UnMount(true))
1688 return false;
1689
Dees_Troy2673cec2013-04-02 20:22:16 +00001690 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001691
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001692 mtd_scan_partitions();
1693 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1694 if (mtd == NULL) {
1695 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1696 return false;
1697 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001698
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001699 MtdWriteContext* ctx = mtd_write_partition(mtd);
1700 if (ctx == NULL) {
1701 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1702 return false;
1703 }
1704 if (mtd_erase_blocks(ctx, -1) == -1) {
1705 mtd_write_close(ctx);
1706 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1707 return false;
1708 }
1709 if (mtd_write_close(ctx) != 0) {
1710 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1711 return false;
1712 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001713 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001714 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001715 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001716 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001717}
1718
1719bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001720 if (!Mount(true))
1721 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06001722 // This is the only wipe that leaves the partition mounted, so we
1723 // must manually remove the partition from MTP if it is a storage
1724 // partition.
1725 if (Is_Storage)
1726 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001727
Dees_Troy2673cec2013-04-02 20:22:16 +00001728 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001729 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001730 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001731 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001732}
1733
Dees_Troye5017042013-08-29 16:38:55 +00001734bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001735 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001736
1737 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1738 if (!UnMount(true))
1739 return false;
1740
1741 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1742 Find_Actual_Block_Device();
dhacker29a3fa75f2015-01-17 19:42:33 -05001743 command = "mkfs.f2fs -t 1";
1744 if (!Is_Decrypted && Length != 0) {
1745 // Only use length if we're not decrypted
1746 char len[32];
1747 int mod_length = Length;
1748 if (Length < 0)
1749 mod_length *= -1;
1750 sprintf(len, "%i", mod_length);
1751 command += " -r ";
1752 command += len;
1753 }
1754 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001755 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001756 Recreate_AndSec_Folder();
1757 gui_print("Done.\n");
1758 return true;
1759 } else {
1760 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1761 return false;
1762 }
1763 return true;
1764 } else {
1765 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1766 return Wipe_RMRF();
1767 }
1768 return false;
1769}
1770
Dees_Troy51a0e822012-09-05 15:24:24 -04001771bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001772#ifdef TW_OEM_BUILD
1773 // In an OEM Build we want to do a full format
1774 return Wipe_Encryption();
1775#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001776 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001777
1778 // This handles wiping data on devices with "sdcard" in /data/media
1779 if (!Mount(true))
1780 return false;
1781
Dees_Troy2673cec2013-04-02 20:22:16 +00001782 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001783
1784 DIR* d;
1785 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001786 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001787 struct dirent* de;
1788 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001789 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001790 // The media folder is the "internal sdcard"
1791 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001792 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001793 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001794 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001795
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001796 dir = "/data/";
1797 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001798 if (de->d_type == DT_DIR) {
1799 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001800 } 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 +00001801 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001802 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001803 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001804 }
1805 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001806
Dees_Troy2673cec2013-04-02 20:22:16 +00001807 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001808 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001809 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001810 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001811 return false;
Ethan Yonker83e82572014-04-04 10:59:28 -05001812#endif // ifdef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001813}
1814
bigbiff7abc5fe2015-01-17 16:53:12 -05001815bool 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 -04001816 char back_name[255], split_index[5];
1817 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001818 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001819 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001820 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001821 twrpTar tar;
1822 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001823
Dees_Troy43d8b002012-09-17 16:00:01 -04001824 if (!Mount(true))
1825 return false;
1826
Dees_Troya13d74f2013-03-24 08:54:55 -05001827 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001828 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001829
1830 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001831 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001832
Dees_Troy83bd4832013-05-04 12:39:56 +00001833#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1834 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1835 if (use_encryption && Can_Encrypt_Backup) {
1836 tar.use_encryption = use_encryption;
1837 if (Use_Userdata_Encryption)
1838 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001839 string Password;
1840 DataManager::GetValue("tw_backup_password", Password);
1841 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001842 } else {
1843 use_encryption = false;
1844 }
1845#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001846
1847 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1848 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001849 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001850 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001851 Full_FileName = backup_folder + "/" + Backup_FileName;
1852 tar.setdir(Backup_Path);
1853 tar.setfn(Full_FileName);
1854 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001855 tar.partition_name = Backup_Name;
1856 tar.backup_folder = backup_folder;
bigbiff7abc5fe2015-01-17 16:53:12 -05001857 if (tar.createTarFork(overall_size, other_backups_size, tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00001858 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001859 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001860}
1861
1862bool TWPartition::Backup_DD(string backup_folder) {
codelover352b75e2015-03-29 02:44:07 +08001863 char back_name[255], block_size[32], dd_count[32];
1864 string Full_FileName, Command, DD_BS, DD_COUNT;
Dees_Troy43d8b002012-09-17 16:00:01 -04001865 int use_compression;
codelover352b75e2015-03-29 02:44:07 +08001866 unsigned long long DD_Block_Size, DD_Count;
Dees_Troy43d8b002012-09-17 16:00:01 -04001867
codelover352b75e2015-03-29 02:44:07 +08001868 DD_Block_Size = 16 * 1024 * 1024;
1869 while (Backup_Size % DD_Block_Size != 0) DD_Block_Size >>= 1;
1870
1871 DD_Count = Backup_Size / DD_Block_Size;
1872
1873 sprintf(dd_count, "%llu", DD_Count);
1874 DD_COUNT = dd_count;
1875
1876 sprintf(block_size, "%llu", DD_Block_Size);
1877 DD_BS = block_size;
igoriok87e3d932013-01-31 21:03:53 +02001878
Dees_Troyb46a6842012-09-25 11:06:46 -04001879 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001880 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001881
1882 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1883 Backup_FileName = back_name;
1884
1885 Full_FileName = backup_folder + "/" + Backup_FileName;
1886
codelover352b75e2015-03-29 02:44:07 +08001887 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + " count=" + DD_COUNT;
Dees_Troy2673cec2013-04-02 20:22:16 +00001888 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001889 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001890 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04001891 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001892 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001893 return false;
1894 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001895 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001896}
1897
1898bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001899 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001900 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001901 int use_compression;
1902
Dees_Troyb46a6842012-09-25 11:06:46 -04001903 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001904 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001905
1906 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1907 Backup_FileName = back_name;
1908
1909 Full_FileName = backup_folder + "/" + Backup_FileName;
1910
1911 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001912 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001913 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001914 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001915 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1916 // 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 +00001917 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001918 return false;
1919 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001920 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001921}
1922
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001923unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
1924 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
1925 if (restore_info.LoadValues() == 0) {
1926 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
1927 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
1928 return Restore_Size;
1929 }
1930 }
1931 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
1932
1933 Full_FileName = restore_folder + "/" + Backup_FileName;
1934
1935 if (Is_Image(Restore_File_System)) {
1936 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
1937 return Restore_Size;
1938 }
1939
1940 twrpTar tar;
1941 tar.setdir(Backup_Path);
1942 tar.setfn(Full_FileName);
1943 tar.backup_name = Backup_Name;
1944#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1945 string Password;
1946 DataManager::GetValue("tw_restore_password", Password);
1947 if (!Password.empty())
1948 tar.setpassword(Password);
1949#endif
1950 tar.partition_name = Backup_Name;
1951 tar.backup_folder = restore_folder;
1952 Restore_Size = tar.get_size();
1953 return Restore_Size;
1954}
1955
1956bool 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 -08001957 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001958 int index = 0;
1959 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00001960 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001961
Dees_Troye58d5262012-09-21 12:27:57 -04001962 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001963 if (!Wipe_AndSec())
1964 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001965 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001966 gui_print("Wiping %s...\n", Display_Name.c_str());
Ethan Yonker5eac2222014-06-11 12:22:55 -05001967 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
1968 gui_print("WARNING: This /data backup was made with %s file system!\n", Restore_File_System.c_str());
1969 gui_print("The backup may not boot unless you change back to %s.\n", Restore_File_System.c_str());
1970 if (!Wipe_Data_Without_Wiping_Media())
1971 return false;
1972 } else {
1973 if (!Wipe(Restore_File_System))
1974 return false;
1975 }
Dees_Troye58d5262012-09-21 12:27:57 -04001976 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001977 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001978 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001979
1980 if (!Mount(true))
1981 return false;
1982
Dees_Troy4a2a1262012-09-18 09:33:47 -04001983 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06001984 twrpTar tar;
1985 tar.setdir(Backup_Path);
1986 tar.setfn(Full_FileName);
1987 tar.backup_name = Backup_Name;
1988#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1989 string Password;
1990 DataManager::GetValue("tw_restore_password", Password);
1991 if (!Password.empty())
1992 tar.setpassword(Password);
1993#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001994 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00001995 ret = false;
1996 else
1997 ret = true;
1998#ifdef HAVE_CAPABILITIES
1999 // Restore capabilities to the run-as binary
2000 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2001 struct vfs_cap_data cap_data;
2002 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2003
2004 memset(&cap_data, 0, sizeof(cap_data));
2005 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2006 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2007 cap_data.data[0].inheritable = 0;
2008 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2009 cap_data.data[1].inheritable = 0;
2010 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2011 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2012 } else {
2013 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2014 }
2015 }
2016#endif
2017 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002018}
2019
Ethan Yonker96af84a2015-01-05 14:58:36 -06002020bool TWPartition::Restore_Image(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size, string Restore_File_System) {
2021 string Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002022 double display_percent, progress_percent;
2023 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04002024
Dees_Troyda8b55a2012-12-12 19:18:30 +00002025 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04002026 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002027
Ethan Yonker96af84a2015-01-05 14:58:36 -06002028 if (Restore_File_System == "emmc") {
2029 if (!Flash_Image_DD(Full_FileName))
2030 return false;
2031 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
2032 if (!Flash_Image_FI(Full_FileName))
2033 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002034 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002035 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
2036 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
2037 DataManager::SetValue("tw_size_progress", size_progress);
2038 progress_percent = (display_percent / 100);
2039 DataManager::SetProgress((float)(progress_percent));
2040 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04002041 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002042}
Dees_Troy5bf43922012-09-07 16:07:55 -04002043
2044bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002045 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002046
Dees_Troyab10ee22012-09-21 14:27:30 -04002047 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002048 return false;
2049
Dees_Troy0550cfb2012-10-13 11:56:13 -04002050 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002051 if (Removable || Is_Encrypted) {
2052 if (!Mount(false))
2053 return true;
2054 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002055 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002056
2057 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002058 if (!ret || Size == 0) {
2059 if (!Get_Size_Via_df(Display_Error)) {
2060 if (!Was_Already_Mounted)
2061 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002062 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002063 }
2064 }
Dees_Troy51127312012-09-08 13:08:49 -04002065
Dees_Troy5bf43922012-09-07 16:07:55 -04002066 if (Has_Data_Media) {
2067 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002068 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002069 Used = du.Get_Folder_Size("/data");
2070 Backup_Size = Used;
2071 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002072 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002073 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002074 } else {
2075 if (!Was_Already_Mounted)
2076 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002077 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002078 }
Dees_Troye58d5262012-09-21 12:27:57 -04002079 } else if (Has_Android_Secure) {
2080 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002081 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002082 else {
2083 if (!Was_Already_Mounted)
2084 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002085 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002086 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002087 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002088 if (!Was_Already_Mounted)
2089 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002090 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002091}
Dees_Troy38bd7602012-09-14 13:33:53 -04002092
2093void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002094 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002095 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002096 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002097 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002098 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002099 Is_Present = true;
2100 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002101 return;
2102 }
2103 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002104 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002105 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002106 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002107 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002108 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002109 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002110}
2111
2112void TWPartition::Recreate_Media_Folder(void) {
2113 string Command;
2114
2115 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002116 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04002117 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002118 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00002119 LOGINFO("Recreating /data/media folder.\n");
xiaolu9416f4f2015-06-04 08:22:23 +08002120 mkdir("/data/media", 0770);
2121 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2122 if (!Internal_path.empty()) {
2123 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2124 mkdir(Internal_path.c_str(), 0770);
2125 }
2126#ifdef TW_INTERNAL_STORAGE_PATH
2127 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2128#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002129#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002130 // Afterwards, we will try to set the
2131 // default metadata that we were hopefully able to get during
2132 // early boot.
2133 tw_set_default_metadata("/data/media");
xiaolu9416f4f2015-06-04 08:22:23 +08002134 if (!Internal_path.empty())
2135 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002136#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002137 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002138 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002139 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002140 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002141}
Dees_Troye58d5262012-09-21 12:27:57 -04002142
2143void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002144 if (!Has_Android_Secure)
2145 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002146 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002147 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002148 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002149 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002150 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002151 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002152 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002153 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002154 }
2155}
bigbiff7cb4c332014-11-26 20:36:07 -05002156
2157uint64_t TWPartition::Get_Max_FileSize() {
2158 uint64_t maxFileSize = 0;
2159 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2160 const uint64_t constTB = (uint64_t) constGB * 1024;
2161 const uint64_t constPB = (uint64_t) constTB * 1024;
2162 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002163 if (Current_File_System == "ext4")
2164 maxFileSize = 16 * constTB; //16 TB
2165 else if (Current_File_System == "vfat")
2166 maxFileSize = 4 * constGB; //4 GB
2167 else if (Current_File_System == "ntfs")
2168 maxFileSize = 256 * constTB; //256 TB
2169 else if (Current_File_System == "exfat")
2170 maxFileSize = 16 * constPB; //16 PB
2171 else if (Current_File_System == "ext3")
2172 maxFileSize = 2 * constTB; //2 TB
2173 else if (Current_File_System == "f2fs")
2174 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002175 else
2176 maxFileSize = 100000000L;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002177 LOGINFO("Get_Max_FileSize::maxFileSize: %llu\n", maxFileSize);
bigbiff7cb4c332014-11-26 20:36:07 -05002178 return maxFileSize - 1;
2179}
2180
Ethan Yonker96af84a2015-01-05 14:58:36 -06002181bool TWPartition::Flash_Image(string Filename) {
2182 string Restore_File_System;
2183
2184 LOGINFO("Image filename is: %s\n", Filename.c_str());
2185
2186 if (Backup_Method == FILES) {
2187 LOGERR("Cannot flash images to file systems\n");
2188 return false;
2189 } else if (!Can_Flash_Img) {
2190 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2191 return false;
2192 } else {
2193 if (!Find_Partition_Size()) {
2194 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2195 return false;
2196 }
2197 unsigned long long image_size = TWFunc::Get_File_Size(Filename);
2198 if (image_size > Size) {
2199 LOGERR("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
2200 image_size, Filename.c_str(), Actual_Block_Device.c_str(), Size);
2201 return false;
2202 }
2203 if (Backup_Method == DD)
2204 return Flash_Image_DD(Filename);
2205 else if (Backup_Method == FLASH_UTILS)
2206 return Flash_Image_FI(Filename);
2207 }
2208
2209 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2210 return false;
2211}
2212
2213bool TWPartition::Flash_Image_DD(string Filename) {
2214 string Command;
2215
2216 gui_print("Flashing %s...\n", Display_Name.c_str());
codelover352b75e2015-03-29 02:44:07 +08002217 Command = "dd bs=8388608 if='" + Filename + "' of=" + Actual_Block_Device;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002218 LOGINFO("Flash command: '%s'\n", Command.c_str());
2219 TWFunc::Exec_Cmd(Command);
2220 return true;
2221}
2222
2223bool TWPartition::Flash_Image_FI(string Filename) {
2224 string Command;
2225
2226 gui_print("Flashing %s...\n", Display_Name.c_str());
2227 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2228 Command = "erase_image " + MTD_Name;
2229 LOGINFO("Erase command: '%s'\n", Command.c_str());
2230 TWFunc::Exec_Cmd(Command);
2231 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2232 LOGINFO("Flash command: '%s'\n", Command.c_str());
2233 TWFunc::Exec_Cmd(Command);
2234 return true;
2235}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002236
2237void TWPartition::Change_Mount_Read_Only(bool new_value) {
2238 Mount_Read_Only = new_value;
2239}
2240
2241int TWPartition::Check_Lifetime_Writes() {
2242 bool original_read_only = Mount_Read_Only;
2243 int ret = 1;
2244
2245 Mount_Read_Only = true;
2246 if (Mount(false)) {
2247 Find_Actual_Block_Device();
2248 string block = basename(Actual_Block_Device.c_str());
2249 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2250 string result;
2251 if (TWFunc::Path_Exists(file)) {
2252 if (TWFunc::read_file(file, result) != 0) {
2253 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2254 } else {
2255 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2256 if (result == "0") {
2257 ret = 0;
2258 }
2259 }
2260 } else {
2261 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2262 }
2263 UnMount(true);
2264 } else {
2265 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2266 }
2267 Mount_Read_Only = original_read_only;
2268 return ret;
2269}