blob: d038c38f106137e6e5a94ba07b37078a972cf070 [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
Dees_Troy51a0e822012-09-05 15:24:24 -040071
bigbiff bigbiff9c754052013-01-09 09:09:08 -050072using namespace std;
73
Dees_Troya95f55c2013-08-17 13:14:43 +000074extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050075extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000076
Hashcode62bd9e02013-11-19 21:59:42 -080077struct flag_list {
78 const char *name;
79 unsigned flag;
80};
81
82static struct flag_list mount_flags[] = {
83 { "noatime", MS_NOATIME },
84 { "noexec", MS_NOEXEC },
85 { "nosuid", MS_NOSUID },
86 { "nodev", MS_NODEV },
87 { "nodiratime", MS_NODIRATIME },
88 { "ro", MS_RDONLY },
89 { "rw", 0 },
90 { "remount", MS_REMOUNT },
91 { "bind", MS_BIND },
92 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000093#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080094 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000095#endif
96#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080097 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000098#endif
99#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800100 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#endif
102#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800103 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800105 { "sync", MS_SYNCHRONOUS },
106 { "defaults", 0 },
107 { 0, 0 },
108};
109
that9e0593e2014-10-08 00:01:24 +0200110TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400111 Can_Be_Mounted = false;
112 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500113 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200114 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400115 Wipe_During_Factory_Reset = false;
116 Wipe_Available_in_GUI = false;
117 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400118 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400119 SubPartition_Of = "";
120 Symlink_Path = "";
121 Symlink_Mount_Point = "";
122 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400123 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400124 Actual_Block_Device = "";
125 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400126 Alternate_Block_Device = "";
127 Removable = false;
128 Is_Present = false;
129 Length = 0;
130 Size = 0;
131 Used = 0;
132 Free = 0;
133 Backup_Size = 0;
134 Can_Be_Encrypted = false;
135 Is_Encrypted = false;
136 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600137 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400138 Decrypted_Block_Device = "";
139 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500140 Backup_Display_Name = "";
141 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400142 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400143 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400144 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400145 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000146 Can_Encrypt_Backup = false;
147 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400148 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400149 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400150 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500151 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400152 Storage_Path = "";
153 Current_File_System = "";
154 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800155 Mount_Flags = 0;
156 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400157 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000158 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000159 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600160 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600161 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600162 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500163 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600164 Is_Adopted_Storage = false;
165 Adopted_GUID = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400166}
167
168TWPartition::~TWPartition(void) {
169 // Do nothing
170}
171
Dees_Troy5bf43922012-09-07 16:07:55 -0400172bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
173 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
174 int line_len = Line.size(), index = 0, item_index = 0;
175 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400176 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400177 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500178 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400179
Dees_Troy51127312012-09-08 13:08:49 -0400180 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500181 if (full_line[index] == 34)
182 skip = !skip;
183 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400184 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400185 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400186 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000187 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400188 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500189 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000190 Display_Name = full_line + 1;
191 Backup_Display_Name = Display_Name;
192 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400193 index = Mount_Point.size();
194 while (index < line_len) {
195 while (index < line_len && full_line[index] == '\0')
196 index++;
197 if (index >= line_len)
198 continue;
199 ptr = full_line + index;
200 if (item_index == 0) {
201 // File System
202 Fstab_File_System = ptr;
203 Current_File_System = ptr;
204 item_index++;
205 } else if (item_index == 1) {
206 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400207 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400208 MTD_Name = ptr;
209 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400210 } else if (Fstab_File_System == "bml") {
211 if (Mount_Point == "/boot")
212 MTD_Name = "boot";
213 else if (Mount_Point == "/recovery")
214 MTD_Name = "recovery";
215 Primary_Block_Device = ptr;
216 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000217 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 -0400218 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400219 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000220 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400221 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000222 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400223 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400224 } else {
225 Primary_Block_Device = ptr;
226 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400227 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400228 item_index++;
229 } else if (item_index > 1) {
230 if (*ptr == '/') {
231 // Alternate Block Device
232 Alternate_Block_Device = ptr;
233 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
234 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
235 // Partition length
236 ptr += 7;
237 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400238 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
239 // Custom flags, save for later so that new values aren't overwritten by defaults
240 ptr += 6;
241 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000242 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400243 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
244 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400245 } else {
246 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000247 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400248 }
249 }
250 while (index < line_len && full_line[index] != '\0')
251 index++;
252 }
253
254 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
255 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000256 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400257 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000258 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400259 return 0;
260 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400261 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400262 Setup_File_System(Display_Error);
263 if (Mount_Point == "/system") {
264 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500265 Backup_Display_Name = Display_Name;
266 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400267 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500268 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500269 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400270 } else if (Mount_Point == "/data") {
Dees Troyc657cc02015-01-16 22:48:47 +0000271 UnMount(false); // added in case /data is mounted as tmpfs for qcom hardware decrypt
Dees_Troy5bf43922012-09-07 16:07:55 -0400272 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500273 Backup_Display_Name = Display_Name;
274 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400275 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400276 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500277 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000278 Can_Encrypt_Backup = true;
279 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500280 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200281 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400282#ifdef TW_INCLUDE_CRYPTO
283 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400284 char crypto_blkdev[255];
285 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
286 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400287 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400288 DataManager::SetValue(TW_IS_DECRYPTED, 1);
289 Is_Encrypted = true;
290 Is_Decrypted = true;
291 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000292 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400293 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600294 if (Is_Present) {
Ethan Yonker253368a2014-11-25 15:00:52 -0600295 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 -0600296 if (cryptfs_check_footer() == 0) {
297 Is_Encrypted = true;
298 Is_Decrypted = false;
299 Can_Be_Mounted = false;
300 Current_File_System = "emmc";
301 Setup_Image(Display_Error);
302 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
Vojtech Bocek7e11ac52015-03-05 23:21:49 +0100303 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
Ethan Yonker71413f42014-02-26 13:36:08 -0600304 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
305 DataManager::SetValue("tw_crypto_display", "");
306 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500307 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
Ethan Yonker71413f42014-02-26 13:36:08 -0600308 }
309 } else {
310 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
311 }
Gary Peck82599a82012-11-21 16:23:12 -0800312 } else {
313 // Filesystem is not encrypted and the mount
314 // succeeded, so get it back to the original
315 // unmounted state
316 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400317 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500318 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400319 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400320#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500321 if (datamedia)
322 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400323#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400324 } else if (Mount_Point == "/cache") {
325 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500326 Backup_Display_Name = Display_Name;
327 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400328 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400329 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500330 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400331 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000332 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500333 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500334 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400335 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400336 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400337 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400338 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500339 Backup_Display_Name = Display_Name;
340 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400341 Is_SubPartition = true;
342 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400343 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500344 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000345 Can_Encrypt_Backup = true;
346 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400347 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400348 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400349 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500350 Backup_Display_Name = Display_Name;
351 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400352 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400353 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500354 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000355 Can_Encrypt_Backup = true;
356 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400357 } else if (Mount_Point == "/boot") {
358 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500359 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400360 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500361 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500362 } else if (Mount_Point == "/vendor") {
363 Display_Name = "Vendor";
364 Backup_Display_Name = Display_Name;
365 Storage_Name = Display_Name;
366 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400367 }
368#ifdef TW_EXTERNAL_STORAGE_PATH
369 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
370 Is_Storage = true;
371 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400372 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500373 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400374#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000375 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400376 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400377 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500378 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400379#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000380 }
Dees_Troy8170a922012-09-18 15:40:25 -0400381#ifdef TW_INTERNAL_STORAGE_PATH
382 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
383 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500384 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400385 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500386 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400387 }
388#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000389 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400390 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500391 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500392 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400393 }
394#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400395 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400396 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400397 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500398 if (Mount_Point == "/boot") {
399 Display_Name = "Boot";
400 Backup_Display_Name = Display_Name;
401 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600402 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500403 } else if (Mount_Point == "/recovery") {
404 Display_Name = "Recovery";
405 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600406 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500407 } else if (Mount_Point == "/system_image") {
408 Display_Name = "System Image";
409 Backup_Display_Name = Display_Name;
410 Can_Flash_Img = false;
411 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500412 } else if (Mount_Point == "/vendor_image") {
413 Display_Name = "Vendor Image";
414 Backup_Display_Name = Display_Name;
415 Can_Flash_Img = false;
416 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500417 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400418 }
419
Dees_Troy51127312012-09-08 13:08:49 -0400420 // Process any custom flags
421 if (Flags.size() > 0)
422 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400423 return true;
424}
425
Ketut Putu Kumajayabfb72b02015-08-10 16:40:55 +0200426bool TWPartition::Process_FS_Flags(string& Options, int& Flags) {
Hashcode62bd9e02013-11-19 21:59:42 -0800427 int i;
428 char *p;
429 char *savep;
430 char fs_options[250];
431
432 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
433 Options = "";
434
435 p = strtok_r(fs_options, ",", &savep);
436 while (p) {
437 /* Look for the flag "p" in the flag list "fl"
438 * If not found, the loop exits with fl[i].name being null.
439 */
440 for (i = 0; mount_flags[i].name; i++) {
441 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
442 Flags |= mount_flags[i].flag;
443 break;
444 }
445 }
446
447 if (!mount_flags[i].name) {
448 if (Options.size() > 0)
449 Options += ",";
450 Options += p;
451 }
452 p = strtok_r(NULL, ",", &savep);
453 }
454
455 return true;
456}
457
Dees_Troy51127312012-09-08 13:08:49 -0400458bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
459 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500460 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400461 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500462 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400463
464 strcpy(flags, Flags.c_str());
465 flags_len = Flags.size();
466 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500467 if (flags[index] == 34)
468 skip = !skip;
469 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400470 flags[index] = '\0';
471 }
472
473 index = 0;
474 while (index < flags_len) {
475 while (index < flags_len && flags[index] == '\0')
476 index++;
477 if (index >= flags_len)
478 continue;
479 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500480 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400481 if (strcmp(ptr, "removable") == 0) {
482 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600483 } else if (strncmp(ptr, "storage", 7) == 0) {
484 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600485 Is_Storage = true;
486 } else if (ptr_len == 9) {
487 ptr += 9;
488 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
489 LOGINFO("storage set to true\n");
490 Is_Storage = true;
491 } else {
492 LOGINFO("storage set to false\n");
493 Is_Storage = false;
494 }
495 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500496 } else if (strcmp(ptr, "settingsstorage") == 0) {
497 Is_Storage = true;
Xueferbfce5042015-04-14 02:02:05 +0800498 Is_Settings_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500499 } else if (strcmp(ptr, "andsec") == 0) {
500 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400501 } else if (strcmp(ptr, "canbewiped") == 0) {
502 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700503 } else if (strcmp(ptr, "usermrf") == 0) {
504 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500505 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
506 ptr += 7;
507 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
508 Can_Be_Backed_Up = true;
509 else
510 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400511 } else if (strcmp(ptr, "wipeingui") == 0) {
512 Can_Be_Wiped = true;
513 Wipe_Available_in_GUI = true;
514 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
515 Can_Be_Wiped = true;
516 Wipe_Available_in_GUI = true;
517 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500518 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000519 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400520 Is_SubPartition = true;
521 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000522 } else if (strcmp(ptr, "ignoreblkid") == 0) {
523 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000524 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
525 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500526 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400527 ptr += 8;
528 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500529 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
530 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400531 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500532 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400533 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500534 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
535 Display_Name.resize(Display_Name.size() - 1);
536 }
537 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
538 has_storage_name = true;
539 ptr += 11;
540 if (*ptr == '\"') ptr++;
541 Storage_Name = ptr;
542 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
543 Storage_Name.resize(Storage_Name.size() - 1);
544 }
545 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
546 has_backup_name = true;
547 ptr += 10;
548 if (*ptr == '\"') ptr++;
549 Backup_Display_Name = ptr;
550 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
551 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
552 }
553 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400554 ptr += 10;
Ethan Yonkera2719152015-05-28 09:44:41 -0500555 Format_Block_Size = (unsigned long)(atol(ptr));
Dees_Troya13d74f2013-03-24 08:54:55 -0500556 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400557 ptr += 7;
558 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000559 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
560 ptr += 17;
561 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
562 Can_Encrypt_Backup = true;
563 else
564 Can_Encrypt_Backup = false;
565 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
566 ptr += 21;
567 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
568 Can_Encrypt_Backup = true;
569 Use_Userdata_Encryption = true;
570 } else {
571 Use_Userdata_Encryption = false;
572 }
Hashcode62bd9e02013-11-19 21:59:42 -0800573 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
574 ptr += 8;
575 if (*ptr == '\"') ptr++;
576
577 Mount_Options = ptr;
578 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
579 Mount_Options.resize(Mount_Options.size() - 1);
580 }
581 Process_FS_Flags(Mount_Options, Mount_Flags);
Ethan Yonker253368a2014-11-25 15:00:52 -0600582 } else if ((ptr_len > 12 && strncmp(ptr, "encryptable=", 12) == 0) || (ptr_len > 13 && strncmp(ptr, "forceencrypt=", 13) == 0)) {
583 ptr += 12;
584 if (*ptr == '=') ptr++;
585 if (*ptr == '\"') ptr++;
586
587 Crypto_Key_Location = ptr;
588 if (Crypto_Key_Location.substr(Crypto_Key_Location.size() - 1, 1) == "\"") {
589 Crypto_Key_Location.resize(Crypto_Key_Location.size() - 1);
590 }
591 } else if (ptr_len > 8 && strncmp(ptr, "mounttodecrypt", 14) == 0) {
592 Mount_To_Decrypt = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600593 } else if (strncmp(ptr, "flashimg", 8) == 0) {
594 if (ptr_len == 8) {
595 Can_Flash_Img = true;
596 } else if (ptr_len == 10) {
597 ptr += 9;
598 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
599 Can_Flash_Img = true;
600 } else {
601 Can_Flash_Img = false;
602 }
603 }
Dees_Troy51127312012-09-08 13:08:49 -0400604 } else {
605 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000606 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400607 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000608 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400609 }
610 while (index < flags_len && flags[index] != '\0')
611 index++;
612 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500613 if (has_display_name && !has_storage_name)
614 Storage_Name = Display_Name;
615 if (!has_display_name && has_storage_name)
616 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000617 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500618 Backup_Display_Name = Display_Name;
619 if (!has_display_name && has_backup_name)
620 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400621 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400622}
623
Dees_Troy5bf43922012-09-07 16:07:55 -0400624bool TWPartition::Is_File_System(string File_System) {
625 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400626 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400627 File_System == "ext4" ||
628 File_System == "vfat" ||
629 File_System == "ntfs" ||
630 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500631 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000632 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400633 File_System == "auto")
634 return true;
635 else
636 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400637}
638
Dees_Troy5bf43922012-09-07 16:07:55 -0400639bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400640 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400641 return true;
642 else
643 return false;
644}
645
Dees_Troy51127312012-09-08 13:08:49 -0400646bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400647 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400648 if (mkdir(Path.c_str(), 0777) == -1) {
649 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500650 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400651 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000652 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400653 return false;
654 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000655 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400656 return true;
657 }
658 }
659 return true;
660}
661
Dees_Troy5bf43922012-09-07 16:07:55 -0400662void TWPartition::Setup_File_System(bool Display_Error) {
663 struct statfs st;
664
665 Can_Be_Mounted = true;
666 Can_Be_Wiped = true;
667
Dees_Troy5bf43922012-09-07 16:07:55 -0400668 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400669 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400670 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
671 Backup_Name = Display_Name;
672 Backup_Method = FILES;
673}
674
675void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400676 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
677 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800678 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400679 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800680 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400681 Backup_Method = FLASH_UTILS;
682 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000683 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 -0400684 if (Find_Partition_Size()) {
685 Used = Size;
686 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400687 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400688 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000689 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400690 else
Dees Troyd932ce12013-10-18 17:12:59 +0000691 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400692 }
693}
694
Dees_Troye58d5262012-09-21 12:27:57 -0400695void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500696 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400697 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500698 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400699 Has_Android_Secure = true;
700 Symlink_Path = Mount_Point + "/.android_secure";
701 Symlink_Mount_Point = "/and-sec";
702 Backup_Path = Symlink_Mount_Point;
703 Make_Dir("/and-sec", true);
704 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600705 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400706}
707
that9e0593e2014-10-08 00:01:24 +0200708void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500709 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600710 if (Storage_Name.empty() || Storage_Name == "Data")
711 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500712 Has_Data_Media = true;
713 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600714 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500715 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600716 if (Mount_Point == "/data") {
717 Is_Settings_Storage = true;
718 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
719 Make_Dir("/emmc", false);
720 Symlink_Mount_Point = "/emmc";
721 } else {
722 Make_Dir("/sdcard", false);
723 Symlink_Mount_Point = "/sdcard";
724 }
725 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
726 Storage_Path = Mount_Point + "/media/0";
727 Symlink_Path = Storage_Path;
728 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
729 UnMount(true);
730 }
731 DataManager::SetValue("tw_has_internal", 1);
732 DataManager::SetValue("tw_has_data_media", 1);
733 du.add_absolute_dir(Mount_Point + "/misc/vold");
734 du.add_absolute_dir(Mount_Point + "/.layout_version");
735 du.add_absolute_dir(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500736 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600737 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
738 Storage_Path = Mount_Point + "/media/0";
739 Symlink_Path = Storage_Path;
740 UnMount(true);
741 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500742 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600743 du.add_absolute_dir(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500744}
745
Dees_Troy5bf43922012-09-07 16:07:55 -0400746void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
747 char device[512], realDevice[512];
748
749 strcpy(device, Block.c_str());
750 memset(realDevice, 0, sizeof(realDevice));
751 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
752 {
753 strcpy(device, realDevice);
754 memset(realDevice, 0, sizeof(realDevice));
755 }
756
757 if (device[0] != '/') {
758 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000759 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400760 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000761 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400762 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400763 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400764 Block = device;
765 return;
766 }
767}
768
Dees_Troy8e337f32012-10-13 22:07:49 -0400769void TWPartition::Mount_Storage_Retry(void) {
770 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500771 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400772 int retry_count = 5;
773 while (retry_count > 0 && !Mount(false)) {
774 usleep(500000);
775 retry_count--;
776 }
777 Mount(true);
778 }
779}
780
Dees_Troy38bd7602012-09-14 13:33:53 -0400781bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
782 FILE *fp = NULL;
783 char line[255];
784
785 fp = fopen("/proc/mtd", "rt");
786 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000787 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400788 return false;
789 }
790
791 while (fgets(line, sizeof(line), fp) != NULL)
792 {
793 char device[32], label[32];
794 unsigned long size = 0;
795 char* fstype = NULL;
796 int deviceId;
797
798 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
799
800 // Skip header and blank lines
801 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
802 continue;
803
804 // Strip off the trailing " from the label
805 label[strlen(label)-1] = '\0';
806
807 if (strcmp(label, MTD_Name.c_str()) == 0) {
808 // We found our device
809 // Strip off the trailing : from the device
810 device[strlen(device)-1] = '\0';
811 if (sscanf(device,"mtd%d", &deviceId) == 1) {
812 sprintf(device, "/dev/block/mtdblock%d", deviceId);
813 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000814 fclose(fp);
815 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400816 }
817 }
818 }
819 fclose(fp);
820
821 return false;
822}
823
Dees_Troy51127312012-09-08 13:08:49 -0400824bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
825 struct statfs st;
826 string Local_Path = Mount_Point + "/.";
827
828 if (!Mount(Display_Error))
829 return false;
830
831 if (statfs(Local_Path.c_str(), &st) != 0) {
832 if (!Removable) {
833 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000834 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400835 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000836 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400837 }
838 return false;
839 }
840 Size = (st.f_blocks * st.f_bsize);
841 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
842 Free = (st.f_bfree * st.f_bsize);
843 Backup_Size = Used;
844 return true;
845}
846
847bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400848 FILE* fp;
849 char command[255], line[512];
850 int include_block = 1;
851 unsigned int min_len;
852
853 if (!Mount(Display_Error))
854 return false;
855
Dees_Troy38bd7602012-09-14 13:33:53 -0400856 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400857 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200858 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400859 fp = fopen("/tmp/dfoutput.txt", "rt");
860 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000861 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400862 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400863 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400864
865 while (fgets(line, sizeof(line), fp) != NULL)
866 {
867 unsigned long blocks, used, available;
868 char device[64];
869 char tmpString[64];
870
871 if (strncmp(line, "Filesystem", 10) == 0)
872 continue;
873 if (strlen(line) < min_len) {
874 include_block = 0;
875 continue;
876 }
877 if (include_block) {
878 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
879 } else {
880 // The device block string is so long that the df information is on the next line
881 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400882 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400883 while (tmpString[space_count] == 32)
884 space_count++;
885 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
886 }
887
888 // Adjust block size to byte size
889 Size = blocks * 1024ULL;
890 Used = used * 1024ULL;
891 Free = available * 1024ULL;
892 Backup_Size = Used;
893 }
894 fclose(fp);
895 return true;
896}
897
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600898unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600899 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -0600900
901 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600902}
903
Dees_Troy5bf43922012-09-07 16:07:55 -0400904bool TWPartition::Find_Partition_Size(void) {
905 FILE* fp;
906 char line[512];
907 string tmpdevice;
908
igoriok87e3d932013-01-31 21:03:53 +0200909 fp = fopen("/proc/dumchar_info", "rt");
910 if (fp != NULL) {
911 while (fgets(line, sizeof(line), fp) != NULL)
912 {
913 char label[32], device[32];
914 unsigned long size = 0;
915
thatd43bf2d2014-09-21 23:13:02 +0200916 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200917
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500918 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200919 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
920 continue;
921
922 tmpdevice = "/dev/";
923 tmpdevice += label;
924 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
925 Size = size;
926 fclose(fp);
927 return true;
928 }
929 }
930 }
931
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600932 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
933 if (ioctl_size) {
934 Size = ioctl_size;
935 return true;
936 }
937
Dees_Troy5bf43922012-09-07 16:07:55 -0400938 // In this case, we'll first get the partitions we care about (with labels)
939 fp = fopen("/proc/partitions", "rt");
940 if (fp == NULL)
941 return false;
942
943 while (fgets(line, sizeof(line), fp) != NULL)
944 {
945 unsigned long major, minor, blocks;
946 char device[512];
947 char tmpString[64];
948
Dees_Troy63c8df72012-09-10 14:02:05 -0400949 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400950 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
951
952 tmpdevice = "/dev/block/";
953 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400954 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400955 // Adjust block size to byte size
956 Size = blocks * 1024ULL;
957 fclose(fp);
958 return true;
959 }
960 }
961 fclose(fp);
962 return false;
963}
964
Dees_Troy5bf43922012-09-07 16:07:55 -0400965bool TWPartition::Is_Mounted(void) {
966 if (!Can_Be_Mounted)
967 return false;
968
969 struct stat st1, st2;
970 string test_path;
971
972 // Check to see if the mount point directory exists
973 test_path = Mount_Point + "/.";
974 if (stat(test_path.c_str(), &st1) != 0) return false;
975
976 // Check to see if the directory above the mount point exists
977 test_path = Mount_Point + "/../.";
978 if (stat(test_path.c_str(), &st2) != 0) return false;
979
980 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
981 int ret = (st1.st_dev != st2.st_dev) ? true : false;
982
983 return ret;
984}
985
986bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000987 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500988 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000989
Dees_Troy5bf43922012-09-07 16:07:55 -0400990 if (Is_Mounted()) {
991 return true;
992 } else if (!Can_Be_Mounted) {
993 return false;
994 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400995
996 Find_Actual_Block_Device();
997
998 // Check the current file system before mounting
999 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001000 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001001 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 +00001002 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001003 string result;
1004 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001005 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001006 Current_File_System = "vfat";
1007 } else {
1008#ifdef TW_NO_EXFAT_FUSE
1009 UnMount(false);
1010 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1011 // Some kernels let us mount vfat as exfat which doesn't work out too well
1012#else
1013 exfat_mounted = 1;
1014#endif
1015 }
1016 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001017
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001018 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001019 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001020 string Ntfsmount_Binary = "";
1021
1022 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1023 Ntfsmount_Binary = "ntfs-3g";
1024 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1025 Ntfsmount_Binary = "mount.ntfs";
1026
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001027 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001028 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001029 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001030 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001031 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001032
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001033 if (TWFunc::Exec_Cmd(cmd) == 0) {
1034 return true;
1035 } else {
1036 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1037 }
1038 }
1039
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001040 if (Mount_Read_Only)
1041 flags |= MS_RDONLY;
1042
Dees_Troy22042032012-12-18 21:23:08 +00001043 if (Fstab_File_System == "yaffs2") {
1044 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001045 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1046 if (Mount_Read_Only)
1047 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001048 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1049 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1050 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001051 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001052 else
1053 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1054 return false;
1055 } else {
1056 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1057 return true;
1058 }
1059 } else {
1060 struct stat st;
1061 string test_path = Mount_Point;
1062 if (stat(test_path.c_str(), &st) < 0) {
1063 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001064 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001065 else
1066 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1067 return false;
1068 }
1069 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1070 if (new_mode != st.st_mode) {
1071 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1072 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1073 if (Display_Error)
1074 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1075 else
1076 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1077 return false;
1078 }
1079 }
Dees_Troy22042032012-12-18 21:23:08 +00001080 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001081 }
thatc7572eb2015-03-31 18:55:30 +02001082 }
1083
1084 string mount_fs = Current_File_System;
1085 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1086 mount_fs = "texfat";
1087
1088 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001089 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1090 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001091#ifdef TW_NO_EXFAT_FUSE
1092 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001093 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001094 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001095 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001096 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001097 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001098 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001099 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 +00001100 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001101 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001102 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001103#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001104 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001105 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001106 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001107 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1108 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 +00001109 return false;
1110#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001111 }
1112#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001113 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001114
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001115 if (Removable)
1116 Update_Size(Display_Error);
1117
xiaolu9416f4f2015-06-04 08:22:23 +08001118 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001119 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001120 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001121 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001122 return true;
1123}
1124
1125bool TWPartition::UnMount(bool Display_Error) {
1126 if (Is_Mounted()) {
1127 int never_unmount_system;
1128
1129 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1130 if (never_unmount_system == 1 && Mount_Point == "/system")
1131 return true; // Never unmount system if you're not supposed to unmount it
1132
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001133 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001134 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001135
Dees_Troy38bd7602012-09-14 13:33:53 -04001136 if (!Symlink_Mount_Point.empty())
1137 umount(Symlink_Mount_Point.c_str());
1138
Dees_Troyb05ddee2013-01-28 20:24:50 +00001139 umount(Mount_Point.c_str());
1140 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001141 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001142 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001143 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001144 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001145 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001146 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001147 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001148 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001149 } else {
1150 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001151 }
1152}
1153
Gary Peck43acadf2012-11-21 21:19:01 -08001154bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001155 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001156 int check;
1157 string Layout_Filename = Mount_Point + "/.layout_version";
1158
Dees_Troy38bd7602012-09-14 13:33:53 -04001159 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001160 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001161 return false;
1162 }
1163
Dees_Troyc51f1f92012-09-20 15:32:13 -04001164 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001165 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001166
Dees_Troy16c2b312013-01-15 16:51:18 +00001167 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1168 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1169 else
1170 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001171
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001172 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001173 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001174 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001175 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001176 DataManager::GetValue(TW_RM_RF_VAR, check);
1177
Hashcodedabfd492013-08-29 22:45:30 -07001178 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001179 wiped = Wipe_RMRF();
1180 else if (New_File_System == "ext4")
1181 wiped = Wipe_EXT4();
1182 else if (New_File_System == "ext2" || New_File_System == "ext3")
1183 wiped = Wipe_EXT23(New_File_System);
1184 else if (New_File_System == "vfat")
1185 wiped = Wipe_FAT();
1186 else if (New_File_System == "exfat")
1187 wiped = Wipe_EXFAT();
1188 else if (New_File_System == "yaffs2")
1189 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001190 else if (New_File_System == "f2fs")
1191 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001192 else if (New_File_System == "ntfs")
1193 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001194 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001195 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 +00001196 unlink("/.layout_version");
1197 return false;
1198 }
1199 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001200 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001201
Gary Pecke8bc5d72012-12-21 06:45:25 -08001202 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001203 if (Mount_Point == "/cache")
1204 DataManager::Output_Version();
1205
Dees_Troy16c2b312013-01-15 16:51:18 +00001206 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1207 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1208
1209 if (update_crypt) {
1210 Setup_File_System(false);
1211 if (Is_Encrypted && !Is_Decrypted) {
1212 // just wiped an encrypted partition back to its unencrypted state
1213 Is_Encrypted = false;
1214 Is_Decrypted = false;
1215 Decrypted_Block_Device = "";
1216 if (Mount_Point == "/data") {
1217 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1218 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1219 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001220 }
1221 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001222
Ethan Yonker66a19492015-12-10 10:19:45 -06001223 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001224 Recreate_Media_Folder();
1225 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001226 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001227 if (Is_Storage) {
1228 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001229 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001230 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001231}
1232
Gary Peck43acadf2012-11-21 21:19:01 -08001233bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001234 if (Is_File_System(Current_File_System))
1235 return Wipe(Current_File_System);
1236 else
1237 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001238}
1239
Dees_Troye58d5262012-09-21 12:27:57 -04001240bool TWPartition::Wipe_AndSec(void) {
1241 if (!Has_Android_Secure)
1242 return false;
1243
Dees_Troye58d5262012-09-21 12:27:57 -04001244 if (!Mount(true))
1245 return false;
1246
Ethan Yonker74db1572015-10-28 12:44:49 -05001247 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001248 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001249 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001250}
1251
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001252bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001253 if (Mount_Read_Only)
1254 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001255 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001256 return true;
1257 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1258 return true;
1259 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1260 return true;
1261 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1262 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001263 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001264 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001265 return false;
1266}
1267
1268bool TWPartition::Repair() {
1269 string command;
1270
1271 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001272 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001273 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001274 return false;
1275 }
1276 if (!UnMount(true))
1277 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001278 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001279 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001280 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001281 LOGINFO("Repair command: %s\n", command.c_str());
1282 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001283 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001284 return true;
1285 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001286 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001287 return false;
1288 }
1289 }
1290 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1291 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001292 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001293 return false;
1294 }
1295 if (!UnMount(true))
1296 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001297 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001298 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001299 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001300 LOGINFO("Repair command: %s\n", command.c_str());
1301 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001302 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001303 return true;
1304 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001305 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001306 return false;
1307 }
1308 }
1309 if (Current_File_System == "exfat") {
1310 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001311 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001312 return false;
1313 }
1314 if (!UnMount(true))
1315 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001316 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001317 Find_Actual_Block_Device();
1318 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1319 LOGINFO("Repair command: %s\n", command.c_str());
1320 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001321 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001322 return true;
1323 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001324 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001325 return false;
1326 }
1327 }
1328 if (Current_File_System == "f2fs") {
1329 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001330 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001331 return false;
1332 }
1333 if (!UnMount(true))
1334 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001335 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001336 Find_Actual_Block_Device();
1337 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1338 LOGINFO("Repair command: %s\n", command.c_str());
1339 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001340 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001341 return true;
1342 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001343 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001344 return false;
1345 }
1346 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001347 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001348 string Ntfsfix_Binary;
1349 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1350 Ntfsfix_Binary = "ntfsfix";
1351 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1352 Ntfsfix_Binary = "fsck.ntfs";
1353 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001354 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001355 return false;
1356 }
1357 if (!UnMount(true))
1358 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001359 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001360 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001361 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001362 LOGINFO("Repair command: %s\n", command.c_str());
1363 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001364 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001365 return true;
1366 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001367 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001368 return false;
1369 }
1370 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001371 return false;
1372}
1373
Ethan Yonkera2719152015-05-28 09:44:41 -05001374bool TWPartition::Can_Resize() {
1375 if (Mount_Read_Only)
1376 return false;
1377 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1378 return true;
1379 return false;
1380}
1381
1382bool TWPartition::Resize() {
1383 string command;
1384
1385 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1386 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001387 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1388 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001389 return false;
1390 }
1391 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001392 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1393 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001394 return false;
1395 }
1396 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001397 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001398 if (!Repair())
1399 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001400 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001401 Find_Actual_Block_Device();
1402 command = "/sbin/resize2fs " + Actual_Block_Device;
1403 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001404 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1405 if (Actual_Size == 0)
1406 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001407
Ethan Yonkera2719152015-05-28 09:44:41 -05001408 unsigned long long Block_Count;
1409 if (Length < 0) {
1410 // Reduce overall size by this length
1411 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1412 } else {
1413 // This is the size, not a size reduction
1414 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1415 }
1416 char temp[256];
1417 sprintf(temp, "%llu", Block_Count);
1418 command += " ";
1419 command += temp;
1420 command += "K";
1421 }
1422 LOGINFO("Resize command: %s\n", command.c_str());
1423 if (TWFunc::Exec_Cmd(command) == 0) {
1424 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001425 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001426 return true;
1427 } else {
1428 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001429 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001430 return false;
1431 }
1432 }
1433 return false;
1434}
1435
bigbiff7abc5fe2015-01-17 16:53:12 -05001436bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
1437 if (Backup_Method == FILES) {
1438 return Backup_Tar(backup_folder, overall_size, other_backups_size, tar_fork_pid);
1439 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001440 else if (Backup_Method == DD)
1441 return Backup_DD(backup_folder);
1442 else if (Backup_Method == FLASH_UTILS)
1443 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001444 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001445 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001446}
1447
Dees_Troy43d8b002012-09-17 16:00:01 -04001448bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001449 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001450 char split_filename[512];
1451 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001452 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001453
Captain Throwbackff5935f2014-09-23 16:08:34 -04001454 sync();
1455
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001456 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001457 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001458 if (!TWFunc::Path_Exists(Full_Filename)) {
1459 // This is a split archive, we presume
1460 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001461 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001462 md5file = split_filename;
1463 md5file += ".md5";
1464 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001465 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 -04001466 return false;
1467 }
1468 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001469 while (index < 1000) {
1470 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001471 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001472 return false;
1473 }
1474 index++;
1475 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001476 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001477 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001478 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001479 } else {
1480 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001481 md5file = Full_Filename + ".md5";
1482 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001483 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 -04001484 return false;
1485 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001486 md5sum.setfn(Full_Filename);
1487 if (md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001488 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001489 return false;
1490 } else
1491 return true;
1492 }
1493 return false;
1494}
1495
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001496bool 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 -08001497 string Restore_File_System;
1498
Matt Mower3c366972015-12-25 19:28:31 -06001499 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Dees_Troy2673cec2013-04-02 20:22:16 +00001500 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001501
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001502 Restore_File_System = Get_Restore_File_System(restore_folder);
1503
1504 if (Is_File_System(Restore_File_System))
1505 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1506 else if (Is_Image(Restore_File_System)) {
Ethan Yonkerf66324f2015-01-09 16:46:57 -06001507 return Restore_Image(restore_folder, total_restore_size, already_restored_size, Restore_File_System);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001508 }
1509
1510 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1511 return false;
1512}
1513
1514string TWPartition::Get_Restore_File_System(string restore_folder) {
1515 size_t first_period, second_period;
1516 string Restore_File_System;
1517
Gary Peck43acadf2012-11-21 21:19:01 -08001518 // Parse backup filename to extract the file system before wiping
1519 first_period = Backup_FileName.find(".");
1520 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001521 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001522 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001523 }
1524 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1525 second_period = Restore_File_System.find(".");
1526 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001527 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001528 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001529 }
1530 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001531 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001532 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001533}
1534
1535string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001536 if (Backup_Method == NONE)
1537 return "none";
1538 else if (Backup_Method == FILES)
1539 return "files";
1540 else if (Backup_Method == DD)
1541 return "dd";
1542 else if (Backup_Method == FLASH_UTILS)
1543 return "flash_utils";
1544 else
1545 return "undefined";
1546 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001547}
1548
1549bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001550 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001551 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001552 return 1;
1553}
1554
1555bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001556 bool Save_Data_Media = Has_Data_Media;
1557
1558 if (!UnMount(true))
1559 return false;
1560
Dees_Troy38bd7602012-09-14 13:33:53 -04001561 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001562 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001563#ifdef TW_INCLUDE_CRYPTO
1564 if (Is_Decrypted) {
1565 if (!UnMount(true))
1566 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001567 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001568 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1569 }
1570 }
1571#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001572 Is_Decrypted = false;
1573 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001574 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001575 if (Crypto_Key_Location == "footer") {
1576 int newlen, fd;
1577 if (Length != 0) {
1578 newlen = Length;
1579 if (newlen < 0)
1580 newlen = newlen * -1;
1581 } else {
1582 newlen = CRYPT_FOOTER_OFFSET;
1583 }
1584 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1585 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1586 } else {
1587 unsigned int block_count;
1588 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1589 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1590 } else {
1591 off64_t offset = ((off64_t)block_count * 512) - newlen;
1592 if (lseek64(fd, offset, SEEK_SET) == -1) {
1593 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1594 } else {
1595 void* buffer = malloc(newlen);
1596 if (!buffer) {
1597 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1598 } else {
1599 memset(buffer, 0, newlen);
1600 int ret = write(fd, buffer, newlen);
1601 if (ret != newlen) {
1602 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1603 } else {
1604 LOGINFO("Successfully wiped crypto footer.\n");
1605 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001606 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001607 }
1608 }
1609 }
1610 close(fd);
1611 }
1612 } else {
1613 string Command = "flash_image " + Crypto_Key_Location + " /dev/zero";
1614 TWFunc::Exec_Cmd(Command);
1615 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001616 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001617 Has_Data_Media = Save_Data_Media;
1618 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1619 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001620 if (Mount(false))
1621 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001622 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001623 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001624#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001625 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 -05001626#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001627 return true;
1628 } else {
1629 Has_Data_Media = Save_Data_Media;
Ethan Yonker