blob: df408e6b7ad067b497e10d0b048bae6b3f26e975 [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"
Ethan Yonker74db1572015-10-28 12:44:49 -050046#include "gui/gui.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040047extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040048 #include "mtdutils/mtdutils.h"
49 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000050#ifdef USE_EXT4
51 #include "make_ext4fs.h"
52#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060053
54#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060055 #include "crypto/lollipop/cryptfs.h"
Ethan Yonkerbc85b632015-08-09 12:48:14 -050056#else
57 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060058#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040059}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040060#ifdef HAVE_SELINUX
61#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060062#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040063#endif
Dees Troy4159aed2014-02-28 17:24:43 +000064#ifdef HAVE_CAPABILITIES
65#include <sys/capability.h>
66#include <sys/xattr.h>
67#include <linux/xattr.h>
68#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040069
bigbiff bigbiff9c754052013-01-09 09:09:08 -050070using namespace std;
71
Dees_Troya95f55c2013-08-17 13:14:43 +000072extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050073extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000074
Hashcode62bd9e02013-11-19 21:59:42 -080075struct flag_list {
76 const char *name;
77 unsigned flag;
78};
79
80static struct flag_list mount_flags[] = {
81 { "noatime", MS_NOATIME },
82 { "noexec", MS_NOEXEC },
83 { "nosuid", MS_NOSUID },
84 { "nodev", MS_NODEV },
85 { "nodiratime", MS_NODIRATIME },
86 { "ro", MS_RDONLY },
87 { "rw", 0 },
88 { "remount", MS_REMOUNT },
89 { "bind", MS_BIND },
90 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000091#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080092 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000093#endif
94#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080095 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000096#endif
97#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -080098 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000099#endif
100#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800101 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000102#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800103 { "sync", MS_SYNCHRONOUS },
104 { "defaults", 0 },
105 { 0, 0 },
106};
107
that9e0593e2014-10-08 00:01:24 +0200108TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400109 Can_Be_Mounted = false;
110 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500111 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200112 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400113 Wipe_During_Factory_Reset = false;
114 Wipe_Available_in_GUI = false;
115 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400116 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400117 SubPartition_Of = "";
118 Symlink_Path = "";
119 Symlink_Mount_Point = "";
120 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400121 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400122 Actual_Block_Device = "";
123 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400124 Alternate_Block_Device = "";
125 Removable = false;
126 Is_Present = false;
127 Length = 0;
128 Size = 0;
129 Used = 0;
130 Free = 0;
131 Backup_Size = 0;
132 Can_Be_Encrypted = false;
133 Is_Encrypted = false;
134 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600135 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400136 Decrypted_Block_Device = "";
137 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500138 Backup_Display_Name = "";
139 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400140 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400141 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400142 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400143 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000144 Can_Encrypt_Backup = false;
145 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400146 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400147 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400148 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500149 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400150 Storage_Path = "";
151 Current_File_System = "";
152 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800153 Mount_Flags = 0;
154 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400155 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000156 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000157 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600158 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600159 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600160 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500161 Mount_Read_Only = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400162}
163
164TWPartition::~TWPartition(void) {
165 // Do nothing
166}
167
Dees_Troy5bf43922012-09-07 16:07:55 -0400168bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
169 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
170 int line_len = Line.size(), index = 0, item_index = 0;
171 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400172 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400173 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500174 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400175
Dees_Troy51127312012-09-08 13:08:49 -0400176 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500177 if (full_line[index] == 34)
178 skip = !skip;
179 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400180 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400181 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400182 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000183 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400184 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500185 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000186 Display_Name = full_line + 1;
187 Backup_Display_Name = Display_Name;
188 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400189 index = Mount_Point.size();
190 while (index < line_len) {
191 while (index < line_len && full_line[index] == '\0')
192 index++;
193 if (index >= line_len)
194 continue;
195 ptr = full_line + index;
196 if (item_index == 0) {
197 // File System
198 Fstab_File_System = ptr;
199 Current_File_System = ptr;
200 item_index++;
201 } else if (item_index == 1) {
202 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400203 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400204 MTD_Name = ptr;
205 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400206 } else if (Fstab_File_System == "bml") {
207 if (Mount_Point == "/boot")
208 MTD_Name = "boot";
209 else if (Mount_Point == "/recovery")
210 MTD_Name = "recovery";
211 Primary_Block_Device = ptr;
212 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000213 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 -0400214 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400215 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000216 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400217 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000218 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400219 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400220 } else {
221 Primary_Block_Device = ptr;
222 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400223 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400224 item_index++;
225 } else if (item_index > 1) {
226 if (*ptr == '/') {
227 // Alternate Block Device
228 Alternate_Block_Device = ptr;
229 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
230 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
231 // Partition length
232 ptr += 7;
233 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400234 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
235 // Custom flags, save for later so that new values aren't overwritten by defaults
236 ptr += 6;
237 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000238 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400239 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
240 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400241 } else {
242 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000243 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400244 }
245 }
246 while (index < line_len && full_line[index] != '\0')
247 index++;
248 }
249
250 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
251 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000252 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400253 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000254 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400255 return 0;
256 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400257 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400258 Setup_File_System(Display_Error);
259 if (Mount_Point == "/system") {
260 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500261 Backup_Display_Name = Display_Name;
262 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400263 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500264 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500265 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400266 } else if (Mount_Point == "/data") {
Dees Troyc657cc02015-01-16 22:48:47 +0000267 UnMount(false); // added in case /data is mounted as tmpfs for qcom hardware decrypt
Dees_Troy5bf43922012-09-07 16:07:55 -0400268 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500269 Backup_Display_Name = Display_Name;
270 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400271 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400272 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500273 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000274 Can_Encrypt_Backup = true;
275 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500276 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200277 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400278#ifdef TW_INCLUDE_CRYPTO
279 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400280 char crypto_blkdev[255];
281 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
282 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400283 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400284 DataManager::SetValue(TW_IS_DECRYPTED, 1);
285 Is_Encrypted = true;
286 Is_Decrypted = true;
287 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000288 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400289 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600290 if (Is_Present) {
Ethan Yonker253368a2014-11-25 15:00:52 -0600291 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 -0600292 if (cryptfs_check_footer() == 0) {
293 Is_Encrypted = true;
294 Is_Decrypted = false;
295 Can_Be_Mounted = false;
296 Current_File_System = "emmc";
297 Setup_Image(Display_Error);
298 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
Vojtech Bocek7e11ac52015-03-05 23:21:49 +0100299 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
Ethan Yonker71413f42014-02-26 13:36:08 -0600300 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
301 DataManager::SetValue("tw_crypto_display", "");
302 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500303 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
Ethan Yonker71413f42014-02-26 13:36:08 -0600304 }
305 } else {
306 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
307 }
Gary Peck82599a82012-11-21 16:23:12 -0800308 } else {
309 // Filesystem is not encrypted and the mount
310 // succeeded, so get it back to the original
311 // unmounted state
312 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400313 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500314 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400315 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400316#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500317 if (datamedia)
318 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400319#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400320 } else if (Mount_Point == "/cache") {
321 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500322 Backup_Display_Name = Display_Name;
323 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400324 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400325 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500326 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400327 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000328 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500329 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500330 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400331 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400332 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400333 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400334 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500335 Backup_Display_Name = Display_Name;
336 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400337 Is_SubPartition = true;
338 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400339 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500340 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000341 Can_Encrypt_Backup = true;
342 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400343 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400344 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400345 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500346 Backup_Display_Name = Display_Name;
347 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400348 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400349 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500350 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000351 Can_Encrypt_Backup = true;
352 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400353 } else if (Mount_Point == "/boot") {
354 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500355 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400356 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500357 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500358 } else if (Mount_Point == "/vendor") {
359 Display_Name = "Vendor";
360 Backup_Display_Name = Display_Name;
361 Storage_Name = Display_Name;
362 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400363 }
364#ifdef TW_EXTERNAL_STORAGE_PATH
365 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
366 Is_Storage = true;
367 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400368 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500369 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400370#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000371 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400372 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400373 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500374 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400375#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000376 }
Dees_Troy8170a922012-09-18 15:40:25 -0400377#ifdef TW_INTERNAL_STORAGE_PATH
378 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
379 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500380 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400381 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500382 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400383 }
384#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000385 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400386 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500387 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500388 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400389 }
390#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400391 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400392 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400393 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500394 if (Mount_Point == "/boot") {
395 Display_Name = "Boot";
396 Backup_Display_Name = Display_Name;
397 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600398 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500399 } else if (Mount_Point == "/recovery") {
400 Display_Name = "Recovery";
401 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600402 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500403 } else if (Mount_Point == "/system_image") {
404 Display_Name = "System Image";
405 Backup_Display_Name = Display_Name;
406 Can_Flash_Img = false;
407 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500408 } else if (Mount_Point == "/vendor_image") {
409 Display_Name = "Vendor Image";
410 Backup_Display_Name = Display_Name;
411 Can_Flash_Img = false;
412 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500413 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400414 }
415
Dees_Troy51127312012-09-08 13:08:49 -0400416 // Process any custom flags
417 if (Flags.size() > 0)
418 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400419 return true;
420}
421
Ketut Putu Kumajayabfb72b02015-08-10 16:40:55 +0200422bool TWPartition::Process_FS_Flags(string& Options, int& Flags) {
Hashcode62bd9e02013-11-19 21:59:42 -0800423 int i;
424 char *p;
425 char *savep;
426 char fs_options[250];
427
428 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
429 Options = "";
430
431 p = strtok_r(fs_options, ",", &savep);
432 while (p) {
433 /* Look for the flag "p" in the flag list "fl"
434 * If not found, the loop exits with fl[i].name being null.
435 */
436 for (i = 0; mount_flags[i].name; i++) {
437 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
438 Flags |= mount_flags[i].flag;
439 break;
440 }
441 }
442
443 if (!mount_flags[i].name) {
444 if (Options.size() > 0)
445 Options += ",";
446 Options += p;
447 }
448 p = strtok_r(NULL, ",", &savep);
449 }
450
451 return true;
452}
453
Dees_Troy51127312012-09-08 13:08:49 -0400454bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
455 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500456 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400457 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500458 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400459
460 strcpy(flags, Flags.c_str());
461 flags_len = Flags.size();
462 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500463 if (flags[index] == 34)
464 skip = !skip;
465 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400466 flags[index] = '\0';
467 }
468
469 index = 0;
470 while (index < flags_len) {
471 while (index < flags_len && flags[index] == '\0')
472 index++;
473 if (index >= flags_len)
474 continue;
475 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500476 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400477 if (strcmp(ptr, "removable") == 0) {
478 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600479 } else if (strncmp(ptr, "storage", 7) == 0) {
480 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600481 Is_Storage = true;
482 } else if (ptr_len == 9) {
483 ptr += 9;
484 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
485 LOGINFO("storage set to true\n");
486 Is_Storage = true;
487 } else {
488 LOGINFO("storage set to false\n");
489 Is_Storage = false;
490 }
491 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500492 } else if (strcmp(ptr, "settingsstorage") == 0) {
493 Is_Storage = true;
Xueferbfce5042015-04-14 02:02:05 +0800494 Is_Settings_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500495 } else if (strcmp(ptr, "andsec") == 0) {
496 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400497 } else if (strcmp(ptr, "canbewiped") == 0) {
498 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700499 } else if (strcmp(ptr, "usermrf") == 0) {
500 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500501 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
502 ptr += 7;
503 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
504 Can_Be_Backed_Up = true;
505 else
506 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400507 } else if (strcmp(ptr, "wipeingui") == 0) {
508 Can_Be_Wiped = true;
509 Wipe_Available_in_GUI = true;
510 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
511 Can_Be_Wiped = true;
512 Wipe_Available_in_GUI = true;
513 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500514 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000515 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400516 Is_SubPartition = true;
517 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000518 } else if (strcmp(ptr, "ignoreblkid") == 0) {
519 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000520 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
521 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500522 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400523 ptr += 8;
524 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500525 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
526 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400527 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500528 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400529 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500530 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
531 Display_Name.resize(Display_Name.size() - 1);
532 }
533 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
534 has_storage_name = true;
535 ptr += 11;
536 if (*ptr == '\"') ptr++;
537 Storage_Name = ptr;
538 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
539 Storage_Name.resize(Storage_Name.size() - 1);
540 }
541 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
542 has_backup_name = true;
543 ptr += 10;
544 if (*ptr == '\"') ptr++;
545 Backup_Display_Name = ptr;
546 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
547 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
548 }
549 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400550 ptr += 10;
Ethan Yonkera2719152015-05-28 09:44:41 -0500551 Format_Block_Size = (unsigned long)(atol(ptr));
Dees_Troya13d74f2013-03-24 08:54:55 -0500552 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400553 ptr += 7;
554 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000555 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
556 ptr += 17;
557 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
558 Can_Encrypt_Backup = true;
559 else
560 Can_Encrypt_Backup = false;
561 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
562 ptr += 21;
563 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
564 Can_Encrypt_Backup = true;
565 Use_Userdata_Encryption = true;
566 } else {
567 Use_Userdata_Encryption = false;
568 }
Hashcode62bd9e02013-11-19 21:59:42 -0800569 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
570 ptr += 8;
571 if (*ptr == '\"') ptr++;
572
573 Mount_Options = ptr;
574 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
575 Mount_Options.resize(Mount_Options.size() - 1);
576 }
577 Process_FS_Flags(Mount_Options, Mount_Flags);
Ethan Yonker253368a2014-11-25 15:00:52 -0600578 } else if ((ptr_len > 12 && strncmp(ptr, "encryptable=", 12) == 0) || (ptr_len > 13 && strncmp(ptr, "forceencrypt=", 13) == 0)) {
579 ptr += 12;
580 if (*ptr == '=') ptr++;
581 if (*ptr == '\"') ptr++;
582
583 Crypto_Key_Location = ptr;
584 if (Crypto_Key_Location.substr(Crypto_Key_Location.size() - 1, 1) == "\"") {
585 Crypto_Key_Location.resize(Crypto_Key_Location.size() - 1);
586 }
587 } else if (ptr_len > 8 && strncmp(ptr, "mounttodecrypt", 14) == 0) {
588 Mount_To_Decrypt = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600589 } else if (strncmp(ptr, "flashimg", 8) == 0) {
590 if (ptr_len == 8) {
591 Can_Flash_Img = true;
592 } else if (ptr_len == 10) {
593 ptr += 9;
594 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
595 Can_Flash_Img = true;
596 } else {
597 Can_Flash_Img = false;
598 }
599 }
Dees_Troy51127312012-09-08 13:08:49 -0400600 } else {
601 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000602 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400603 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000604 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400605 }
606 while (index < flags_len && flags[index] != '\0')
607 index++;
608 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500609 if (has_display_name && !has_storage_name)
610 Storage_Name = Display_Name;
611 if (!has_display_name && has_storage_name)
612 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000613 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500614 Backup_Display_Name = Display_Name;
615 if (!has_display_name && has_backup_name)
616 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400617 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400618}
619
Dees_Troy5bf43922012-09-07 16:07:55 -0400620bool TWPartition::Is_File_System(string File_System) {
621 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400622 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400623 File_System == "ext4" ||
624 File_System == "vfat" ||
625 File_System == "ntfs" ||
626 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500627 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000628 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400629 File_System == "auto")
630 return true;
631 else
632 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400633}
634
Dees_Troy5bf43922012-09-07 16:07:55 -0400635bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400636 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400637 return true;
638 else
639 return false;
640}
641
Dees_Troy51127312012-09-08 13:08:49 -0400642bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400643 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400644 if (mkdir(Path.c_str(), 0777) == -1) {
645 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500646 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400647 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000648 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400649 return false;
650 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000651 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400652 return true;
653 }
654 }
655 return true;
656}
657
Dees_Troy5bf43922012-09-07 16:07:55 -0400658void TWPartition::Setup_File_System(bool Display_Error) {
659 struct statfs st;
660
661 Can_Be_Mounted = true;
662 Can_Be_Wiped = true;
663
Dees_Troy5bf43922012-09-07 16:07:55 -0400664 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400665 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400666 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
667 Backup_Name = Display_Name;
668 Backup_Method = FILES;
669}
670
671void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400672 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
673 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800674 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400675 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800676 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400677 Backup_Method = FLASH_UTILS;
678 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000679 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 -0400680 if (Find_Partition_Size()) {
681 Used = Size;
682 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400683 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400684 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000685 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400686 else
Dees Troyd932ce12013-10-18 17:12:59 +0000687 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400688 }
689}
690
Dees_Troye58d5262012-09-21 12:27:57 -0400691void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500692 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400693 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500694 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400695 Has_Android_Secure = true;
696 Symlink_Path = Mount_Point + "/.android_secure";
697 Symlink_Mount_Point = "/and-sec";
698 Backup_Path = Symlink_Mount_Point;
699 Make_Dir("/and-sec", true);
700 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600701 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400702}
703
that9e0593e2014-10-08 00:01:24 +0200704void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500705 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
706 Storage_Name = "Internal Storage";
707 Has_Data_Media = true;
708 Is_Storage = true;
709 Is_Settings_Storage = true;
710 Storage_Path = "/data/media";
711 Symlink_Path = Storage_Path;
712 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
713 Make_Dir("/emmc", false);
714 Symlink_Mount_Point = "/emmc";
715 } else {
716 Make_Dir("/sdcard", false);
717 Symlink_Mount_Point = "/sdcard";
718 }
719 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
720 Storage_Path = "/data/media/0";
721 Symlink_Path = Storage_Path;
722 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
723 UnMount(true);
724 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500725 DataManager::SetValue("tw_has_internal", 1);
726 DataManager::SetValue("tw_has_data_media", 1);
727 du.add_absolute_dir("/data/media");
728}
729
Dees_Troy5bf43922012-09-07 16:07:55 -0400730void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
731 char device[512], realDevice[512];
732
733 strcpy(device, Block.c_str());
734 memset(realDevice, 0, sizeof(realDevice));
735 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
736 {
737 strcpy(device, realDevice);
738 memset(realDevice, 0, sizeof(realDevice));
739 }
740
741 if (device[0] != '/') {
742 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000743 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400744 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000745 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400746 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400747 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400748 Block = device;
749 return;
750 }
751}
752
Dees_Troy8e337f32012-10-13 22:07:49 -0400753void TWPartition::Mount_Storage_Retry(void) {
754 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500755 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400756 int retry_count = 5;
757 while (retry_count > 0 && !Mount(false)) {
758 usleep(500000);
759 retry_count--;
760 }
761 Mount(true);
762 }
763}
764
Dees_Troy38bd7602012-09-14 13:33:53 -0400765bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
766 FILE *fp = NULL;
767 char line[255];
768
769 fp = fopen("/proc/mtd", "rt");
770 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000771 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400772 return false;
773 }
774
775 while (fgets(line, sizeof(line), fp) != NULL)
776 {
777 char device[32], label[32];
778 unsigned long size = 0;
779 char* fstype = NULL;
780 int deviceId;
781
782 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
783
784 // Skip header and blank lines
785 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
786 continue;
787
788 // Strip off the trailing " from the label
789 label[strlen(label)-1] = '\0';
790
791 if (strcmp(label, MTD_Name.c_str()) == 0) {
792 // We found our device
793 // Strip off the trailing : from the device
794 device[strlen(device)-1] = '\0';
795 if (sscanf(device,"mtd%d", &deviceId) == 1) {
796 sprintf(device, "/dev/block/mtdblock%d", deviceId);
797 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000798 fclose(fp);
799 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400800 }
801 }
802 }
803 fclose(fp);
804
805 return false;
806}
807
Dees_Troy51127312012-09-08 13:08:49 -0400808bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
809 struct statfs st;
810 string Local_Path = Mount_Point + "/.";
811
812 if (!Mount(Display_Error))
813 return false;
814
815 if (statfs(Local_Path.c_str(), &st) != 0) {
816 if (!Removable) {
817 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000818 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400819 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000820 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400821 }
822 return false;
823 }
824 Size = (st.f_blocks * st.f_bsize);
825 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
826 Free = (st.f_bfree * st.f_bsize);
827 Backup_Size = Used;
828 return true;
829}
830
831bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400832 FILE* fp;
833 char command[255], line[512];
834 int include_block = 1;
835 unsigned int min_len;
836
837 if (!Mount(Display_Error))
838 return false;
839
Dees_Troy38bd7602012-09-14 13:33:53 -0400840 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400841 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200842 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400843 fp = fopen("/tmp/dfoutput.txt", "rt");
844 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000845 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400846 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400847 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400848
849 while (fgets(line, sizeof(line), fp) != NULL)
850 {
851 unsigned long blocks, used, available;
852 char device[64];
853 char tmpString[64];
854
855 if (strncmp(line, "Filesystem", 10) == 0)
856 continue;
857 if (strlen(line) < min_len) {
858 include_block = 0;
859 continue;
860 }
861 if (include_block) {
862 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
863 } else {
864 // The device block string is so long that the df information is on the next line
865 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400866 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400867 while (tmpString[space_count] == 32)
868 space_count++;
869 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
870 }
871
872 // Adjust block size to byte size
873 Size = blocks * 1024ULL;
874 Used = used * 1024ULL;
875 Free = available * 1024ULL;
876 Backup_Size = Used;
877 }
878 fclose(fp);
879 return true;
880}
881
Dees_Troy5bf43922012-09-07 16:07:55 -0400882bool TWPartition::Find_Partition_Size(void) {
883 FILE* fp;
884 char line[512];
885 string tmpdevice;
886
igoriok87e3d932013-01-31 21:03:53 +0200887 fp = fopen("/proc/dumchar_info", "rt");
888 if (fp != NULL) {
889 while (fgets(line, sizeof(line), fp) != NULL)
890 {
891 char label[32], device[32];
892 unsigned long size = 0;
893
thatd43bf2d2014-09-21 23:13:02 +0200894 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200895
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500896 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200897 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
898 continue;
899
900 tmpdevice = "/dev/";
901 tmpdevice += label;
902 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
903 Size = size;
904 fclose(fp);
905 return true;
906 }
907 }
908 }
909
Dees_Troy5bf43922012-09-07 16:07:55 -0400910 // In this case, we'll first get the partitions we care about (with labels)
911 fp = fopen("/proc/partitions", "rt");
912 if (fp == NULL)
913 return false;
914
915 while (fgets(line, sizeof(line), fp) != NULL)
916 {
917 unsigned long major, minor, blocks;
918 char device[512];
919 char tmpString[64];
920
Dees_Troy63c8df72012-09-10 14:02:05 -0400921 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400922 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
923
924 tmpdevice = "/dev/block/";
925 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400926 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400927 // Adjust block size to byte size
928 Size = blocks * 1024ULL;
929 fclose(fp);
930 return true;
931 }
932 }
933 fclose(fp);
934 return false;
935}
936
Dees_Troy5bf43922012-09-07 16:07:55 -0400937bool TWPartition::Is_Mounted(void) {
938 if (!Can_Be_Mounted)
939 return false;
940
941 struct stat st1, st2;
942 string test_path;
943
944 // Check to see if the mount point directory exists
945 test_path = Mount_Point + "/.";
946 if (stat(test_path.c_str(), &st1) != 0) return false;
947
948 // Check to see if the directory above the mount point exists
949 test_path = Mount_Point + "/../.";
950 if (stat(test_path.c_str(), &st2) != 0) return false;
951
952 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
953 int ret = (st1.st_dev != st2.st_dev) ? true : false;
954
955 return ret;
956}
957
958bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000959 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500960 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000961
Dees_Troy5bf43922012-09-07 16:07:55 -0400962 if (Is_Mounted()) {
963 return true;
964 } else if (!Can_Be_Mounted) {
965 return false;
966 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400967
968 Find_Actual_Block_Device();
969
970 // Check the current file system before mounting
971 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000972 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000973 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 +0000974 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000975 string result;
976 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000977 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000978 Current_File_System = "vfat";
979 } else {
980#ifdef TW_NO_EXFAT_FUSE
981 UnMount(false);
982 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
983 // Some kernels let us mount vfat as exfat which doesn't work out too well
984#else
985 exfat_mounted = 1;
986#endif
987 }
988 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500989
Ethan Yonkerb81d9052015-07-09 13:20:53 -0500990 if (Current_File_System == "ntfs" && TWFunc::Path_Exists("/sbin/ntfs-3g")) {
991 string cmd;
992 if (Mount_Read_Only)
993 cmd = "/sbin/ntfs-3g -o ro " + Actual_Block_Device + " " + Mount_Point;
994 else
995 cmd = "/sbin/ntfs-3g " + Actual_Block_Device + " " + Mount_Point;
996 LOGINFO("cmd: '%s'\n", cmd.c_str());
997 if (TWFunc::Exec_Cmd(cmd) == 0) {
998 return true;
999 } else {
1000 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1001 }
1002 }
1003
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001004 if (Mount_Read_Only)
1005 flags |= MS_RDONLY;
1006
Dees_Troy22042032012-12-18 21:23:08 +00001007 if (Fstab_File_System == "yaffs2") {
1008 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001009 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1010 if (Mount_Read_Only)
1011 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001012 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1013 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1014 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001015 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001016 else
1017 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1018 return false;
1019 } else {
1020 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1021 return true;
1022 }
1023 } else {
1024 struct stat st;
1025 string test_path = Mount_Point;
1026 if (stat(test_path.c_str(), &st) < 0) {
1027 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001028 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001029 else
1030 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1031 return false;
1032 }
1033 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1034 if (new_mode != st.st_mode) {
1035 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1036 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1037 if (Display_Error)
1038 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1039 else
1040 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1041 return false;
1042 }
1043 }
Dees_Troy22042032012-12-18 21:23:08 +00001044 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001045 }
thatc7572eb2015-03-31 18:55:30 +02001046 }
1047
1048 string mount_fs = Current_File_System;
1049 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1050 mount_fs = "texfat";
1051
1052 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001053 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1054 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001055#ifdef TW_NO_EXFAT_FUSE
1056 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001057 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001058 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001059 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001060 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001061 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001062 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001063 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 +00001064 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001065 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001066 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001067#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001068 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001069 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001070 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001071 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1072 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 +00001073 return false;
1074#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001075 }
1076#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001077 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001078
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001079 if (Removable)
1080 Update_Size(Display_Error);
1081
xiaolu9416f4f2015-06-04 08:22:23 +08001082 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001083 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001084 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001085 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001086 return true;
1087}
1088
1089bool TWPartition::UnMount(bool Display_Error) {
1090 if (Is_Mounted()) {
1091 int never_unmount_system;
1092
1093 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1094 if (never_unmount_system == 1 && Mount_Point == "/system")
1095 return true; // Never unmount system if you're not supposed to unmount it
1096
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001097 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001098 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001099
Dees_Troy38bd7602012-09-14 13:33:53 -04001100 if (!Symlink_Mount_Point.empty())
1101 umount(Symlink_Mount_Point.c_str());
1102
Dees_Troyb05ddee2013-01-28 20:24:50 +00001103 umount(Mount_Point.c_str());
1104 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001105 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001106 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001107 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001108 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001109 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001110 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001111 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001112 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001113 } else {
1114 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001115 }
1116}
1117
Gary Peck43acadf2012-11-21 21:19:01 -08001118bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001119 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001120 int check;
1121 string Layout_Filename = Mount_Point + "/.layout_version";
1122
Dees_Troy38bd7602012-09-14 13:33:53 -04001123 if (!Can_Be_Wiped) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001124 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.\n")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001125 return false;
1126 }
1127
Dees_Troyc51f1f92012-09-20 15:32:13 -04001128 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001129 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001130
Dees_Troy16c2b312013-01-15 16:51:18 +00001131 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1132 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1133 else
1134 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001135
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001136 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001137 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001138 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001139 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001140 DataManager::GetValue(TW_RM_RF_VAR, check);
1141
Hashcodedabfd492013-08-29 22:45:30 -07001142 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001143 wiped = Wipe_RMRF();
1144 else if (New_File_System == "ext4")
1145 wiped = Wipe_EXT4();
1146 else if (New_File_System == "ext2" || New_File_System == "ext3")
1147 wiped = Wipe_EXT23(New_File_System);
1148 else if (New_File_System == "vfat")
1149 wiped = Wipe_FAT();
1150 else if (New_File_System == "exfat")
1151 wiped = Wipe_EXFAT();
1152 else if (New_File_System == "yaffs2")
1153 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001154 else if (New_File_System == "f2fs")
1155 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001156 else if (New_File_System == "ntfs")
1157 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001158 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001159 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 +00001160 unlink("/.layout_version");
1161 return false;
1162 }
1163 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001164 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001165
Gary Pecke8bc5d72012-12-21 06:45:25 -08001166 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001167 if (Mount_Point == "/cache")
1168 DataManager::Output_Version();
1169
Dees_Troy16c2b312013-01-15 16:51:18 +00001170 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1171 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1172
1173 if (update_crypt) {
1174 Setup_File_System(false);
1175 if (Is_Encrypted && !Is_Decrypted) {
1176 // just wiped an encrypted partition back to its unencrypted state
1177 Is_Encrypted = false;
1178 Is_Decrypted = false;
1179 Decrypted_Block_Device = "";
1180 if (Mount_Point == "/data") {
1181 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1182 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1183 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001184 }
1185 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001186
1187 if (Mount_Point == "/data" && Has_Data_Media && recreate_media) {
1188 Recreate_Media_Folder();
1189 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001190 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001191 if (Is_Storage) {
1192 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001193 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001194 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001195}
1196
Gary Peck43acadf2012-11-21 21:19:01 -08001197bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001198 if (Is_File_System(Current_File_System))
1199 return Wipe(Current_File_System);
1200 else
1201 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001202}
1203
Dees_Troye58d5262012-09-21 12:27:57 -04001204bool TWPartition::Wipe_AndSec(void) {
1205 if (!Has_Android_Secure)
1206 return false;
1207
Dees_Troye58d5262012-09-21 12:27:57 -04001208 if (!Mount(true))
1209 return false;
1210
Ethan Yonker74db1572015-10-28 12:44:49 -05001211 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001212 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001213 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001214}
1215
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001216bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001217 if (Mount_Read_Only)
1218 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001219 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001220 return true;
1221 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1222 return true;
1223 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1224 return true;
1225 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1226 return true;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001227 else if (Current_File_System == "ntfs" && TWFunc::Path_Exists("/sbin/ntfsfix"))
1228 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001229 return false;
1230}
1231
1232bool TWPartition::Repair() {
1233 string command;
1234
1235 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001236 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001237 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001238 return false;
1239 }
1240 if (!UnMount(true))
1241 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001242 gui_msg(Msg("reparing=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001243 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001244 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001245 LOGINFO("Repair command: %s\n", command.c_str());
1246 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001247 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001248 return true;
1249 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001250 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001251 return false;
1252 }
1253 }
1254 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1255 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001256 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001257 return false;
1258 }
1259 if (!UnMount(true))
1260 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001261 gui_msg(Msg("reparing=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001262 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001263 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001264 LOGINFO("Repair command: %s\n", command.c_str());
1265 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001266 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001267 return true;
1268 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001269 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001270 return false;
1271 }
1272 }
1273 if (Current_File_System == "exfat") {
1274 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001275 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001276 return false;
1277 }
1278 if (!UnMount(true))
1279 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001280 gui_msg(Msg("reparing=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001281 Find_Actual_Block_Device();
1282 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1283 LOGINFO("Repair command: %s\n", command.c_str());
1284 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001285 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001286 return true;
1287 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001288 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001289 return false;
1290 }
1291 }
1292 if (Current_File_System == "f2fs") {
1293 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001294 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001295 return false;
1296 }
1297 if (!UnMount(true))
1298 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001299 gui_msg(Msg("reparing=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001300 Find_Actual_Block_Device();
1301 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1302 LOGINFO("Repair command: %s\n", command.c_str());
1303 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001304 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001305 return true;
1306 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001307 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001308 return false;
1309 }
1310 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001311 if (Current_File_System == "ntfs") {
1312 if (!TWFunc::Path_Exists("/sbin/ntfsfix")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001313 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001314 return false;
1315 }
1316 if (!UnMount(true))
1317 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001318 gui_msg(Msg("reparing=Repairing {1} using {2}...")(Display_Name)("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001319 Find_Actual_Block_Device();
1320 command = "/sbin/ntfsfix " + Actual_Block_Device;
1321 LOGINFO("Repair command: %s\n", command.c_str());
1322 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001323 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001324 return true;
1325 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001326 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001327 return false;
1328 }
1329 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001330 return false;
1331}
1332
Ethan Yonkera2719152015-05-28 09:44:41 -05001333bool TWPartition::Can_Resize() {
1334 if (Mount_Read_Only)
1335 return false;
1336 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1337 return true;
1338 return false;
1339}
1340
1341bool TWPartition::Resize() {
1342 string command;
1343
1344 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1345 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001346 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1347 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001348 return false;
1349 }
1350 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001351 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1352 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001353 return false;
1354 }
1355 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001356 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001357 if (!Repair())
1358 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001359 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001360 Find_Actual_Block_Device();
1361 command = "/sbin/resize2fs " + Actual_Block_Device;
1362 if (Length != 0) {
1363 unsigned int block_device_size;
1364 int fd, ret;
1365
1366 fd = open(Actual_Block_Device.c_str(), O_RDONLY);
1367 if (fd < 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001368 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Actual_Block_Device)(strerror(errno)));
Ethan Yonkera2719152015-05-28 09:44:41 -05001369 return false;
1370 }
1371 ret = ioctl(fd, BLKGETSIZE, &block_device_size);
1372 close(fd);
1373 if (ret) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001374 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
1375 LOGINFO("Resize: ioctl error\n");
Ethan Yonkera2719152015-05-28 09:44:41 -05001376 return false;
1377 }
1378 unsigned long long Actual_Size = (unsigned long long)(block_device_size) * 512LLU;
1379 unsigned long long Block_Count;
1380 if (Length < 0) {
1381 // Reduce overall size by this length
1382 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1383 } else {
1384 // This is the size, not a size reduction
1385 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1386 }
1387 char temp[256];
1388 sprintf(temp, "%llu", Block_Count);
1389 command += " ";
1390 command += temp;
1391 command += "K";
1392 }
1393 LOGINFO("Resize command: %s\n", command.c_str());
1394 if (TWFunc::Exec_Cmd(command) == 0) {
1395 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001396 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001397 return true;
1398 } else {
1399 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001400 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001401 return false;
1402 }
1403 }
1404 return false;
1405}
1406
bigbiff7abc5fe2015-01-17 16:53:12 -05001407bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
1408 if (Backup_Method == FILES) {
1409 return Backup_Tar(backup_folder, overall_size, other_backups_size, tar_fork_pid);
1410 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001411 else if (Backup_Method == DD)
1412 return Backup_DD(backup_folder);
1413 else if (Backup_Method == FLASH_UTILS)
1414 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001415 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001416 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001417}
1418
Dees_Troy43d8b002012-09-17 16:00:01 -04001419bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001420 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001421 char split_filename[512];
1422 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001423 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001424
Captain Throwbackff5935f2014-09-23 16:08:34 -04001425 sync();
1426
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001427 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001428 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001429 if (!TWFunc::Path_Exists(Full_Filename)) {
1430 // This is a split archive, we presume
1431 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001432 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001433 md5file = split_filename;
1434 md5file += ".md5";
1435 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001436 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(split_filename));
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001437 return false;
1438 }
1439 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001440 while (index < 1000) {
1441 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001442 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001443 return false;
1444 }
1445 index++;
1446 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001447 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001448 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001449 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001450 } else {
1451 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001452 md5file = Full_Filename + ".md5";
1453 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001454 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(split_filename));
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001455 return false;
1456 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001457 md5sum.setfn(Full_Filename);
1458 if (md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001459 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001460 return false;
1461 } else
1462 return true;
1463 }
1464 return false;
1465}
1466
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001467bool 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 -08001468 string Restore_File_System;
1469
Ethan Yonker74db1572015-10-28 12:44:49 -05001470 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring}"));
Dees_Troy2673cec2013-04-02 20:22:16 +00001471 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001472
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001473 Restore_File_System = Get_Restore_File_System(restore_folder);
1474
1475 if (Is_File_System(Restore_File_System))
1476 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1477 else if (Is_Image(Restore_File_System)) {
Ethan Yonkerf66324f2015-01-09 16:46:57 -06001478 return Restore_Image(restore_folder, total_restore_size, already_restored_size, Restore_File_System);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001479 }
1480
1481 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1482 return false;
1483}
1484
1485string TWPartition::Get_Restore_File_System(string restore_folder) {
1486 size_t first_period, second_period;
1487 string Restore_File_System;
1488
Gary Peck43acadf2012-11-21 21:19:01 -08001489 // Parse backup filename to extract the file system before wiping
1490 first_period = Backup_FileName.find(".");
1491 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001492 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001493 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001494 }
1495 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1496 second_period = Restore_File_System.find(".");
1497 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001498 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001499 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001500 }
1501 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001502 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001503 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001504}
1505
1506string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001507 if (Backup_Method == NONE)
1508 return "none";
1509 else if (Backup_Method == FILES)
1510 return "files";
1511 else if (Backup_Method == DD)
1512 return "dd";
1513 else if (Backup_Method == FLASH_UTILS)
1514 return "flash_utils";
1515 else
1516 return "undefined";
1517 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001518}
1519
1520bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001521 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001522 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001523 return 1;
1524}
1525
1526bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001527 bool Save_Data_Media = Has_Data_Media;
1528
1529 if (!UnMount(true))
1530 return false;
1531
Dees_Troy38bd7602012-09-14 13:33:53 -04001532 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001533 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001534#ifdef TW_INCLUDE_CRYPTO
1535 if (Is_Decrypted) {
1536 if (!UnMount(true))
1537 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001538 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001539 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1540 }
1541 }
1542#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001543 Is_Decrypted = false;
1544 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001545 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001546 if (Crypto_Key_Location == "footer") {
1547 int newlen, fd;
1548 if (Length != 0) {
1549 newlen = Length;
1550 if (newlen < 0)
1551 newlen = newlen * -1;
1552 } else {
1553 newlen = CRYPT_FOOTER_OFFSET;
1554 }
1555 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1556 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1557 } else {
1558 unsigned int block_count;
1559 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1560 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1561 } else {
1562 off64_t offset = ((off64_t)block_count * 512) - newlen;
1563 if (lseek64(fd, offset, SEEK_SET) == -1) {
1564 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1565 } else {
1566 void* buffer = malloc(newlen);
1567 if (!buffer) {
1568 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1569 } else {
1570 memset(buffer, 0, newlen);
1571 int ret = write(fd, buffer, newlen);
1572 if (ret != newlen) {
1573 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1574 } else {
1575 LOGINFO("Successfully wiped crypto footer.\n");
1576 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001577 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001578 }
1579 }
1580 }
1581 close(fd);
1582 }
1583 } else {
1584 string Command = "flash_image " + Crypto_Key_Location + " /dev/zero";
1585 TWFunc::Exec_Cmd(Command);
1586 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001587 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001588 Has_Data_Media = Save_Data_Media;
1589 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1590 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001591 if (Mount(false))
1592 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001593 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001594 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001595#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001596 gui_msg("format_data_msg=You may need to reboot recovery to be able to use /data again.");
Ethan Yonker83e82572014-04-04 10:59:28 -05001597#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001598 return true;
1599 } else {
1600 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001601 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001602 if (Has_Data_Media && Mount(false))
1603 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001604 return false;
1605 }
1606 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001607}
1608
1609void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001610 const char* type;
1611 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001612
Dees_Troy68cab492012-12-12 19:29:35 +00001613 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1614 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001615
Dees_Troy38bd7602012-09-14 13:33:53 -04001616 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001617 if (!Is_Present)
1618 return;
Dees_Troy51127312012-09-08 13:08:49 -04001619
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001620 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1621 if (blkid_do_fullprobe(pr)) {
1622 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001623 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001624 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001625 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001626
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001627 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001628 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001629 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001630 return;
1631 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001632
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001633 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001634 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001635}
1636
Gary Peck43acadf2012-11-21 21:19:01 -08001637bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001638 if (!UnMount(true))
1639 return false;
1640
Dees_Troy43d8b002012-09-17 16:00:01 -04001641 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001642 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001643
Ethan Yonker74db1572015-10-28 12:44:49 -05001644 gui_msg(Msg("formating_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001645 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001646 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001647 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001648 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001649 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001650 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001651 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001652 return true;
1653 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001654 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001655 return false;
1656 }
1657 } else
1658 return Wipe_RMRF();
1659
1660 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001661}
1662
1663bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001664 Find_Actual_Block_Device();
1665 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001666 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1667 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001668 return false;
1669 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001670 if (!UnMount(true))
1671 return false;
1672
Dees_Troyb3265ab2013-08-30 02:59:14 +00001673#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001674 int ret;
1675 char *secontext = NULL;
1676
Ethan Yonker74db1572015-10-28 12:44:49 -05001677 gui_msg(Msg("formating_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001678
Dees Troy99c8dbf2014-03-10 16:53:58 +00001679 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001680 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1681 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1682 } else {
1683 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1684 }
1685 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001686 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001687 return false;
1688 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001689 string sedir = Mount_Point + "/lost+found";
1690 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1691 rmdir(sedir.c_str());
1692 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001693 return true;
1694 }
1695#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001696 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001697 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001698
Ethan Yonker74db1572015-10-28 12:44:49 -05001699 gui_msg(Msg("formating_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001700 Find_Actual_Block_Device();
1701 Command = "make_ext4fs";
1702 if (!Is_Decrypted && Length != 0) {
1703 // Only use length if we're not decrypted
1704 char len[32];
1705 sprintf(len, "%i", Length);
1706 Command += " -l ";
1707 Command += len;
1708 }
Dees_Troy5295d582013-09-06 15:51:08 +00001709 if (TWFunc::Path_Exists("/file_contexts")) {
1710 Command += " -S /file_contexts";
1711 }
1712 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001713 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001714 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001715 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001716 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001717 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001718 return true;
1719 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001720 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001721 return false;
1722 }
1723 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001724 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001725#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001726 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001727}
1728
1729bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001730 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001731
Matt Mower18794c82015-11-11 16:22:45 -06001732 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001733 if (!UnMount(true))
1734 return false;
1735
Ethan Yonker74db1572015-10-28 12:44:49 -05001736 gui_msg(Msg("formating_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001737 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001738 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001739 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001740 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001741 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001742 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001743 return true;
1744 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001745 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001746 return false;
1747 }
1748 return true;
1749 }
1750 else
1751 return Wipe_RMRF();
1752
1753 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001754}
1755
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001756bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001757 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001758
1759 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1760 if (!UnMount(true))
1761 return false;
1762
Ethan Yonker74db1572015-10-28 12:44:49 -05001763 gui_msg(Msg("formating_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001764 Find_Actual_Block_Device();
1765 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001766 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001767 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001768 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001769 return true;
1770 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001771 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001772 return false;
1773 }
1774 return true;
1775 }
1776 return false;
1777}
1778
Dees_Troy38bd7602012-09-14 13:33:53 -04001779bool TWPartition::Wipe_MTD() {
1780 if (!UnMount(true))
1781 return false;
1782
Ethan Yonker74db1572015-10-28 12:44:49 -05001783 gui_msg(Msg("formating_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001784
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001785 mtd_scan_partitions();
1786 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1787 if (mtd == NULL) {
1788 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1789 return false;
1790 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001791
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001792 MtdWriteContext* ctx = mtd_write_partition(mtd);
1793 if (ctx == NULL) {
1794 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1795 return false;
1796 }
1797 if (mtd_erase_blocks(ctx, -1) == -1) {
1798 mtd_write_close(ctx);
1799 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1800 return false;
1801 }
1802 if (mtd_write_close(ctx) != 0) {
1803 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1804 return false;
1805 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001806 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001807 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001808 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001809 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001810}
1811
1812bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001813 if (!Mount(true))
1814 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06001815 // This is the only wipe that leaves the partition mounted, so we
1816 // must manually remove the partition from MTP if it is a storage
1817 // partition.
1818 if (Is_Storage)
1819 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001820
Ethan Yonker74db1572015-10-28 12:44:49 -05001821 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001822 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001823 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001824 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001825}
1826
Dees_Troye5017042013-08-29 16:38:55 +00001827bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001828 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001829
1830 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1831 if (!UnMount(true))
1832 return false;
1833
Ethan Yonker74db1572015-10-28 12:44:49 -05001834 gui_msg(Msg("formating_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00001835 Find_Actual_Block_Device();
dhacker29a3fa75f2015-01-17 19:42:33 -05001836 command = "mkfs.f2fs -t 1";
1837 if (!Is_Decrypted && Length != 0) {
1838 // Only use length if we're not decrypted
1839 char len[32];
1840 int mod_length = Length;
1841 if (Length < 0)
1842 mod_length *= -1;
1843 sprintf(len, "%i", mod_length);
1844 command += " -r ";
1845 command += len;
1846 }
1847 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001848 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001849 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001850 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00001851 return true;
1852 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001853 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00001854 return false;
1855 }
1856 return true;
1857 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001858 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00001859 return Wipe_RMRF();
1860 }
1861 return false;
1862}
1863
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001864bool TWPartition::Wipe_NTFS() {
1865 string command;
1866
1867 if (TWFunc::Path_Exists("/sbin/mkntfs")) {
1868 if (!UnMount(true))
1869 return false;
1870
Ethan Yonker74db1572015-10-28 12:44:49 -05001871 gui_msg(Msg("formating_using=Formatting {1} using {2}...")(Display_Name)("mkntfs"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001872 Find_Actual_Block_Device();
1873 command = "mkntfs " + Actual_Block_Device;
1874 if (TWFunc::Exec_Cmd(command) == 0) {
1875 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001876 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001877 return true;
1878 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001879 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001880 return false;
1881 }
1882 return true;
1883 }
1884 return false;
1885}
1886
Dees_Troy51a0e822012-09-05 15:24:24 -04001887bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001888#ifdef TW_OEM_BUILD
1889 // In an OEM Build we want to do a full format
1890 return Wipe_Encryption();
1891#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001892 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001893
1894 // This handles wiping data on devices with "sdcard" in /data/media
1895 if (!Mount(true))
1896 return false;
1897
Ethan Yonker74db1572015-10-28 12:44:49 -05001898 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Dees_Troy38bd7602012-09-14 13:33:53 -04001899
1900 DIR* d;
1901 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001902 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001903 struct dirent* de;
1904 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001905 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001906 // The media folder is the "internal sdcard"
1907 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001908 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001909 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001910 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001911
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001912 dir = "/data/";
1913 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001914 if (de->d_type == DT_DIR) {
1915 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001916 } 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 +00001917 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001918 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001919 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001920 }
1921 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001922
Ethan Yonker74db1572015-10-28 12:44:49 -05001923 gui_msg("done=Done.");
Dees_Troy16b74352012-11-14 22:27:31 +00001924 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001925 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001926 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00001927 return false;
Ethan Yonker83e82572014-04-04 10:59:28 -05001928#endif // ifdef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001929}
1930
bigbiff7abc5fe2015-01-17 16:53:12 -05001931bool 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 -04001932 char back_name[255], split_index[5];
1933 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001934 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001935 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001936 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001937 twrpTar tar;
1938 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001939
Dees_Troy43d8b002012-09-17 16:00:01 -04001940 if (!Mount(true))
1941 return false;
1942
Dees_Troya13d74f2013-03-24 08:54:55 -05001943 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05001944 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04001945
1946 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001947 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001948
Dees_Troy83bd4832013-05-04 12:39:56 +00001949#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1950 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1951 if (use_encryption && Can_Encrypt_Backup) {
1952 tar.use_encryption = use_encryption;
1953 if (Use_Userdata_Encryption)
1954 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001955 string Password;
1956 DataManager::GetValue("tw_backup_password", Password);
1957 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001958 } else {
1959 use_encryption = false;
1960 }
1961#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001962
1963 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1964 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001965 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001966 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001967 Full_FileName = backup_folder + "/" + Backup_FileName;
1968 tar.setdir(Backup_Path);
1969 tar.setfn(Full_FileName);
1970 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001971 tar.partition_name = Backup_Name;
1972 tar.backup_folder = backup_folder;
bigbiff7abc5fe2015-01-17 16:53:12 -05001973 if (tar.createTarFork(overall_size, other_backups_size, tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00001974 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001975 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001976}
1977
1978bool TWPartition::Backup_DD(string backup_folder) {
codelover352b75e2015-03-29 02:44:07 +08001979 char back_name[255], block_size[32], dd_count[32];
1980 string Full_FileName, Command, DD_BS, DD_COUNT;
Dees_Troy43d8b002012-09-17 16:00:01 -04001981 int use_compression;
codelover352b75e2015-03-29 02:44:07 +08001982 unsigned long long DD_Block_Size, DD_Count;
Dees_Troy43d8b002012-09-17 16:00:01 -04001983
codelover352b75e2015-03-29 02:44:07 +08001984 DD_Block_Size = 16 * 1024 * 1024;
1985 while (Backup_Size % DD_Block_Size != 0) DD_Block_Size >>= 1;
1986
1987 DD_Count = Backup_Size / DD_Block_Size;
1988
1989 sprintf(dd_count, "%llu", DD_Count);
1990 DD_COUNT = dd_count;
1991
1992 sprintf(block_size, "%llu", DD_Block_Size);
1993 DD_BS = block_size;
igoriok87e3d932013-01-31 21:03:53 +02001994
Ethan Yonker74db1572015-10-28 12:44:49 -05001995 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
1996 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04001997
1998 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1999 Backup_FileName = back_name;
2000
2001 Full_FileName = backup_folder + "/" + Backup_FileName;
2002
codelover352b75e2015-03-29 02:44:07 +08002003 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + " count=" + DD_COUNT;
Dees_Troy2673cec2013-04-02 20:22:16 +00002004 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002005 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002006 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04002007 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002008 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002009 return false;
2010 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002011 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002012}
2013
2014bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002015 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02002016 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002017 int use_compression;
2018
Ethan Yonker74db1572015-10-28 12:44:49 -05002019 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2020 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002021
2022 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
2023 Backup_FileName = back_name;
2024
2025 Full_FileName = backup_folder + "/" + Backup_FileName;
2026
2027 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00002028 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002029 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002030 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002031 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2032 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
Ethan Yonker74db1572015-10-28 12:44:49 -05002033 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '%s' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002034 return false;
2035 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002036 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002037}
2038
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002039unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
2040 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
2041 if (restore_info.LoadValues() == 0) {
2042 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2043 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2044 return Restore_Size;
2045 }
2046 }
2047 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
2048
2049 Full_FileName = restore_folder + "/" + Backup_FileName;
2050
2051 if (Is_Image(Restore_File_System)) {
2052 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2053 return Restore_Size;
2054 }
2055
2056 twrpTar tar;
2057 tar.setdir(Backup_Path);
2058 tar.setfn(Full_FileName);
2059 tar.backup_name = Backup_Name;
2060#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2061 string Password;
2062 DataManager::GetValue("tw_restore_password", Password);
2063 if (!Password.empty())
2064 tar.setpassword(Password);
2065#endif
2066 tar.partition_name = Backup_Name;
2067 tar.backup_folder = restore_folder;
2068 Restore_Size = tar.get_size();
2069 return Restore_Size;
2070}
2071
2072bool 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 -08002073 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04002074 int index = 0;
2075 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00002076 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002077
Dees_Troye58d5262012-09-21 12:27:57 -04002078 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002079 if (!Wipe_AndSec())
2080 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002081 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002082 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002083 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002084 gui_msg(Msg(msg::kWarning, "datamedia_fs_restore=WARNING: This /data backup was made with {1} file system! The backup may not boot unless you change back to {1}.")(Restore_File_System));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002085 if (!Wipe_Data_Without_Wiping_Media())
2086 return false;
2087 } else {
2088 if (!Wipe(Restore_File_System))
2089 return false;
2090 }
Dees_Troye58d5262012-09-21 12:27:57 -04002091 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002092 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restore}"));
2093 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002094
2095 if (!Mount(true))
2096 return false;
2097
Dees_Troy4a2a1262012-09-18 09:33:47 -04002098 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002099 twrpTar tar;
2100 tar.setdir(Backup_Path);
2101 tar.setfn(Full_FileName);
2102 tar.backup_name = Backup_Name;
2103#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2104 string Password;
2105 DataManager::GetValue("tw_restore_password", Password);
2106 if (!Password.empty())
2107 tar.setpassword(Password);
2108#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002109 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002110 ret = false;
2111 else
2112 ret = true;
2113#ifdef HAVE_CAPABILITIES
2114 // Restore capabilities to the run-as binary
2115 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2116 struct vfs_cap_data cap_data;
2117 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2118
2119 memset(&cap_data, 0, sizeof(cap_data));
2120 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2121 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2122 cap_data.data[0].inheritable = 0;
2123 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2124 cap_data.data[1].inheritable = 0;
2125 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2126 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2127 } else {
2128 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2129 }
2130 }
2131#endif
2132 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002133}
2134
Ethan Yonker96af84a2015-01-05 14:58:36 -06002135bool TWPartition::Restore_Image(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size, string Restore_File_System) {
2136 string Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002137 double display_percent, progress_percent;
2138 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04002139
Ethan Yonker74db1572015-10-28 12:44:49 -05002140 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restore}"));
2141 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002142 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002143
Ethan Yonker96af84a2015-01-05 14:58:36 -06002144 if (Restore_File_System == "emmc") {
2145 if (!Flash_Image_DD(Full_FileName))
2146 return false;
2147 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
2148 if (!Flash_Image_FI(Full_FileName))
2149 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002150 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002151 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
2152 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
2153 DataManager::SetValue("tw_size_progress", size_progress);
2154 progress_percent = (display_percent / 100);
2155 DataManager::SetProgress((float)(progress_percent));
2156 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04002157 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002158}
Dees_Troy5bf43922012-09-07 16:07:55 -04002159
2160bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002161 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002162
Dees_Troyab10ee22012-09-21 14:27:30 -04002163 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002164 return false;
2165
Dees_Troy0550cfb2012-10-13 11:56:13 -04002166 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002167 if (Removable || Is_Encrypted) {
2168 if (!Mount(false))
2169 return true;
2170 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002171 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002172
2173 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002174 if (!ret || Size == 0) {
2175 if (!Get_Size_Via_df(Display_Error)) {
2176 if (!Was_Already_Mounted)
2177 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002178 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002179 }
2180 }
Dees_Troy51127312012-09-08 13:08:49 -04002181
Dees_Troy5bf43922012-09-07 16:07:55 -04002182 if (Has_Data_Media) {
2183 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002184 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002185 Used = du.Get_Folder_Size("/data");
2186 Backup_Size = Used;
2187 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002188 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002189 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002190 } else {
2191 if (!Was_Already_Mounted)
2192 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002193 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002194 }
Dees_Troye58d5262012-09-21 12:27:57 -04002195 } else if (Has_Android_Secure) {
2196 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002197 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002198 else {
2199 if (!Was_Already_Mounted)
2200 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002201 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002202 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002203 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002204 if (!Was_Already_Mounted)
2205 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002206 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002207}
Dees_Troy38bd7602012-09-14 13:33:53 -04002208
2209void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002210 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002211 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002212 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002213 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002214 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002215 Is_Present = true;
2216 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002217 return;
2218 }
2219 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002220 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002221 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002222 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002223 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002224 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002225 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002226}
2227
2228void TWPartition::Recreate_Media_Folder(void) {
2229 string Command;
2230
2231 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002232 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")("/data/media"));
Dees_Troyb46a6842012-09-25 11:06:46 -04002233 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002234 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00002235 LOGINFO("Recreating /data/media folder.\n");
xiaolu9416f4f2015-06-04 08:22:23 +08002236 mkdir("/data/media", 0770);
2237 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2238 if (!Internal_path.empty()) {
2239 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2240 mkdir(Internal_path.c_str(), 0770);
2241 }
2242#ifdef TW_INTERNAL_STORAGE_PATH
2243 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2244#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002245#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002246 // Afterwards, we will try to set the
2247 // default metadata that we were hopefully able to get during
2248 // early boot.
2249 tw_set_default_metadata("/data/media");
xiaolu9416f4f2015-06-04 08:22:23 +08002250 if (!Internal_path.empty())
2251 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002252#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002253 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002254 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002255 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002256 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002257}
Dees_Troye58d5262012-09-21 12:27:57 -04002258
2259void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002260 if (!Has_Android_Secure)
2261 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002262 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002263 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002264 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002265 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002266 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002267 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002268 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002269 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002270 }
2271}
bigbiff7cb4c332014-11-26 20:36:07 -05002272
2273uint64_t TWPartition::Get_Max_FileSize() {
2274 uint64_t maxFileSize = 0;
2275 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2276 const uint64_t constTB = (uint64_t) constGB * 1024;
2277 const uint64_t constPB = (uint64_t) constTB * 1024;
2278 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002279 if (Current_File_System == "ext4")
2280 maxFileSize = 16 * constTB; //16 TB
2281 else if (Current_File_System == "vfat")
2282 maxFileSize = 4 * constGB; //4 GB
2283 else if (Current_File_System == "ntfs")
2284 maxFileSize = 256 * constTB; //256 TB
2285 else if (Current_File_System == "exfat")
2286 maxFileSize = 16 * constPB; //16 PB
2287 else if (Current_File_System == "ext3")
2288 maxFileSize = 2 * constTB; //2 TB
2289 else if (Current_File_System == "f2fs")
2290 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002291 else
2292 maxFileSize = 100000000L;
2293 return maxFileSize - 1;
2294}
2295
Ethan Yonker96af84a2015-01-05 14:58:36 -06002296bool TWPartition::Flash_Image(string Filename) {
2297 string Restore_File_System;
2298
2299 LOGINFO("Image filename is: %s\n", Filename.c_str());
2300
2301 if (Backup_Method == FILES) {
2302 LOGERR("Cannot flash images to file systems\n");
2303 return false;
2304 } else if (!Can_Flash_Img) {
2305 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2306 return false;
2307 } else {
2308 if (!Find_Partition_Size()) {
2309 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2310 return false;
2311 }
2312 unsigned long long image_size = TWFunc::Get_File_Size(Filename);
2313 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002314 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonker96af84a2015-01-05 14:58:36 -06002315 image_size, Filename.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002316 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002317 return false;
2318 }
2319 if (Backup_Method == DD)
2320 return Flash_Image_DD(Filename);
2321 else if (Backup_Method == FLASH_UTILS)
2322 return Flash_Image_FI(Filename);
2323 }
2324
2325 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2326 return false;
2327}
2328
2329bool TWPartition::Flash_Image_DD(string Filename) {
2330 string Command;
2331
Ethan Yonker74db1572015-10-28 12:44:49 -05002332 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
codelover352b75e2015-03-29 02:44:07 +08002333 Command = "dd bs=8388608 if='" + Filename + "' of=" + Actual_Block_Device;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002334 LOGINFO("Flash command: '%s'\n", Command.c_str());
2335 TWFunc::Exec_Cmd(Command);
2336 return true;
2337}
2338
2339bool TWPartition::Flash_Image_FI(string Filename) {
2340 string Command;
2341
Ethan Yonker74db1572015-10-28 12:44:49 -05002342 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker96af84a2015-01-05 14:58:36 -06002343 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2344 Command = "erase_image " + MTD_Name;
2345 LOGINFO("Erase command: '%s'\n", Command.c_str());
2346 TWFunc::Exec_Cmd(Command);
2347 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2348 LOGINFO("Flash command: '%s'\n", Command.c_str());
2349 TWFunc::Exec_Cmd(Command);
2350 return true;
2351}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002352
2353void TWPartition::Change_Mount_Read_Only(bool new_value) {
2354 Mount_Read_Only = new_value;
2355}
2356
2357int TWPartition::Check_Lifetime_Writes() {
2358 bool original_read_only = Mount_Read_Only;
2359 int ret = 1;
2360
2361 Mount_Read_Only = true;
2362 if (Mount(false)) {
2363 Find_Actual_Block_Device();
2364 string block = basename(Actual_Block_Device.c_str());
2365 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2366 string result;
2367 if (TWFunc::Path_Exists(file)) {
2368 if (TWFunc::read_file(file, result) != 0) {
2369 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2370 } else {
2371 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2372 if (result == "0") {
2373 ret = 0;
2374 }
2375 }
2376 } else {
2377 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2378 }
2379 UnMount(true);
2380 } else {
2381 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2382 }
2383 Mount_Read_Only = original_read_only;
2384 return ret;
2385}