blob: 979272a44ee14f9d00ee85d68b94c272b13cfe18 [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>
thatf54e5392016-01-29 22:04:43 +010027#include <libgen.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050028#include <iostream>
29#include <sstream>
Ethan Yonker66a19492015-12-10 10:19:45 -060030#include <sys/param.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040031
Dees_Troy657c3092012-09-10 20:32:10 -040032#ifdef TW_INCLUDE_CRYPTO
33 #include "cutils/properties.h"
34#endif
35
bigbiff7b4c7a62015-01-01 19:44:14 -050036#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040037#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000038#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040040#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040041#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050042#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050043#include "twrpTar.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050044#include "twrpDU.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050045#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060046#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050047#include "gui/gui.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040048extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040049 #include "mtdutils/mtdutils.h"
50 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000051#ifdef USE_EXT4
52 #include "make_ext4fs.h"
53#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060054
55#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060056 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060057 #include "gpt/gpt.h"
Ethan Yonkerbc85b632015-08-09 12:48:14 -050058#else
59 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060060#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040061}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040062#ifdef HAVE_SELINUX
63#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060064#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040065#endif
Dees Troy4159aed2014-02-28 17:24:43 +000066#ifdef HAVE_CAPABILITIES
67#include <sys/capability.h>
68#include <sys/xattr.h>
69#include <linux/xattr.h>
70#endif
HashBanged974bb2016-01-30 14:20:09 -050071#include <sparse_format.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040072
bigbiff bigbiff9c754052013-01-09 09:09:08 -050073using namespace std;
74
Dees_Troya95f55c2013-08-17 13:14:43 +000075extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050076extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000077
Hashcode62bd9e02013-11-19 21:59:42 -080078struct flag_list {
79 const char *name;
80 unsigned flag;
81};
82
83static struct flag_list mount_flags[] = {
84 { "noatime", MS_NOATIME },
85 { "noexec", MS_NOEXEC },
86 { "nosuid", MS_NOSUID },
87 { "nodev", MS_NODEV },
88 { "nodiratime", MS_NODIRATIME },
89 { "ro", MS_RDONLY },
90 { "rw", 0 },
91 { "remount", MS_REMOUNT },
92 { "bind", MS_BIND },
93 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000094#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080095 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000096#endif
97#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080098 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000099#endif
100#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800101 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000102#endif
103#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800104 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000105#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800106 { "sync", MS_SYNCHRONOUS },
107 { "defaults", 0 },
108 { 0, 0 },
109};
110
that9e0593e2014-10-08 00:01:24 +0200111TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400112 Can_Be_Mounted = false;
113 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500114 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200115 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400116 Wipe_During_Factory_Reset = false;
117 Wipe_Available_in_GUI = false;
118 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400119 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400120 SubPartition_Of = "";
121 Symlink_Path = "";
122 Symlink_Mount_Point = "";
123 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400124 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400125 Actual_Block_Device = "";
126 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400127 Alternate_Block_Device = "";
128 Removable = false;
129 Is_Present = false;
130 Length = 0;
131 Size = 0;
132 Used = 0;
133 Free = 0;
134 Backup_Size = 0;
135 Can_Be_Encrypted = false;
136 Is_Encrypted = false;
137 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600138 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400139 Decrypted_Block_Device = "";
140 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500141 Backup_Display_Name = "";
142 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400143 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400144 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400145 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400146 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000147 Can_Encrypt_Backup = false;
148 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400149 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400150 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400151 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500152 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400153 Storage_Path = "";
154 Current_File_System = "";
155 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800156 Mount_Flags = 0;
157 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400158 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000159 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000160 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600161 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600162 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600163 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500164 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600165 Is_Adopted_Storage = false;
166 Adopted_GUID = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400167}
168
169TWPartition::~TWPartition(void) {
170 // Do nothing
171}
172
Dees_Troy5bf43922012-09-07 16:07:55 -0400173bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
174 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
175 int line_len = Line.size(), index = 0, item_index = 0;
176 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400177 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400178 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500179 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400180
Dees_Troy51127312012-09-08 13:08:49 -0400181 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500182 if (full_line[index] == 34)
183 skip = !skip;
184 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400185 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400186 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400187 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000188 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400189 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500190 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000191 Display_Name = full_line + 1;
192 Backup_Display_Name = Display_Name;
193 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400194 index = Mount_Point.size();
195 while (index < line_len) {
196 while (index < line_len && full_line[index] == '\0')
197 index++;
198 if (index >= line_len)
199 continue;
200 ptr = full_line + index;
201 if (item_index == 0) {
202 // File System
203 Fstab_File_System = ptr;
204 Current_File_System = ptr;
205 item_index++;
206 } else if (item_index == 1) {
207 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400208 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400209 MTD_Name = ptr;
210 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400211 } else if (Fstab_File_System == "bml") {
212 if (Mount_Point == "/boot")
213 MTD_Name = "boot";
214 else if (Mount_Point == "/recovery")
215 MTD_Name = "recovery";
216 Primary_Block_Device = ptr;
217 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000218 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 -0400219 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400220 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000221 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400222 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000223 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400224 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400225 } else {
226 Primary_Block_Device = ptr;
227 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400228 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400229 item_index++;
230 } else if (item_index > 1) {
231 if (*ptr == '/') {
232 // Alternate Block Device
233 Alternate_Block_Device = ptr;
234 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
235 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
236 // Partition length
237 ptr += 7;
238 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400239 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
240 // Custom flags, save for later so that new values aren't overwritten by defaults
241 ptr += 6;
242 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000243 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400244 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
245 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400246 } else {
247 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000248 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400249 }
250 }
251 while (index < line_len && full_line[index] != '\0')
252 index++;
253 }
254
255 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
256 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000257 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400258 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000259 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400260 return 0;
261 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400262 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400263 Setup_File_System(Display_Error);
264 if (Mount_Point == "/system") {
265 Display_Name = "System";
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_Troya13d74f2013-03-24 08:54:55 -0500269 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500270 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400271 } else if (Mount_Point == "/data") {
Dees Troyc657cc02015-01-16 22:48:47 +0000272 UnMount(false); // added in case /data is mounted as tmpfs for qcom hardware decrypt
Dees_Troy5bf43922012-09-07 16:07:55 -0400273 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500274 Backup_Display_Name = Display_Name;
275 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400276 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400277 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500278 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000279 Can_Encrypt_Backup = true;
280 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500281 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200282 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400283#ifdef TW_INCLUDE_CRYPTO
284 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400285 char crypto_blkdev[255];
286 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
287 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400288 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400289 DataManager::SetValue(TW_IS_DECRYPTED, 1);
290 Is_Encrypted = true;
291 Is_Decrypted = true;
292 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000293 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400294 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600295 if (Is_Present) {
Ethan Yonker253368a2014-11-25 15:00:52 -0600296 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 -0600297 if (cryptfs_check_footer() == 0) {
298 Is_Encrypted = true;
299 Is_Decrypted = false;
300 Can_Be_Mounted = false;
301 Current_File_System = "emmc";
302 Setup_Image(Display_Error);
303 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
Vojtech Bocek7e11ac52015-03-05 23:21:49 +0100304 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
Ethan Yonker71413f42014-02-26 13:36:08 -0600305 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
306 DataManager::SetValue("tw_crypto_display", "");
307 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500308 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
Ethan Yonker71413f42014-02-26 13:36:08 -0600309 }
310 } else {
311 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
312 }
Gary Peck82599a82012-11-21 16:23:12 -0800313 } else {
314 // Filesystem is not encrypted and the mount
315 // succeeded, so get it back to the original
316 // unmounted state
317 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400318 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500319 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400320 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400321#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500322 if (datamedia)
323 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400324#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400325 } else if (Mount_Point == "/cache") {
326 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500327 Backup_Display_Name = Display_Name;
328 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400329 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400330 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500331 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400332 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000333 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500334 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500335 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400336 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400337 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400338 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400339 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500340 Backup_Display_Name = Display_Name;
341 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400342 Is_SubPartition = true;
343 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400344 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500345 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000346 Can_Encrypt_Backup = true;
347 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400348 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400349 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400350 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500351 Backup_Display_Name = Display_Name;
352 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400353 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400354 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500355 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000356 Can_Encrypt_Backup = true;
357 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400358 } else if (Mount_Point == "/boot") {
359 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500360 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400361 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500362 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500363 } else if (Mount_Point == "/vendor") {
364 Display_Name = "Vendor";
365 Backup_Display_Name = Display_Name;
366 Storage_Name = Display_Name;
367 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400368 }
369#ifdef TW_EXTERNAL_STORAGE_PATH
370 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
371 Is_Storage = true;
372 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400373 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500374 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400375#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000376 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400377 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400378 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500379 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400380#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000381 }
Dees_Troy8170a922012-09-18 15:40:25 -0400382#ifdef TW_INTERNAL_STORAGE_PATH
383 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
384 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500385 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400386 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500387 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400388 }
389#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000390 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400391 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500392 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500393 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400394 }
395#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400396 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400397 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400398 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500399 if (Mount_Point == "/boot") {
400 Display_Name = "Boot";
401 Backup_Display_Name = Display_Name;
402 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600403 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500404 } else if (Mount_Point == "/recovery") {
405 Display_Name = "Recovery";
406 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600407 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500408 } else if (Mount_Point == "/system_image") {
409 Display_Name = "System Image";
410 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500411 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500412 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500413 } else if (Mount_Point == "/vendor_image") {
414 Display_Name = "Vendor Image";
415 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500416 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500417 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500418 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400419 }
420
Dees_Troy51127312012-09-08 13:08:49 -0400421 // Process any custom flags
422 if (Flags.size() > 0)
423 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400424 return true;
425}
426
Ketut Putu Kumajayabfb72b02015-08-10 16:40:55 +0200427bool TWPartition::Process_FS_Flags(string& Options, int& Flags) {
Hashcode62bd9e02013-11-19 21:59:42 -0800428 int i;
429 char *p;
430 char *savep;
431 char fs_options[250];
432
433 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
434 Options = "";
435
436 p = strtok_r(fs_options, ",", &savep);
437 while (p) {
438 /* Look for the flag "p" in the flag list "fl"
439 * If not found, the loop exits with fl[i].name being null.
440 */
441 for (i = 0; mount_flags[i].name; i++) {
442 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
443 Flags |= mount_flags[i].flag;
444 break;
445 }
446 }
447
448 if (!mount_flags[i].name) {
449 if (Options.size() > 0)
450 Options += ",";
451 Options += p;
452 }
453 p = strtok_r(NULL, ",", &savep);
454 }
455
456 return true;
457}
458
Dees_Troy51127312012-09-08 13:08:49 -0400459bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
460 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500461 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400462 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500463 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400464
465 strcpy(flags, Flags.c_str());
466 flags_len = Flags.size();
467 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500468 if (flags[index] == 34)
469 skip = !skip;
470 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400471 flags[index] = '\0';
472 }
473
474 index = 0;
475 while (index < flags_len) {
476 while (index < flags_len && flags[index] == '\0')
477 index++;
478 if (index >= flags_len)
479 continue;
480 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500481 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400482 if (strcmp(ptr, "removable") == 0) {
483 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600484 } else if (strncmp(ptr, "storage", 7) == 0) {
485 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600486 Is_Storage = true;
487 } else if (ptr_len == 9) {
488 ptr += 9;
489 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
490 LOGINFO("storage set to true\n");
491 Is_Storage = true;
492 } else {
493 LOGINFO("storage set to false\n");
494 Is_Storage = false;
495 }
496 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500497 } else if (strcmp(ptr, "settingsstorage") == 0) {
498 Is_Storage = true;
Xueferbfce5042015-04-14 02:02:05 +0800499 Is_Settings_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500500 } else if (strcmp(ptr, "andsec") == 0) {
501 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400502 } else if (strcmp(ptr, "canbewiped") == 0) {
503 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700504 } else if (strcmp(ptr, "usermrf") == 0) {
505 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500506 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
507 ptr += 7;
508 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
509 Can_Be_Backed_Up = true;
510 else
511 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400512 } else if (strcmp(ptr, "wipeingui") == 0) {
513 Can_Be_Wiped = true;
514 Wipe_Available_in_GUI = true;
515 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
516 Can_Be_Wiped = true;
517 Wipe_Available_in_GUI = true;
518 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500519 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000520 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400521 Is_SubPartition = true;
522 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000523 } else if (strcmp(ptr, "ignoreblkid") == 0) {
524 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000525 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
526 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500527 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400528 ptr += 8;
529 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500530 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
531 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400532 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500533 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400534 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500535 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
536 Display_Name.resize(Display_Name.size() - 1);
537 }
538 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
539 has_storage_name = true;
540 ptr += 11;
541 if (*ptr == '\"') ptr++;
542 Storage_Name = ptr;
543 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
544 Storage_Name.resize(Storage_Name.size() - 1);
545 }
546 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
547 has_backup_name = true;
548 ptr += 10;
549 if (*ptr == '\"') ptr++;
550 Backup_Display_Name = ptr;
551 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
552 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
553 }
554 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400555 ptr += 10;
Ethan Yonkera2719152015-05-28 09:44:41 -0500556 Format_Block_Size = (unsigned long)(atol(ptr));
Dees_Troya13d74f2013-03-24 08:54:55 -0500557 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400558 ptr += 7;
559 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000560 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
561 ptr += 17;
562 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
563 Can_Encrypt_Backup = true;
564 else
565 Can_Encrypt_Backup = false;
566 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
567 ptr += 21;
568 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
569 Can_Encrypt_Backup = true;
570 Use_Userdata_Encryption = true;
571 } else {
572 Use_Userdata_Encryption = false;
573 }
Hashcode62bd9e02013-11-19 21:59:42 -0800574 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
575 ptr += 8;
576 if (*ptr == '\"') ptr++;
577
578 Mount_Options = ptr;
579 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
580 Mount_Options.resize(Mount_Options.size() - 1);
581 }
582 Process_FS_Flags(Mount_Options, Mount_Flags);
Ethan Yonker253368a2014-11-25 15:00:52 -0600583 } else if ((ptr_len > 12 && strncmp(ptr, "encryptable=", 12) == 0) || (ptr_len > 13 && strncmp(ptr, "forceencrypt=", 13) == 0)) {
584 ptr += 12;
585 if (*ptr == '=') ptr++;
586 if (*ptr == '\"') ptr++;
587
588 Crypto_Key_Location = ptr;
589 if (Crypto_Key_Location.substr(Crypto_Key_Location.size() - 1, 1) == "\"") {
590 Crypto_Key_Location.resize(Crypto_Key_Location.size() - 1);
591 }
592 } else if (ptr_len > 8 && strncmp(ptr, "mounttodecrypt", 14) == 0) {
593 Mount_To_Decrypt = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600594 } else if (strncmp(ptr, "flashimg", 8) == 0) {
595 if (ptr_len == 8) {
596 Can_Flash_Img = true;
597 } else if (ptr_len == 10) {
598 ptr += 9;
599 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
600 Can_Flash_Img = true;
601 } else {
602 Can_Flash_Img = false;
603 }
604 }
Dees_Troy51127312012-09-08 13:08:49 -0400605 } else {
606 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000607 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400608 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000609 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400610 }
611 while (index < flags_len && flags[index] != '\0')
612 index++;
613 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500614 if (has_display_name && !has_storage_name)
615 Storage_Name = Display_Name;
616 if (!has_display_name && has_storage_name)
617 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000618 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500619 Backup_Display_Name = Display_Name;
620 if (!has_display_name && has_backup_name)
621 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400622 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400623}
624
Dees_Troy5bf43922012-09-07 16:07:55 -0400625bool TWPartition::Is_File_System(string File_System) {
626 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400627 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400628 File_System == "ext4" ||
629 File_System == "vfat" ||
630 File_System == "ntfs" ||
631 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500632 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000633 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400634 File_System == "auto")
635 return true;
636 else
637 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400638}
639
Dees_Troy5bf43922012-09-07 16:07:55 -0400640bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400641 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400642 return true;
643 else
644 return false;
645}
646
Dees_Troy51127312012-09-08 13:08:49 -0400647bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400648 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400649 if (mkdir(Path.c_str(), 0777) == -1) {
650 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500651 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400652 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000653 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400654 return false;
655 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000656 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400657 return true;
658 }
659 }
660 return true;
661}
662
Dees_Troy5bf43922012-09-07 16:07:55 -0400663void TWPartition::Setup_File_System(bool Display_Error) {
664 struct statfs st;
665
666 Can_Be_Mounted = true;
667 Can_Be_Wiped = true;
668
Dees_Troy5bf43922012-09-07 16:07:55 -0400669 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400670 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400671 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
672 Backup_Name = Display_Name;
673 Backup_Method = FILES;
674}
675
676void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400677 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
678 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800679 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400680 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800681 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400682 Backup_Method = FLASH_UTILS;
683 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000684 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 -0400685 if (Find_Partition_Size()) {
686 Used = Size;
687 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400688 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400689 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000690 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400691 else
Dees Troyd932ce12013-10-18 17:12:59 +0000692 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400693 }
694}
695
Dees_Troye58d5262012-09-21 12:27:57 -0400696void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500697 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400698 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500699 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400700 Has_Android_Secure = true;
701 Symlink_Path = Mount_Point + "/.android_secure";
702 Symlink_Mount_Point = "/and-sec";
703 Backup_Path = Symlink_Mount_Point;
704 Make_Dir("/and-sec", true);
705 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600706 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400707}
708
that9e0593e2014-10-08 00:01:24 +0200709void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500710 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600711 if (Storage_Name.empty() || Storage_Name == "Data")
712 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500713 Has_Data_Media = true;
714 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600715 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500716 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600717 if (Mount_Point == "/data") {
718 Is_Settings_Storage = true;
719 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
720 Make_Dir("/emmc", false);
721 Symlink_Mount_Point = "/emmc";
722 } else {
723 Make_Dir("/sdcard", false);
724 Symlink_Mount_Point = "/sdcard";
725 }
726 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
727 Storage_Path = Mount_Point + "/media/0";
728 Symlink_Path = Storage_Path;
729 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
730 UnMount(true);
731 }
732 DataManager::SetValue("tw_has_internal", 1);
733 DataManager::SetValue("tw_has_data_media", 1);
734 du.add_absolute_dir(Mount_Point + "/misc/vold");
735 du.add_absolute_dir(Mount_Point + "/.layout_version");
736 du.add_absolute_dir(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500737 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600738 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
739 Storage_Path = Mount_Point + "/media/0";
740 Symlink_Path = Storage_Path;
741 UnMount(true);
742 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500743 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600744 du.add_absolute_dir(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500745}
746
Dees_Troy5bf43922012-09-07 16:07:55 -0400747void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
748 char device[512], realDevice[512];
749
750 strcpy(device, Block.c_str());
751 memset(realDevice, 0, sizeof(realDevice));
752 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
753 {
754 strcpy(device, realDevice);
755 memset(realDevice, 0, sizeof(realDevice));
756 }
757
758 if (device[0] != '/') {
759 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000760 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400761 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000762 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400763 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400764 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400765 Block = device;
766 return;
767 }
768}
769
Dees_Troy8e337f32012-10-13 22:07:49 -0400770void TWPartition::Mount_Storage_Retry(void) {
771 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500772 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400773 int retry_count = 5;
774 while (retry_count > 0 && !Mount(false)) {
775 usleep(500000);
776 retry_count--;
777 }
778 Mount(true);
779 }
780}
781
Dees_Troy38bd7602012-09-14 13:33:53 -0400782bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
783 FILE *fp = NULL;
784 char line[255];
785
786 fp = fopen("/proc/mtd", "rt");
787 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000788 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400789 return false;
790 }
791
792 while (fgets(line, sizeof(line), fp) != NULL)
793 {
794 char device[32], label[32];
795 unsigned long size = 0;
796 char* fstype = NULL;
797 int deviceId;
798
799 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
800
801 // Skip header and blank lines
802 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
803 continue;
804
805 // Strip off the trailing " from the label
806 label[strlen(label)-1] = '\0';
807
808 if (strcmp(label, MTD_Name.c_str()) == 0) {
809 // We found our device
810 // Strip off the trailing : from the device
811 device[strlen(device)-1] = '\0';
812 if (sscanf(device,"mtd%d", &deviceId) == 1) {
813 sprintf(device, "/dev/block/mtdblock%d", deviceId);
814 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000815 fclose(fp);
816 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400817 }
818 }
819 }
820 fclose(fp);
821
822 return false;
823}
824
Dees_Troy51127312012-09-08 13:08:49 -0400825bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
826 struct statfs st;
827 string Local_Path = Mount_Point + "/.";
828
829 if (!Mount(Display_Error))
830 return false;
831
832 if (statfs(Local_Path.c_str(), &st) != 0) {
833 if (!Removable) {
834 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000835 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400836 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000837 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400838 }
839 return false;
840 }
841 Size = (st.f_blocks * st.f_bsize);
842 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
843 Free = (st.f_bfree * st.f_bsize);
844 Backup_Size = Used;
845 return true;
846}
847
848bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400849 FILE* fp;
850 char command[255], line[512];
851 int include_block = 1;
852 unsigned int min_len;
853
854 if (!Mount(Display_Error))
855 return false;
856
Dees_Troy38bd7602012-09-14 13:33:53 -0400857 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400858 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200859 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400860 fp = fopen("/tmp/dfoutput.txt", "rt");
861 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000862 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400863 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400864 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400865
866 while (fgets(line, sizeof(line), fp) != NULL)
867 {
868 unsigned long blocks, used, available;
869 char device[64];
870 char tmpString[64];
871
872 if (strncmp(line, "Filesystem", 10) == 0)
873 continue;
874 if (strlen(line) < min_len) {
875 include_block = 0;
876 continue;
877 }
878 if (include_block) {
879 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
880 } else {
881 // The device block string is so long that the df information is on the next line
882 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400883 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400884 while (tmpString[space_count] == 32)
885 space_count++;
886 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
887 }
888
889 // Adjust block size to byte size
890 Size = blocks * 1024ULL;
891 Used = used * 1024ULL;
892 Free = available * 1024ULL;
893 Backup_Size = Used;
894 }
895 fclose(fp);
896 return true;
897}
898
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600899unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600900 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -0600901
902 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600903}
904
Dees_Troy5bf43922012-09-07 16:07:55 -0400905bool TWPartition::Find_Partition_Size(void) {
906 FILE* fp;
907 char line[512];
908 string tmpdevice;
909
igoriok87e3d932013-01-31 21:03:53 +0200910 fp = fopen("/proc/dumchar_info", "rt");
911 if (fp != NULL) {
912 while (fgets(line, sizeof(line), fp) != NULL)
913 {
914 char label[32], device[32];
915 unsigned long size = 0;
916
thatd43bf2d2014-09-21 23:13:02 +0200917 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200918
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500919 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200920 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
921 continue;
922
923 tmpdevice = "/dev/";
924 tmpdevice += label;
925 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
926 Size = size;
927 fclose(fp);
928 return true;
929 }
930 }
931 }
932
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600933 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
934 if (ioctl_size) {
935 Size = ioctl_size;
936 return true;
937 }
938
Dees_Troy5bf43922012-09-07 16:07:55 -0400939 // In this case, we'll first get the partitions we care about (with labels)
940 fp = fopen("/proc/partitions", "rt");
941 if (fp == NULL)
942 return false;
943
944 while (fgets(line, sizeof(line), fp) != NULL)
945 {
946 unsigned long major, minor, blocks;
947 char device[512];
948 char tmpString[64];
949
Dees_Troy63c8df72012-09-10 14:02:05 -0400950 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400951 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
952
953 tmpdevice = "/dev/block/";
954 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400955 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400956 // Adjust block size to byte size
957 Size = blocks * 1024ULL;
958 fclose(fp);
959 return true;
960 }
961 }
962 fclose(fp);
963 return false;
964}
965
Dees_Troy5bf43922012-09-07 16:07:55 -0400966bool TWPartition::Is_Mounted(void) {
967 if (!Can_Be_Mounted)
968 return false;
969
970 struct stat st1, st2;
971 string test_path;
972
973 // Check to see if the mount point directory exists
974 test_path = Mount_Point + "/.";
975 if (stat(test_path.c_str(), &st1) != 0) return false;
976
977 // Check to see if the directory above the mount point exists
978 test_path = Mount_Point + "/../.";
979 if (stat(test_path.c_str(), &st2) != 0) return false;
980
981 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
982 int ret = (st1.st_dev != st2.st_dev) ? true : false;
983
984 return ret;
985}
986
987bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000988 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500989 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000990
Dees_Troy5bf43922012-09-07 16:07:55 -0400991 if (Is_Mounted()) {
992 return true;
993 } else if (!Can_Be_Mounted) {
994 return false;
995 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400996
997 Find_Actual_Block_Device();
998
999 // Check the current file system before mounting
1000 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001001 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001002 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 +00001003 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001004 string result;
1005 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001006 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001007 Current_File_System = "vfat";
1008 } else {
1009#ifdef TW_NO_EXFAT_FUSE
1010 UnMount(false);
1011 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1012 // Some kernels let us mount vfat as exfat which doesn't work out too well
1013#else
1014 exfat_mounted = 1;
1015#endif
1016 }
1017 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001018
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001019 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001020 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001021 string Ntfsmount_Binary = "";
1022
1023 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1024 Ntfsmount_Binary = "ntfs-3g";
1025 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1026 Ntfsmount_Binary = "mount.ntfs";
1027
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001028 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001029 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001030 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001031 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001032 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001033
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001034 if (TWFunc::Exec_Cmd(cmd) == 0) {
1035 return true;
1036 } else {
1037 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1038 }
1039 }
1040
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001041 if (Mount_Read_Only)
1042 flags |= MS_RDONLY;
1043
Dees_Troy22042032012-12-18 21:23:08 +00001044 if (Fstab_File_System == "yaffs2") {
1045 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001046 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1047 if (Mount_Read_Only)
1048 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001049 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1050 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1051 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001052 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001053 else
1054 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1055 return false;
1056 } else {
1057 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1058 return true;
1059 }
1060 } else {
1061 struct stat st;
1062 string test_path = Mount_Point;
1063 if (stat(test_path.c_str(), &st) < 0) {
1064 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001065 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001066 else
1067 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1068 return false;
1069 }
1070 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1071 if (new_mode != st.st_mode) {
1072 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1073 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1074 if (Display_Error)
1075 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1076 else
1077 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1078 return false;
1079 }
1080 }
Dees_Troy22042032012-12-18 21:23:08 +00001081 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001082 }
thatc7572eb2015-03-31 18:55:30 +02001083 }
1084
1085 string mount_fs = Current_File_System;
1086 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1087 mount_fs = "texfat";
1088
1089 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001090 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1091 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001092#ifdef TW_NO_EXFAT_FUSE
1093 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001094 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001095 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001096 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001097 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001098 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001099 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001100 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 +00001101 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001102 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001103 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001104#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001105 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001106 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001107 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001108 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1109 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 +00001110 return false;
1111#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001112 }
1113#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001114 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001115
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001116 if (Removable)
1117 Update_Size(Display_Error);
1118
xiaolu9416f4f2015-06-04 08:22:23 +08001119 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001120 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001121 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001122 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001123 return true;
1124}
1125
1126bool TWPartition::UnMount(bool Display_Error) {
1127 if (Is_Mounted()) {
1128 int never_unmount_system;
1129
1130 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1131 if (never_unmount_system == 1 && Mount_Point == "/system")
1132 return true; // Never unmount system if you're not supposed to unmount it
1133
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001134 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001135 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001136
Dees_Troy38bd7602012-09-14 13:33:53 -04001137 if (!Symlink_Mount_Point.empty())
1138 umount(Symlink_Mount_Point.c_str());
1139
Dees_Troyb05ddee2013-01-28 20:24:50 +00001140 umount(Mount_Point.c_str());
1141 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001142 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001143 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001144 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001145 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001146 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001147 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001148 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001149 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001150 } else {
1151 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001152 }
1153}
1154
Gary Peck43acadf2012-11-21 21:19:01 -08001155bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001156 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001157 int check;
1158 string Layout_Filename = Mount_Point + "/.layout_version";
1159
Dees_Troy38bd7602012-09-14 13:33:53 -04001160 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001161 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001162 return false;
1163 }
1164
Dees_Troyc51f1f92012-09-20 15:32:13 -04001165 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001166 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001167
Dees_Troy16c2b312013-01-15 16:51:18 +00001168 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1169 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1170 else
1171 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001172
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001173 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001174 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001175 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001176 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001177 DataManager::GetValue(TW_RM_RF_VAR, check);
1178
Hashcodedabfd492013-08-29 22:45:30 -07001179 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001180 wiped = Wipe_RMRF();
1181 else if (New_File_System == "ext4")
1182 wiped = Wipe_EXT4();
1183 else if (New_File_System == "ext2" || New_File_System == "ext3")
1184 wiped = Wipe_EXT23(New_File_System);
1185 else if (New_File_System == "vfat")
1186 wiped = Wipe_FAT();
1187 else if (New_File_System == "exfat")
1188 wiped = Wipe_EXFAT();
1189 else if (New_File_System == "yaffs2")
1190 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001191 else if (New_File_System == "f2fs")
1192 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001193 else if (New_File_System == "ntfs")
1194 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001195 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001196 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 +00001197 unlink("/.layout_version");
1198 return false;
1199 }
1200 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001201 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001202
Gary Pecke8bc5d72012-12-21 06:45:25 -08001203 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001204 if (Mount_Point == "/cache")
1205 DataManager::Output_Version();
1206
Dees_Troy16c2b312013-01-15 16:51:18 +00001207 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1208 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1209
1210 if (update_crypt) {
1211 Setup_File_System(false);
1212 if (Is_Encrypted && !Is_Decrypted) {
1213 // just wiped an encrypted partition back to its unencrypted state
1214 Is_Encrypted = false;
1215 Is_Decrypted = false;
1216 Decrypted_Block_Device = "";
1217 if (Mount_Point == "/data") {
1218 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1219 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1220 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001221 }
1222 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001223
Ethan Yonker66a19492015-12-10 10:19:45 -06001224 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001225 Recreate_Media_Folder();
1226 }
Matt Mower209c9632016-01-20 12:08:35 -06001227 if (Is_Storage && Mount(false))
1228 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001229 }
Matt Mower209c9632016-01-20 12:08:35 -06001230
Gary Pecke8bc5d72012-12-21 06:45:25 -08001231 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001232}
1233
Gary Peck43acadf2012-11-21 21:19:01 -08001234bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001235 if (Is_File_System(Current_File_System))
1236 return Wipe(Current_File_System);
1237 else
1238 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001239}
1240
Dees_Troye58d5262012-09-21 12:27:57 -04001241bool TWPartition::Wipe_AndSec(void) {
1242 if (!Has_Android_Secure)
1243 return false;
1244
Dees_Troye58d5262012-09-21 12:27:57 -04001245 if (!Mount(true))
1246 return false;
1247
Ethan Yonker74db1572015-10-28 12:44:49 -05001248 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001249 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001250 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001251}
1252
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001253bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001254 if (Mount_Read_Only)
1255 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001256 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001257 return true;
1258 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1259 return true;
1260 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1261 return true;
1262 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1263 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001264 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001265 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001266 return false;
1267}
1268
1269bool TWPartition::Repair() {
1270 string command;
1271
1272 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001273 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001274 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001275 return false;
1276 }
1277 if (!UnMount(true))
1278 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001279 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001280 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001281 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001282 LOGINFO("Repair command: %s\n", command.c_str());
1283 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001284 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001285 return true;
1286 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001287 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001288 return false;
1289 }
1290 }
1291 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1292 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001293 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001294 return false;
1295 }
1296 if (!UnMount(true))
1297 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001298 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001299 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001300 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001301 LOGINFO("Repair command: %s\n", command.c_str());
1302 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001303 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001304 return true;
1305 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001306 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001307 return false;
1308 }
1309 }
1310 if (Current_File_System == "exfat") {
1311 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001312 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001313 return false;
1314 }
1315 if (!UnMount(true))
1316 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001317 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001318 Find_Actual_Block_Device();
1319 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1320 LOGINFO("Repair command: %s\n", command.c_str());
1321 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001322 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001323 return true;
1324 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001325 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001326 return false;
1327 }
1328 }
1329 if (Current_File_System == "f2fs") {
1330 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001331 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001332 return false;
1333 }
1334 if (!UnMount(true))
1335 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001336 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001337 Find_Actual_Block_Device();
1338 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1339 LOGINFO("Repair command: %s\n", command.c_str());
1340 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001341 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001342 return true;
1343 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001344 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001345 return false;
1346 }
1347 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001348 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001349 string Ntfsfix_Binary;
1350 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1351 Ntfsfix_Binary = "ntfsfix";
1352 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1353 Ntfsfix_Binary = "fsck.ntfs";
1354 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001355 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001356 return false;
1357 }
1358 if (!UnMount(true))
1359 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001360 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001361 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001362 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001363 LOGINFO("Repair command: %s\n", command.c_str());
1364 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001365 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001366 return true;
1367 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001368 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001369 return false;
1370 }
1371 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001372 return false;
1373}
1374
Ethan Yonkera2719152015-05-28 09:44:41 -05001375bool TWPartition::Can_Resize() {
1376 if (Mount_Read_Only)
1377 return false;
1378 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1379 return true;
1380 return false;
1381}
1382
1383bool TWPartition::Resize() {
1384 string command;
1385
1386 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1387 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001388 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1389 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001390 return false;
1391 }
1392 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001393 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1394 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001395 return false;
1396 }
1397 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001398 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001399 if (!Repair())
1400 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001401 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001402 Find_Actual_Block_Device();
1403 command = "/sbin/resize2fs " + Actual_Block_Device;
1404 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001405 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1406 if (Actual_Size == 0)
1407 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001408
Ethan Yonkera2719152015-05-28 09:44:41 -05001409 unsigned long long Block_Count;
1410 if (Length < 0) {
1411 // Reduce overall size by this length
1412 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1413 } else {
1414 // This is the size, not a size reduction
1415 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1416 }
1417 char temp[256];
1418 sprintf(temp, "%llu", Block_Count);
1419 command += " ";
1420 command += temp;
1421 command += "K";
1422 }
1423 LOGINFO("Resize command: %s\n", command.c_str());
1424 if (TWFunc::Exec_Cmd(command) == 0) {
1425 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001426 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001427 return true;
1428 } else {
1429 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001430 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001431 return false;
1432 }
1433 }
1434 return false;
1435}
1436
bigbiff7abc5fe2015-01-17 16:53:12 -05001437bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
1438 if (Backup_Method == FILES) {
1439 return Backup_Tar(backup_folder, overall_size, other_backups_size, tar_fork_pid);
1440 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001441 else if (Backup_Method == DD)
1442 return Backup_DD(backup_folder);
1443 else if (Backup_Method == FLASH_UTILS)
1444 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001445 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001446 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001447}
1448
Dees_Troy43d8b002012-09-17 16:00:01 -04001449bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001450 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001451 char split_filename[512];
1452 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001453 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001454
Captain Throwbackff5935f2014-09-23 16:08:34 -04001455 sync();
1456
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001457 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001458 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001459 if (!TWFunc::Path_Exists(Full_Filename)) {
1460 // This is a split archive, we presume
1461 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001462 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001463 md5file = split_filename;
1464 md5file += ".md5";
1465 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001466 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(split_filename));
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001467 return false;
1468 }
1469 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001470 while (index < 1000) {
1471 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001472 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001473 return false;
1474 }
1475 index++;
1476 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001477 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001478 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001479 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001480 } else {
1481 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001482 md5file = Full_Filename + ".md5";
1483 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001484 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(split_filename));
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001485 return false;
1486 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001487 md5sum.setfn(Full_Filename);
1488 if (md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001489 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001490 return false;
1491 } else
1492 return true;
1493 }
1494 return false;
1495}
1496
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001497bool 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 -08001498 string Restore_File_System;
1499
Matt Mower3c366972015-12-25 19:28:31 -06001500 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Dees_Troy2673cec2013-04-02 20:22:16 +00001501 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001502
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001503 Restore_File_System = Get_Restore_File_System(restore_folder);
1504
1505 if (Is_File_System(Restore_File_System))
1506 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1507 else if (Is_Image(Restore_File_System)) {
Ethan Yonkerf66324f2015-01-09 16:46:57 -06001508 return Restore_Image(restore_folder, total_restore_size, already_restored_size, Restore_File_System);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001509 }
1510
1511 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1512 return false;
1513}
1514
1515string TWPartition::Get_Restore_File_System(string restore_folder) {
1516 size_t first_period, second_period;
1517 string Restore_File_System;
1518
Gary Peck43acadf2012-11-21 21:19:01 -08001519 // Parse backup filename to extract the file system before wiping
1520 first_period = Backup_FileName.find(".");
1521 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001522 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001523 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001524 }
1525 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1526 second_period = Restore_File_System.find(".");
1527 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001528 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001529 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001530 }
1531 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001532 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001533 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001534}
1535
1536string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001537 if (Backup_Method == NONE)
1538 return "none";
1539 else if (Backup_Method == FILES)
1540 return "files";
1541 else if (Backup_Method == DD)
1542 return "dd";
1543 else if (Backup_Method == FLASH_UTILS)
1544 return "flash_utils";
1545 else
1546 return "undefined";
1547 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001548}
1549
1550bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001551 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001552 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001553 return 1;
1554}
1555
1556bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001557 bool Save_Data_Media = Has_Data_Media;
1558
1559 if (!UnMount(true))
1560 return false;
1561
Dees_Troy38bd7602012-09-14 13:33:53 -04001562 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001563 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001564#ifdef TW_INCLUDE_CRYPTO
1565 if (Is_Decrypted) {
1566 if (!UnMount(true))
1567 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001568 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001569 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1570 }
1571 }
1572#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001573 Is_Decrypted = false;
1574 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001575 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001576 if (Crypto_Key_Location == "footer") {
1577 int newlen, fd;
1578 if (Length != 0) {
1579 newlen = Length;
1580 if (newlen < 0)
1581 newlen = newlen * -1;
1582 } else {
1583 newlen = CRYPT_FOOTER_OFFSET;
1584 }
1585 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1586 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1587 } else {
1588 unsigned int block_count;
1589 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1590 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1591 } else {
1592 off64_t offset = ((off64_t)block_count * 512) - newlen;
1593 if (lseek64(fd, offset, SEEK_SET) == -1) {
1594 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1595 } else {
1596 void* buffer = malloc(newlen);
1597 if (!buffer) {
1598 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1599 } else {
1600 memset(buffer, 0, newlen);
1601 int ret = write(fd, buffer, newlen);
1602 if (ret != newlen) {
1603 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1604 } else {
1605 LOGINFO("Successfully wiped crypto footer.\n");
1606 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001607 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001608 }
1609 }
1610 }
1611 close(fd);
1612 }
1613 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001614 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1615 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1616 TWFunc::Exec_Cmd(Command);
1617 } else {
1618 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1619 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001620 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001621 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001622 Has_Data_Media = Save_Data_Media;
1623 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1624 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001625 if (Mount(false))
1626 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001627 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001628 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001629#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001630 gui_msg("format_data_msg=You may need to reboot recovery to be able to use /data again.");
Ethan Yonker83e82572014-04-04 10:59:28 -05001631#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001632 return true;
1633 } else {
1634 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001635 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001636 if (Has_Data_Media && Mount(false))
1637 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001638 return false;
1639 }
1640 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001641}
1642
1643void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001644 const char* type;
1645 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001646
Dees_Troy68cab492012-12-12 19:29:35 +00001647 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1648 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001649
Dees_Troy38bd7602012-09-14 13:33:53 -04001650 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001651 if (!Is_Present)
1652 return;
Dees_Troy51127312012-09-08 13:08:49 -04001653
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001654 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1655 if (blkid_do_fullprobe(pr)) {
1656 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001657 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001658 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001659 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001660
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001661 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001662 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001663 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001664 return;
1665 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001666
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001667 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001668 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001669}
1670
Gary Peck43acadf2012-11-21 21:19:01 -08001671bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001672 if (!UnMount(true))
1673 return false;
1674
Dees_Troy43d8b002012-09-17 16:00:01 -04001675 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001676 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001677
Greg Wallace4b44fef2015-12-29 15:33:24 -05001678 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001679 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001680 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001681 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001682 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001683 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001684 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001685 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001686 return true;
1687 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001688 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001689 return false;
1690 }
1691 } else
1692 return Wipe_RMRF();
1693
1694 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001695}
1696
1697bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001698 Find_Actual_Block_Device();
1699 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001700 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1701 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001702 return false;
1703 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001704 if (!UnMount(true))
1705 return false;
1706
Dees_Troyb3265ab2013-08-30 02:59:14 +00001707#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001708 int ret;
1709 char *secontext = NULL;
1710
Greg Wallace4b44fef2015-12-29 15:33:24 -05001711 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001712
Dees Troy99c8dbf2014-03-10 16:53:58 +00001713 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001714 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1715 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1716 } else {
1717 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1718 }
1719 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001720 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001721 return false;
1722 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001723 string sedir = Mount_Point + "/lost+found";
1724 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1725 rmdir(sedir.c_str());
1726 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001727 return true;
1728 }
1729#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001730 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001731 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001732
Greg Wallace4b44fef2015-12-29 15:33:24 -05001733 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001734 Find_Actual_Block_Device();
1735 Command = "make_ext4fs";
1736 if (!Is_Decrypted && Length != 0) {
1737 // Only use length if we're not decrypted
1738 char len[32];
1739 sprintf(len, "%i", Length);
1740 Command += " -l ";
1741 Command += len;
1742 }
Dees_Troy5295d582013-09-06 15:51:08 +00001743 if (TWFunc::Path_Exists("/file_contexts")) {
1744 Command += " -S /file_contexts";
1745 }
1746 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001747 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001748 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001749 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001750 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001751 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001752 return true;
1753 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001754 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001755 return false;
1756 }
1757 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001758 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001759#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001760 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001761}
1762
1763bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001764 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001765
Matt Mower18794c82015-11-11 16:22:45 -06001766 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001767 if (!UnMount(true))
1768 return false;
1769
Greg Wallace4b44fef2015-12-29 15:33:24 -05001770 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001771 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001772 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001773 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001774 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001775 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001776 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001777 return true;
1778 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001779 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001780 return false;
1781 }
1782 return true;
1783 }
1784 else
1785 return Wipe_RMRF();
1786
1787 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001788}
1789
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001790bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001791 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001792
1793 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1794 if (!UnMount(true))
1795 return false;
1796
Greg Wallace4b44fef2015-12-29 15:33:24 -05001797 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001798 Find_Actual_Block_Device();
1799 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001800 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001801 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001802 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001803 return true;
1804 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001805 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001806 return false;
1807 }
1808 return true;
1809 }
1810 return false;
1811}
1812
Dees_Troy38bd7602012-09-14 13:33:53 -04001813bool TWPartition::Wipe_MTD() {
1814 if (!UnMount(true))
1815 return false;
1816
Greg Wallace4b44fef2015-12-29 15:33:24 -05001817 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001818
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001819 mtd_scan_partitions();
1820 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1821 if (mtd == NULL) {
1822 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1823 return false;
1824 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001825
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001826 MtdWriteContext* ctx = mtd_write_partition(mtd);
1827 if (ctx == NULL) {
1828 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1829 return false;
1830 }
1831 if (mtd_erase_blocks(ctx, -1) == -1) {
1832 mtd_write_close(ctx);
1833 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1834 return false;
1835 }
1836 if (mtd_write_close(ctx) != 0) {
1837 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1838 return false;
1839 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001840 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001841 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001842 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001843 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001844}
1845
1846bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001847 if (!Mount(true))
1848 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06001849 // This is the only wipe that leaves the partition mounted, so we
1850 // must manually remove the partition from MTP if it is a storage
1851 // partition.
1852 if (Is_Storage)
1853 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001854
Ethan Yonker74db1572015-10-28 12:44:49 -05001855 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001856 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001857 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001858 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001859}
1860
Dees_Troye5017042013-08-29 16:38:55 +00001861bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001862 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001863
1864 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1865 if (!UnMount(true))
1866 return false;
1867
Greg Wallace4b44fef2015-12-29 15:33:24 -05001868 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00001869 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05001870 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05001871 if (!Is_Decrypted && Length != 0) {
1872 // Only use length if we're not decrypted
1873 char len[32];
1874 int mod_length = Length;
1875 if (Length < 0)
1876 mod_length *= -1;
1877 sprintf(len, "%i", mod_length);
1878 command += " -r ";
1879 command += len;
1880 }
1881 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001882 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001883 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001884 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00001885 return true;
1886 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001887 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00001888 return false;
1889 }
1890 return true;
1891 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001892 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00001893 return Wipe_RMRF();
1894 }
1895 return false;
1896}
1897
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001898bool TWPartition::Wipe_NTFS() {
1899 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001900 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001901
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001902 if (TWFunc::Path_Exists("/sbin/mkntfs"))
1903 Ntfsmake_Binary = "mkntfs";
1904 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
1905 Ntfsmake_Binary = "mkfs.ntfs";
1906 else
1907 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001908
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001909 if (!UnMount(true))
1910 return false;
1911
Greg Wallace4b44fef2015-12-29 15:33:24 -05001912 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001913 Find_Actual_Block_Device();
1914 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
1915 if (TWFunc::Exec_Cmd(command) == 0) {
1916 Recreate_AndSec_Folder();
1917 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001918 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001919 } else {
1920 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
1921 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001922 }
1923 return false;
1924}
1925
Dees_Troy51a0e822012-09-05 15:24:24 -04001926bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001927#ifdef TW_OEM_BUILD
1928 // In an OEM Build we want to do a full format
1929 return Wipe_Encryption();
1930#else
Ethan Yonker66a19492015-12-10 10:19:45 -06001931 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001932
Dees_Troy38bd7602012-09-14 13:33:53 -04001933 if (!Mount(true))
1934 return false;
1935
Ethan Yonker74db1572015-10-28 12:44:49 -05001936 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06001937 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
1938 if (ret)
1939 gui_msg("done=Done.");
1940 return ret;
1941#endif // ifdef TW_OEM_BUILD
1942}
1943
1944bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
1945 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001946
1947 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06001948 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00001949 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001950 struct dirent* de;
1951 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001952 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001953
Ethan Yonker66a19492015-12-10 10:19:45 -06001954 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001955 dir.append(de->d_name);
Ethan Yonker66a19492015-12-10 10:19:45 -06001956 if (du.check_skip_dirs(dir)) {
1957 LOGINFO("skipped '%s'\n", dir.c_str());
1958 continue;
1959 }
Dees_Troyce675462013-01-09 19:48:21 +00001960 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06001961 dir.append("/");
1962 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
1963 closedir(d);
1964 return false;
1965 }
1966 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001967 } 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 +00001968 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001969 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001970 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001971 }
1972 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001973
Dees_Troy16b74352012-11-14 22:27:31 +00001974 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001975 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001976 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00001977 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001978}
1979
bigbiff7abc5fe2015-01-17 16:53:12 -05001980bool 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 -04001981 char back_name[255], split_index[5];
1982 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001983 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001984 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001985 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001986 twrpTar tar;
1987 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001988
Dees_Troy43d8b002012-09-17 16:00:01 -04001989 if (!Mount(true))
1990 return false;
1991
Dees_Troya13d74f2013-03-24 08:54:55 -05001992 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05001993 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04001994
1995 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001996 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001997
Dees_Troy83bd4832013-05-04 12:39:56 +00001998#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1999 DataManager::GetValue("tw_encrypt_backup", use_encryption);
2000 if (use_encryption && Can_Encrypt_Backup) {
2001 tar.use_encryption = use_encryption;
2002 if (Use_Userdata_Encryption)
2003 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06002004 string Password;
2005 DataManager::GetValue("tw_backup_password", Password);
2006 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00002007 } else {
2008 use_encryption = false;
2009 }
2010#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002011
2012 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
2013 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002014 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00002015 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00002016 Full_FileName = backup_folder + "/" + Backup_FileName;
2017 tar.setdir(Backup_Path);
2018 tar.setfn(Full_FileName);
2019 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002020 tar.partition_name = Backup_Name;
2021 tar.backup_folder = backup_folder;
bigbiff7abc5fe2015-01-17 16:53:12 -05002022 if (tar.createTarFork(overall_size, other_backups_size, tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002023 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002024 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002025}
2026
2027bool TWPartition::Backup_DD(string backup_folder) {
codelover352b75e2015-03-29 02:44:07 +08002028 char back_name[255], block_size[32], dd_count[32];
2029 string Full_FileName, Command, DD_BS, DD_COUNT;
Dees_Troy43d8b002012-09-17 16:00:01 -04002030 int use_compression;
codelover352b75e2015-03-29 02:44:07 +08002031 unsigned long long DD_Block_Size, DD_Count;
Dees_Troy43d8b002012-09-17 16:00:01 -04002032
codelover352b75e2015-03-29 02:44:07 +08002033 DD_Block_Size = 16 * 1024 * 1024;
2034 while (Backup_Size % DD_Block_Size != 0) DD_Block_Size >>= 1;
2035
2036 DD_Count = Backup_Size / DD_Block_Size;
2037
2038 sprintf(dd_count, "%llu", DD_Count);
2039 DD_COUNT = dd_count;
2040
2041 sprintf(block_size, "%llu", DD_Block_Size);
2042 DD_BS = block_size;
igoriok87e3d932013-01-31 21:03:53 +02002043
Ethan Yonker74db1572015-10-28 12:44:49 -05002044 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2045 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002046
2047 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
2048 Backup_FileName = back_name;
2049
2050 Full_FileName = backup_folder + "/" + Backup_FileName;
2051
codelover352b75e2015-03-29 02:44:07 +08002052 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + " count=" + DD_COUNT;
Dees_Troy2673cec2013-04-02 20:22:16 +00002053 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002054 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002055 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04002056 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002057 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002058 return false;
2059 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002060 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002061}
2062
2063bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002064 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02002065 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002066 int use_compression;
2067
Ethan Yonker74db1572015-10-28 12:44:49 -05002068 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2069 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002070
2071 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
2072 Backup_FileName = back_name;
2073
2074 Full_FileName = backup_folder + "/" + Backup_FileName;
2075
2076 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00002077 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002078 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002079 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002080 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2081 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
Matt Mower3c366972015-12-25 19:28:31 -06002082 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002083 return false;
2084 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002085 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002086}
2087
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002088unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
2089 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
2090 if (restore_info.LoadValues() == 0) {
2091 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2092 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2093 return Restore_Size;
2094 }
2095 }
2096 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
2097
2098 Full_FileName = restore_folder + "/" + Backup_FileName;
2099
2100 if (Is_Image(Restore_File_System)) {
2101 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2102 return Restore_Size;
2103 }
2104
2105 twrpTar tar;
2106 tar.setdir(Backup_Path);
2107 tar.setfn(Full_FileName);
2108 tar.backup_name = Backup_Name;
2109#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2110 string Password;
2111 DataManager::GetValue("tw_restore_password", Password);
2112 if (!Password.empty())
2113 tar.setpassword(Password);
2114#endif
2115 tar.partition_name = Backup_Name;
2116 tar.backup_folder = restore_folder;
2117 Restore_Size = tar.get_size();
2118 return Restore_Size;
2119}
2120
2121bool 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 -08002122 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04002123 int index = 0;
2124 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00002125 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002126
Dees_Troye58d5262012-09-21 12:27:57 -04002127 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002128 if (!Wipe_AndSec())
2129 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002130 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002131 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002132 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002133 gui_msg(Msg(msg::kWarning, "datamedia_fs_restore=WARNING: This /data backup was made with {1} file system! The backup may not boot unless you change back to {1}.")(Restore_File_System));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002134 if (!Wipe_Data_Without_Wiping_Media())
2135 return false;
2136 } else {
2137 if (!Wipe(Restore_File_System))
2138 return false;
2139 }
Dees_Troye58d5262012-09-21 12:27:57 -04002140 }
Matt Mower3c366972015-12-25 19:28:31 -06002141 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002142 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002143
2144 if (!Mount(true))
2145 return false;
2146
Dees_Troy4a2a1262012-09-18 09:33:47 -04002147 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002148 twrpTar tar;
2149 tar.setdir(Backup_Path);
2150 tar.setfn(Full_FileName);
2151 tar.backup_name = Backup_Name;
2152#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2153 string Password;
2154 DataManager::GetValue("tw_restore_password", Password);
2155 if (!Password.empty())
2156 tar.setpassword(Password);
2157#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002158 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002159 ret = false;
2160 else
2161 ret = true;
2162#ifdef HAVE_CAPABILITIES
2163 // Restore capabilities to the run-as binary
2164 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2165 struct vfs_cap_data cap_data;
2166 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2167
2168 memset(&cap_data, 0, sizeof(cap_data));
2169 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2170 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2171 cap_data.data[0].inheritable = 0;
2172 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2173 cap_data.data[1].inheritable = 0;
2174 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2175 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2176 } else {
2177 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2178 }
2179 }
2180#endif
2181 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002182}
2183
Ethan Yonker96af84a2015-01-05 14:58:36 -06002184bool TWPartition::Restore_Image(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size, string Restore_File_System) {
2185 string Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002186 double display_percent, progress_percent;
2187 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04002188
Matt Mower3c366972015-12-25 19:28:31 -06002189 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002190 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002191 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002192
Ethan Yonker96af84a2015-01-05 14:58:36 -06002193 if (Restore_File_System == "emmc") {
2194 if (!Flash_Image_DD(Full_FileName))
2195 return false;
2196 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
2197 if (!Flash_Image_FI(Full_FileName))
2198 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002199 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002200 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
2201 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
2202 DataManager::SetValue("tw_size_progress", size_progress);
2203 progress_percent = (display_percent / 100);
2204 DataManager::SetProgress((float)(progress_percent));
2205 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04002206 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002207}
Dees_Troy5bf43922012-09-07 16:07:55 -04002208
2209bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002210 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002211
Dees_Troyab10ee22012-09-21 14:27:30 -04002212 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002213 return false;
2214
Dees_Troy0550cfb2012-10-13 11:56:13 -04002215 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002216 if (Removable || Is_Encrypted) {
2217 if (!Mount(false))
2218 return true;
2219 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002220 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002221
2222 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002223 if (!ret || Size == 0) {
2224 if (!Get_Size_Via_df(Display_Error)) {
2225 if (!Was_Already_Mounted)
2226 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002227 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002228 }
2229 }
Dees_Troy51127312012-09-08 13:08:49 -04002230
Dees_Troy5bf43922012-09-07 16:07:55 -04002231 if (Has_Data_Media) {
2232 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002233 unsigned long long data_media_used, actual_data;
Ethan Yonker66a19492015-12-10 10:19:45 -06002234 Used = du.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002235 Backup_Size = Used;
2236 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002237 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002238 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002239 } else {
2240 if (!Was_Already_Mounted)
2241 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002242 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002243 }
Dees_Troye58d5262012-09-21 12:27:57 -04002244 } else if (Has_Android_Secure) {
2245 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002246 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002247 else {
2248 if (!Was_Already_Mounted)
2249 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002250 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002251 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002252 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002253 if (!Was_Already_Mounted)
2254 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002255 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002256}
Dees_Troy38bd7602012-09-14 13:33:53 -04002257
2258void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002259 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002260 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002261 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002262 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002263 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002264 Is_Present = true;
2265 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002266 return;
2267 }
2268 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002269 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002270 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002271 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002272 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002273 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002274 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002275}
2276
2277void TWPartition::Recreate_Media_Folder(void) {
2278 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002279 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002280
2281 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002282 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2283 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002284 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002285 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2286 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002287 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2288 if (!Internal_path.empty()) {
2289 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2290 mkdir(Internal_path.c_str(), 0770);
2291 }
2292#ifdef TW_INTERNAL_STORAGE_PATH
2293 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2294#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002295#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002296 // Afterwards, we will try to set the
2297 // default metadata that we were hopefully able to get during
2298 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002299 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002300 if (!Internal_path.empty())
2301 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002302#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002303 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002304 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002305 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002306 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002307}
Dees_Troye58d5262012-09-21 12:27:57 -04002308
2309void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002310 if (!Has_Android_Secure)
2311 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002312 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002313 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002314 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002315 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002316 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002317 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002318 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002319 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002320 }
2321}
bigbiff7cb4c332014-11-26 20:36:07 -05002322
2323uint64_t TWPartition::Get_Max_FileSize() {
2324 uint64_t maxFileSize = 0;
2325 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2326 const uint64_t constTB = (uint64_t) constGB * 1024;
2327 const uint64_t constPB = (uint64_t) constTB * 1024;
2328 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002329 if (Current_File_System == "ext4")
2330 maxFileSize = 16 * constTB; //16 TB
2331 else if (Current_File_System == "vfat")
2332 maxFileSize = 4 * constGB; //4 GB
2333 else if (Current_File_System == "ntfs")
2334 maxFileSize = 256 * constTB; //256 TB
2335 else if (Current_File_System == "exfat")
2336 maxFileSize = 16 * constPB; //16 PB
2337 else if (Current_File_System == "ext3")
2338 maxFileSize = 2 * constTB; //2 TB
2339 else if (Current_File_System == "f2fs")
2340 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002341 else
2342 maxFileSize = 100000000L;
2343 return maxFileSize - 1;
2344}
2345
Ethan Yonker96af84a2015-01-05 14:58:36 -06002346bool TWPartition::Flash_Image(string Filename) {
2347 string Restore_File_System;
2348
2349 LOGINFO("Image filename is: %s\n", Filename.c_str());
2350
2351 if (Backup_Method == FILES) {
2352 LOGERR("Cannot flash images to file systems\n");
2353 return false;
2354 } else if (!Can_Flash_Img) {
2355 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2356 return false;
2357 } else {
2358 if (!Find_Partition_Size()) {
2359 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2360 return false;
2361 }
2362 unsigned long long image_size = TWFunc::Get_File_Size(Filename);
2363 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002364 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonker96af84a2015-01-05 14:58:36 -06002365 image_size, Filename.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002366 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002367 return false;
2368 }
2369 if (Backup_Method == DD)
2370 return Flash_Image_DD(Filename);
2371 else if (Backup_Method == FLASH_UTILS)
2372 return Flash_Image_FI(Filename);
2373 }
2374
2375 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2376 return false;
2377}
2378
2379bool TWPartition::Flash_Image_DD(string Filename) {
2380 string Command;
2381
Ethan Yonker74db1572015-10-28 12:44:49 -05002382 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
HashBanged974bb2016-01-30 14:20:09 -05002383
2384 uint32_t magic = 0;
2385 int fd = open(Filename.c_str(), O_RDONLY);
2386 if (fd < 0) {
2387 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2388 return false;
2389 }
2390 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2391 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2392 close(fd);
2393 return false;
2394 }
2395 close(fd);
2396 if (magic == SPARSE_HEADER_MAGIC) {
2397 Command = "simg2img '" + Filename + "' " + Actual_Block_Device;
2398 } else {
2399 Command = "dd bs=8388608 if='" + Filename + "' of=" + Actual_Block_Device;
2400 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002401 LOGINFO("Flash command: '%s'\n", Command.c_str());
2402 TWFunc::Exec_Cmd(Command);
2403 return true;
2404}
2405
2406bool TWPartition::Flash_Image_FI(string Filename) {
2407 string Command;
2408
Ethan Yonker74db1572015-10-28 12:44:49 -05002409 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker96af84a2015-01-05 14:58:36 -06002410 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2411 Command = "erase_image " + MTD_Name;
2412 LOGINFO("Erase command: '%s'\n", Command.c_str());
2413 TWFunc::Exec_Cmd(Command);
2414 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2415 LOGINFO("Flash command: '%s'\n", Command.c_str());
2416 TWFunc::Exec_Cmd(Command);
2417 return true;
2418}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002419
2420void TWPartition::Change_Mount_Read_Only(bool new_value) {
2421 Mount_Read_Only = new_value;
2422}
2423
2424int TWPartition::Check_Lifetime_Writes() {
2425 bool original_read_only = Mount_Read_Only;
2426 int ret = 1;
2427
2428 Mount_Read_Only = true;
2429 if (Mount(false)) {
2430 Find_Actual_Block_Device();
2431 string block = basename(Actual_Block_Device.c_str());
2432 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2433 string result;
2434 if (TWFunc::Path_Exists(file)) {
2435 if (TWFunc::read_file(file, result) != 0) {
2436 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2437 } else {
2438 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2439 if (result == "0") {
2440 ret = 0;
2441 }
2442 }
2443 } else {
2444 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2445 }
2446 UnMount(true);
2447 } else {
2448 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2449 }
2450 Mount_Read_Only = original_read_only;
2451 return ret;
2452}
Ethan Yonker66a19492015-12-10 10:19:45 -06002453
2454int TWPartition::Decrypt_Adopted() {
2455#ifdef TW_INCLUDE_CRYPTO
2456 int ret = 1;
2457 Is_Adopted_Storage = false;
2458 string Adopted_Key_File = "";
2459
2460 if (!Removable)
2461 return ret;
2462
2463 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
2464 if (fd < 0) {
2465 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
2466 return ret;
2467 }
2468 char type_guid[80];
2469 char part_guid[80];
2470
2471 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
2472 LOGINFO("type: '%s'\n", type_guid);
2473 LOGINFO("part: '%s'\n", part_guid);
2474 Adopted_GUID = part_guid;
2475 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
2476 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
2477 LOGINFO("android_expand found\n");
2478 Adopted_Key_File = "/data/misc/vold/expand_";
2479 Adopted_Key_File += part_guid;
2480 Adopted_Key_File += ".key";
2481 if (TWFunc::Path_Exists(Adopted_Key_File)) {
2482 Is_Adopted_Storage = true;
2483 /* Until we find a use case for this, I think it is safe
2484 * to disable USB Mass Storage whenever adopted storage
2485 * is present.
2486 */
2487 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
2488 DataManager::SetValue("tw_has_usb_storage", 0);
2489 }
2490 }
2491 }
2492
2493 if (Is_Adopted_Storage) {
2494 string Adopted_Block_Device = Alternate_Block_Device + "p2";
2495 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2496 Adopted_Block_Device = Alternate_Block_Device + "2";
2497 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2498 LOGINFO("Adopted block device does not exist\n");
2499 goto exit;
2500 }
2501 }
2502 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
2503 char crypto_blkdev[MAXPATHLEN];
2504 std::string thekey;
2505 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
2506 if (fdkey < 0) {
2507 LOGINFO("failed to open key file\n");
2508 goto exit;
2509 }
2510 char buf[512];
2511 ssize_t n;
2512 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
2513 thekey.append(buf, n);
2514 }
2515 close(fdkey);
2516 unsigned char* key = (unsigned char*) thekey.data();
2517 cryptfs_revert_ext_volume(part_guid);
2518
2519 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
2520 if (ret == 0) {
2521 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
2522 Decrypted_Block_Device = crypto_blkdev;
2523 Is_Decrypted = true;
2524 Is_Encrypted = true;
2525 Find_Actual_Block_Device();
2526 if (!Mount(false)) {
2527 LOGERR("Failed to mount decrypted adopted storage device\n");
2528 Is_Decrypted = false;
2529 Is_Encrypted = false;
2530 cryptfs_revert_ext_volume(part_guid);
2531 ret = 1;
2532 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002533 UnMount(false);
2534 Has_Android_Secure = false;
2535 Symlink_Path = "";
2536 Symlink_Mount_Point = "";
2537 Backup_Name = Mount_Point.substr(1);
2538 Backup_Path = Mount_Point;
2539 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
2540 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
2541 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
2542 PartitionManager.Remove_Partition_By_Path("/sd-ext");
2543 }
Ethan Yonker66a19492015-12-10 10:19:45 -06002544 Setup_Data_Media();
2545 Recreate_Media_Folder();
2546 Wipe_Available_in_GUI = true;
2547 Wipe_During_Factory_Reset = true;
2548 Can_Be_Backed_Up = true;
2549 Can_Encrypt_Backup = true;
2550 Use_Userdata_Encryption = true;
2551 Is_Storage = true;
2552 Storage_Name = "Adopted Storage";
2553 Is_SubPartition = true;
2554 SubPartition_Of = "/data";
2555 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2556 DataManager::SetValue("tw_has_adopted_storage", 1);
2557 }
2558 } else {
2559 LOGERR("Failed to setup adopted storage decryption\n");
2560 }
2561 }
2562exit:
2563 return ret;
2564#else
2565 LOGINFO("Decrypt_Adopted: no crypto support\n");
2566 return 1;
2567#endif
2568}
2569
2570void TWPartition::Revert_Adopted() {
2571#ifdef TW_INCLUDE_CRYPTO
2572 if (!Adopted_GUID.empty()) {
2573 PartitionManager.Remove_MTP_Storage(Mount_Point);
2574 UnMount(false);
2575 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
2576 Is_Adopted_Storage = false;
2577 Is_Encrypted = false;
2578 Is_Decrypted = false;
2579 Decrypted_Block_Device = "";
2580 Find_Actual_Block_Device();
2581 Wipe_During_Factory_Reset = false;
2582 Can_Be_Backed_Up = false;
2583 Can_Encrypt_Backup = false;
2584 Use_Userdata_Encryption = false;
2585 Is_SubPartition = false;
2586 SubPartition_Of = "";
2587 Has_Data_Media = false;
2588 Storage_Path = Mount_Point;
2589 if (!Symlink_Mount_Point.empty()) {
2590 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
2591 if (Dat) {
2592 Dat->UnMount(false);
2593 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
2594 }
2595 Symlink_Mount_Point = "";
2596 }
2597 }
2598#else
2599 LOGINFO("Revert_Adopted: no crypto support\n");
2600#endif
2601}