blob: 4626a5ff6edd12317b06fbe3145c82a17c81025d [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002 Copyright 2013 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050027#include <iostream>
28#include <sstream>
Ethan Yonker66a19492015-12-10 10:19:45 -060029#include <sys/param.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040030
Dees_Troy657c3092012-09-10 20:32:10 -040031#ifdef TW_INCLUDE_CRYPTO
32 #include "cutils/properties.h"
33#endif
34
bigbiff7b4c7a62015-01-01 19:44:14 -050035#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040036#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040039#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040040#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050041#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050042#include "twrpTar.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050043#include "twrpDU.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050044#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060045#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050046#include "gui/gui.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040047extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040048 #include "mtdutils/mtdutils.h"
49 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000050#ifdef USE_EXT4
51 #include "make_ext4fs.h"
52#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060053
54#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060055 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060056 #include "gpt/gpt.h"
Ethan Yonkerbc85b632015-08-09 12:48:14 -050057#else
58 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060059#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040060}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040061#ifdef HAVE_SELINUX
62#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060063#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040064#endif
Dees Troy4159aed2014-02-28 17:24:43 +000065#ifdef HAVE_CAPABILITIES
66#include <sys/capability.h>
67#include <sys/xattr.h>
68#include <linux/xattr.h>
69#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040070
bigbiff bigbiff9c754052013-01-09 09:09:08 -050071using namespace std;
72
Dees_Troya95f55c2013-08-17 13:14:43 +000073extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050074extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000075
Hashcode62bd9e02013-11-19 21:59:42 -080076struct flag_list {
77 const char *name;
78 unsigned flag;
79};
80
81static struct flag_list mount_flags[] = {
82 { "noatime", MS_NOATIME },
83 { "noexec", MS_NOEXEC },
84 { "nosuid", MS_NOSUID },
85 { "nodev", MS_NODEV },
86 { "nodiratime", MS_NODIRATIME },
87 { "ro", MS_RDONLY },
88 { "rw", 0 },
89 { "remount", MS_REMOUNT },
90 { "bind", MS_BIND },
91 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000092#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080093 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000094#endif
95#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080096 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000097#endif
98#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -080099 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000100#endif
101#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800102 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000103#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800104 { "sync", MS_SYNCHRONOUS },
105 { "defaults", 0 },
106 { 0, 0 },
107};
108
that9e0593e2014-10-08 00:01:24 +0200109TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400110 Can_Be_Mounted = false;
111 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500112 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200113 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400114 Wipe_During_Factory_Reset = false;
115 Wipe_Available_in_GUI = false;
116 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400117 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400118 SubPartition_Of = "";
119 Symlink_Path = "";
120 Symlink_Mount_Point = "";
121 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400122 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400123 Actual_Block_Device = "";
124 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400125 Alternate_Block_Device = "";
126 Removable = false;
127 Is_Present = false;
128 Length = 0;
129 Size = 0;
130 Used = 0;
131 Free = 0;
132 Backup_Size = 0;
133 Can_Be_Encrypted = false;
134 Is_Encrypted = false;
135 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600136 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400137 Decrypted_Block_Device = "";
138 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500139 Backup_Display_Name = "";
140 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400141 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400142 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400143 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400144 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000145 Can_Encrypt_Backup = false;
146 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400147 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400148 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400149 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500150 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400151 Storage_Path = "";
152 Current_File_System = "";
153 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800154 Mount_Flags = 0;
155 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400156 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000157 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000158 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600159 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600160 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600161 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500162 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600163 Is_Adopted_Storage = false;
164 Adopted_GUID = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400165}
166
167TWPartition::~TWPartition(void) {
168 // Do nothing
169}
170
Dees_Troy5bf43922012-09-07 16:07:55 -0400171bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
172 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
173 int line_len = Line.size(), index = 0, item_index = 0;
174 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400175 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400176 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500177 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400178
Dees_Troy51127312012-09-08 13:08:49 -0400179 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500180 if (full_line[index] == 34)
181 skip = !skip;
182 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400183 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400184 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400185 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000186 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400187 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500188 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000189 Display_Name = full_line + 1;
190 Backup_Display_Name = Display_Name;
191 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400192 index = Mount_Point.size();
193 while (index < line_len) {
194 while (index < line_len && full_line[index] == '\0')
195 index++;
196 if (index >= line_len)
197 continue;
198 ptr = full_line + index;
199 if (item_index == 0) {
200 // File System
201 Fstab_File_System = ptr;
202 Current_File_System = ptr;
203 item_index++;
204 } else if (item_index == 1) {
205 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400206 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400207 MTD_Name = ptr;
208 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400209 } else if (Fstab_File_System == "bml") {
210 if (Mount_Point == "/boot")
211 MTD_Name = "boot";
212 else if (Mount_Point == "/recovery")
213 MTD_Name = "recovery";
214 Primary_Block_Device = ptr;
215 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000216 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 -0400217 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400218 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000219 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400220 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000221 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400222 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400223 } else {
224 Primary_Block_Device = ptr;
225 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400226 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400227 item_index++;
228 } else if (item_index > 1) {
229 if (*ptr == '/') {
230 // Alternate Block Device
231 Alternate_Block_Device = ptr;
232 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
233 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
234 // Partition length
235 ptr += 7;
236 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400237 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
238 // Custom flags, save for later so that new values aren't overwritten by defaults
239 ptr += 6;
240 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000241 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400242 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
243 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400244 } else {
245 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000246 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400247 }
248 }
249 while (index < line_len && full_line[index] != '\0')
250 index++;
251 }
252
253 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
254 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000255 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400256 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000257 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400258 return 0;
259 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400260 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 Setup_File_System(Display_Error);
262 if (Mount_Point == "/system") {
263 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500264 Backup_Display_Name = Display_Name;
265 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400266 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500267 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500268 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400269 } else if (Mount_Point == "/data") {
Dees Troyc657cc02015-01-16 22:48:47 +0000270 UnMount(false); // added in case /data is mounted as tmpfs for qcom hardware decrypt
Dees_Troy5bf43922012-09-07 16:07:55 -0400271 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500272 Backup_Display_Name = Display_Name;
273 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400274 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400275 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500276 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000277 Can_Encrypt_Backup = true;
278 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500279 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200280 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400281#ifdef TW_INCLUDE_CRYPTO
282 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400283 char crypto_blkdev[255];
284 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
285 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400286 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400287 DataManager::SetValue(TW_IS_DECRYPTED, 1);
288 Is_Encrypted = true;
289 Is_Decrypted = true;
290 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000291 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400292 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600293 if (Is_Present) {
Ethan Yonker253368a2014-11-25 15:00:52 -0600294 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 -0600295 if (cryptfs_check_footer() == 0) {
296 Is_Encrypted = true;
297 Is_Decrypted = false;
298 Can_Be_Mounted = false;
299 Current_File_System = "emmc";
300 Setup_Image(Display_Error);
301 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
Vojtech Bocek7e11ac52015-03-05 23:21:49 +0100302 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
Ethan Yonker71413f42014-02-26 13:36:08 -0600303 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
304 DataManager::SetValue("tw_crypto_display", "");
305 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500306 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
Ethan Yonker71413f42014-02-26 13:36:08 -0600307 }
308 } else {
309 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
310 }
Gary Peck82599a82012-11-21 16:23:12 -0800311 } else {
312 // Filesystem is not encrypted and the mount
313 // succeeded, so get it back to the original
314 // unmounted state
315 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400316 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500317 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400318 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400319#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500320 if (datamedia)
321 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400322#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400323 } else if (Mount_Point == "/cache") {
324 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500325 Backup_Display_Name = Display_Name;
326 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400327 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400328 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500329 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400330 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000331 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500332 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500333 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400334 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400335 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400336 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400337 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500338 Backup_Display_Name = Display_Name;
339 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400340 Is_SubPartition = true;
341 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400342 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500343 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000344 Can_Encrypt_Backup = true;
345 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400346 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400347 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400348 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500349 Backup_Display_Name = Display_Name;
350 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400351 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400352 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500353 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000354 Can_Encrypt_Backup = true;
355 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400356 } else if (Mount_Point == "/boot") {
357 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500358 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400359 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500360 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500361 } else if (Mount_Point == "/vendor") {
362 Display_Name = "Vendor";
363 Backup_Display_Name = Display_Name;
364 Storage_Name = Display_Name;
365 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400366 }
367#ifdef TW_EXTERNAL_STORAGE_PATH
368 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
369 Is_Storage = true;
370 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400371 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500372 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400373#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000374 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400375 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400376 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500377 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400378#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000379 }
Dees_Troy8170a922012-09-18 15:40:25 -0400380#ifdef TW_INTERNAL_STORAGE_PATH
381 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
382 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500383 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400384 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500385 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400386 }
387#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000388 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400389 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500390 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500391 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400392 }
393#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400394 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400395 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400396 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500397 if (Mount_Point == "/boot") {
398 Display_Name = "Boot";
399 Backup_Display_Name = Display_Name;
400 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600401 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500402 } else if (Mount_Point == "/recovery") {
403 Display_Name = "Recovery";
404 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600405 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500406 } else if (Mount_Point == "/system_image") {
407 Display_Name = "System Image";
408 Backup_Display_Name = Display_Name;
409 Can_Flash_Img = false;
410 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500411 } else if (Mount_Point == "/vendor_image") {
412 Display_Name = "Vendor Image";
413 Backup_Display_Name = Display_Name;
414 Can_Flash_Img = false;
415 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500416 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400417 }
418
Dees_Troy51127312012-09-08 13:08:49 -0400419 // Process any custom flags
420 if (Flags.size() > 0)
421 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400422 return true;
423}
424
Ketut Putu Kumajayabfb72b02015-08-10 16:40:55 +0200425bool TWPartition::Process_FS_Flags(string& Options, int& Flags) {
Hashcode62bd9e02013-11-19 21:59:42 -0800426 int i;
427 char *p;
428 char *savep;
429 char fs_options[250];
430
431 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
432 Options = "";
433
434 p = strtok_r(fs_options, ",", &savep);
435 while (p) {
436 /* Look for the flag "p" in the flag list "fl"
437 * If not found, the loop exits with fl[i].name being null.
438 */
439 for (i = 0; mount_flags[i].name; i++) {
440 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
441 Flags |= mount_flags[i].flag;
442 break;
443 }
444 }
445
446 if (!mount_flags[i].name) {
447 if (Options.size() > 0)
448 Options += ",";
449 Options += p;
450 }
451 p = strtok_r(NULL, ",", &savep);
452 }
453
454 return true;
455}
456
Dees_Troy51127312012-09-08 13:08:49 -0400457bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
458 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500459 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400460 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500461 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400462
463 strcpy(flags, Flags.c_str());
464 flags_len = Flags.size();
465 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500466 if (flags[index] == 34)
467 skip = !skip;
468 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400469 flags[index] = '\0';
470 }
471
472 index = 0;
473 while (index < flags_len) {
474 while (index < flags_len && flags[index] == '\0')
475 index++;
476 if (index >= flags_len)
477 continue;
478 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500479 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400480 if (strcmp(ptr, "removable") == 0) {
481 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600482 } else if (strncmp(ptr, "storage", 7) == 0) {
483 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600484 Is_Storage = true;
485 } else if (ptr_len == 9) {
486 ptr += 9;
487 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
488 LOGINFO("storage set to true\n");
489 Is_Storage = true;
490 } else {
491 LOGINFO("storage set to false\n");
492 Is_Storage = false;
493 }
494 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500495 } else if (strcmp(ptr, "settingsstorage") == 0) {
496 Is_Storage = true;
Xueferbfce5042015-04-14 02:02:05 +0800497 Is_Settings_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500498 } else if (strcmp(ptr, "andsec") == 0) {
499 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400500 } else if (strcmp(ptr, "canbewiped") == 0) {
501 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700502 } else if (strcmp(ptr, "usermrf") == 0) {
503 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500504 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
505 ptr += 7;
506 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
507 Can_Be_Backed_Up = true;
508 else
509 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400510 } else if (strcmp(ptr, "wipeingui") == 0) {
511 Can_Be_Wiped = true;
512 Wipe_Available_in_GUI = true;
513 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
514 Can_Be_Wiped = true;
515 Wipe_Available_in_GUI = true;
516 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500517 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000518 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400519 Is_SubPartition = true;
520 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000521 } else if (strcmp(ptr, "ignoreblkid") == 0) {
522 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000523 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
524 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500525 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400526 ptr += 8;
527 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500528 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
529 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400530 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500531 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400532 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500533 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
534 Display_Name.resize(Display_Name.size() - 1);
535 }
536 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
537 has_storage_name = true;
538 ptr += 11;
539 if (*ptr == '\"') ptr++;
540 Storage_Name = ptr;
541 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
542 Storage_Name.resize(Storage_Name.size() - 1);
543 }
544 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
545 has_backup_name = true;
546 ptr += 10;
547 if (*ptr == '\"') ptr++;
548 Backup_Display_Name = ptr;
549 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
550 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
551 }
552 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400553 ptr += 10;
Ethan Yonkera2719152015-05-28 09:44:41 -0500554 Format_Block_Size = (unsigned long)(atol(ptr));
Dees_Troya13d74f2013-03-24 08:54:55 -0500555 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400556 ptr += 7;
557 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000558 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
559 ptr += 17;
560 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
561 Can_Encrypt_Backup = true;
562 else
563 Can_Encrypt_Backup = false;
564 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
565 ptr += 21;
566 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
567 Can_Encrypt_Backup = true;
568 Use_Userdata_Encryption = true;
569 } else {
570 Use_Userdata_Encryption = false;
571 }
Hashcode62bd9e02013-11-19 21:59:42 -0800572 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
573 ptr += 8;
574 if (*ptr == '\"') ptr++;
575
576 Mount_Options = ptr;
577 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
578 Mount_Options.resize(Mount_Options.size() - 1);
579 }
580 Process_FS_Flags(Mount_Options, Mount_Flags);
Ethan Yonker253368a2014-11-25 15:00:52 -0600581 } else if ((ptr_len > 12 && strncmp(ptr, "encryptable=", 12) == 0) || (ptr_len > 13 && strncmp(ptr, "forceencrypt=", 13) == 0)) {
582 ptr += 12;
583 if (*ptr == '=') ptr++;
584 if (*ptr == '\"') ptr++;
585
586 Crypto_Key_Location = ptr;
587 if (Crypto_Key_Location.substr(Crypto_Key_Location.size() - 1, 1) == "\"") {
588 Crypto_Key_Location.resize(Crypto_Key_Location.size() - 1);
589 }
590 } else if (ptr_len > 8 && strncmp(ptr, "mounttodecrypt", 14) == 0) {
591 Mount_To_Decrypt = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600592 } else if (strncmp(ptr, "flashimg", 8) == 0) {
593 if (ptr_len == 8) {
594 Can_Flash_Img = true;
595 } else if (ptr_len == 10) {
596 ptr += 9;
597 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
598 Can_Flash_Img = true;
599 } else {
600 Can_Flash_Img = false;
601 }
602 }
Dees_Troy51127312012-09-08 13:08:49 -0400603 } else {
604 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000605 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400606 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000607 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400608 }
609 while (index < flags_len && flags[index] != '\0')
610 index++;
611 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500612 if (has_display_name && !has_storage_name)
613 Storage_Name = Display_Name;
614 if (!has_display_name && has_storage_name)
615 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000616 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500617 Backup_Display_Name = Display_Name;
618 if (!has_display_name && has_backup_name)
619 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400620 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400621}
622
Dees_Troy5bf43922012-09-07 16:07:55 -0400623bool TWPartition::Is_File_System(string File_System) {
624 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400625 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400626 File_System == "ext4" ||
627 File_System == "vfat" ||
628 File_System == "ntfs" ||
629 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500630 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000631 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400632 File_System == "auto")
633 return true;
634 else
635 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400636}
637
Dees_Troy5bf43922012-09-07 16:07:55 -0400638bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400639 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400640 return true;
641 else
642 return false;
643}
644
Dees_Troy51127312012-09-08 13:08:49 -0400645bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400646 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400647 if (mkdir(Path.c_str(), 0777) == -1) {
648 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500649 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400650 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000651 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400652 return false;
653 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000654 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400655 return true;
656 }
657 }
658 return true;
659}
660
Dees_Troy5bf43922012-09-07 16:07:55 -0400661void TWPartition::Setup_File_System(bool Display_Error) {
662 struct statfs st;
663
664 Can_Be_Mounted = true;
665 Can_Be_Wiped = true;
666
Dees_Troy5bf43922012-09-07 16:07:55 -0400667 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400668 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400669 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
670 Backup_Name = Display_Name;
671 Backup_Method = FILES;
672}
673
674void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400675 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
676 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800677 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400678 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800679 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400680 Backup_Method = FLASH_UTILS;
681 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000682 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 -0400683 if (Find_Partition_Size()) {
684 Used = Size;
685 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400686 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400687 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000688 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400689 else
Dees Troyd932ce12013-10-18 17:12:59 +0000690 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400691 }
692}
693
Dees_Troye58d5262012-09-21 12:27:57 -0400694void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500695 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400696 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500697 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400698 Has_Android_Secure = true;
699 Symlink_Path = Mount_Point + "/.android_secure";
700 Symlink_Mount_Point = "/and-sec";
701 Backup_Path = Symlink_Mount_Point;
702 Make_Dir("/and-sec", true);
703 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600704 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400705}
706
that9e0593e2014-10-08 00:01:24 +0200707void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500708 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600709 if (Storage_Name.empty() || Storage_Name == "Data")
710 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500711 Has_Data_Media = true;
712 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600713 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500714 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600715 if (Mount_Point == "/data") {
716 Is_Settings_Storage = true;
717 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
718 Make_Dir("/emmc", false);
719 Symlink_Mount_Point = "/emmc";
720 } else {
721 Make_Dir("/sdcard", false);
722 Symlink_Mount_Point = "/sdcard";
723 }
724 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
725 Storage_Path = Mount_Point + "/media/0";
726 Symlink_Path = Storage_Path;
727 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
728 UnMount(true);
729 }
730 DataManager::SetValue("tw_has_internal", 1);
731 DataManager::SetValue("tw_has_data_media", 1);
732 du.add_absolute_dir(Mount_Point + "/misc/vold");
733 du.add_absolute_dir(Mount_Point + "/.layout_version");
734 du.add_absolute_dir(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500735 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600736 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
737 Storage_Path = Mount_Point + "/media/0";
738 Symlink_Path = Storage_Path;
739 UnMount(true);
740 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500741 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600742 du.add_absolute_dir(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500743}
744
Dees_Troy5bf43922012-09-07 16:07:55 -0400745void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
746 char device[512], realDevice[512];
747
748 strcpy(device, Block.c_str());
749 memset(realDevice, 0, sizeof(realDevice));
750 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
751 {
752 strcpy(device, realDevice);
753 memset(realDevice, 0, sizeof(realDevice));
754 }
755
756 if (device[0] != '/') {
757 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000758 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400759 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000760 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400761 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400762 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400763 Block = device;
764 return;
765 }
766}
767
Dees_Troy8e337f32012-10-13 22:07:49 -0400768void TWPartition::Mount_Storage_Retry(void) {
769 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500770 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400771 int retry_count = 5;
772 while (retry_count > 0 && !Mount(false)) {
773 usleep(500000);
774 retry_count--;
775 }
776 Mount(true);
777 }
778}
779
Dees_Troy38bd7602012-09-14 13:33:53 -0400780bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
781 FILE *fp = NULL;
782 char line[255];
783
784 fp = fopen("/proc/mtd", "rt");
785 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000786 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400787 return false;
788 }
789
790 while (fgets(line, sizeof(line), fp) != NULL)
791 {
792 char device[32], label[32];
793 unsigned long size = 0;
794 char* fstype = NULL;
795 int deviceId;
796
797 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
798
799 // Skip header and blank lines
800 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
801 continue;
802
803 // Strip off the trailing " from the label
804 label[strlen(label)-1] = '\0';
805
806 if (strcmp(label, MTD_Name.c_str()) == 0) {
807 // We found our device
808 // Strip off the trailing : from the device
809 device[strlen(device)-1] = '\0';
810 if (sscanf(device,"mtd%d", &deviceId) == 1) {
811 sprintf(device, "/dev/block/mtdblock%d", deviceId);
812 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000813 fclose(fp);
814 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400815 }
816 }
817 }
818 fclose(fp);
819
820 return false;
821}
822
Dees_Troy51127312012-09-08 13:08:49 -0400823bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
824 struct statfs st;
825 string Local_Path = Mount_Point + "/.";
826
827 if (!Mount(Display_Error))
828 return false;
829
830 if (statfs(Local_Path.c_str(), &st) != 0) {
831 if (!Removable) {
832 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000833 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400834 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000835 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400836 }
837 return false;
838 }
839 Size = (st.f_blocks * st.f_bsize);
840 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
841 Free = (st.f_bfree * st.f_bsize);
842 Backup_Size = Used;
843 return true;
844}
845
846bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400847 FILE* fp;
848 char command[255], line[512];
849 int include_block = 1;
850 unsigned int min_len;
851
852 if (!Mount(Display_Error))
853 return false;
854
Dees_Troy38bd7602012-09-14 13:33:53 -0400855 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400856 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200857 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400858 fp = fopen("/tmp/dfoutput.txt", "rt");
859 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000860 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400861 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400862 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400863
864 while (fgets(line, sizeof(line), fp) != NULL)
865 {
866 unsigned long blocks, used, available;
867 char device[64];
868 char tmpString[64];
869
870 if (strncmp(line, "Filesystem", 10) == 0)
871 continue;
872 if (strlen(line) < min_len) {
873 include_block = 0;
874 continue;
875 }
876 if (include_block) {
877 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
878 } else {
879 // The device block string is so long that the df information is on the next line
880 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400881 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400882 while (tmpString[space_count] == 32)
883 space_count++;
884 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
885 }
886
887 // Adjust block size to byte size
888 Size = blocks * 1024ULL;
889 Used = used * 1024ULL;
890 Free = available * 1024ULL;
891 Backup_Size = Used;
892 }
893 fclose(fp);
894 return true;
895}
896
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600897unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600898 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -0600899
900 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600901}
902
Dees_Troy5bf43922012-09-07 16:07:55 -0400903bool TWPartition::Find_Partition_Size(void) {
904 FILE* fp;
905 char line[512];
906 string tmpdevice;
907
igoriok87e3d932013-01-31 21:03:53 +0200908 fp = fopen("/proc/dumchar_info", "rt");
909 if (fp != NULL) {
910 while (fgets(line, sizeof(line), fp) != NULL)
911 {
912 char label[32], device[32];
913 unsigned long size = 0;
914
thatd43bf2d2014-09-21 23:13:02 +0200915 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200916
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500917 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200918 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
919 continue;
920
921 tmpdevice = "/dev/";
922 tmpdevice += label;
923 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
924 Size = size;
925 fclose(fp);
926 return true;
927 }
928 }
929 }
930
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600931 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
932 if (ioctl_size) {
933 Size = ioctl_size;
934 return true;
935 }
936
Dees_Troy5bf43922012-09-07 16:07:55 -0400937 // In this case, we'll first get the partitions we care about (with labels)
938 fp = fopen("/proc/partitions", "rt");
939 if (fp == NULL)
940 return false;
941
942 while (fgets(line, sizeof(line), fp) != NULL)
943 {
944 unsigned long major, minor, blocks;
945 char device[512];
946 char tmpString[64];
947
Dees_Troy63c8df72012-09-10 14:02:05 -0400948 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400949 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
950
951 tmpdevice = "/dev/block/";
952 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400953 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400954 // Adjust block size to byte size
955 Size = blocks * 1024ULL;
956 fclose(fp);
957 return true;
958 }
959 }
960 fclose(fp);
961 return false;
962}
963
Dees_Troy5bf43922012-09-07 16:07:55 -0400964bool TWPartition::Is_Mounted(void) {
965 if (!Can_Be_Mounted)
966 return false;
967
968 struct stat st1, st2;
969 string test_path;
970
971 // Check to see if the mount point directory exists
972 test_path = Mount_Point + "/.";
973 if (stat(test_path.c_str(), &st1) != 0) return false;
974
975 // Check to see if the directory above the mount point exists
976 test_path = Mount_Point + "/../.";
977 if (stat(test_path.c_str(), &st2) != 0) return false;
978
979 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
980 int ret = (st1.st_dev != st2.st_dev) ? true : false;
981
982 return ret;
983}
984
985bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000986 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500987 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000988
Dees_Troy5bf43922012-09-07 16:07:55 -0400989 if (Is_Mounted()) {
990 return true;
991 } else if (!Can_Be_Mounted) {
992 return false;
993 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400994
995 Find_Actual_Block_Device();
996
997 // Check the current file system before mounting
998 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000999 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001000 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 +00001001 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001002 string result;
1003 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001004 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001005 Current_File_System = "vfat";
1006 } else {
1007#ifdef TW_NO_EXFAT_FUSE
1008 UnMount(false);
1009 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1010 // Some kernels let us mount vfat as exfat which doesn't work out too well
1011#else
1012 exfat_mounted = 1;
1013#endif
1014 }
1015 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001016
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001017 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001018 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001019 string Ntfsmount_Binary = "";
1020
1021 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1022 Ntfsmount_Binary = "ntfs-3g";
1023 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1024 Ntfsmount_Binary = "mount.ntfs";
1025
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001026 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001027 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001028 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001029 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001030 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001031
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001032 if (TWFunc::Exec_Cmd(cmd) == 0) {
1033 return true;
1034 } else {
1035 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1036 }
1037 }
1038
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001039 if (Mount_Read_Only)
1040 flags |= MS_RDONLY;
1041
Dees_Troy22042032012-12-18 21:23:08 +00001042 if (Fstab_File_System == "yaffs2") {
1043 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001044 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1045 if (Mount_Read_Only)
1046 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001047 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1048 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1049 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001050 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001051 else
1052 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1053 return false;
1054 } else {
1055 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1056 return true;
1057 }
1058 } else {
1059 struct stat st;
1060 string test_path = Mount_Point;
1061 if (stat(test_path.c_str(), &st) < 0) {
1062 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001063 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001064 else
1065 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1066 return false;
1067 }
1068 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1069 if (new_mode != st.st_mode) {
1070 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1071 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1072 if (Display_Error)
1073 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1074 else
1075 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1076 return false;
1077 }
1078 }
Dees_Troy22042032012-12-18 21:23:08 +00001079 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001080 }
thatc7572eb2015-03-31 18:55:30 +02001081 }
1082
1083 string mount_fs = Current_File_System;
1084 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1085 mount_fs = "texfat";
1086
1087 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001088 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1089 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001090#ifdef TW_NO_EXFAT_FUSE
1091 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001092 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001093 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001094 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001095 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001096 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001097 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001098 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 +00001099 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001100 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001101 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001102#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001103 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001104 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001105 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001106 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1107 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 +00001108 return false;
1109#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001110 }
1111#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001112 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001113
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001114 if (Removable)
1115 Update_Size(Display_Error);
1116
xiaolu9416f4f2015-06-04 08:22:23 +08001117 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001118 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001119 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001120 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001121 return true;
1122}
1123
1124bool TWPartition::UnMount(bool Display_Error) {
1125 if (Is_Mounted()) {
1126 int never_unmount_system;
1127
1128 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1129 if (never_unmount_system == 1 && Mount_Point == "/system")
1130 return true; // Never unmount system if you're not supposed to unmount it
1131
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001132 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001133 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001134
Dees_Troy38bd7602012-09-14 13:33:53 -04001135 if (!Symlink_Mount_Point.empty())
1136 umount(Symlink_Mount_Point.c_str());
1137
Dees_Troyb05ddee2013-01-28 20:24:50 +00001138 umount(Mount_Point.c_str());
1139 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001140 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001141 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001142 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001143 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001144 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001145 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001146 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001147 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001148 } else {
1149 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001150 }
1151}
1152
Gary Peck43acadf2012-11-21 21:19:01 -08001153bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001154 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001155 int check;
1156 string Layout_Filename = Mount_Point + "/.layout_version";
1157
Dees_Troy38bd7602012-09-14 13:33:53 -04001158 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001159 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001160 return false;
1161 }
1162
Dees_Troyc51f1f92012-09-20 15:32:13 -04001163 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001164 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001165
Dees_Troy16c2b312013-01-15 16:51:18 +00001166 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1167 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1168 else
1169 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001170
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001171 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001172 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001173 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001174 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001175 DataManager::GetValue(TW_RM_RF_VAR, check);
1176
Hashcodedabfd492013-08-29 22:45:30 -07001177 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001178 wiped = Wipe_RMRF();
1179 else if (New_File_System == "ext4")
1180 wiped = Wipe_EXT4();
1181 else if (New_File_System == "ext2" || New_File_System == "ext3")
1182 wiped = Wipe_EXT23(New_File_System);
1183 else if (New_File_System == "vfat")
1184 wiped = Wipe_FAT();
1185 else if (New_File_System == "exfat")
1186 wiped = Wipe_EXFAT();
1187 else if (New_File_System == "yaffs2")
1188 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001189 else if (New_File_System == "f2fs")
1190 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001191 else if (New_File_System == "ntfs")
1192 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001193 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001194 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 +00001195 unlink("/.layout_version");
1196 return false;
1197 }
1198 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001199 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001200
Gary Pecke8bc5d72012-12-21 06:45:25 -08001201 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001202 if (Mount_Point == "/cache")
1203 DataManager::Output_Version();
1204
Dees_Troy16c2b312013-01-15 16:51:18 +00001205 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1206 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1207
1208 if (update_crypt) {
1209 Setup_File_System(false);
1210 if (Is_Encrypted && !Is_Decrypted) {
1211 // just wiped an encrypted partition back to its unencrypted state
1212 Is_Encrypted = false;
1213 Is_Decrypted = false;
1214 Decrypted_Block_Device = "";
1215 if (Mount_Point == "/data") {
1216 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1217 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1218 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001219 }
1220 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001221
Ethan Yonker66a19492015-12-10 10:19:45 -06001222 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001223 Recreate_Media_Folder();
1224 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001225 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001226 if (Is_Storage) {
1227 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001228 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001229 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001230}
1231
Gary Peck43acadf2012-11-21 21:19:01 -08001232bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001233 if (Is_File_System(Current_File_System))
1234 return Wipe(Current_File_System);
1235 else
1236 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001237}
1238
Dees_Troye58d5262012-09-21 12:27:57 -04001239bool TWPartition::Wipe_AndSec(void) {
1240 if (!Has_Android_Secure)
1241 return false;
1242
Dees_Troye58d5262012-09-21 12:27:57 -04001243 if (!Mount(true))
1244 return false;
1245
Ethan Yonker74db1572015-10-28 12:44:49 -05001246 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001247 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001248 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001249}
1250
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001251bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001252 if (Mount_Read_Only)
1253 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001254 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001255 return true;
1256 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1257 return true;
1258 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1259 return true;
1260 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1261 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001262 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001263 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001264 return false;
1265}
1266
1267bool TWPartition::Repair() {
1268 string command;
1269
1270 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001271 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001272 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001273 return false;
1274 }
1275 if (!UnMount(true))
1276 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001277 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001278 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001279 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001280 LOGINFO("Repair command: %s\n", command.c_str());
1281 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001282 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001283 return true;
1284 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001285 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001286 return false;
1287 }
1288 }
1289 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1290 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001291 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001292 return false;
1293 }
1294 if (!UnMount(true))
1295 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001296 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001297 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001298 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001299 LOGINFO("Repair command: %s\n", command.c_str());
1300 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001301 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001302 return true;
1303 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001304 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001305 return false;
1306 }
1307 }
1308 if (Current_File_System == "exfat") {
1309 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001310 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001311 return false;
1312 }
1313 if (!UnMount(true))
1314 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001315 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001316 Find_Actual_Block_Device();
1317 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1318 LOGINFO("Repair command: %s\n", command.c_str());
1319 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001320 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001321 return true;
1322 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001323 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001324 return false;
1325 }
1326 }
1327 if (Current_File_System == "f2fs") {
1328 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001329 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001330 return false;
1331 }
1332 if (!UnMount(true))
1333 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001334 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001335 Find_Actual_Block_Device();
1336 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1337 LOGINFO("Repair command: %s\n", command.c_str());
1338 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001339 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001340 return true;
1341 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001342 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001343 return false;
1344 }
1345 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001346 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001347 string Ntfsfix_Binary;
1348 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1349 Ntfsfix_Binary = "ntfsfix";
1350 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1351 Ntfsfix_Binary = "fsck.ntfs";
1352 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001353 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001354 return false;
1355 }
1356 if (!UnMount(true))
1357 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001358 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001359 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001360 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001361 LOGINFO("Repair command: %s\n", command.c_str());
1362 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001363 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001364 return true;
1365 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001366 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001367 return false;
1368 }
1369 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001370 return false;
1371}
1372
Ethan Yonkera2719152015-05-28 09:44:41 -05001373bool TWPartition::Can_Resize() {
1374 if (Mount_Read_Only)
1375 return false;
1376 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1377 return true;
1378 return false;
1379}
1380
1381bool TWPartition::Resize() {
1382 string command;
1383
1384 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1385 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001386 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1387 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001388 return false;
1389 }
1390 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001391 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1392 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001393 return false;
1394 }
1395 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001396 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001397 if (!Repair())
1398 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001399 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001400 Find_Actual_Block_Device();
1401 command = "/sbin/resize2fs " + Actual_Block_Device;
1402 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001403 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1404 if (Actual_Size == 0)
1405 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001406
Ethan Yonkera2719152015-05-28 09:44:41 -05001407 unsigned long long Block_Count;
1408 if (Length < 0) {
1409 // Reduce overall size by this length
1410 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1411 } else {
1412 // This is the size, not a size reduction
1413 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1414 }
1415 char temp[256];
1416 sprintf(temp, "%llu", Block_Count);
1417 command += " ";
1418 command += temp;
1419 command += "K";
1420 }
1421 LOGINFO("Resize command: %s\n", command.c_str());
1422 if (TWFunc::Exec_Cmd(command) == 0) {
1423 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001424 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001425 return true;
1426 } else {
1427 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001428 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001429 return false;
1430 }
1431 }
1432 return false;
1433}
1434
bigbiff7abc5fe2015-01-17 16:53:12 -05001435bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
1436 if (Backup_Method == FILES) {
1437 return Backup_Tar(backup_folder, overall_size, other_backups_size, tar_fork_pid);
1438 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001439 else if (Backup_Method == DD)
1440 return Backup_DD(backup_folder);
1441 else if (Backup_Method == FLASH_UTILS)
1442 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001443 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001444 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001445}
1446
Dees_Troy43d8b002012-09-17 16:00:01 -04001447bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001448 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001449 char split_filename[512];
1450 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001451 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001452
Captain Throwbackff5935f2014-09-23 16:08:34 -04001453 sync();
1454
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001455 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001456 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001457 if (!TWFunc::Path_Exists(Full_Filename)) {
1458 // This is a split archive, we presume
1459 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001460 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001461 md5file = split_filename;
1462 md5file += ".md5";
1463 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001464 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 -04001465 return false;
1466 }
1467 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001468 while (index < 1000) {
1469 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001470 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001471 return false;
1472 }
1473 index++;
1474 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001475 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001476 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001477 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001478 } else {
1479 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001480 md5file = Full_Filename + ".md5";
1481 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001482 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 -04001483 return false;
1484 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001485 md5sum.setfn(Full_Filename);
1486 if (md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001487 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001488 return false;
1489 } else
1490 return true;
1491 }
1492 return false;
1493}
1494
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001495bool 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 -08001496 string Restore_File_System;
1497
Matt Mower3c366972015-12-25 19:28:31 -06001498 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Dees_Troy2673cec2013-04-02 20:22:16 +00001499 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001500
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001501 Restore_File_System = Get_Restore_File_System(restore_folder);
1502
1503 if (Is_File_System(Restore_File_System))
1504 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1505 else if (Is_Image(Restore_File_System)) {
Ethan Yonkerf66324f2015-01-09 16:46:57 -06001506 return Restore_Image(restore_folder, total_restore_size, already_restored_size, Restore_File_System);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001507 }
1508
1509 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1510 return false;
1511}
1512
1513string TWPartition::Get_Restore_File_System(string restore_folder) {
1514 size_t first_period, second_period;
1515 string Restore_File_System;
1516
Gary Peck43acadf2012-11-21 21:19:01 -08001517 // Parse backup filename to extract the file system before wiping
1518 first_period = Backup_FileName.find(".");
1519 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001520 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001521 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001522 }
1523 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1524 second_period = Restore_File_System.find(".");
1525 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001526 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001527 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001528 }
1529 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001530 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001531 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001532}
1533
1534string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001535 if (Backup_Method == NONE)
1536 return "none";
1537 else if (Backup_Method == FILES)
1538 return "files";
1539 else if (Backup_Method == DD)
1540 return "dd";
1541 else if (Backup_Method == FLASH_UTILS)
1542 return "flash_utils";
1543 else
1544 return "undefined";
1545 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001546}
1547
1548bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001549 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001550 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001551 return 1;
1552}
1553
1554bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001555 bool Save_Data_Media = Has_Data_Media;
1556
1557 if (!UnMount(true))
1558 return false;
1559
Dees_Troy38bd7602012-09-14 13:33:53 -04001560 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001561 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001562#ifdef TW_INCLUDE_CRYPTO
1563 if (Is_Decrypted) {
1564 if (!UnMount(true))
1565 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001566 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001567 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1568 }
1569 }
1570#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001571 Is_Decrypted = false;
1572 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001573 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001574 if (Crypto_Key_Location == "footer") {
1575 int newlen, fd;
1576 if (Length != 0) {
1577 newlen = Length;
1578 if (newlen < 0)
1579 newlen = newlen * -1;
1580 } else {
1581 newlen = CRYPT_FOOTER_OFFSET;
1582 }
1583 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1584 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1585 } else {
1586 unsigned int block_count;
1587 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1588 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1589 } else {
1590 off64_t offset = ((off64_t)block_count * 512) - newlen;
1591 if (lseek64(fd, offset, SEEK_SET) == -1) {
1592 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1593 } else {
1594 void* buffer = malloc(newlen);
1595 if (!buffer) {
1596 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1597 } else {
1598 memset(buffer, 0, newlen);
1599 int ret = write(fd, buffer, newlen);
1600 if (ret != newlen) {
1601 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1602 } else {
1603 LOGINFO("Successfully wiped crypto footer.\n");
1604 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001605 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001606 }
1607 }
1608 }
1609 close(fd);
1610 }
1611 } else {
1612 string Command = "flash_image " + Crypto_Key_Location + " /dev/zero";
1613 TWFunc::Exec_Cmd(Command);
1614 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001615 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001616 Has_Data_Media = Save_Data_Media;
1617 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1618 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001619 if (Mount(false))
1620 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001621 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001622 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001623#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001624 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 -05001625#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001626 return true;
1627 } else {
1628 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001629 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001630 if (Has_Data_Media && Mount(false))
1631 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001632 return false;
1633 }
1634 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001635}
1636
1637void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001638 const char* type;
1639 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001640
Dees_Troy68cab492012-12-12 19:29:35 +00001641 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1642 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001643
Dees_Troy38bd7602012-09-14 13:33:53 -04001644 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001645 if (!Is_Present)
1646 return;
Dees_Troy51127312012-09-08 13:08:49 -04001647
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001648 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1649 if (blkid_do_fullprobe(pr)) {
1650 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001651 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001652 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001653 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001654
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001655 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001656 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001657 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001658 return;
1659 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001660
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001661 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001662 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001663}
1664
Gary Peck43acadf2012-11-21 21:19:01 -08001665bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001666 if (!UnMount(true))
1667 return false;
1668
Dees_Troy43d8b002012-09-17 16:00:01 -04001669 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001670 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001671
Greg Wallace4b44fef2015-12-29 15:33:24 -05001672 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001673 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001674 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001675 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001676 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001677 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001678 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001679 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001680 return true;
1681 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001682 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001683 return false;
1684 }
1685 } else
1686 return Wipe_RMRF();
1687
1688 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001689}
1690
1691bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001692 Find_Actual_Block_Device();
1693 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001694 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1695 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001696 return false;
1697 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001698 if (!UnMount(true))
1699 return false;
1700
Dees_Troyb3265ab2013-08-30 02:59:14 +00001701#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001702 int ret;
1703 char *secontext = NULL;
1704
Greg Wallace4b44fef2015-12-29 15:33:24 -05001705 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001706
Dees Troy99c8dbf2014-03-10 16:53:58 +00001707 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001708 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1709 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1710 } else {
1711 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1712 }
1713 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001714 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001715 return false;
1716 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001717 string sedir = Mount_Point + "/lost+found";
1718 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1719 rmdir(sedir.c_str());
1720 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001721 return true;
1722 }
1723#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001724 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001725 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001726
Greg Wallace4b44fef2015-12-29 15:33:24 -05001727 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001728 Find_Actual_Block_Device();
1729 Command = "make_ext4fs";
1730 if (!Is_Decrypted && Length != 0) {
1731 // Only use length if we're not decrypted
1732 char len[32];
1733 sprintf(len, "%i", Length);
1734 Command += " -l ";
1735 Command += len;
1736 }
Dees_Troy5295d582013-09-06 15:51:08 +00001737 if (TWFunc::Path_Exists("/file_contexts")) {
1738 Command += " -S /file_contexts";
1739 }
1740 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001741 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001742 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001743 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001744 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001745 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001746 return true;
1747 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001748 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001749 return false;
1750 }
1751 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001752 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001753#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001754 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001755}
1756
1757bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001758 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001759
Matt Mower18794c82015-11-11 16:22:45 -06001760 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001761 if (!UnMount(true))
1762 return false;
1763
Greg Wallace4b44fef2015-12-29 15:33:24 -05001764 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001765 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001766 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001767 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001768 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001769 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001770 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001771 return true;
1772 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001773 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001774 return false;
1775 }
1776 return true;
1777 }
1778 else
1779 return Wipe_RMRF();
1780
1781 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001782}
1783
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001784bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001785 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001786
1787 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1788 if (!UnMount(true))
1789 return false;
1790
Greg Wallace4b44fef2015-12-29 15:33:24 -05001791 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001792 Find_Actual_Block_Device();
1793 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001794 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001795 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001796 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001797 return true;
1798 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001799 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001800 return false;
1801 }
1802 return true;
1803 }
1804 return false;
1805}
1806
Dees_Troy38bd7602012-09-14 13:33:53 -04001807bool TWPartition::Wipe_MTD() {
1808 if (!UnMount(true))
1809 return false;
1810
Greg Wallace4b44fef2015-12-29 15:33:24 -05001811 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001812
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001813 mtd_scan_partitions();
1814 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1815 if (mtd == NULL) {
1816 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1817 return false;
1818 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001819
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001820 MtdWriteContext* ctx = mtd_write_partition(mtd);
1821 if (ctx == NULL) {
1822 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1823 return false;
1824 }
1825 if (mtd_erase_blocks(ctx, -1) == -1) {
1826 mtd_write_close(ctx);
1827 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1828 return false;
1829 }
1830 if (mtd_write_close(ctx) != 0) {
1831 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1832 return false;
1833 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001834 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001835 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001836 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001837 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001838}
1839
1840bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001841 if (!Mount(true))
1842 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06001843 // This is the only wipe that leaves the partition mounted, so we
1844 // must manually remove the partition from MTP if it is a storage
1845 // partition.
1846 if (Is_Storage)
1847 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001848
Ethan Yonker74db1572015-10-28 12:44:49 -05001849 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001850 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001851 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001852 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001853}
1854
Dees_Troye5017042013-08-29 16:38:55 +00001855bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001856 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001857
1858 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1859 if (!UnMount(true))
1860 return false;
1861
Greg Wallace4b44fef2015-12-29 15:33:24 -05001862 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00001863 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05001864 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05001865 if (!Is_Decrypted && Length != 0) {
1866 // Only use length if we're not decrypted
1867 char len[32];
1868 int mod_length = Length;
1869 if (Length < 0)
1870 mod_length *= -1;
1871 sprintf(len, "%i", mod_length);
1872 command += " -r ";
1873 command += len;
1874 }
1875 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001876 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001877 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001878 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00001879 return true;
1880 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001881 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00001882 return false;
1883 }
1884 return true;
1885 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001886 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00001887 return Wipe_RMRF();
1888 }
1889 return false;
1890}
1891
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001892bool TWPartition::Wipe_NTFS() {
1893 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001894 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001895
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001896 if (TWFunc::Path_Exists("/sbin/mkntfs"))
1897 Ntfsmake_Binary = "mkntfs";
1898 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
1899 Ntfsmake_Binary = "mkfs.ntfs";
1900 else
1901 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001902
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001903 if (!UnMount(true))
1904 return false;
1905
Greg Wallace4b44fef2015-12-29 15:33:24 -05001906 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001907 Find_Actual_Block_Device();
1908 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
1909 if (TWFunc::Exec_Cmd(command) == 0) {
1910 Recreate_AndSec_Folder();
1911 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001912 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001913 } else {
1914 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
1915 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001916 }
1917 return false;
1918}
1919
Dees_Troy51a0e822012-09-05 15:24:24 -04001920bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001921#ifdef TW_OEM_BUILD
1922 // In an OEM Build we want to do a full format
1923 return Wipe_Encryption();
1924#else
Ethan Yonker66a19492015-12-10 10:19:45 -06001925 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001926
Dees_Troy38bd7602012-09-14 13:33:53 -04001927 if (!Mount(true))
1928 return false;
1929
Ethan Yonker74db1572015-10-28 12:44:49 -05001930 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06001931 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
1932 if (ret)
1933 gui_msg("done=Done.");
1934 return ret;
1935#endif // ifdef TW_OEM_BUILD
1936}
1937
1938bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
1939 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001940
1941 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06001942 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00001943 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001944 struct dirent* de;
1945 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001946 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001947
Ethan Yonker66a19492015-12-10 10:19:45 -06001948 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001949 dir.append(de->d_name);
Ethan Yonker66a19492015-12-10 10:19:45 -06001950 if (du.check_skip_dirs(dir)) {
1951 LOGINFO("skipped '%s'\n", dir.c_str());
1952 continue;
1953 }
Dees_Troyce675462013-01-09 19:48:21 +00001954 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06001955 dir.append("/");
1956 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
1957 closedir(d);
1958 return false;
1959 }
1960 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001961 } 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 +00001962 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001963 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001964 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001965 }
1966 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001967
Dees_Troy16b74352012-11-14 22:27:31 +00001968 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001969 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001970 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00001971 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001972}
1973
bigbiff7abc5fe2015-01-17 16:53:12 -05001974bool 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 -04001975 char back_name[255], split_index[5];
1976 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001977 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001978 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001979 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001980 twrpTar tar;
1981 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001982
Dees_Troy43d8b002012-09-17 16:00:01 -04001983 if (!Mount(true))
1984 return false;
1985
Dees_Troya13d74f2013-03-24 08:54:55 -05001986 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05001987 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04001988
1989 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001990 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001991
Dees_Troy83bd4832013-05-04 12:39:56 +00001992#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1993 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1994 if (use_encryption && Can_Encrypt_Backup) {
1995 tar.use_encryption = use_encryption;
1996 if (Use_Userdata_Encryption)
1997 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001998 string Password;
1999 DataManager::GetValue("tw_backup_password", Password);
2000 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00002001 } else {
2002 use_encryption = false;
2003 }
2004#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002005
2006 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
2007 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002008 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00002009 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00002010 Full_FileName = backup_folder + "/" + Backup_FileName;
2011 tar.setdir(Backup_Path);
2012 tar.setfn(Full_FileName);
2013 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002014 tar.partition_name = Backup_Name;
2015 tar.backup_folder = backup_folder;
bigbiff7abc5fe2015-01-17 16:53:12 -05002016 if (tar.createTarFork(overall_size, other_backups_size, tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002017 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002018 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002019}
2020
2021bool TWPartition::Backup_DD(string backup_folder) {
codelover352b75e2015-03-29 02:44:07 +08002022 char back_name[255], block_size[32], dd_count[32];
2023 string Full_FileName, Command, DD_BS, DD_COUNT;
Dees_Troy43d8b002012-09-17 16:00:01 -04002024 int use_compression;
codelover352b75e2015-03-29 02:44:07 +08002025 unsigned long long DD_Block_Size, DD_Count;
Dees_Troy43d8b002012-09-17 16:00:01 -04002026
codelover352b75e2015-03-29 02:44:07 +08002027 DD_Block_Size = 16 * 1024 * 1024;
2028 while (Backup_Size % DD_Block_Size != 0) DD_Block_Size >>= 1;
2029
2030 DD_Count = Backup_Size / DD_Block_Size;
2031
2032 sprintf(dd_count, "%llu", DD_Count);
2033 DD_COUNT = dd_count;
2034
2035 sprintf(block_size, "%llu", DD_Block_Size);
2036 DD_BS = block_size;
igoriok87e3d932013-01-31 21:03:53 +02002037
Ethan Yonker74db1572015-10-28 12:44:49 -05002038 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2039 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002040
2041 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
2042 Backup_FileName = back_name;
2043
2044 Full_FileName = backup_folder + "/" + Backup_FileName;
2045
codelover352b75e2015-03-29 02:44:07 +08002046 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + " count=" + DD_COUNT;
Dees_Troy2673cec2013-04-02 20:22:16 +00002047 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002048 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002049 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04002050 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002051 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002052 return false;
2053 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002054 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002055}
2056
2057bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002058 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02002059 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002060 int use_compression;
2061
Ethan Yonker74db1572015-10-28 12:44:49 -05002062 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2063 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002064
2065 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
2066 Backup_FileName = back_name;
2067
2068 Full_FileName = backup_folder + "/" + Backup_FileName;
2069
2070 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00002071 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002072 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002073 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002074 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2075 // 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 -06002076 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002077 return false;
2078 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002079 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002080}
2081
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002082unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
2083 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
2084 if (restore_info.LoadValues() == 0) {
2085 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2086 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2087 return Restore_Size;
2088 }
2089 }
2090 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
2091
2092 Full_FileName = restore_folder + "/" + Backup_FileName;
2093
2094 if (Is_Image(Restore_File_System)) {
2095 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2096 return Restore_Size;
2097 }
2098
2099 twrpTar tar;
2100 tar.setdir(Backup_Path);
2101 tar.setfn(Full_FileName);
2102 tar.backup_name = Backup_Name;
2103#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2104 string Password;
2105 DataManager::GetValue("tw_restore_password", Password);
2106 if (!Password.empty())
2107 tar.setpassword(Password);
2108#endif
2109 tar.partition_name = Backup_Name;
2110 tar.backup_folder = restore_folder;
2111 Restore_Size = tar.get_size();
2112 return Restore_Size;
2113}
2114
2115bool 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 -08002116 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04002117 int index = 0;
2118 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00002119 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002120
Dees_Troye58d5262012-09-21 12:27:57 -04002121 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002122 if (!Wipe_AndSec())
2123 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002124 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002125 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002126 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002127 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 -05002128 if (!Wipe_Data_Without_Wiping_Media())
2129 return false;
2130 } else {
2131 if (!Wipe(Restore_File_System))
2132 return false;
2133 }
Dees_Troye58d5262012-09-21 12:27:57 -04002134 }
Matt Mower3c366972015-12-25 19:28:31 -06002135 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002136 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002137
2138 if (!Mount(true))
2139 return false;
2140
Dees_Troy4a2a1262012-09-18 09:33:47 -04002141 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002142 twrpTar tar;
2143 tar.setdir(Backup_Path);
2144 tar.setfn(Full_FileName);
2145 tar.backup_name = Backup_Name;
2146#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2147 string Password;
2148 DataManager::GetValue("tw_restore_password", Password);
2149 if (!Password.empty())
2150 tar.setpassword(Password);
2151#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002152 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002153 ret = false;
2154 else
2155 ret = true;
2156#ifdef HAVE_CAPABILITIES
2157 // Restore capabilities to the run-as binary
2158 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2159 struct vfs_cap_data cap_data;
2160 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2161
2162 memset(&cap_data, 0, sizeof(cap_data));
2163 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2164 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2165 cap_data.data[0].inheritable = 0;
2166 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2167 cap_data.data[1].inheritable = 0;
2168 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2169 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2170 } else {
2171 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2172 }
2173 }
2174#endif
2175 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002176}
2177
Ethan Yonker96af84a2015-01-05 14:58:36 -06002178bool TWPartition::Restore_Image(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size, string Restore_File_System) {
2179 string Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002180 double display_percent, progress_percent;
2181 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04002182
Matt Mower3c366972015-12-25 19:28:31 -06002183 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002184 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002185 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002186
Ethan Yonker96af84a2015-01-05 14:58:36 -06002187 if (Restore_File_System == "emmc") {
2188 if (!Flash_Image_DD(Full_FileName))
2189 return false;
2190 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
2191 if (!Flash_Image_FI(Full_FileName))
2192 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002193 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002194 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
2195 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
2196 DataManager::SetValue("tw_size_progress", size_progress);
2197 progress_percent = (display_percent / 100);
2198 DataManager::SetProgress((float)(progress_percent));
2199 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04002200 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002201}
Dees_Troy5bf43922012-09-07 16:07:55 -04002202
2203bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002204 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002205
Dees_Troyab10ee22012-09-21 14:27:30 -04002206 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002207 return false;
2208
Dees_Troy0550cfb2012-10-13 11:56:13 -04002209 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002210 if (Removable || Is_Encrypted) {
2211 if (!Mount(false))
2212 return true;
2213 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002214 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002215
2216 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002217 if (!ret || Size == 0) {
2218 if (!Get_Size_Via_df(Display_Error)) {
2219 if (!Was_Already_Mounted)
2220 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002221 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002222 }
2223 }
Dees_Troy51127312012-09-08 13:08:49 -04002224
Dees_Troy5bf43922012-09-07 16:07:55 -04002225 if (Has_Data_Media) {
2226 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002227 unsigned long long data_media_used, actual_data;
Ethan Yonker66a19492015-12-10 10:19:45 -06002228 Used = du.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002229 Backup_Size = Used;
2230 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002231 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002232 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002233 } else {
2234 if (!Was_Already_Mounted)
2235 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002236 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002237 }
Dees_Troye58d5262012-09-21 12:27:57 -04002238 } else if (Has_Android_Secure) {
2239 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002240 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002241 else {
2242 if (!Was_Already_Mounted)
2243 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002244 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002245 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002246 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002247 if (!Was_Already_Mounted)
2248 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002249 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002250}
Dees_Troy38bd7602012-09-14 13:33:53 -04002251
2252void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002253 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002254 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002255 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002256 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002257 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002258 Is_Present = true;
2259 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002260 return;
2261 }
2262 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002263 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002264 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002265 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002266 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002267 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002268 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002269}
2270
2271void TWPartition::Recreate_Media_Folder(void) {
2272 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002273 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002274
2275 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002276 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2277 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002278 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002279 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2280 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002281 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2282 if (!Internal_path.empty()) {
2283 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2284 mkdir(Internal_path.c_str(), 0770);
2285 }
2286#ifdef TW_INTERNAL_STORAGE_PATH
2287 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2288#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002289#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002290 // Afterwards, we will try to set the
2291 // default metadata that we were hopefully able to get during
2292 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002293 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002294 if (!Internal_path.empty())
2295 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002296#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002297 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002298 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002299 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002300 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002301}
Dees_Troye58d5262012-09-21 12:27:57 -04002302
2303void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002304 if (!Has_Android_Secure)
2305 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002306 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002307 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002308 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002309 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002310 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002311 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002312 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002313 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002314 }
2315}
bigbiff7cb4c332014-11-26 20:36:07 -05002316
2317uint64_t TWPartition::Get_Max_FileSize() {
2318 uint64_t maxFileSize = 0;
2319 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2320 const uint64_t constTB = (uint64_t) constGB * 1024;
2321 const uint64_t constPB = (uint64_t) constTB * 1024;
2322 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002323 if (Current_File_System == "ext4")
2324 maxFileSize = 16 * constTB; //16 TB
2325 else if (Current_File_System == "vfat")
2326 maxFileSize = 4 * constGB; //4 GB
2327 else if (Current_File_System == "ntfs")
2328 maxFileSize = 256 * constTB; //256 TB
2329 else if (Current_File_System == "exfat")
2330 maxFileSize = 16 * constPB; //16 PB
2331 else if (Current_File_System == "ext3")
2332 maxFileSize = 2 * constTB; //2 TB
2333 else if (Current_File_System == "f2fs")
2334 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002335 else
2336 maxFileSize = 100000000L;
2337 return maxFileSize - 1;
2338}
2339
Ethan Yonker96af84a2015-01-05 14:58:36 -06002340bool TWPartition::Flash_Image(string Filename) {
2341 string Restore_File_System;
2342
2343 LOGINFO("Image filename is: %s\n", Filename.c_str());
2344
2345 if (Backup_Method == FILES) {
2346 LOGERR("Cannot flash images to file systems\n");
2347 return false;
2348 } else if (!Can_Flash_Img) {
2349 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2350 return false;
2351 } else {
2352 if (!Find_Partition_Size()) {
2353 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2354 return false;
2355 }
2356 unsigned long long image_size = TWFunc::Get_File_Size(Filename);
2357 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002358 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonker96af84a2015-01-05 14:58:36 -06002359 image_size, Filename.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002360 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002361 return false;
2362 }
2363 if (Backup_Method == DD)
2364 return Flash_Image_DD(Filename);
2365 else if (Backup_Method == FLASH_UTILS)
2366 return Flash_Image_FI(Filename);
2367 }
2368
2369 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2370 return false;
2371}
2372
2373bool TWPartition::Flash_Image_DD(string Filename) {
2374 string Command;
2375
Ethan Yonker74db1572015-10-28 12:44:49 -05002376 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
codelover352b75e2015-03-29 02:44:07 +08002377 Command = "dd bs=8388608 if='" + Filename + "' of=" + Actual_Block_Device;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002378 LOGINFO("Flash command: '%s'\n", Command.c_str());
2379 TWFunc::Exec_Cmd(Command);
2380 return true;
2381}
2382
2383bool TWPartition::Flash_Image_FI(string Filename) {
2384 string Command;
2385
Ethan Yonker74db1572015-10-28 12:44:49 -05002386 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker96af84a2015-01-05 14:58:36 -06002387 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2388 Command = "erase_image " + MTD_Name;
2389 LOGINFO("Erase command: '%s'\n", Command.c_str());
2390 TWFunc::Exec_Cmd(Command);
2391 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2392 LOGINFO("Flash command: '%s'\n", Command.c_str());
2393 TWFunc::Exec_Cmd(Command);
2394 return true;
2395}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002396
2397void TWPartition::Change_Mount_Read_Only(bool new_value) {
2398 Mount_Read_Only = new_value;
2399}
2400
2401int TWPartition::Check_Lifetime_Writes() {
2402 bool original_read_only = Mount_Read_Only;
2403 int ret = 1;
2404
2405 Mount_Read_Only = true;
2406 if (Mount(false)) {
2407 Find_Actual_Block_Device();
2408 string block = basename(Actual_Block_Device.c_str());
2409 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2410 string result;
2411 if (TWFunc::Path_Exists(file)) {
2412 if (TWFunc::read_file(file, result) != 0) {
2413 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2414 } else {
2415 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2416 if (result == "0") {
2417 ret = 0;
2418 }
2419 }
2420 } else {
2421 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2422 }
2423 UnMount(true);
2424 } else {
2425 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2426 }
2427 Mount_Read_Only = original_read_only;
2428 return ret;
2429}
Ethan Yonker66a19492015-12-10 10:19:45 -06002430
2431int TWPartition::Decrypt_Adopted() {
2432#ifdef TW_INCLUDE_CRYPTO
2433 int ret = 1;
2434 Is_Adopted_Storage = false;
2435 string Adopted_Key_File = "";
2436
2437 if (!Removable)
2438 return ret;
2439
2440 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
2441 if (fd < 0) {
2442 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
2443 return ret;
2444 }
2445 char type_guid[80];
2446 char part_guid[80];
2447
2448 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
2449 LOGINFO("type: '%s'\n", type_guid);
2450 LOGINFO("part: '%s'\n", part_guid);
2451 Adopted_GUID = part_guid;
2452 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
2453 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
2454 LOGINFO("android_expand found\n");
2455 Adopted_Key_File = "/data/misc/vold/expand_";
2456 Adopted_Key_File += part_guid;
2457 Adopted_Key_File += ".key";
2458 if (TWFunc::Path_Exists(Adopted_Key_File)) {
2459 Is_Adopted_Storage = true;
2460 /* Until we find a use case for this, I think it is safe
2461 * to disable USB Mass Storage whenever adopted storage
2462 * is present.
2463 */
2464 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
2465 DataManager::SetValue("tw_has_usb_storage", 0);
2466 }
2467 }
2468 }
2469
2470 if (Is_Adopted_Storage) {
2471 string Adopted_Block_Device = Alternate_Block_Device + "p2";
2472 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2473 Adopted_Block_Device = Alternate_Block_Device + "2";
2474 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2475 LOGINFO("Adopted block device does not exist\n");
2476 goto exit;
2477 }
2478 }
2479 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
2480 char crypto_blkdev[MAXPATHLEN];
2481 std::string thekey;
2482 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
2483 if (fdkey < 0) {
2484 LOGINFO("failed to open key file\n");
2485 goto exit;
2486 }
2487 char buf[512];
2488 ssize_t n;
2489 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
2490 thekey.append(buf, n);
2491 }
2492 close(fdkey);
2493 unsigned char* key = (unsigned char*) thekey.data();
2494 cryptfs_revert_ext_volume(part_guid);
2495
2496 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
2497 if (ret == 0) {
2498 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
2499 Decrypted_Block_Device = crypto_blkdev;
2500 Is_Decrypted = true;
2501 Is_Encrypted = true;
2502 Find_Actual_Block_Device();
2503 if (!Mount(false)) {
2504 LOGERR("Failed to mount decrypted adopted storage device\n");
2505 Is_Decrypted = false;
2506 Is_Encrypted = false;
2507 cryptfs_revert_ext_volume(part_guid);
2508 ret = 1;
2509 } else {
2510 Setup_Data_Media();
2511 Recreate_Media_Folder();
2512 Wipe_Available_in_GUI = true;
2513 Wipe_During_Factory_Reset = true;
2514 Can_Be_Backed_Up = true;
2515 Can_Encrypt_Backup = true;
2516 Use_Userdata_Encryption = true;
2517 Is_Storage = true;
2518 Storage_Name = "Adopted Storage";
2519 Is_SubPartition = true;
2520 SubPartition_Of = "/data";
2521 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2522 DataManager::SetValue("tw_has_adopted_storage", 1);
2523 }
2524 } else {
2525 LOGERR("Failed to setup adopted storage decryption\n");
2526 }
2527 }
2528exit:
2529 return ret;
2530#else
2531 LOGINFO("Decrypt_Adopted: no crypto support\n");
2532 return 1;
2533#endif
2534}
2535
2536void TWPartition::Revert_Adopted() {
2537#ifdef TW_INCLUDE_CRYPTO
2538 if (!Adopted_GUID.empty()) {
2539 PartitionManager.Remove_MTP_Storage(Mount_Point);
2540 UnMount(false);
2541 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
2542 Is_Adopted_Storage = false;
2543 Is_Encrypted = false;
2544 Is_Decrypted = false;
2545 Decrypted_Block_Device = "";
2546 Find_Actual_Block_Device();
2547 Wipe_During_Factory_Reset = false;
2548 Can_Be_Backed_Up = false;
2549 Can_Encrypt_Backup = false;
2550 Use_Userdata_Encryption = false;
2551 Is_SubPartition = false;
2552 SubPartition_Of = "";
2553 Has_Data_Media = false;
2554 Storage_Path = Mount_Point;
2555 if (!Symlink_Mount_Point.empty()) {
2556 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
2557 if (Dat) {
2558 Dat->UnMount(false);
2559 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
2560 }
2561 Symlink_Mount_Point = "";
2562 }
2563 }
2564#else
2565 LOGINFO("Revert_Adopted: no crypto support\n");
2566#endif
2567}