blob: a17a647c4d61f89cde9b265c8f3c78b0539e2cc5 [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
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600882unsigned long long TWPartition::IOCTL_Get_Block_Size() {
883 unsigned long block_device_size;
884 int ret = 0;
885
886 Find_Actual_Block_Device();
887 int fd = open(Actual_Block_Device.c_str(), O_RDONLY);
888 if (fd < 0) {
889 LOGINFO("Find_Partition_Size: Failed to open '%s', (%s)\n", Actual_Block_Device.c_str(), strerror(errno));
890 } else {
891 ret = ioctl(fd, BLKGETSIZE, &block_device_size);
892 close(fd);
893 if (ret) {
894 LOGINFO("Find_Partition_Size: ioctl error: (%s)\n", strerror(errno));
895 } else {
896 return (unsigned long long)(block_device_size) * 512LLU;
897 }
898 }
899 return 0;
900}
901
Dees_Troy5bf43922012-09-07 16:07:55 -0400902bool TWPartition::Find_Partition_Size(void) {
903 FILE* fp;
904 char line[512];
905 string tmpdevice;
906
igoriok87e3d932013-01-31 21:03:53 +0200907 fp = fopen("/proc/dumchar_info", "rt");
908 if (fp != NULL) {
909 while (fgets(line, sizeof(line), fp) != NULL)
910 {
911 char label[32], device[32];
912 unsigned long size = 0;
913
thatd43bf2d2014-09-21 23:13:02 +0200914 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200915
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500916 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200917 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
918 continue;
919
920 tmpdevice = "/dev/";
921 tmpdevice += label;
922 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
923 Size = size;
924 fclose(fp);
925 return true;
926 }
927 }
928 }
929
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600930 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
931 if (ioctl_size) {
932 Size = ioctl_size;
933 return true;
934 }
935
Dees_Troy5bf43922012-09-07 16:07:55 -0400936 // In this case, we'll first get the partitions we care about (with labels)
937 fp = fopen("/proc/partitions", "rt");
938 if (fp == NULL)
939 return false;
940
941 while (fgets(line, sizeof(line), fp) != NULL)
942 {
943 unsigned long major, minor, blocks;
944 char device[512];
945 char tmpString[64];
946
Dees_Troy63c8df72012-09-10 14:02:05 -0400947 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400948 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
949
950 tmpdevice = "/dev/block/";
951 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400952 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400953 // Adjust block size to byte size
954 Size = blocks * 1024ULL;
955 fclose(fp);
956 return true;
957 }
958 }
959 fclose(fp);
960 return false;
961}
962
Dees_Troy5bf43922012-09-07 16:07:55 -0400963bool TWPartition::Is_Mounted(void) {
964 if (!Can_Be_Mounted)
965 return false;
966
967 struct stat st1, st2;
968 string test_path;
969
970 // Check to see if the mount point directory exists
971 test_path = Mount_Point + "/.";
972 if (stat(test_path.c_str(), &st1) != 0) return false;
973
974 // Check to see if the directory above the mount point exists
975 test_path = Mount_Point + "/../.";
976 if (stat(test_path.c_str(), &st2) != 0) return false;
977
978 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
979 int ret = (st1.st_dev != st2.st_dev) ? true : false;
980
981 return ret;
982}
983
984bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000985 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500986 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000987
Dees_Troy5bf43922012-09-07 16:07:55 -0400988 if (Is_Mounted()) {
989 return true;
990 } else if (!Can_Be_Mounted) {
991 return false;
992 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400993
994 Find_Actual_Block_Device();
995
996 // Check the current file system before mounting
997 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000998 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000999 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 +00001000 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001001 string result;
1002 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001003 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001004 Current_File_System = "vfat";
1005 } else {
1006#ifdef TW_NO_EXFAT_FUSE
1007 UnMount(false);
1008 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1009 // Some kernels let us mount vfat as exfat which doesn't work out too well
1010#else
1011 exfat_mounted = 1;
1012#endif
1013 }
1014 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001015
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001016 if (Current_File_System == "ntfs" && TWFunc::Path_Exists("/sbin/ntfs-3g")) {
1017 string cmd;
1018 if (Mount_Read_Only)
1019 cmd = "/sbin/ntfs-3g -o ro " + Actual_Block_Device + " " + Mount_Point;
1020 else
1021 cmd = "/sbin/ntfs-3g " + Actual_Block_Device + " " + Mount_Point;
1022 LOGINFO("cmd: '%s'\n", cmd.c_str());
1023 if (TWFunc::Exec_Cmd(cmd) == 0) {
1024 return true;
1025 } else {
1026 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1027 }
1028 }
1029
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001030 if (Mount_Read_Only)
1031 flags |= MS_RDONLY;
1032
Dees_Troy22042032012-12-18 21:23:08 +00001033 if (Fstab_File_System == "yaffs2") {
1034 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001035 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1036 if (Mount_Read_Only)
1037 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001038 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1039 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1040 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001041 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001042 else
1043 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1044 return false;
1045 } else {
1046 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1047 return true;
1048 }
1049 } else {
1050 struct stat st;
1051 string test_path = Mount_Point;
1052 if (stat(test_path.c_str(), &st) < 0) {
1053 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001054 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001055 else
1056 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1057 return false;
1058 }
1059 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1060 if (new_mode != st.st_mode) {
1061 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1062 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1063 if (Display_Error)
1064 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1065 else
1066 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1067 return false;
1068 }
1069 }
Dees_Troy22042032012-12-18 21:23:08 +00001070 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001071 }
thatc7572eb2015-03-31 18:55:30 +02001072 }
1073
1074 string mount_fs = Current_File_System;
1075 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1076 mount_fs = "texfat";
1077
1078 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001079 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1080 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001081#ifdef TW_NO_EXFAT_FUSE
1082 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001083 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001084 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001085 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001086 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001087 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001088 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001089 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 +00001090 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001091 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001092 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001093#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001094 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001095 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001096 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001097 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1098 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 +00001099 return false;
1100#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001101 }
1102#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001103 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001104
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001105 if (Removable)
1106 Update_Size(Display_Error);
1107
xiaolu9416f4f2015-06-04 08:22:23 +08001108 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001109 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001110 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001111 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001112 return true;
1113}
1114
1115bool TWPartition::UnMount(bool Display_Error) {
1116 if (Is_Mounted()) {
1117 int never_unmount_system;
1118
1119 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1120 if (never_unmount_system == 1 && Mount_Point == "/system")
1121 return true; // Never unmount system if you're not supposed to unmount it
1122
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001123 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001124 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001125
Dees_Troy38bd7602012-09-14 13:33:53 -04001126 if (!Symlink_Mount_Point.empty())
1127 umount(Symlink_Mount_Point.c_str());
1128
Dees_Troyb05ddee2013-01-28 20:24:50 +00001129 umount(Mount_Point.c_str());
1130 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001131 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001132 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001133 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001134 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001135 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001136 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001137 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001138 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001139 } else {
1140 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001141 }
1142}
1143
Gary Peck43acadf2012-11-21 21:19:01 -08001144bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001145 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001146 int check;
1147 string Layout_Filename = Mount_Point + "/.layout_version";
1148
Dees_Troy38bd7602012-09-14 13:33:53 -04001149 if (!Can_Be_Wiped) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001150 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.\n")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001151 return false;
1152 }
1153
Dees_Troyc51f1f92012-09-20 15:32:13 -04001154 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001155 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001156
Dees_Troy16c2b312013-01-15 16:51:18 +00001157 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1158 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1159 else
1160 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001161
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001162 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001163 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001164 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001165 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001166 DataManager::GetValue(TW_RM_RF_VAR, check);
1167
Hashcodedabfd492013-08-29 22:45:30 -07001168 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001169 wiped = Wipe_RMRF();
1170 else if (New_File_System == "ext4")
1171 wiped = Wipe_EXT4();
1172 else if (New_File_System == "ext2" || New_File_System == "ext3")
1173 wiped = Wipe_EXT23(New_File_System);
1174 else if (New_File_System == "vfat")
1175 wiped = Wipe_FAT();
1176 else if (New_File_System == "exfat")
1177 wiped = Wipe_EXFAT();
1178 else if (New_File_System == "yaffs2")
1179 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001180 else if (New_File_System == "f2fs")
1181 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001182 else if (New_File_System == "ntfs")
1183 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001184 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001185 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 +00001186 unlink("/.layout_version");
1187 return false;
1188 }
1189 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001190 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001191
Gary Pecke8bc5d72012-12-21 06:45:25 -08001192 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001193 if (Mount_Point == "/cache")
1194 DataManager::Output_Version();
1195
Dees_Troy16c2b312013-01-15 16:51:18 +00001196 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1197 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1198
1199 if (update_crypt) {
1200 Setup_File_System(false);
1201 if (Is_Encrypted && !Is_Decrypted) {
1202 // just wiped an encrypted partition back to its unencrypted state
1203 Is_Encrypted = false;
1204 Is_Decrypted = false;
1205 Decrypted_Block_Device = "";
1206 if (Mount_Point == "/data") {
1207 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1208 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1209 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001210 }
1211 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001212
1213 if (Mount_Point == "/data" && Has_Data_Media && recreate_media) {
1214 Recreate_Media_Folder();
1215 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001216 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001217 if (Is_Storage) {
1218 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001219 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001220 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001221}
1222
Gary Peck43acadf2012-11-21 21:19:01 -08001223bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001224 if (Is_File_System(Current_File_System))
1225 return Wipe(Current_File_System);
1226 else
1227 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001228}
1229
Dees_Troye58d5262012-09-21 12:27:57 -04001230bool TWPartition::Wipe_AndSec(void) {
1231 if (!Has_Android_Secure)
1232 return false;
1233
Dees_Troye58d5262012-09-21 12:27:57 -04001234 if (!Mount(true))
1235 return false;
1236
Ethan Yonker74db1572015-10-28 12:44:49 -05001237 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001238 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001239 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001240}
1241
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001242bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001243 if (Mount_Read_Only)
1244 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001245 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001246 return true;
1247 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1248 return true;
1249 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1250 return true;
1251 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1252 return true;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001253 else if (Current_File_System == "ntfs" && TWFunc::Path_Exists("/sbin/ntfsfix"))
1254 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001255 return false;
1256}
1257
1258bool TWPartition::Repair() {
1259 string command;
1260
1261 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001262 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001263 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001264 return false;
1265 }
1266 if (!UnMount(true))
1267 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001268 gui_msg(Msg("reparing=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001269 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001270 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001271 LOGINFO("Repair command: %s\n", command.c_str());
1272 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001273 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001274 return true;
1275 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001276 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001277 return false;
1278 }
1279 }
1280 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1281 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001282 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001283 return false;
1284 }
1285 if (!UnMount(true))
1286 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001287 gui_msg(Msg("reparing=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001288 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001289 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001290 LOGINFO("Repair command: %s\n", command.c_str());
1291 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001292 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001293 return true;
1294 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001295 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001296 return false;
1297 }
1298 }
1299 if (Current_File_System == "exfat") {
1300 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001301 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001302 return false;
1303 }
1304 if (!UnMount(true))
1305 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001306 gui_msg(Msg("reparing=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001307 Find_Actual_Block_Device();
1308 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1309 LOGINFO("Repair command: %s\n", command.c_str());
1310 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001311 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001312 return true;
1313 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001314 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001315 return false;
1316 }
1317 }
1318 if (Current_File_System == "f2fs") {
1319 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001320 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001321 return false;
1322 }
1323 if (!UnMount(true))
1324 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001325 gui_msg(Msg("reparing=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001326 Find_Actual_Block_Device();
1327 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1328 LOGINFO("Repair command: %s\n", command.c_str());
1329 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001330 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001331 return true;
1332 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001333 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001334 return false;
1335 }
1336 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001337 if (Current_File_System == "ntfs") {
1338 if (!TWFunc::Path_Exists("/sbin/ntfsfix")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001339 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001340 return false;
1341 }
1342 if (!UnMount(true))
1343 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001344 gui_msg(Msg("reparing=Repairing {1} using {2}...")(Display_Name)("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001345 Find_Actual_Block_Device();
1346 command = "/sbin/ntfsfix " + Actual_Block_Device;
1347 LOGINFO("Repair command: %s\n", command.c_str());
1348 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001349 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001350 return true;
1351 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001352 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001353 return false;
1354 }
1355 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001356 return false;
1357}
1358
Ethan Yonkera2719152015-05-28 09:44:41 -05001359bool TWPartition::Can_Resize() {
1360 if (Mount_Read_Only)
1361 return false;
1362 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1363 return true;
1364 return false;
1365}
1366
1367bool TWPartition::Resize() {
1368 string command;
1369
1370 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1371 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001372 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1373 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001374 return false;
1375 }
1376 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001377 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1378 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001379 return false;
1380 }
1381 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001382 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001383 if (!Repair())
1384 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001385 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001386 Find_Actual_Block_Device();
1387 command = "/sbin/resize2fs " + Actual_Block_Device;
1388 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001389 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1390 if (Actual_Size == 0)
1391 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001392
Ethan Yonkera2719152015-05-28 09:44:41 -05001393 unsigned long long Block_Count;
1394 if (Length < 0) {
1395 // Reduce overall size by this length
1396 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1397 } else {
1398 // This is the size, not a size reduction
1399 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1400 }
1401 char temp[256];
1402 sprintf(temp, "%llu", Block_Count);
1403 command += " ";
1404 command += temp;
1405 command += "K";
1406 }
1407 LOGINFO("Resize command: %s\n", command.c_str());
1408 if (TWFunc::Exec_Cmd(command) == 0) {
1409 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001410 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001411 return true;
1412 } else {
1413 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001414 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001415 return false;
1416 }
1417 }
1418 return false;
1419}
1420
bigbiff7abc5fe2015-01-17 16:53:12 -05001421bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
1422 if (Backup_Method == FILES) {
1423 return Backup_Tar(backup_folder, overall_size, other_backups_size, tar_fork_pid);
1424 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001425 else if (Backup_Method == DD)
1426 return Backup_DD(backup_folder);
1427 else if (Backup_Method == FLASH_UTILS)
1428 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001429 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001430 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001431}
1432
Dees_Troy43d8b002012-09-17 16:00:01 -04001433bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001434 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001435 char split_filename[512];
1436 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001437 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001438
Captain Throwbackff5935f2014-09-23 16:08:34 -04001439 sync();
1440
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001441 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001442 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001443 if (!TWFunc::Path_Exists(Full_Filename)) {
1444 // This is a split archive, we presume
1445 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001446 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001447 md5file = split_filename;
1448 md5file += ".md5";
1449 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001450 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 -04001451 return false;
1452 }
1453 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001454 while (index < 1000) {
1455 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001456 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001457 return false;
1458 }
1459 index++;
1460 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001461 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001462 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001463 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001464 } else {
1465 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001466 md5file = Full_Filename + ".md5";
1467 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001468 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 -04001469 return false;
1470 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001471 md5sum.setfn(Full_Filename);
1472 if (md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001473 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001474 return false;
1475 } else
1476 return true;
1477 }
1478 return false;
1479}
1480
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001481bool 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 -08001482 string Restore_File_System;
1483
Ethan Yonker74db1572015-10-28 12:44:49 -05001484 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring}"));
Dees_Troy2673cec2013-04-02 20:22:16 +00001485 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001486
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001487 Restore_File_System = Get_Restore_File_System(restore_folder);
1488
1489 if (Is_File_System(Restore_File_System))
1490 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1491 else if (Is_Image(Restore_File_System)) {
Ethan Yonkerf66324f2015-01-09 16:46:57 -06001492 return Restore_Image(restore_folder, total_restore_size, already_restored_size, Restore_File_System);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001493 }
1494
1495 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1496 return false;
1497}
1498
1499string TWPartition::Get_Restore_File_System(string restore_folder) {
1500 size_t first_period, second_period;
1501 string Restore_File_System;
1502
Gary Peck43acadf2012-11-21 21:19:01 -08001503 // Parse backup filename to extract the file system before wiping
1504 first_period = Backup_FileName.find(".");
1505 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001506 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001507 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001508 }
1509 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1510 second_period = Restore_File_System.find(".");
1511 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001512 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001513 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001514 }
1515 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001516 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001517 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001518}
1519
1520string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001521 if (Backup_Method == NONE)
1522 return "none";
1523 else if (Backup_Method == FILES)
1524 return "files";
1525 else if (Backup_Method == DD)
1526 return "dd";
1527 else if (Backup_Method == FLASH_UTILS)
1528 return "flash_utils";
1529 else
1530 return "undefined";
1531 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001532}
1533
1534bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001535 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001536 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001537 return 1;
1538}
1539
1540bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001541 bool Save_Data_Media = Has_Data_Media;
1542
1543 if (!UnMount(true))
1544 return false;
1545
Dees_Troy38bd7602012-09-14 13:33:53 -04001546 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001547 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001548#ifdef TW_INCLUDE_CRYPTO
1549 if (Is_Decrypted) {
1550 if (!UnMount(true))
1551 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001552 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001553 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1554 }
1555 }
1556#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001557 Is_Decrypted = false;
1558 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001559 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001560 if (Crypto_Key_Location == "footer") {
1561 int newlen, fd;
1562 if (Length != 0) {
1563 newlen = Length;
1564 if (newlen < 0)
1565 newlen = newlen * -1;
1566 } else {
1567 newlen = CRYPT_FOOTER_OFFSET;
1568 }
1569 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1570 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1571 } else {
1572 unsigned int block_count;
1573 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1574 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1575 } else {
1576 off64_t offset = ((off64_t)block_count * 512) - newlen;
1577 if (lseek64(fd, offset, SEEK_SET) == -1) {
1578 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1579 } else {
1580 void* buffer = malloc(newlen);
1581 if (!buffer) {
1582 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1583 } else {
1584 memset(buffer, 0, newlen);
1585 int ret = write(fd, buffer, newlen);
1586 if (ret != newlen) {
1587 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1588 } else {
1589 LOGINFO("Successfully wiped crypto footer.\n");
1590 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001591 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001592 }
1593 }
1594 }
1595 close(fd);
1596 }
1597 } else {
1598 string Command = "flash_image " + Crypto_Key_Location + " /dev/zero";
1599 TWFunc::Exec_Cmd(Command);
1600 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001601 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001602 Has_Data_Media = Save_Data_Media;
1603 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1604 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001605 if (Mount(false))
1606 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001607 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001608 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001609#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001610 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 -05001611#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001612 return true;
1613 } else {
1614 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001615 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001616 if (Has_Data_Media && Mount(false))
1617 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001618 return false;
1619 }
1620 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001621}
1622
1623void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001624 const char* type;
1625 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001626
Dees_Troy68cab492012-12-12 19:29:35 +00001627 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1628 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001629
Dees_Troy38bd7602012-09-14 13:33:53 -04001630 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001631 if (!Is_Present)
1632 return;
Dees_Troy51127312012-09-08 13:08:49 -04001633
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001634 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1635 if (blkid_do_fullprobe(pr)) {
1636 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001637 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001638 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001639 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001640
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001641 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001642 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001643 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001644 return;
1645 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001646
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500