blob: f6adec9dade1c535fdd5cb3aacd975970716e84d [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002 Copyright 2013 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050027#include <iostream>
28#include <sstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040029
Dees_Troy657c3092012-09-10 20:32:10 -040030#ifdef TW_INCLUDE_CRYPTO
31 #include "cutils/properties.h"
32#endif
33
bigbiff7b4c7a62015-01-01 19:44:14 -050034#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000036#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040037#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040038#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040039#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050040#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041#include "twrpTar.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050042#include "twrpDU.hpp"
bigbiff bigbiff6b600f92014-01-05 18:13:43 -050043#include "fixPermissions.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050044#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060045#include "set_metadata.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040046extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040047 #include "mtdutils/mtdutils.h"
48 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000049#ifdef USE_EXT4
50 #include "make_ext4fs.h"
51#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060052
53#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060054 #include "crypto/lollipop/cryptfs.h"
Ethan Yonkerbc85b632015-08-09 12:48:14 -050055#else
56 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060057#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040058}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040059#ifdef HAVE_SELINUX
60#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060061#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040062#endif
Dees Troy4159aed2014-02-28 17:24:43 +000063#ifdef HAVE_CAPABILITIES
64#include <sys/capability.h>
65#include <sys/xattr.h>
66#include <linux/xattr.h>
67#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040068
bigbiff bigbiff9c754052013-01-09 09:09:08 -050069using namespace std;
70
Dees_Troya95f55c2013-08-17 13:14:43 +000071extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050072extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000073
Hashcode62bd9e02013-11-19 21:59:42 -080074struct flag_list {
75 const char *name;
76 unsigned flag;
77};
78
79static struct flag_list mount_flags[] = {
80 { "noatime", MS_NOATIME },
81 { "noexec", MS_NOEXEC },
82 { "nosuid", MS_NOSUID },
83 { "nodev", MS_NODEV },
84 { "nodiratime", MS_NODIRATIME },
85 { "ro", MS_RDONLY },
86 { "rw", 0 },
87 { "remount", MS_REMOUNT },
88 { "bind", MS_BIND },
89 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000090#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080091 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000092#endif
93#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080094 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000095#endif
96#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -080097 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000098#endif
99#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800100 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800102 { "sync", MS_SYNCHRONOUS },
103 { "defaults", 0 },
104 { 0, 0 },
105};
106
that9e0593e2014-10-08 00:01:24 +0200107TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400108 Can_Be_Mounted = false;
109 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500110 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200111 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400112 Wipe_During_Factory_Reset = false;
113 Wipe_Available_in_GUI = false;
114 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400115 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400116 SubPartition_Of = "";
117 Symlink_Path = "";
118 Symlink_Mount_Point = "";
119 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400120 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400121 Actual_Block_Device = "";
122 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400123 Alternate_Block_Device = "";
124 Removable = false;
125 Is_Present = false;
126 Length = 0;
127 Size = 0;
128 Used = 0;
129 Free = 0;
130 Backup_Size = 0;
131 Can_Be_Encrypted = false;
132 Is_Encrypted = false;
133 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600134 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400135 Decrypted_Block_Device = "";
136 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500137 Backup_Display_Name = "";
138 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400139 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400140 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400141 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400142 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000143 Can_Encrypt_Backup = false;
144 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400145 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400146 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400147 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500148 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400149 Storage_Path = "";
150 Current_File_System = "";
151 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800152 Mount_Flags = 0;
153 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400154 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000155 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000156 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600157 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600158 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600159 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500160 Mount_Read_Only = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400161}
162
163TWPartition::~TWPartition(void) {
164 // Do nothing
165}
166
Dees_Troy5bf43922012-09-07 16:07:55 -0400167bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
168 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
169 int line_len = Line.size(), index = 0, item_index = 0;
170 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400171 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400172 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500173 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400174
Dees_Troy51127312012-09-08 13:08:49 -0400175 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500176 if (full_line[index] == 34)
177 skip = !skip;
178 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400179 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400180 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400181 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000182 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400183 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500184 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000185 Display_Name = full_line + 1;
186 Backup_Display_Name = Display_Name;
187 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400188 index = Mount_Point.size();
189 while (index < line_len) {
190 while (index < line_len && full_line[index] == '\0')
191 index++;
192 if (index >= line_len)
193 continue;
194 ptr = full_line + index;
195 if (item_index == 0) {
196 // File System
197 Fstab_File_System = ptr;
198 Current_File_System = ptr;
199 item_index++;
200 } else if (item_index == 1) {
201 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400202 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400203 MTD_Name = ptr;
204 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400205 } else if (Fstab_File_System == "bml") {
206 if (Mount_Point == "/boot")
207 MTD_Name = "boot";
208 else if (Mount_Point == "/recovery")
209 MTD_Name = "recovery";
210 Primary_Block_Device = ptr;
211 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000212 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 -0400213 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400214 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000215 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400216 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000217 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400218 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400219 } else {
220 Primary_Block_Device = ptr;
221 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400222 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400223 item_index++;
224 } else if (item_index > 1) {
225 if (*ptr == '/') {
226 // Alternate Block Device
227 Alternate_Block_Device = ptr;
228 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
229 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
230 // Partition length
231 ptr += 7;
232 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400233 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
234 // Custom flags, save for later so that new values aren't overwritten by defaults
235 ptr += 6;
236 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000237 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400238 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
239 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400240 } else {
241 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000242 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400243 }
244 }
245 while (index < line_len && full_line[index] != '\0')
246 index++;
247 }
248
249 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
250 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000251 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400252 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000253 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400254 return 0;
255 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400256 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400257 Setup_File_System(Display_Error);
258 if (Mount_Point == "/system") {
259 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500260 Backup_Display_Name = Display_Name;
261 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400262 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500263 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500264 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400265 } else if (Mount_Point == "/data") {
Dees Troyc657cc02015-01-16 22:48:47 +0000266 UnMount(false); // added in case /data is mounted as tmpfs for qcom hardware decrypt
Dees_Troy5bf43922012-09-07 16:07:55 -0400267 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500268 Backup_Display_Name = Display_Name;
269 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400270 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400271 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500272 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000273 Can_Encrypt_Backup = true;
274 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500275 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200276 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400277#ifdef TW_INCLUDE_CRYPTO
278 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400279 char crypto_blkdev[255];
280 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
281 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400282 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400283 DataManager::SetValue(TW_IS_DECRYPTED, 1);
284 Is_Encrypted = true;
285 Is_Decrypted = true;
286 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000287 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400288 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600289 if (Is_Present) {
Ethan Yonker253368a2014-11-25 15:00:52 -0600290 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 -0600291 if (cryptfs_check_footer() == 0) {
292 Is_Encrypted = true;
293 Is_Decrypted = false;
294 Can_Be_Mounted = false;
295 Current_File_System = "emmc";
296 Setup_Image(Display_Error);
297 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
Vojtech Bocek7e11ac52015-03-05 23:21:49 +0100298 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
Ethan Yonker71413f42014-02-26 13:36:08 -0600299 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
300 DataManager::SetValue("tw_crypto_display", "");
301 } else {
302 LOGERR("Could not mount /data and unable to find crypto footer.\n");
303 }
304 } else {
305 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
306 }
Gary Peck82599a82012-11-21 16:23:12 -0800307 } else {
308 // Filesystem is not encrypted and the mount
309 // succeeded, so get it back to the original
310 // unmounted state
311 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400312 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500313 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400314 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400315#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500316 if (datamedia)
317 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400318#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400319 } else if (Mount_Point == "/cache") {
320 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500321 Backup_Display_Name = Display_Name;
322 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400323 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400324 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500325 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400326 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000327 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500328 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500329 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400330 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400331 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400332 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400333 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500334 Backup_Display_Name = Display_Name;
335 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400336 Is_SubPartition = true;
337 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400338 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500339 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000340 Can_Encrypt_Backup = true;
341 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400342 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400343 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400344 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500345 Backup_Display_Name = Display_Name;
346 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400347 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400348 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500349 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000350 Can_Encrypt_Backup = true;
351 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400352 } else if (Mount_Point == "/boot") {
353 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500354 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400355 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500356 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500357 } else if (Mount_Point == "/vendor") {
358 Display_Name = "Vendor";
359 Backup_Display_Name = Display_Name;
360 Storage_Name = Display_Name;
361 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400362 }
363#ifdef TW_EXTERNAL_STORAGE_PATH
364 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
365 Is_Storage = true;
366 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400367 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500368 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400369#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000370 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400371 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400372 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500373 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400374#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000375 }
Dees_Troy8170a922012-09-18 15:40:25 -0400376#ifdef TW_INTERNAL_STORAGE_PATH
377 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
378 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500379 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400380 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500381 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400382 }
383#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000384 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400385 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500386 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500387 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400388 }
389#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400390 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400391 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400392 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500393 if (Mount_Point == "/boot") {
394 Display_Name = "Boot";
395 Backup_Display_Name = Display_Name;
396 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600397 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500398 } else if (Mount_Point == "/recovery") {
399 Display_Name = "Recovery";
400 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600401 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500402 } else if (Mount_Point == "/system_image") {
403 Display_Name = "System Image";
404 Backup_Display_Name = Display_Name;
405 Can_Flash_Img = false;
406 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500407 } else if (Mount_Point == "/vendor_image") {
408 Display_Name = "Vendor Image";
409 Backup_Display_Name = Display_Name;
410 Can_Flash_Img = false;
411 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500412 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400413 }
414
Dees_Troy51127312012-09-08 13:08:49 -0400415 // Process any custom flags
416 if (Flags.size() > 0)
417 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400418 return true;
419}
420
Ketut Putu Kumajayabfb72b02015-08-10 16:40:55 +0200421bool TWPartition::Process_FS_Flags(string& Options, int& Flags) {
Hashcode62bd9e02013-11-19 21:59:42 -0800422 int i;
423 char *p;
424 char *savep;
425 char fs_options[250];
426
427 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
428 Options = "";
429
430 p = strtok_r(fs_options, ",", &savep);
431 while (p) {
432 /* Look for the flag "p" in the flag list "fl"
433 * If not found, the loop exits with fl[i].name being null.
434 */
435 for (i = 0; mount_flags[i].name; i++) {
436 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
437 Flags |= mount_flags[i].flag;
438 break;
439 }
440 }
441
442 if (!mount_flags[i].name) {
443 if (Options.size() > 0)
444 Options += ",";
445 Options += p;
446 }
447 p = strtok_r(NULL, ",", &savep);
448 }
449
450 return true;
451}
452
Dees_Troy51127312012-09-08 13:08:49 -0400453bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
454 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500455 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400456 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500457 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400458
459 strcpy(flags, Flags.c_str());
460 flags_len = Flags.size();
461 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500462 if (flags[index] == 34)
463 skip = !skip;
464 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400465 flags[index] = '\0';
466 }
467
468 index = 0;
469 while (index < flags_len) {
470 while (index < flags_len && flags[index] == '\0')
471 index++;
472 if (index >= flags_len)
473 continue;
474 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500475 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400476 if (strcmp(ptr, "removable") == 0) {
477 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600478 } else if (strncmp(ptr, "storage", 7) == 0) {
479 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600480 Is_Storage = true;
481 } else if (ptr_len == 9) {
482 ptr += 9;
483 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
484 LOGINFO("storage set to true\n");
485 Is_Storage = true;
486 } else {
487 LOGINFO("storage set to false\n");
488 Is_Storage = false;
489 }
490 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500491 } else if (strcmp(ptr, "settingsstorage") == 0) {
492 Is_Storage = true;
Xueferbfce5042015-04-14 02:02:05 +0800493 Is_Settings_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500494 } else if (strcmp(ptr, "andsec") == 0) {
495 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400496 } else if (strcmp(ptr, "canbewiped") == 0) {
497 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700498 } else if (strcmp(ptr, "usermrf") == 0) {
499 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500500 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
501 ptr += 7;
502 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
503 Can_Be_Backed_Up = true;
504 else
505 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400506 } else if (strcmp(ptr, "wipeingui") == 0) {
507 Can_Be_Wiped = true;
508 Wipe_Available_in_GUI = true;
509 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
510 Can_Be_Wiped = true;
511 Wipe_Available_in_GUI = true;
512 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500513 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000514 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400515 Is_SubPartition = true;
516 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000517 } else if (strcmp(ptr, "ignoreblkid") == 0) {
518 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000519 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
520 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500521 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400522 ptr += 8;
523 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500524 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
525 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400526 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500527 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400528 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500529 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
530 Display_Name.resize(Display_Name.size() - 1);
531 }
532 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
533 has_storage_name = true;
534 ptr += 11;
535 if (*ptr == '\"') ptr++;
536 Storage_Name = ptr;
537 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
538 Storage_Name.resize(Storage_Name.size() - 1);
539 }
540 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
541 has_backup_name = true;
542 ptr += 10;
543 if (*ptr == '\"') ptr++;
544 Backup_Display_Name = ptr;
545 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
546 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
547 }
548 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400549 ptr += 10;
Ethan Yonkera2719152015-05-28 09:44:41 -0500550 Format_Block_Size = (unsigned long)(atol(ptr));
Dees_Troya13d74f2013-03-24 08:54:55 -0500551 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400552 ptr += 7;
553 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000554 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
555 ptr += 17;
556 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
557 Can_Encrypt_Backup = true;
558 else
559 Can_Encrypt_Backup = false;
560 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
561 ptr += 21;
562 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
563 Can_Encrypt_Backup = true;
564 Use_Userdata_Encryption = true;
565 } else {
566 Use_Userdata_Encryption = false;
567 }
Hashcode62bd9e02013-11-19 21:59:42 -0800568 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
569 ptr += 8;
570 if (*ptr == '\"') ptr++;
571
572 Mount_Options = ptr;
573 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
574 Mount_Options.resize(Mount_Options.size() - 1);
575 }
576 Process_FS_Flags(Mount_Options, Mount_Flags);
Ethan Yonker253368a2014-11-25 15:00:52 -0600577 } else if ((ptr_len > 12 && strncmp(ptr, "encryptable=", 12) == 0) || (ptr_len > 13 && strncmp(ptr, "forceencrypt=", 13) == 0)) {
578 ptr += 12;
579 if (*ptr == '=') ptr++;
580 if (*ptr == '\"') ptr++;
581
582 Crypto_Key_Location = ptr;
583 if (Crypto_Key_Location.substr(Crypto_Key_Location.size() - 1, 1) == "\"") {
584 Crypto_Key_Location.resize(Crypto_Key_Location.size() - 1);
585 }
586 } else if (ptr_len > 8 && strncmp(ptr, "mounttodecrypt", 14) == 0) {
587 Mount_To_Decrypt = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600588 } else if (strncmp(ptr, "flashimg", 8) == 0) {
589 if (ptr_len == 8) {
590 Can_Flash_Img = true;
591 } else if (ptr_len == 10) {
592 ptr += 9;
593 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
594 Can_Flash_Img = true;
595 } else {
596 Can_Flash_Img = false;
597 }
598 }
Dees_Troy51127312012-09-08 13:08:49 -0400599 } else {
600 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000601 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400602 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000603 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400604 }
605 while (index < flags_len && flags[index] != '\0')
606 index++;
607 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500608 if (has_display_name && !has_storage_name)
609 Storage_Name = Display_Name;
610 if (!has_display_name && has_storage_name)
611 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000612 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500613 Backup_Display_Name = Display_Name;
614 if (!has_display_name && has_backup_name)
615 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400616 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400617}
618
Dees_Troy5bf43922012-09-07 16:07:55 -0400619bool TWPartition::Is_File_System(string File_System) {
620 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400621 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400622 File_System == "ext4" ||
623 File_System == "vfat" ||
624 File_System == "ntfs" ||
625 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500626 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000627 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400628 File_System == "auto")
629 return true;
630 else
631 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400632}
633
Dees_Troy5bf43922012-09-07 16:07:55 -0400634bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400635 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400636 return true;
637 else
638 return false;
639}
640
Dees_Troy51127312012-09-08 13:08:49 -0400641bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400642 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400643 if (mkdir(Path.c_str(), 0777) == -1) {
644 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000645 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400646 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000647 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400648 return false;
649 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000650 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400651 return true;
652 }
653 }
654 return true;
655}
656
Dees_Troy5bf43922012-09-07 16:07:55 -0400657void TWPartition::Setup_File_System(bool Display_Error) {
658 struct statfs st;
659
660 Can_Be_Mounted = true;
661 Can_Be_Wiped = true;
662
Dees_Troy5bf43922012-09-07 16:07:55 -0400663 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400664 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400665 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
666 Backup_Name = Display_Name;
667 Backup_Method = FILES;
668}
669
670void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400671 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
672 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800673 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400674 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800675 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400676 Backup_Method = FLASH_UTILS;
677 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000678 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 -0400679 if (Find_Partition_Size()) {
680 Used = Size;
681 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400682 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400683 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000684 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400685 else
Dees Troyd932ce12013-10-18 17:12:59 +0000686 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400687 }
688}
689
Dees_Troye58d5262012-09-21 12:27:57 -0400690void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500691 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400692 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500693 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400694 Has_Android_Secure = true;
695 Symlink_Path = Mount_Point + "/.android_secure";
696 Symlink_Mount_Point = "/and-sec";
697 Backup_Path = Symlink_Mount_Point;
698 Make_Dir("/and-sec", true);
699 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600700 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400701}
702
that9e0593e2014-10-08 00:01:24 +0200703void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500704 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
705 Storage_Name = "Internal Storage";
706 Has_Data_Media = true;
707 Is_Storage = true;
708 Is_Settings_Storage = true;
709 Storage_Path = "/data/media";
710 Symlink_Path = Storage_Path;
711 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
712 Make_Dir("/emmc", false);
713 Symlink_Mount_Point = "/emmc";
714 } else {
715 Make_Dir("/sdcard", false);
716 Symlink_Mount_Point = "/sdcard";
717 }
718 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
719 Storage_Path = "/data/media/0";
720 Symlink_Path = Storage_Path;
721 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
722 UnMount(true);
723 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500724 DataManager::SetValue("tw_has_internal", 1);
725 DataManager::SetValue("tw_has_data_media", 1);
726 du.add_absolute_dir("/data/media");
727}
728
Dees_Troy5bf43922012-09-07 16:07:55 -0400729void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
730 char device[512], realDevice[512];
731
732 strcpy(device, Block.c_str());
733 memset(realDevice, 0, sizeof(realDevice));
734 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
735 {
736 strcpy(device, realDevice);
737 memset(realDevice, 0, sizeof(realDevice));
738 }
739
740 if (device[0] != '/') {
741 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000742 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400743 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000744 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400745 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400746 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400747 Block = device;
748 return;
749 }
750}
751
Dees_Troy8e337f32012-10-13 22:07:49 -0400752void TWPartition::Mount_Storage_Retry(void) {
753 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500754 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400755 int retry_count = 5;
756 while (retry_count > 0 && !Mount(false)) {
757 usleep(500000);
758 retry_count--;
759 }
760 Mount(true);
761 }
762}
763
Dees_Troy38bd7602012-09-14 13:33:53 -0400764bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
765 FILE *fp = NULL;
766 char line[255];
767
768 fp = fopen("/proc/mtd", "rt");
769 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000770 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400771 return false;
772 }
773
774 while (fgets(line, sizeof(line), fp) != NULL)
775 {
776 char device[32], label[32];
777 unsigned long size = 0;
778 char* fstype = NULL;
779 int deviceId;
780
781 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
782
783 // Skip header and blank lines
784 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
785 continue;
786
787 // Strip off the trailing " from the label
788 label[strlen(label)-1] = '\0';
789
790 if (strcmp(label, MTD_Name.c_str()) == 0) {
791 // We found our device
792 // Strip off the trailing : from the device
793 device[strlen(device)-1] = '\0';
794 if (sscanf(device,"mtd%d", &deviceId) == 1) {
795 sprintf(device, "/dev/block/mtdblock%d", deviceId);
796 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000797 fclose(fp);
798 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400799 }
800 }
801 }
802 fclose(fp);
803
804 return false;
805}
806
Dees_Troy51127312012-09-08 13:08:49 -0400807bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
808 struct statfs st;
809 string Local_Path = Mount_Point + "/.";
810
811 if (!Mount(Display_Error))
812 return false;
813
814 if (statfs(Local_Path.c_str(), &st) != 0) {
815 if (!Removable) {
816 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000817 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400818 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000819 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400820 }
821 return false;
822 }
823 Size = (st.f_blocks * st.f_bsize);
824 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
825 Free = (st.f_bfree * st.f_bsize);
826 Backup_Size = Used;
827 return true;
828}
829
830bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400831 FILE* fp;
832 char command[255], line[512];
833 int include_block = 1;
834 unsigned int min_len;
835
836 if (!Mount(Display_Error))
837 return false;
838
Dees_Troy38bd7602012-09-14 13:33:53 -0400839 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400840 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200841 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400842 fp = fopen("/tmp/dfoutput.txt", "rt");
843 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000844 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400845 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400846 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400847
848 while (fgets(line, sizeof(line), fp) != NULL)
849 {
850 unsigned long blocks, used, available;
851 char device[64];
852 char tmpString[64];
853
854 if (strncmp(line, "Filesystem", 10) == 0)
855 continue;
856 if (strlen(line) < min_len) {
857 include_block = 0;
858 continue;
859 }
860 if (include_block) {
861 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
862 } else {
863 // The device block string is so long that the df information is on the next line
864 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400865 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400866 while (tmpString[space_count] == 32)
867 space_count++;
868 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
869 }
870
871 // Adjust block size to byte size
872 Size = blocks * 1024ULL;
873 Used = used * 1024ULL;
874 Free = available * 1024ULL;
875 Backup_Size = Used;
876 }
877 fclose(fp);
878 return true;
879}
880
Dees_Troy5bf43922012-09-07 16:07:55 -0400881bool TWPartition::Find_Partition_Size(void) {
882 FILE* fp;
883 char line[512];
884 string tmpdevice;
885
igoriok87e3d932013-01-31 21:03:53 +0200886 fp = fopen("/proc/dumchar_info", "rt");
887 if (fp != NULL) {
888 while (fgets(line, sizeof(line), fp) != NULL)
889 {
890 char label[32], device[32];
891 unsigned long size = 0;
892
thatd43bf2d2014-09-21 23:13:02 +0200893 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200894
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500895 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200896 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
897 continue;
898
899 tmpdevice = "/dev/";
900 tmpdevice += label;
901 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
902 Size = size;
903 fclose(fp);
904 return true;
905 }
906 }
907 }
908
Dees_Troy5bf43922012-09-07 16:07:55 -0400909 // In this case, we'll first get the partitions we care about (with labels)
910 fp = fopen("/proc/partitions", "rt");
911 if (fp == NULL)
912 return false;
913
914 while (fgets(line, sizeof(line), fp) != NULL)
915 {
916 unsigned long major, minor, blocks;
917 char device[512];
918 char tmpString[64];
919
Dees_Troy63c8df72012-09-10 14:02:05 -0400920 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400921 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
922
923 tmpdevice = "/dev/block/";
924 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400925 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400926 // Adjust block size to byte size
927 Size = blocks * 1024ULL;
928 fclose(fp);
929 return true;
930 }
931 }
932 fclose(fp);
933 return false;
934}
935
Dees_Troy5bf43922012-09-07 16:07:55 -0400936bool TWPartition::Is_Mounted(void) {
937 if (!Can_Be_Mounted)
938 return false;
939
940 struct stat st1, st2;
941 string test_path;
942
943 // Check to see if the mount point directory exists
944 test_path = Mount_Point + "/.";
945 if (stat(test_path.c_str(), &st1) != 0) return false;
946
947 // Check to see if the directory above the mount point exists
948 test_path = Mount_Point + "/../.";
949 if (stat(test_path.c_str(), &st2) != 0) return false;
950
951 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
952 int ret = (st1.st_dev != st2.st_dev) ? true : false;
953
954 return ret;
955}
956
957bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000958 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500959 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000960
Dees_Troy5bf43922012-09-07 16:07:55 -0400961 if (Is_Mounted()) {
962 return true;
963 } else if (!Can_Be_Mounted) {
964 return false;
965 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400966
967 Find_Actual_Block_Device();
968
969 // Check the current file system before mounting
970 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000971 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000972 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 +0000973 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000974 string result;
975 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000976 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000977 Current_File_System = "vfat";
978 } else {
979#ifdef TW_NO_EXFAT_FUSE
980 UnMount(false);
981 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
982 // Some kernels let us mount vfat as exfat which doesn't work out too well
983#else
984 exfat_mounted = 1;
985#endif
986 }
987 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500988
Ethan Yonkerb81d9052015-07-09 13:20:53 -0500989 if (Current_File_System == "ntfs" && TWFunc::Path_Exists("/sbin/ntfs-3g")) {
990 string cmd;
991 if (Mount_Read_Only)
992 cmd = "/sbin/ntfs-3g -o ro " + Actual_Block_Device + " " + Mount_Point;
993 else
994 cmd = "/sbin/ntfs-3g " + Actual_Block_Device + " " + Mount_Point;
995 LOGINFO("cmd: '%s'\n", cmd.c_str());
996 if (TWFunc::Exec_Cmd(cmd) == 0) {
997 return true;
998 } else {
999 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1000 }
1001 }
1002
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001003 if (Mount_Read_Only)
1004 flags |= MS_RDONLY;
1005
Dees_Troy22042032012-12-18 21:23:08 +00001006 if (Fstab_File_System == "yaffs2") {
1007 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001008 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1009 if (Mount_Read_Only)
1010 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001011 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1012 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1013 if (Display_Error)
1014 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1015 else
1016 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1017 return false;
1018 } else {
1019 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1020 return true;
1021 }
1022 } else {
1023 struct stat st;
1024 string test_path = Mount_Point;
1025 if (stat(test_path.c_str(), &st) < 0) {
1026 if (Display_Error)
1027 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1028 else
1029 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1030 return false;
1031 }
1032 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1033 if (new_mode != st.st_mode) {
1034 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1035 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1036 if (Display_Error)
1037 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1038 else
1039 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1040 return false;
1041 }
1042 }
Dees_Troy22042032012-12-18 21:23:08 +00001043 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001044 }
thatc7572eb2015-03-31 18:55:30 +02001045 }
1046
1047 string mount_fs = Current_File_System;
1048 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1049 mount_fs = "texfat";
1050
1051 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001052 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1053 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001054#ifdef TW_NO_EXFAT_FUSE
1055 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001056 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001057 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001058 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001059 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001060 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001061 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001062 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 +00001063 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001064 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001065 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001066#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001067 if (!Removable && Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001068 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001069 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001070 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1071 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 +00001072 return false;
1073#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001074 }
1075#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001076 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001077
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001078 if (Removable)
1079 Update_Size(Display_Error);
1080
xiaolu9416f4f2015-06-04 08:22:23 +08001081 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001082 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001083 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001084 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001085 return true;
1086}
1087
1088bool TWPartition::UnMount(bool Display_Error) {
1089 if (Is_Mounted()) {
1090 int never_unmount_system;
1091
1092 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1093 if (never_unmount_system == 1 && Mount_Point == "/system")
1094 return true; // Never unmount system if you're not supposed to unmount it
1095
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001096 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001097 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001098
Dees_Troy38bd7602012-09-14 13:33:53 -04001099 if (!Symlink_Mount_Point.empty())
1100 umount(Symlink_Mount_Point.c_str());
1101
Dees_Troyb05ddee2013-01-28 20:24:50 +00001102 umount(Mount_Point.c_str());
1103 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001104 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001105 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001106 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001107 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001108 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001109 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001110 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001111 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001112 } else {
1113 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001114 }
1115}
1116
Gary Peck43acadf2012-11-21 21:19:01 -08001117bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001118 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001119 int check;
1120 string Layout_Filename = Mount_Point + "/.layout_version";
1121
Dees_Troy38bd7602012-09-14 13:33:53 -04001122 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001123 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001124 return false;
1125 }
1126
Dees_Troyc51f1f92012-09-20 15:32:13 -04001127 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001128 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001129
Dees_Troy16c2b312013-01-15 16:51:18 +00001130 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1131 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1132 else
1133 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001134
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001135 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001136 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001137 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001138 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001139 DataManager::GetValue(TW_RM_RF_VAR, check);
1140
Hashcodedabfd492013-08-29 22:45:30 -07001141 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001142 wiped = Wipe_RMRF();
1143 else if (New_File_System == "ext4")
1144 wiped = Wipe_EXT4();
1145 else if (New_File_System == "ext2" || New_File_System == "ext3")
1146 wiped = Wipe_EXT23(New_File_System);
1147 else if (New_File_System == "vfat")
1148 wiped = Wipe_FAT();
1149 else if (New_File_System == "exfat")
1150 wiped = Wipe_EXFAT();
1151 else if (New_File_System == "yaffs2")
1152 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001153 else if (New_File_System == "f2fs")
1154 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001155 else if (New_File_System == "ntfs")
1156 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001157 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001158 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 +00001159 unlink("/.layout_version");
1160 return false;
1161 }
1162 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001163 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001164
Gary Pecke8bc5d72012-12-21 06:45:25 -08001165 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001166 if (Mount_Point == "/cache")
1167 DataManager::Output_Version();
1168
Dees_Troy16c2b312013-01-15 16:51:18 +00001169 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1170 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1171
1172 if (update_crypt) {
1173 Setup_File_System(false);
1174 if (Is_Encrypted && !Is_Decrypted) {
1175 // just wiped an encrypted partition back to its unencrypted state
1176 Is_Encrypted = false;
1177 Is_Decrypted = false;
1178 Decrypted_Block_Device = "";
1179 if (Mount_Point == "/data") {
1180 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1181 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1182 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001183 }
1184 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001185
1186 if (Mount_Point == "/data" && Has_Data_Media && recreate_media) {
1187 Recreate_Media_Folder();
1188 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001189 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001190 if (Is_Storage) {
1191 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001192 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001193 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001194}
1195
Gary Peck43acadf2012-11-21 21:19:01 -08001196bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001197 if (Is_File_System(Current_File_System))
1198 return Wipe(Current_File_System);
1199 else
1200 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001201}
1202
Dees_Troye58d5262012-09-21 12:27:57 -04001203bool TWPartition::Wipe_AndSec(void) {
1204 if (!Has_Android_Secure)
1205 return false;
1206
Dees_Troye58d5262012-09-21 12:27:57 -04001207 if (!Mount(true))
1208 return false;
1209
Dees_Troy2673cec2013-04-02 20:22:16 +00001210 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001211 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001212 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001213}
1214
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001215bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001216 if (Mount_Read_Only)
1217 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001218 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001219 return true;
1220 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1221 return true;
1222 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1223 return true;
1224 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1225 return true;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001226 else if (Current_File_System == "ntfs" && TWFunc::Path_Exists("/sbin/ntfsfix"))
1227 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001228 return false;
1229}
1230
1231bool TWPartition::Repair() {
1232 string command;
1233
1234 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001235 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
1236 gui_print("fsck.fat does not exist! Cannot repair!\n");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001237 return false;
1238 }
1239 if (!UnMount(true))
1240 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001241 gui_print("Repairing %s using fsck.fat...\n", Display_Name.c_str());
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001242 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001243 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001244 LOGINFO("Repair command: %s\n", command.c_str());
1245 if (TWFunc::Exec_Cmd(command) == 0) {
1246 gui_print("Done.\n");
1247 return true;
1248 } else {
1249 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1250 return false;
1251 }
1252 }
1253 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1254 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
1255 gui_print("e2fsck does not exist! Cannot repair!\n");
1256 return false;
1257 }
1258 if (!UnMount(true))
1259 return false;
1260 gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str());
1261 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001262 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001263 LOGINFO("Repair command: %s\n", command.c_str());
1264 if (TWFunc::Exec_Cmd(command) == 0) {
1265 gui_print("Done.\n");
1266 return true;
1267 } else {
1268 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1269 return false;
1270 }
1271 }
1272 if (Current_File_System == "exfat") {
1273 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
1274 gui_print("fsck.exfat does not exist! Cannot repair!\n");
1275 return false;
1276 }
1277 if (!UnMount(true))
1278 return false;
1279 gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str());
1280 Find_Actual_Block_Device();
1281 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1282 LOGINFO("Repair command: %s\n", command.c_str());
1283 if (TWFunc::Exec_Cmd(command) == 0) {
1284 gui_print("Done.\n");
1285 return true;
1286 } else {
1287 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1288 return false;
1289 }
1290 }
1291 if (Current_File_System == "f2fs") {
1292 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
1293 gui_print("fsck.f2fs does not exist! Cannot repair!\n");
1294 return false;
1295 }
1296 if (!UnMount(true))
1297 return false;
1298 gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str());
1299 Find_Actual_Block_Device();
1300 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1301 LOGINFO("Repair command: %s\n", command.c_str());
1302 if (TWFunc::Exec_Cmd(command) == 0) {
1303 gui_print("Done.\n");
1304 return true;
1305 } else {
1306 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1307 return false;
1308 }
1309 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001310 if (Current_File_System == "ntfs") {
1311 if (!TWFunc::Path_Exists("/sbin/ntfsfix")) {
1312 gui_print("ntfsfix does not exist! Cannot repair!\n");
1313 return false;
1314 }
1315 if (!UnMount(true))
1316 return false;
1317 gui_print("Repairing %s using ntfsfix...\n", Display_Name.c_str());
1318 Find_Actual_Block_Device();
1319 command = "/sbin/ntfsfix " + Actual_Block_Device;
1320 LOGINFO("Repair command: %s\n", command.c_str());
1321 if (TWFunc::Exec_Cmd(command) == 0) {
1322 gui_print("Done.\n");
1323 return true;
1324 } else {
1325 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1326 return false;
1327 }
1328 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001329 return false;
1330}
1331
Ethan Yonkera2719152015-05-28 09:44:41 -05001332bool TWPartition::Can_Resize() {
1333 if (Mount_Read_Only)
1334 return false;
1335 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1336 return true;
1337 return false;
1338}
1339
1340bool TWPartition::Resize() {
1341 string command;
1342
1343 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1344 if (!Can_Repair()) {
1345 LOGERR("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1346 return false;
1347 }
1348 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
1349 gui_print("resize2fs does not exist! Cannot resize!\n");
1350 return false;
1351 }
1352 // Repair will unmount so no need to do it twice
1353 gui_print("Repairing %s before resizing.\n", Display_Name.c_str());
1354 if (!Repair())
1355 return false;
1356 gui_print("Resizing %s using resize2fs...\n", Display_Name.c_str());
1357 Find_Actual_Block_Device();
1358 command = "/sbin/resize2fs " + Actual_Block_Device;
1359 if (Length != 0) {
1360 unsigned int block_device_size;
1361 int fd, ret;
1362
1363 fd = open(Actual_Block_Device.c_str(), O_RDONLY);
1364 if (fd < 0) {
1365 LOGERR("Resize: Failed to open '%s'\n", Actual_Block_Device.c_str());
1366 return false;
1367 }
1368 ret = ioctl(fd, BLKGETSIZE, &block_device_size);
1369 close(fd);
1370 if (ret) {
1371 LOGERR("Resize: ioctl error\n");
1372 return false;
1373 }
1374 unsigned long long Actual_Size = (unsigned long long)(block_device_size) * 512LLU;
1375 unsigned long long Block_Count;
1376 if (Length < 0) {
1377 // Reduce overall size by this length
1378 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1379 } else {
1380 // This is the size, not a size reduction
1381 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1382 }
1383 char temp[256];
1384 sprintf(temp, "%llu", Block_Count);
1385 command += " ";
1386 command += temp;
1387 command += "K";
1388 }
1389 LOGINFO("Resize command: %s\n", command.c_str());
1390 if (TWFunc::Exec_Cmd(command) == 0) {
1391 Update_Size(true);
1392 gui_print("Done.\n");
1393 return true;
1394 } else {
1395 Update_Size(true);
1396 LOGERR("Unable to resize '%s'.\n", Mount_Point.c_str());
1397 return false;
1398 }
1399 }
1400 return false;
1401}
1402
bigbiff7abc5fe2015-01-17 16:53:12 -05001403bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
1404 if (Backup_Method == FILES) {
1405 return Backup_Tar(backup_folder, overall_size, other_backups_size, tar_fork_pid);
1406 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001407 else if (Backup_Method == DD)
1408 return Backup_DD(backup_folder);
1409 else if (Backup_Method == FLASH_UTILS)
1410 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001411 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001412 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001413}
1414
Dees_Troy43d8b002012-09-17 16:00:01 -04001415bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001416 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001417 char split_filename[512];
1418 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001419 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001420
Captain Throwbackff5935f2014-09-23 16:08:34 -04001421 sync();
1422
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001423 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001424 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001425 if (!TWFunc::Path_Exists(Full_Filename)) {
1426 // This is a split archive, we presume
1427 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001428 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001429 md5file = split_filename;
1430 md5file += ".md5";
1431 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001432 LOGERR("No md5 file found for '%s'.\n", split_filename);
1433 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001434 return false;
1435 }
1436 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001437 while (index < 1000) {
1438 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001439 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001440 return false;
1441 }
1442 index++;
1443 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001444 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001445 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001446 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001447 } else {
1448 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001449 md5file = Full_Filename + ".md5";
1450 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001451 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1452 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001453 return false;
1454 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001455 md5sum.setfn(Full_Filename);
1456 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001457 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001458 return false;
1459 } else
1460 return true;
1461 }
1462 return false;
1463}
1464
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001465bool 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 -08001466 string Restore_File_System;
1467
1468 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001469 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001470
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001471 Restore_File_System = Get_Restore_File_System(restore_folder);
1472
1473 if (Is_File_System(Restore_File_System))
1474 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1475 else if (Is_Image(Restore_File_System)) {
Ethan Yonkerf66324f2015-01-09 16:46:57 -06001476 return Restore_Image(restore_folder, total_restore_size, already_restored_size, Restore_File_System);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001477 }
1478
1479 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1480 return false;
1481}
1482
1483string TWPartition::Get_Restore_File_System(string restore_folder) {
1484 size_t first_period, second_period;
1485 string Restore_File_System;
1486
Gary Peck43acadf2012-11-21 21:19:01 -08001487 // Parse backup filename to extract the file system before wiping
1488 first_period = Backup_FileName.find(".");
1489 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001490 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001491 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001492 }
1493 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1494 second_period = Restore_File_System.find(".");
1495 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001496 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001497 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001498 }
1499 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001500 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001501 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001502}
1503
1504string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001505 if (Backup_Method == NONE)
1506 return "none";
1507 else if (Backup_Method == FILES)
1508 return "files";
1509 else if (Backup_Method == DD)
1510 return "dd";
1511 else if (Backup_Method == FLASH_UTILS)
1512 return "flash_utils";
1513 else
1514 return "undefined";
1515 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001516}
1517
1518bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001519 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001520 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001521 return 1;
1522}
1523
1524bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001525 bool Save_Data_Media = Has_Data_Media;
1526
1527 if (!UnMount(true))
1528 return false;
1529
Dees_Troy38bd7602012-09-14 13:33:53 -04001530 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001531 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001532#ifdef TW_INCLUDE_CRYPTO
1533 if (Is_Decrypted) {
1534 if (!UnMount(true))
1535 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001536 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001537 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1538 }
1539 }
1540#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001541 Is_Decrypted = false;
1542 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001543 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001544 if (Crypto_Key_Location == "footer") {
1545 int newlen, fd;
1546 if (Length != 0) {
1547 newlen = Length;
1548 if (newlen < 0)
1549 newlen = newlen * -1;
1550 } else {
1551 newlen = CRYPT_FOOTER_OFFSET;
1552 }
1553 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1554 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1555 } else {
1556 unsigned int block_count;
1557 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1558 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1559 } else {
1560 off64_t offset = ((off64_t)block_count * 512) - newlen;
1561 if (lseek64(fd, offset, SEEK_SET) == -1) {
1562 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1563 } else {
1564 void* buffer = malloc(newlen);
1565 if (!buffer) {
1566 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1567 } else {
1568 memset(buffer, 0, newlen);
1569 int ret = write(fd, buffer, newlen);
1570 if (ret != newlen) {
1571 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1572 } else {
1573 LOGINFO("Successfully wiped crypto footer.\n");
1574 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001575 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001576 }
1577 }
1578 }
1579 close(fd);
1580 }
1581 } else {
1582 string Command = "flash_image " + Crypto_Key_Location + " /dev/zero";
1583 TWFunc::Exec_Cmd(Command);
1584 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001585 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001586 Has_Data_Media = Save_Data_Media;
1587 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1588 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001589 if (Mount(false))
1590 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001591 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001592 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001593#ifndef TW_OEM_BUILD
Dees_Troy2673cec2013-04-02 20:22:16 +00001594 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Ethan Yonker83e82572014-04-04 10:59:28 -05001595#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001596 return true;
1597 } else {
1598 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001599 LOGERR("Unable to format to remove encryption.\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06001600 if (Has_Data_Media && Mount(false))
1601 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001602 return false;
1603 }
1604 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001605}
1606
1607void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001608 const char* type;
1609 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001610
Dees_Troy68cab492012-12-12 19:29:35 +00001611 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1612 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001613
Dees_Troy38bd7602012-09-14 13:33:53 -04001614 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001615 if (!Is_Present)
1616 return;
Dees_Troy51127312012-09-08 13:08:49 -04001617
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001618 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1619 if (blkid_do_fullprobe(pr)) {
1620 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001621 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001622 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001623 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001624
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001625 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001626 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001627 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001628 return;
1629 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001630
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001631 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001632 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001633}
1634
Gary Peck43acadf2012-11-21 21:19:01 -08001635bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001636 if (!UnMount(true))
1637 return false;
1638
Dees_Troy43d8b002012-09-17 16:00:01 -04001639 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001640 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001641
Dees_Troy2673cec2013-04-02 20:22:16 +00001642 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001643 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001644 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001645 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001646 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001647 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001648 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001649 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001650 return true;
1651 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001652 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001653 return false;
1654 }
1655 } else
1656 return Wipe_RMRF();
1657
1658 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001659}
1660
1661bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001662 Find_Actual_Block_Device();
1663 if (!Is_Present) {
1664 LOGERR("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1665 return false;
1666 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001667 if (!UnMount(true))
1668 return false;
1669
Dees_Troyb3265ab2013-08-30 02:59:14 +00001670#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001671 int ret;
1672 char *secontext = NULL;
1673
Dees_Troya95f55c2013-08-17 13:14:43 +00001674 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001675
Dees Troy99c8dbf2014-03-10 16:53:58 +00001676 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001677 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1678 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1679 } else {
1680 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1681 }
1682 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001683 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1684 return false;
1685 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001686 string sedir = Mount_Point + "/lost+found";
1687 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1688 rmdir(sedir.c_str());
1689 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001690 return true;
1691 }
1692#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001693 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001694 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001695
Dees_Troy2673cec2013-04-02 20:22:16 +00001696 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001697 Find_Actual_Block_Device();
1698 Command = "make_ext4fs";
1699 if (!Is_Decrypted && Length != 0) {
1700 // Only use length if we're not decrypted
1701 char len[32];
1702 sprintf(len, "%i", Length);
1703 Command += " -l ";
1704 Command += len;
1705 }
Dees_Troy5295d582013-09-06 15:51:08 +00001706 if (TWFunc::Path_Exists("/file_contexts")) {
1707 Command += " -S /file_contexts";
1708 }
1709 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001710 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001711 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001712 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001713 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001714 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001715 return true;
1716 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001717 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001718 return false;
1719 }
1720 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001721 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001722#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001723 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001724}
1725
1726bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001727 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001728
Matt Mower18794c82015-11-11 16:22:45 -06001729 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001730 if (!UnMount(true))
1731 return false;
1732
Matt Mower18794c82015-11-11 16:22:45 -06001733 gui_print("Formatting %s using mkfs.fat...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001734 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001735 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001736 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001737 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001738 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001739 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001740 return true;
1741 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001742 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001743 return false;
1744 }
1745 return true;
1746 }
1747 else
1748 return Wipe_RMRF();
1749
1750 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001751}
1752
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001753bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001754 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001755
1756 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1757 if (!UnMount(true))
1758 return false;
1759
Dees_Troy2673cec2013-04-02 20:22:16 +00001760 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001761 Find_Actual_Block_Device();
1762 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001763 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001764 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001765 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001766 return true;
1767 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001768 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001769 return false;
1770 }
1771 return true;
1772 }
1773 return false;
1774}
1775
Dees_Troy38bd7602012-09-14 13:33:53 -04001776bool TWPartition::Wipe_MTD() {
1777 if (!UnMount(true))
1778 return false;
1779
Dees_Troy2673cec2013-04-02 20:22:16 +00001780 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001781
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001782 mtd_scan_partitions();
1783 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1784 if (mtd == NULL) {
1785 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1786 return false;
1787 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001788
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001789 MtdWriteContext* ctx = mtd_write_partition(mtd);
1790 if (ctx == NULL) {
1791 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1792 return false;
1793 }
1794 if (mtd_erase_blocks(ctx, -1) == -1) {
1795 mtd_write_close(ctx);
1796 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1797 return false;
1798 }
1799 if (mtd_write_close(ctx) != 0) {
1800 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1801 return false;
1802 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001803 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001804 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001805 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001806 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001807}
1808
1809bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001810 if (!Mount(true))
1811 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06001812 // This is the only wipe that leaves the partition mounted, so we
1813 // must manually remove the partition from MTP if it is a storage
1814 // partition.
1815 if (Is_Storage)
1816 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001817
Dees_Troy2673cec2013-04-02 20:22:16 +00001818 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001819 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001820 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001821 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001822}
1823
Dees_Troye5017042013-08-29 16:38:55 +00001824bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001825 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001826
1827 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1828 if (!UnMount(true))
1829 return false;
1830
1831 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1832 Find_Actual_Block_Device();
dhacker29a3fa75f2015-01-17 19:42:33 -05001833 command = "mkfs.f2fs -t 1";
1834 if (!Is_Decrypted && Length != 0) {
1835 // Only use length if we're not decrypted
1836 char len[32];
1837 int mod_length = Length;
1838 if (Length < 0)
1839 mod_length *= -1;
1840 sprintf(len, "%i", mod_length);
1841 command += " -r ";
1842 command += len;
1843 }
1844 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001845 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001846 Recreate_AndSec_Folder();
1847 gui_print("Done.\n");
1848 return true;
1849 } else {
1850 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1851 return false;
1852 }
1853 return true;
1854 } else {
1855 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1856 return Wipe_RMRF();
1857 }
1858 return false;
1859}
1860
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001861bool TWPartition::Wipe_NTFS() {
1862 string command;
1863
1864 if (TWFunc::Path_Exists("/sbin/mkntfs")) {
1865 if (!UnMount(true))
1866 return false;
1867
1868 gui_print("Formatting %s using mkntfs...\n", Display_Name.c_str());
1869 Find_Actual_Block_Device();
1870 command = "mkntfs " + Actual_Block_Device;
1871 if (TWFunc::Exec_Cmd(command) == 0) {
1872 Recreate_AndSec_Folder();
1873 gui_print("Done.\n");
1874 return true;
1875 } else {
1876 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1877 return false;
1878 }
1879 return true;
1880 }
1881 return false;
1882}
1883
Dees_Troy51a0e822012-09-05 15:24:24 -04001884bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001885#ifdef TW_OEM_BUILD
1886 // In an OEM Build we want to do a full format
1887 return Wipe_Encryption();
1888#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001889 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001890
1891 // This handles wiping data on devices with "sdcard" in /data/media
1892 if (!Mount(true))
1893 return false;
1894
Dees_Troy2673cec2013-04-02 20:22:16 +00001895 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001896
1897 DIR* d;
1898 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001899 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001900 struct dirent* de;
1901 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001902 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001903 // The media folder is the "internal sdcard"
1904 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001905 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001906 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001907 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001908
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001909 dir = "/data/";
1910 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001911 if (de->d_type == DT_DIR) {
1912 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001913 } 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 +00001914 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001915 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001916 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001917 }
1918 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001919
Dees_Troy2673cec2013-04-02 20:22:16 +00001920 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001921 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001922 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001923 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001924 return false;
Ethan Yonker83e82572014-04-04 10:59:28 -05001925#endif // ifdef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001926}
1927
bigbiff7abc5fe2015-01-17 16:53:12 -05001928bool 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 -04001929 char back_name[255], split_index[5];
1930 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001931 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001932 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001933 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001934 twrpTar tar;
1935 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001936
Dees_Troy43d8b002012-09-17 16:00:01 -04001937 if (!Mount(true))
1938 return false;
1939
Dees_Troya13d74f2013-03-24 08:54:55 -05001940 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001941 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001942
1943 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001944 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001945
Dees_Troy83bd4832013-05-04 12:39:56 +00001946#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1947 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1948 if (use_encryption && Can_Encrypt_Backup) {
1949 tar.use_encryption = use_encryption;
1950 if (Use_Userdata_Encryption)
1951 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001952 string Password;
1953 DataManager::GetValue("tw_backup_password", Password);
1954 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001955 } else {
1956 use_encryption = false;
1957 }
1958#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001959
1960 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1961 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001962 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001963 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001964 Full_FileName = backup_folder + "/" + Backup_FileName;
1965 tar.setdir(Backup_Path);
1966 tar.setfn(Full_FileName);
1967 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001968 tar.partition_name = Backup_Name;
1969 tar.backup_folder = backup_folder;
bigbiff7abc5fe2015-01-17 16:53:12 -05001970 if (tar.createTarFork(overall_size, other_backups_size, tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00001971 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001972 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001973}
1974
1975bool TWPartition::Backup_DD(string backup_folder) {
codelover352b75e2015-03-29 02:44:07 +08001976 char back_name[255], block_size[32], dd_count[32];
1977 string Full_FileName, Command, DD_BS, DD_COUNT;
Dees_Troy43d8b002012-09-17 16:00:01 -04001978 int use_compression;
codelover352b75e2015-03-29 02:44:07 +08001979 unsigned long long DD_Block_Size, DD_Count;
Dees_Troy43d8b002012-09-17 16:00:01 -04001980
codelover352b75e2015-03-29 02:44:07 +08001981 DD_Block_Size = 16 * 1024 * 1024;
1982 while (Backup_Size % DD_Block_Size != 0) DD_Block_Size >>= 1;
1983
1984 DD_Count = Backup_Size / DD_Block_Size;
1985
1986 sprintf(dd_count, "%llu", DD_Count);
1987 DD_COUNT = dd_count;
1988
1989 sprintf(block_size, "%llu", DD_Block_Size);
1990 DD_BS = block_size;
igoriok87e3d932013-01-31 21:03:53 +02001991
Dees_Troyb46a6842012-09-25 11:06:46 -04001992 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001993 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001994
1995 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1996 Backup_FileName = back_name;
1997
1998 Full_FileName = backup_folder + "/" + Backup_FileName;
1999
codelover352b75e2015-03-29 02:44:07 +08002000 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + " count=" + DD_COUNT;
Dees_Troy2673cec2013-04-02 20:22:16 +00002001 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002002 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002003 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04002004 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002005 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002006 return false;
2007 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002008 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002009}
2010
2011bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002012 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02002013 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002014 int use_compression;
2015
Dees_Troyb46a6842012-09-25 11:06:46 -04002016 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00002017 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04002018
2019 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
2020 Backup_FileName = back_name;
2021
2022 Full_FileName = backup_folder + "/" + Backup_FileName;
2023
2024 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00002025 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002026 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002027 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002028 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2029 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
Dees_Troy2673cec2013-04-02 20:22:16 +00002030 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002031 return false;
2032 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002033 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002034}
2035
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002036unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
2037 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
2038 if (restore_info.LoadValues() == 0) {
2039 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2040 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2041 return Restore_Size;
2042 }
2043 }
2044 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
2045
2046 Full_FileName = restore_folder + "/" + Backup_FileName;
2047
2048 if (Is_Image(Restore_File_System)) {
2049 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2050 return Restore_Size;
2051 }
2052
2053 twrpTar tar;
2054 tar.setdir(Backup_Path);
2055 tar.setfn(Full_FileName);
2056 tar.backup_name = Backup_Name;
2057#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2058 string Password;
2059 DataManager::GetValue("tw_restore_password", Password);
2060 if (!Password.empty())
2061 tar.setpassword(Password);
2062#endif
2063 tar.partition_name = Backup_Name;
2064 tar.backup_folder = restore_folder;
2065 Restore_Size = tar.get_size();
2066 return Restore_Size;
2067}
2068
2069bool 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 -08002070 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04002071 int index = 0;
2072 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00002073 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002074
Dees_Troye58d5262012-09-21 12:27:57 -04002075 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002076 if (!Wipe_AndSec())
2077 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002078 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00002079 gui_print("Wiping %s...\n", Display_Name.c_str());
Ethan Yonker5eac2222014-06-11 12:22:55 -05002080 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
2081 gui_print("WARNING: This /data backup was made with %s file system!\n", Restore_File_System.c_str());
2082 gui_print("The backup may not boot unless you change back to %s.\n", Restore_File_System.c_str());
2083 if (!Wipe_Data_Without_Wiping_Media())
2084 return false;
2085 } else {
2086 if (!Wipe(Restore_File_System))
2087 return false;
2088 }
Dees_Troye58d5262012-09-21 12:27:57 -04002089 }
Dees_Troya13d74f2013-03-24 08:54:55 -05002090 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00002091 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04002092
2093 if (!Mount(true))
2094 return false;
2095
Dees_Troy4a2a1262012-09-18 09:33:47 -04002096 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002097 twrpTar tar;
2098 tar.setdir(Backup_Path);
2099 tar.setfn(Full_FileName);
2100 tar.backup_name = Backup_Name;
2101#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2102 string Password;
2103 DataManager::GetValue("tw_restore_password", Password);
2104 if (!Password.empty())
2105 tar.setpassword(Password);
2106#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002107 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002108 ret = false;
2109 else
2110 ret = true;
2111#ifdef HAVE_CAPABILITIES
2112 // Restore capabilities to the run-as binary
2113 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2114 struct vfs_cap_data cap_data;
2115 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2116
2117 memset(&cap_data, 0, sizeof(cap_data));
2118 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2119 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2120 cap_data.data[0].inheritable = 0;
2121 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2122 cap_data.data[1].inheritable = 0;
2123 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2124 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2125 } else {
2126 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2127 }
2128 }
2129#endif
2130 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002131}
2132
Ethan Yonker96af84a2015-01-05 14:58:36 -06002133bool TWPartition::Restore_Image(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size, string Restore_File_System) {
2134 string Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002135 double display_percent, progress_percent;
2136 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04002137
Dees_Troyda8b55a2012-12-12 19:18:30 +00002138 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04002139 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002140
Ethan Yonker96af84a2015-01-05 14:58:36 -06002141 if (Restore_File_System == "emmc") {
2142 if (!Flash_Image_DD(Full_FileName))
2143 return false;
2144 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
2145 if (!Flash_Image_FI(Full_FileName))
2146 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002147 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002148 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
2149 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
2150 DataManager::SetValue("tw_size_progress", size_progress);
2151 progress_percent = (display_percent / 100);
2152 DataManager::SetProgress((float)(progress_percent));
2153 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04002154 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002155}
Dees_Troy5bf43922012-09-07 16:07:55 -04002156
2157bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002158 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002159
Dees_Troyab10ee22012-09-21 14:27:30 -04002160 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002161 return false;
2162
Dees_Troy0550cfb2012-10-13 11:56:13 -04002163 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002164 if (Removable || Is_Encrypted) {
2165 if (!Mount(false))
2166 return true;
2167 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002168 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002169
2170 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002171 if (!ret || Size == 0) {
2172 if (!Get_Size_Via_df(Display_Error)) {
2173 if (!Was_Already_Mounted)
2174 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002175 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002176 }
2177 }
Dees_Troy51127312012-09-08 13:08:49 -04002178
Dees_Troy5bf43922012-09-07 16:07:55 -04002179 if (Has_Data_Media) {
2180 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002181 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002182 Used = du.Get_Folder_Size("/data");
2183 Backup_Size = Used;
2184 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002185 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002186 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002187 } else {
2188 if (!Was_Already_Mounted)
2189 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002190 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002191 }
Dees_Troye58d5262012-09-21 12:27:57 -04002192 } else if (Has_Android_Secure) {
2193 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002194 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002195 else {
2196 if (!Was_Already_Mounted)
2197 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002198 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002199 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002200 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002201 if (!Was_Already_Mounted)
2202 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002203 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002204}
Dees_Troy38bd7602012-09-14 13:33:53 -04002205
2206void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002207 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002208 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002209 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002210 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002211 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002212 Is_Present = true;
2213 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002214 return;
2215 }
2216 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002217 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002218 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002219 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002220 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002221 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002222 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002223}
2224
2225void TWPartition::Recreate_Media_Folder(void) {
2226 string Command;
2227
2228 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002229 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04002230 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002231 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00002232 LOGINFO("Recreating /data/media folder.\n");
xiaolu9416f4f2015-06-04 08:22:23 +08002233 mkdir("/data/media", 0770);
2234 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2235 if (!Internal_path.empty()) {
2236 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2237 mkdir(Internal_path.c_str(), 0770);
2238 }
2239#ifdef TW_INTERNAL_STORAGE_PATH
2240 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2241#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002242#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002243 // Afterwards, we will try to set the
2244 // default metadata that we were hopefully able to get during
2245 // early boot.
2246 tw_set_default_metadata("/data/media");
xiaolu9416f4f2015-06-04 08:22:23 +08002247 if (!Internal_path.empty())
2248 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002249#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002250 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002251 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002252 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002253 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002254}
Dees_Troye58d5262012-09-21 12:27:57 -04002255
2256void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002257 if (!Has_Android_Secure)
2258 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002259 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002260 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002261 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002262 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002263 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002264 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002265 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002266 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002267 }
2268}
bigbiff7cb4c332014-11-26 20:36:07 -05002269
2270uint64_t TWPartition::Get_Max_FileSize() {
2271 uint64_t maxFileSize = 0;
2272 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2273 const uint64_t constTB = (uint64_t) constGB * 1024;
2274 const uint64_t constPB = (uint64_t) constTB * 1024;
2275 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002276 if (Current_File_System == "ext4")
2277 maxFileSize = 16 * constTB; //16 TB
2278 else if (Current_File_System == "vfat")
2279 maxFileSize = 4 * constGB; //4 GB
2280 else if (Current_File_System == "ntfs")
2281 maxFileSize = 256 * constTB; //256 TB
2282 else if (Current_File_System == "exfat")
2283 maxFileSize = 16 * constPB; //16 PB
2284 else if (Current_File_System == "ext3")
2285 maxFileSize = 2 * constTB; //2 TB
2286 else if (Current_File_System == "f2fs")
2287 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002288 else
2289 maxFileSize = 100000000L;
2290 return maxFileSize - 1;
2291}
2292
Ethan Yonker96af84a2015-01-05 14:58:36 -06002293bool TWPartition::Flash_Image(string Filename) {
2294 string Restore_File_System;
2295
2296 LOGINFO("Image filename is: %s\n", Filename.c_str());
2297
2298 if (Backup_Method == FILES) {
2299 LOGERR("Cannot flash images to file systems\n");
2300 return false;
2301 } else if (!Can_Flash_Img) {
2302 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2303 return false;
2304 } else {
2305 if (!Find_Partition_Size()) {
2306 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2307 return false;
2308 }
2309 unsigned long long image_size = TWFunc::Get_File_Size(Filename);
2310 if (image_size > Size) {
2311 LOGERR("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
2312 image_size, Filename.c_str(), Actual_Block_Device.c_str(), Size);
2313 return false;
2314 }
2315 if (Backup_Method == DD)
2316 return Flash_Image_DD(Filename);
2317 else if (Backup_Method == FLASH_UTILS)
2318 return Flash_Image_FI(Filename);
2319 }
2320
2321 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2322 return false;
2323}
2324
2325bool TWPartition::Flash_Image_DD(string Filename) {
2326 string Command;
2327
2328 gui_print("Flashing %s...\n", Display_Name.c_str());
codelover352b75e2015-03-29 02:44:07 +08002329 Command = "dd bs=8388608 if='" + Filename + "' of=" + Actual_Block_Device;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002330 LOGINFO("Flash command: '%s'\n", Command.c_str());
2331 TWFunc::Exec_Cmd(Command);
2332 return true;
2333}
2334
2335bool TWPartition::Flash_Image_FI(string Filename) {
2336 string Command;
2337
2338 gui_print("Flashing %s...\n", Display_Name.c_str());
2339 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2340 Command = "erase_image " + MTD_Name;
2341 LOGINFO("Erase command: '%s'\n", Command.c_str());
2342 TWFunc::Exec_Cmd(Command);
2343 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2344 LOGINFO("Flash command: '%s'\n", Command.c_str());
2345 TWFunc::Exec_Cmd(Command);
2346 return true;
2347}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002348
2349void TWPartition::Change_Mount_Read_Only(bool new_value) {
2350 Mount_Read_Only = new_value;
2351}
2352
2353int TWPartition::Check_Lifetime_Writes() {
2354 bool original_read_only = Mount_Read_Only;
2355 int ret = 1;
2356
2357 Mount_Read_Only = true;
2358 if (Mount(false)) {
2359 Find_Actual_Block_Device();
2360 string block = basename(Actual_Block_Device.c_str());
2361 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2362 string result;
2363 if (TWFunc::Path_Exists(file)) {
2364 if (TWFunc::read_file(file, result) != 0) {
2365 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2366 } else {
2367 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2368 if (result == "0") {
2369 ret = 0;
2370 }
2371 }
2372 } else {
2373 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2374 }
2375 UnMount(true);
2376 } else {
2377 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2378 }
2379 Mount_Read_Only = original_read_only;
2380 return ret;
2381}