blob: 4f66c5f1aa083b97809baa6536f1ba39afd3176f [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
Ethan Yonker472f5062016-02-25 13:47:30 -06002 Copyright 2013 to 2016 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
thatf54e5392016-01-29 22:04:43 +010027#include <libgen.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050028#include <iostream>
29#include <sstream>
Ethan Yonker66a19492015-12-10 10:19:45 -060030#include <sys/param.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060031#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040032
Dees_Troy657c3092012-09-10 20:32:10 -040033#ifdef TW_INCLUDE_CRYPTO
34 #include "cutils/properties.h"
35#endif
36
bigbiff7b4c7a62015-01-01 19:44:14 -050037#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000039#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040040#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040041#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040042#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050043#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050044#include "twrpTar.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050045#include "twrpDU.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050046#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060047#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050048#include "gui/gui.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040049extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040050 #include "mtdutils/mtdutils.h"
51 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000052#ifdef USE_EXT4
53 #include "make_ext4fs.h"
54#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060055
56#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060057 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060058 #include "gpt/gpt.h"
Ethan Yonkerbc85b632015-08-09 12:48:14 -050059#else
60 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060061#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040062}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040063#ifdef HAVE_SELINUX
64#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060065#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040066#endif
Dees Troy4159aed2014-02-28 17:24:43 +000067#ifdef HAVE_CAPABILITIES
68#include <sys/capability.h>
69#include <sys/xattr.h>
70#include <linux/xattr.h>
71#endif
HashBanged974bb2016-01-30 14:20:09 -050072#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060073#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040074
bigbiff bigbiff9c754052013-01-09 09:09:08 -050075using namespace std;
76
Dees_Troya95f55c2013-08-17 13:14:43 +000077extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050078extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000079
Hashcode62bd9e02013-11-19 21:59:42 -080080struct flag_list {
81 const char *name;
82 unsigned flag;
83};
84
85static struct flag_list mount_flags[] = {
86 { "noatime", MS_NOATIME },
87 { "noexec", MS_NOEXEC },
88 { "nosuid", MS_NOSUID },
89 { "nodev", MS_NODEV },
90 { "nodiratime", MS_NODIRATIME },
91 { "ro", MS_RDONLY },
92 { "rw", 0 },
93 { "remount", MS_REMOUNT },
94 { "bind", MS_BIND },
95 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000096#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080097 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000098#endif
99#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -0800100 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#endif
102#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800103 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
105#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800106 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000107#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800108 { "sync", MS_SYNCHRONOUS },
109 { "defaults", 0 },
110 { 0, 0 },
111};
112
that9e0593e2014-10-08 00:01:24 +0200113TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400114 Can_Be_Mounted = false;
115 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500116 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200117 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400118 Wipe_During_Factory_Reset = false;
119 Wipe_Available_in_GUI = false;
120 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400121 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400122 SubPartition_Of = "";
123 Symlink_Path = "";
124 Symlink_Mount_Point = "";
125 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400126 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400127 Actual_Block_Device = "";
128 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400129 Alternate_Block_Device = "";
130 Removable = false;
131 Is_Present = false;
132 Length = 0;
133 Size = 0;
134 Used = 0;
135 Free = 0;
136 Backup_Size = 0;
137 Can_Be_Encrypted = false;
138 Is_Encrypted = false;
139 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600140 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400141 Decrypted_Block_Device = "";
142 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500143 Backup_Display_Name = "";
144 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400145 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400146 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400147 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400148 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000149 Can_Encrypt_Backup = false;
150 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400151 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400152 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400153 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500154 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400155 Storage_Path = "";
156 Current_File_System = "";
157 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800158 Mount_Flags = 0;
159 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400160 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000161 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000162 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600163 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600164 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600165 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500166 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600167 Is_Adopted_Storage = false;
168 Adopted_GUID = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400169}
170
171TWPartition::~TWPartition(void) {
172 // Do nothing
173}
174
Dees_Troy5bf43922012-09-07 16:07:55 -0400175bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
176 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
177 int line_len = Line.size(), index = 0, item_index = 0;
178 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400179 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400180 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500181 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400182
Dees_Troy51127312012-09-08 13:08:49 -0400183 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500184 if (full_line[index] == 34)
185 skip = !skip;
186 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400187 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400188 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400189 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000190 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400191 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500192 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000193 Display_Name = full_line + 1;
194 Backup_Display_Name = Display_Name;
195 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400196 index = Mount_Point.size();
197 while (index < line_len) {
198 while (index < line_len && full_line[index] == '\0')
199 index++;
200 if (index >= line_len)
201 continue;
202 ptr = full_line + index;
203 if (item_index == 0) {
204 // File System
205 Fstab_File_System = ptr;
206 Current_File_System = ptr;
207 item_index++;
208 } else if (item_index == 1) {
209 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400210 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400211 MTD_Name = ptr;
212 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400213 } else if (Fstab_File_System == "bml") {
214 if (Mount_Point == "/boot")
215 MTD_Name = "boot";
216 else if (Mount_Point == "/recovery")
217 MTD_Name = "recovery";
218 Primary_Block_Device = ptr;
219 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000220 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 -0400221 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400222 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000223 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400224 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000225 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400226 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400227 } else {
228 Primary_Block_Device = ptr;
229 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400230 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400231 item_index++;
232 } else if (item_index > 1) {
233 if (*ptr == '/') {
234 // Alternate Block Device
235 Alternate_Block_Device = ptr;
236 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
237 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
238 // Partition length
239 ptr += 7;
240 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400241 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
242 // Custom flags, save for later so that new values aren't overwritten by defaults
243 ptr += 6;
244 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000245 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400246 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
247 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400248 } else {
249 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000250 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400251 }
252 }
253 while (index < line_len && full_line[index] != '\0')
254 index++;
255 }
256
257 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
258 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000259 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400260 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000261 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400262 return 0;
263 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400264 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400265 Setup_File_System(Display_Error);
266 if (Mount_Point == "/system") {
267 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500268 Backup_Display_Name = Display_Name;
269 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400270 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500271 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500272 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400273 } else if (Mount_Point == "/data") {
Dees Troyc657cc02015-01-16 22:48:47 +0000274 UnMount(false); // added in case /data is mounted as tmpfs for qcom hardware decrypt
Dees_Troy5bf43922012-09-07 16:07:55 -0400275 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500276 Backup_Display_Name = Display_Name;
277 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400278 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400279 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500280 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000281 Can_Encrypt_Backup = true;
282 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500283 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200284 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400285#ifdef TW_INCLUDE_CRYPTO
286 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400287 char crypto_blkdev[255];
288 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
289 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy657c3092012-09-10 20:32:10 -0400290 DataManager::SetValue(TW_IS_DECRYPTED, 1);
291 Is_Encrypted = true;
292 Is_Decrypted = true;
293 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000294 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400295 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600296 if (Is_Present) {
Ethan Yonker253368a2014-11-25 15:00:52 -0600297 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 -0600298 if (cryptfs_check_footer() == 0) {
299 Is_Encrypted = true;
300 Is_Decrypted = false;
301 Can_Be_Mounted = false;
302 Current_File_System = "emmc";
303 Setup_Image(Display_Error);
304 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
Vojtech Bocek7e11ac52015-03-05 23:21:49 +0100305 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
Ethan Yonker71413f42014-02-26 13:36:08 -0600306 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
307 DataManager::SetValue("tw_crypto_display", "");
308 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500309 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
Ethan Yonker71413f42014-02-26 13:36:08 -0600310 }
311 } else {
312 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
313 }
Gary Peck82599a82012-11-21 16:23:12 -0800314 } else {
315 // Filesystem is not encrypted and the mount
316 // succeeded, so get it back to the original
317 // unmounted state
318 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400319 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500320 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400321 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400322#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500323 if (datamedia)
324 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400325#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400326 } else if (Mount_Point == "/cache") {
327 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500328 Backup_Display_Name = Display_Name;
329 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400330 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400331 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500332 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400333 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000334 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500335 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500336 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400337 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400338 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400339 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400340 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500341 Backup_Display_Name = Display_Name;
342 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400343 Is_SubPartition = true;
344 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400345 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500346 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000347 Can_Encrypt_Backup = true;
348 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400349 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400350 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400351 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500352 Backup_Display_Name = Display_Name;
353 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400354 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400355 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500356 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000357 Can_Encrypt_Backup = true;
358 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400359 } else if (Mount_Point == "/boot") {
360 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500361 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400362 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500363 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500364 } else if (Mount_Point == "/vendor") {
365 Display_Name = "Vendor";
366 Backup_Display_Name = Display_Name;
367 Storage_Name = Display_Name;
368 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400369 }
370#ifdef TW_EXTERNAL_STORAGE_PATH
371 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
372 Is_Storage = true;
373 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400374 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500375 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400376#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000377 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400378 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400379 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500380 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400381#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000382 }
Dees_Troy8170a922012-09-18 15:40:25 -0400383#ifdef TW_INTERNAL_STORAGE_PATH
384 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
385 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500386 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400387 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500388 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400389 }
390#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000391 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400392 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500393 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500394 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400395 }
396#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400397 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400398 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400399 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500400 if (Mount_Point == "/boot") {
401 Display_Name = "Boot";
402 Backup_Display_Name = Display_Name;
403 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600404 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500405 } else if (Mount_Point == "/recovery") {
406 Display_Name = "Recovery";
407 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600408 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500409 } else if (Mount_Point == "/system_image") {
410 Display_Name = "System Image";
411 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500412 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500413 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500414 } else if (Mount_Point == "/vendor_image") {
415 Display_Name = "Vendor Image";
416 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500417 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500418 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500419 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400420 }
421
Dees_Troy51127312012-09-08 13:08:49 -0400422 // Process any custom flags
423 if (Flags.size() > 0)
424 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400425 return true;
426}
427
Ketut Putu Kumajayabfb72b02015-08-10 16:40:55 +0200428bool TWPartition::Process_FS_Flags(string& Options, int& Flags) {
Hashcode62bd9e02013-11-19 21:59:42 -0800429 int i;
430 char *p;
431 char *savep;
432 char fs_options[250];
433
434 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
435 Options = "";
436
437 p = strtok_r(fs_options, ",", &savep);
438 while (p) {
439 /* Look for the flag "p" in the flag list "fl"
440 * If not found, the loop exits with fl[i].name being null.
441 */
442 for (i = 0; mount_flags[i].name; i++) {
443 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
444 Flags |= mount_flags[i].flag;
445 break;
446 }
447 }
448
449 if (!mount_flags[i].name) {
450 if (Options.size() > 0)
451 Options += ",";
452 Options += p;
453 }
454 p = strtok_r(NULL, ",", &savep);
455 }
456
457 return true;
458}
459
Dees_Troy51127312012-09-08 13:08:49 -0400460bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
461 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500462 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400463 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500464 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400465
466 strcpy(flags, Flags.c_str());
467 flags_len = Flags.size();
468 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500469 if (flags[index] == 34)
470 skip = !skip;
471 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400472 flags[index] = '\0';
473 }
474
475 index = 0;
476 while (index < flags_len) {
477 while (index < flags_len && flags[index] == '\0')
478 index++;
479 if (index >= flags_len)
480 continue;
481 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500482 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400483 if (strcmp(ptr, "removable") == 0) {
484 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600485 } else if (strncmp(ptr, "storage", 7) == 0) {
486 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600487 Is_Storage = true;
488 } else if (ptr_len == 9) {
Gordon Freeman5da13022016-04-10 18:35:25 +0200489 ptr += 8;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600490 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
491 LOGINFO("storage set to true\n");
492 Is_Storage = true;
493 } else {
494 LOGINFO("storage set to false\n");
495 Is_Storage = false;
496 }
497 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500498 } else if (strcmp(ptr, "settingsstorage") == 0) {
499 Is_Storage = true;
Xueferbfce5042015-04-14 02:02:05 +0800500 Is_Settings_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500501 } else if (strcmp(ptr, "andsec") == 0) {
502 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400503 } else if (strcmp(ptr, "canbewiped") == 0) {
504 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700505 } else if (strcmp(ptr, "usermrf") == 0) {
506 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500507 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
508 ptr += 7;
509 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
510 Can_Be_Backed_Up = true;
511 else
512 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400513 } else if (strcmp(ptr, "wipeingui") == 0) {
514 Can_Be_Wiped = true;
515 Wipe_Available_in_GUI = true;
516 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
517 Can_Be_Wiped = true;
518 Wipe_Available_in_GUI = true;
519 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500520 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000521 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400522 Is_SubPartition = true;
523 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000524 } else if (strcmp(ptr, "ignoreblkid") == 0) {
525 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000526 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
527 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500528 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400529 ptr += 8;
530 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500531 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
532 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400533 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500534 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400535 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500536 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
537 Display_Name.resize(Display_Name.size() - 1);
538 }
Gordon Freeman5da13022016-04-10 18:35:25 +0200539 } else if (ptr_len > 12 && strncmp(ptr, "storagename=", 12) == 0) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500540 has_storage_name = true;
Gordon Freeman5da13022016-04-10 18:35:25 +0200541 ptr += 12;
Dees_Troya13d74f2013-03-24 08:54:55 -0500542 if (*ptr == '\"') ptr++;
543 Storage_Name = ptr;
544 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
545 Storage_Name.resize(Storage_Name.size() - 1);
546 }
Gordon Freeman5da13022016-04-10 18:35:25 +0200547 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 11) == 0) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500548 has_backup_name = true;
Gordon Freeman5da13022016-04-10 18:35:25 +0200549 ptr += 11;
Dees_Troya13d74f2013-03-24 08:54:55 -0500550 if (*ptr == '\"') ptr++;
551 Backup_Display_Name = ptr;
552 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
553 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
554 }
555 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400556 ptr += 10;
Ethan Yonkera2719152015-05-28 09:44:41 -0500557 Format_Block_Size = (unsigned long)(atol(ptr));
Dees_Troya13d74f2013-03-24 08:54:55 -0500558 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400559 ptr += 7;
560 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000561 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
562 ptr += 17;
563 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
564 Can_Encrypt_Backup = true;
565 else
566 Can_Encrypt_Backup = false;
Gordon Freeman5da13022016-04-10 18:35:25 +0200567 } else if (ptr_len > 22 && strncmp(ptr, "userdataencryptbackup=", 22) == 0) {
568 ptr += 22;
Dees_Troy83bd4832013-05-04 12:39:56 +0000569 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
570 Can_Encrypt_Backup = true;
571 Use_Userdata_Encryption = true;
572 } else {
573 Use_Userdata_Encryption = false;
574 }
Hashcode62bd9e02013-11-19 21:59:42 -0800575 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
576 ptr += 8;
577 if (*ptr == '\"') ptr++;
578
579 Mount_Options = ptr;
580 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
581 Mount_Options.resize(Mount_Options.size() - 1);
582 }
583 Process_FS_Flags(Mount_Options, Mount_Flags);
Gordon Freeman5da13022016-04-10 18:35:25 +0200584 } else if (ptr_len > 12 && strncmp(ptr, "encryptable=", 12) == 0) {
Ethan Yonker253368a2014-11-25 15:00:52 -0600585 ptr += 12;
Ethan Yonker253368a2014-11-25 15:00:52 -0600586 if (*ptr == '\"') ptr++;
Gordon Freeman5da13022016-04-10 18:35:25 +0200587 Crypto_Key_Location = ptr;
588 if (Crypto_Key_Location.substr(Crypto_Key_Location.size() - 1, 1) == "\"") {
589 Crypto_Key_Location.resize(Crypto_Key_Location.size() - 1);
590 }
591 } else if (ptr_len > 13 && strncmp(ptr, "forceencrypt=", 13) == 0) {
592 ptr += 13;
593 if (*ptr == '\"') ptr++;
Ethan Yonker253368a2014-11-25 15:00:52 -0600594 Crypto_Key_Location = ptr;
595 if (Crypto_Key_Location.substr(Crypto_Key_Location.size() - 1, 1) == "\"") {
596 Crypto_Key_Location.resize(Crypto_Key_Location.size() - 1);
597 }
598 } else if (ptr_len > 8 && strncmp(ptr, "mounttodecrypt", 14) == 0) {
599 Mount_To_Decrypt = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600600 } else if (strncmp(ptr, "flashimg", 8) == 0) {
601 if (ptr_len == 8) {
602 Can_Flash_Img = true;
603 } else if (ptr_len == 10) {
604 ptr += 9;
605 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
606 Can_Flash_Img = true;
607 } else {
608 Can_Flash_Img = false;
609 }
610 }
Dees_Troy51127312012-09-08 13:08:49 -0400611 } else {
612 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000613 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400614 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000615 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400616 }
617 while (index < flags_len && flags[index] != '\0')
618 index++;
619 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500620 if (has_display_name && !has_storage_name)
621 Storage_Name = Display_Name;
622 if (!has_display_name && has_storage_name)
623 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000624 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500625 Backup_Display_Name = Display_Name;
626 if (!has_display_name && has_backup_name)
627 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400628 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400629}
630
Dees_Troy5bf43922012-09-07 16:07:55 -0400631bool TWPartition::Is_File_System(string File_System) {
632 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400633 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400634 File_System == "ext4" ||
635 File_System == "vfat" ||
636 File_System == "ntfs" ||
637 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500638 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000639 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400640 File_System == "auto")
641 return true;
642 else
643 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400644}
645
Dees_Troy5bf43922012-09-07 16:07:55 -0400646bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400647 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400648 return true;
649 else
650 return false;
651}
652
Dees_Troy51127312012-09-08 13:08:49 -0400653bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400654 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400655 if (mkdir(Path.c_str(), 0777) == -1) {
656 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500657 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400658 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000659 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400660 return false;
661 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000662 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400663 return true;
664 }
665 }
666 return true;
667}
668
Dees_Troy5bf43922012-09-07 16:07:55 -0400669void TWPartition::Setup_File_System(bool Display_Error) {
670 struct statfs st;
671
672 Can_Be_Mounted = true;
673 Can_Be_Wiped = true;
674
Dees_Troy5bf43922012-09-07 16:07:55 -0400675 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400676 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400677 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
678 Backup_Name = Display_Name;
679 Backup_Method = FILES;
680}
681
682void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400683 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
684 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800685 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400686 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800687 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400688 Backup_Method = FLASH_UTILS;
689 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000690 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 -0400691 if (Find_Partition_Size()) {
692 Used = Size;
693 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400694 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400695 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000696 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400697 else
Dees Troyd932ce12013-10-18 17:12:59 +0000698 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400699 }
700}
701
Dees_Troye58d5262012-09-21 12:27:57 -0400702void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500703 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400704 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500705 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400706 Has_Android_Secure = true;
707 Symlink_Path = Mount_Point + "/.android_secure";
708 Symlink_Mount_Point = "/and-sec";
709 Backup_Path = Symlink_Mount_Point;
710 Make_Dir("/and-sec", true);
711 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600712 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400713}
714
that9e0593e2014-10-08 00:01:24 +0200715void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500716 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600717 if (Storage_Name.empty() || Storage_Name == "Data")
718 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500719 Has_Data_Media = true;
720 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600721 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500722 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600723 if (Mount_Point == "/data") {
724 Is_Settings_Storage = true;
725 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
726 Make_Dir("/emmc", false);
727 Symlink_Mount_Point = "/emmc";
728 } else {
729 Make_Dir("/sdcard", false);
730 Symlink_Mount_Point = "/sdcard";
731 }
732 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
733 Storage_Path = Mount_Point + "/media/0";
734 Symlink_Path = Storage_Path;
735 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
736 UnMount(true);
737 }
738 DataManager::SetValue("tw_has_internal", 1);
739 DataManager::SetValue("tw_has_data_media", 1);
740 du.add_absolute_dir(Mount_Point + "/misc/vold");
741 du.add_absolute_dir(Mount_Point + "/.layout_version");
742 du.add_absolute_dir(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500743 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600744 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
745 Storage_Path = Mount_Point + "/media/0";
746 Symlink_Path = Storage_Path;
747 UnMount(true);
748 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500749 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600750 du.add_absolute_dir(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500751}
752
Dees_Troy5bf43922012-09-07 16:07:55 -0400753void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
754 char device[512], realDevice[512];
755
756 strcpy(device, Block.c_str());
757 memset(realDevice, 0, sizeof(realDevice));
758 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
759 {
760 strcpy(device, realDevice);
761 memset(realDevice, 0, sizeof(realDevice));
762 }
763
764 if (device[0] != '/') {
765 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000766 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400767 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000768 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400769 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400770 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400771 Block = device;
772 return;
773 }
774}
775
Dees_Troy8e337f32012-10-13 22:07:49 -0400776void TWPartition::Mount_Storage_Retry(void) {
777 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500778 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400779 int retry_count = 5;
780 while (retry_count > 0 && !Mount(false)) {
781 usleep(500000);
782 retry_count--;
783 }
784 Mount(true);
785 }
786}
787
Dees_Troy38bd7602012-09-14 13:33:53 -0400788bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
789 FILE *fp = NULL;
790 char line[255];
791
792 fp = fopen("/proc/mtd", "rt");
793 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000794 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400795 return false;
796 }
797
798 while (fgets(line, sizeof(line), fp) != NULL)
799 {
800 char device[32], label[32];
801 unsigned long size = 0;
802 char* fstype = NULL;
803 int deviceId;
804
805 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
806
807 // Skip header and blank lines
808 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
809 continue;
810
811 // Strip off the trailing " from the label
812 label[strlen(label)-1] = '\0';
813
814 if (strcmp(label, MTD_Name.c_str()) == 0) {
815 // We found our device
816 // Strip off the trailing : from the device
817 device[strlen(device)-1] = '\0';
818 if (sscanf(device,"mtd%d", &deviceId) == 1) {
819 sprintf(device, "/dev/block/mtdblock%d", deviceId);
820 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000821 fclose(fp);
822 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400823 }
824 }
825 }
826 fclose(fp);
827
828 return false;
829}
830
Dees_Troy51127312012-09-08 13:08:49 -0400831bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
832 struct statfs st;
833 string Local_Path = Mount_Point + "/.";
834
835 if (!Mount(Display_Error))
836 return false;
837
838 if (statfs(Local_Path.c_str(), &st) != 0) {
839 if (!Removable) {
840 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000841 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400842 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000843 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400844 }
845 return false;
846 }
847 Size = (st.f_blocks * st.f_bsize);
848 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
849 Free = (st.f_bfree * st.f_bsize);
850 Backup_Size = Used;
851 return true;
852}
853
854bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400855 FILE* fp;
856 char command[255], line[512];
857 int include_block = 1;
858 unsigned int min_len;
859
860 if (!Mount(Display_Error))
861 return false;
862
Dees_Troy38bd7602012-09-14 13:33:53 -0400863 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400864 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200865 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400866 fp = fopen("/tmp/dfoutput.txt", "rt");
867 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000868 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400869 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400870 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400871
872 while (fgets(line, sizeof(line), fp) != NULL)
873 {
874 unsigned long blocks, used, available;
875 char device[64];
876 char tmpString[64];
877
878 if (strncmp(line, "Filesystem", 10) == 0)
879 continue;
880 if (strlen(line) < min_len) {
881 include_block = 0;
882 continue;
883 }
884 if (include_block) {
885 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
886 } else {
887 // The device block string is so long that the df information is on the next line
888 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400889 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400890 while (tmpString[space_count] == 32)
891 space_count++;
892 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
893 }
894
895 // Adjust block size to byte size
896 Size = blocks * 1024ULL;
897 Used = used * 1024ULL;
898 Free = available * 1024ULL;
899 Backup_Size = Used;
900 }
901 fclose(fp);
902 return true;
903}
904
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600905unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600906 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -0600907
908 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600909}
910
Dees_Troy5bf43922012-09-07 16:07:55 -0400911bool TWPartition::Find_Partition_Size(void) {
912 FILE* fp;
913 char line[512];
914 string tmpdevice;
915
igoriok87e3d932013-01-31 21:03:53 +0200916 fp = fopen("/proc/dumchar_info", "rt");
917 if (fp != NULL) {
918 while (fgets(line, sizeof(line), fp) != NULL)
919 {
920 char label[32], device[32];
921 unsigned long size = 0;
922
thatd43bf2d2014-09-21 23:13:02 +0200923 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200924
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500925 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200926 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
927 continue;
928
929 tmpdevice = "/dev/";
930 tmpdevice += label;
931 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
932 Size = size;
933 fclose(fp);
934 return true;
935 }
936 }
937 }
938
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600939 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
940 if (ioctl_size) {
941 Size = ioctl_size;
942 return true;
943 }
944
Dees_Troy5bf43922012-09-07 16:07:55 -0400945 // In this case, we'll first get the partitions we care about (with labels)
946 fp = fopen("/proc/partitions", "rt");
947 if (fp == NULL)
948 return false;
949
950 while (fgets(line, sizeof(line), fp) != NULL)
951 {
952 unsigned long major, minor, blocks;
953 char device[512];
954 char tmpString[64];
955
Dees_Troy63c8df72012-09-10 14:02:05 -0400956 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400957 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
958
959 tmpdevice = "/dev/block/";
960 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400961 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400962 // Adjust block size to byte size
963 Size = blocks * 1024ULL;
964 fclose(fp);
965 return true;
966 }
967 }
968 fclose(fp);
969 return false;
970}
971
Dees_Troy5bf43922012-09-07 16:07:55 -0400972bool TWPartition::Is_Mounted(void) {
973 if (!Can_Be_Mounted)
974 return false;
975
976 struct stat st1, st2;
977 string test_path;
978
979 // Check to see if the mount point directory exists
980 test_path = Mount_Point + "/.";
981 if (stat(test_path.c_str(), &st1) != 0) return false;
982
983 // Check to see if the directory above the mount point exists
984 test_path = Mount_Point + "/../.";
985 if (stat(test_path.c_str(), &st2) != 0) return false;
986
987 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
988 int ret = (st1.st_dev != st2.st_dev) ? true : false;
989
990 return ret;
991}
992
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -0500993bool TWPartition::Is_File_System_Writable(void) {
994 if (!Is_File_System(Current_File_System) || !Is_Mounted())
995 return false;
996
997 string test_path = Mount_Point + "/.";
998 return (access(test_path.c_str(), W_OK) == 0);
999}
1000
Dees_Troy5bf43922012-09-07 16:07:55 -04001001bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001002 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001003 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001004
Dees_Troy5bf43922012-09-07 16:07:55 -04001005 if (Is_Mounted()) {
1006 return true;
1007 } else if (!Can_Be_Mounted) {
1008 return false;
1009 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001010
1011 Find_Actual_Block_Device();
1012
1013 // Check the current file system before mounting
1014 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001015 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001016 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 +00001017 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001018 string result;
1019 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001020 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001021 Current_File_System = "vfat";
1022 } else {
1023#ifdef TW_NO_EXFAT_FUSE
1024 UnMount(false);
1025 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1026 // Some kernels let us mount vfat as exfat which doesn't work out too well
1027#else
1028 exfat_mounted = 1;
1029#endif
1030 }
1031 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001032
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001033 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001034 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001035 string Ntfsmount_Binary = "";
1036
1037 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1038 Ntfsmount_Binary = "ntfs-3g";
1039 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1040 Ntfsmount_Binary = "mount.ntfs";
1041
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001042 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001043 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001044 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001045 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001046 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001047
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001048 if (TWFunc::Exec_Cmd(cmd) == 0) {
1049 return true;
1050 } else {
1051 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1052 }
1053 }
1054
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001055 if (Mount_Read_Only)
1056 flags |= MS_RDONLY;
1057
Dees_Troy22042032012-12-18 21:23:08 +00001058 if (Fstab_File_System == "yaffs2") {
1059 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001060 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1061 if (Mount_Read_Only)
1062 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001063 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1064 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1065 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001066 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001067 else
1068 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1069 return false;
1070 } else {
1071 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1072 return true;
1073 }
1074 } else {
1075 struct stat st;
1076 string test_path = Mount_Point;
1077 if (stat(test_path.c_str(), &st) < 0) {
1078 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001079 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001080 else
1081 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1082 return false;
1083 }
1084 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1085 if (new_mode != st.st_mode) {
1086 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1087 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1088 if (Display_Error)
1089 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1090 else
1091 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1092 return false;
1093 }
1094 }
Dees_Troy22042032012-12-18 21:23:08 +00001095 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001096 }
thatc7572eb2015-03-31 18:55:30 +02001097 }
1098
1099 string mount_fs = Current_File_System;
1100 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1101 mount_fs = "texfat";
1102
1103 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001104 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1105 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001106#ifdef TW_NO_EXFAT_FUSE
1107 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001108 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001109 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001110 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001111 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001112 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001113 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001114 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 +00001115 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001116 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001117 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001118#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001119 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001120 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001121 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001122 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1123 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 +00001124 return false;
1125#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001126 }
1127#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001128 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001129
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001130 if (Removable)
1131 Update_Size(Display_Error);
1132
xiaolu9416f4f2015-06-04 08:22:23 +08001133 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001134 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001135 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001136 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001137 return true;
1138}
1139
1140bool TWPartition::UnMount(bool Display_Error) {
1141 if (Is_Mounted()) {
1142 int never_unmount_system;
1143
1144 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1145 if (never_unmount_system == 1 && Mount_Point == "/system")
1146 return true; // Never unmount system if you're not supposed to unmount it
1147
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001148 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001149 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001150
Dees_Troy38bd7602012-09-14 13:33:53 -04001151 if (!Symlink_Mount_Point.empty())
1152 umount(Symlink_Mount_Point.c_str());
1153
Dees_Troyb05ddee2013-01-28 20:24:50 +00001154 umount(Mount_Point.c_str());
1155 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001156 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001157 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001158 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001159 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001160 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001161 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001162 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001163 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001164 } else {
1165 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001166 }
1167}
1168
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001169bool TWPartition::ReMount(bool Display_Error) {
1170 if (UnMount(Display_Error))
1171 return Mount(Display_Error);
1172 return false;
1173}
1174
1175bool TWPartition::ReMount_RW(bool Display_Error) {
1176 // No need to remount if already mounted rw
1177 if (Is_File_System_Writable())
1178 return true;
1179
1180 bool ro = Mount_Read_Only;
1181 int flags = Mount_Flags;
1182
1183 Mount_Read_Only = false;
1184 Mount_Flags &= ~MS_RDONLY;
1185
1186 bool ret = ReMount(Display_Error);
1187
1188 Mount_Read_Only = ro;
1189 Mount_Flags = flags;
1190
1191 return ret;
1192}
1193
Gary Peck43acadf2012-11-21 21:19:01 -08001194bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001195 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001196 int check;
1197 string Layout_Filename = Mount_Point + "/.layout_version";
1198
Dees_Troy38bd7602012-09-14 13:33:53 -04001199 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001200 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001201 return false;
1202 }
1203
Dees_Troyc51f1f92012-09-20 15:32:13 -04001204 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001205 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001206
Dees_Troy16c2b312013-01-15 16:51:18 +00001207 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1208 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1209 else
1210 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001211
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001212 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001213 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001214 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001215 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001216 DataManager::GetValue(TW_RM_RF_VAR, check);
1217
Hashcodedabfd492013-08-29 22:45:30 -07001218 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001219 wiped = Wipe_RMRF();
1220 else if (New_File_System == "ext4")
1221 wiped = Wipe_EXT4();
1222 else if (New_File_System == "ext2" || New_File_System == "ext3")
1223 wiped = Wipe_EXT23(New_File_System);
1224 else if (New_File_System == "vfat")
1225 wiped = Wipe_FAT();
1226 else if (New_File_System == "exfat")
1227 wiped = Wipe_EXFAT();
1228 else if (New_File_System == "yaffs2")
1229 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001230 else if (New_File_System == "f2fs")
1231 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001232 else if (New_File_System == "ntfs")
1233 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001234 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001235 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 +00001236 unlink("/.layout_version");
1237 return false;
1238 }
1239 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001240 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001241
Gary Pecke8bc5d72012-12-21 06:45:25 -08001242 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001243 if (Mount_Point == "/cache")
1244 DataManager::Output_Version();
1245
Dees_Troy16c2b312013-01-15 16:51:18 +00001246 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1247 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1248
1249 if (update_crypt) {
1250 Setup_File_System(false);
1251 if (Is_Encrypted && !Is_Decrypted) {
1252 // just wiped an encrypted partition back to its unencrypted state
1253 Is_Encrypted = false;
1254 Is_Decrypted = false;
1255 Decrypted_Block_Device = "";
1256 if (Mount_Point == "/data") {
1257 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1258 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1259 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001260 }
1261 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001262
Ethan Yonker66a19492015-12-10 10:19:45 -06001263 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001264 Recreate_Media_Folder();
1265 }
Matt Mower209c9632016-01-20 12:08:35 -06001266 if (Is_Storage && Mount(false))
1267 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001268 }
Matt Mower209c9632016-01-20 12:08:35 -06001269
Gary Pecke8bc5d72012-12-21 06:45:25 -08001270 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001271}
1272
Gary Peck43acadf2012-11-21 21:19:01 -08001273bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001274 if (Is_File_System(Current_File_System))
1275 return Wipe(Current_File_System);
1276 else
1277 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001278}
1279
Dees_Troye58d5262012-09-21 12:27:57 -04001280bool TWPartition::Wipe_AndSec(void) {
1281 if (!Has_Android_Secure)
1282 return false;
1283
Dees_Troye58d5262012-09-21 12:27:57 -04001284 if (!Mount(true))
1285 return false;
1286
Ethan Yonker74db1572015-10-28 12:44:49 -05001287 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001288 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001289 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001290}
1291
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001292bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001293 if (Mount_Read_Only)
1294 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001295 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001296 return true;
1297 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1298 return true;
1299 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1300 return true;
1301 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1302 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001303 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001304 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001305 return false;
1306}
1307
1308bool TWPartition::Repair() {
1309 string command;
1310
1311 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001312 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001313 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001314 return false;
1315 }
1316 if (!UnMount(true))
1317 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001318 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001319 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001320 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001321 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 Yonker87c7bac2014-05-25 21:41:08 -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 Yonker87c7bac2014-05-25 21:41:08 -05001327 return false;
1328 }
1329 }
1330 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1331 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001332 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001333 return false;
1334 }
1335 if (!UnMount(true))
1336 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001337 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001338 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001339 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001340 LOGINFO("Repair command: %s\n", command.c_str());
1341 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001342 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001343 return true;
1344 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001345 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001346 return false;
1347 }
1348 }
1349 if (Current_File_System == "exfat") {
1350 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001351 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001352 return false;
1353 }
1354 if (!UnMount(true))
1355 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001356 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001357 Find_Actual_Block_Device();
1358 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1359 LOGINFO("Repair command: %s\n", command.c_str());
1360 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001361 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001362 return true;
1363 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001364 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001365 return false;
1366 }
1367 }
1368 if (Current_File_System == "f2fs") {
1369 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001370 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001371 return false;
1372 }
1373 if (!UnMount(true))
1374 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001375 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001376 Find_Actual_Block_Device();
1377 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1378 LOGINFO("Repair command: %s\n", command.c_str());
1379 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001380 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001381 return true;
1382 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001383 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001384 return false;
1385 }
1386 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001387 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001388 string Ntfsfix_Binary;
1389 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1390 Ntfsfix_Binary = "ntfsfix";
1391 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1392 Ntfsfix_Binary = "fsck.ntfs";
1393 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001394 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001395 return false;
1396 }
1397 if (!UnMount(true))
1398 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001399 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001400 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001401 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001402 LOGINFO("Repair command: %s\n", command.c_str());
1403 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001404 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001405 return true;
1406 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001407 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001408 return false;
1409 }
1410 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001411 return false;
1412}
1413
Ethan Yonkera2719152015-05-28 09:44:41 -05001414bool TWPartition::Can_Resize() {
1415 if (Mount_Read_Only)
1416 return false;
1417 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1418 return true;
1419 return false;
1420}
1421
1422bool TWPartition::Resize() {
1423 string command;
1424
1425 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1426 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001427 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1428 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001429 return false;
1430 }
1431 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001432 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1433 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001434 return false;
1435 }
1436 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001437 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001438 if (!Repair())
1439 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001440 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001441 Find_Actual_Block_Device();
1442 command = "/sbin/resize2fs " + Actual_Block_Device;
1443 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001444 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1445 if (Actual_Size == 0)
1446 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001447
Ethan Yonkera2719152015-05-28 09:44:41 -05001448 unsigned long long Block_Count;
1449 if (Length < 0) {
1450 // Reduce overall size by this length
1451 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1452 } else {
1453 // This is the size, not a size reduction
1454 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1455 }
1456 char temp[256];
1457 sprintf(temp, "%llu", Block_Count);
1458 command += " ";
1459 command += temp;
1460 command += "K";
1461 }
1462 LOGINFO("Resize command: %s\n", command.c_str());
1463 if (TWFunc::Exec_Cmd(command) == 0) {
1464 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001465 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001466 return true;
1467 } else {
1468 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001469 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001470 return false;
1471 }
1472 }
1473 return false;
1474}
1475
Ethan Yonker472f5062016-02-25 13:47:30 -06001476bool TWPartition::Backup(const string& backup_folder, pid_t &tar_fork_pid, ProgressTracking *progress) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001477 if (Backup_Method == FILES) {
Ethan Yonker472f5062016-02-25 13:47:30 -06001478 return Backup_Tar(backup_folder, progress, tar_fork_pid);
bigbiff7abc5fe2015-01-17 16:53:12 -05001479 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001480 else if (Backup_Method == DD)
Ethan Yonker472f5062016-02-25 13:47:30 -06001481 return Backup_Image(backup_folder, progress);
Dees_Troy38bd7602012-09-14 13:33:53 -04001482 else if (Backup_Method == FLASH_UTILS)
Ethan Yonker472f5062016-02-25 13:47:30 -06001483 return Backup_Dump_Image(backup_folder, progress);
Dees_Troy2673cec2013-04-02 20:22:16 +00001484 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001485 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001486}
1487
Dees_Troy43d8b002012-09-17 16:00:01 -04001488bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001489 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001490 char split_filename[512];
1491 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001492 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001493
Captain Throwbackff5935f2014-09-23 16:08:34 -04001494 sync();
1495
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001496 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001497 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001498 if (!TWFunc::Path_Exists(Full_Filename)) {
1499 // This is a split archive, we presume
1500 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001501 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001502 md5file = split_filename;
1503 md5file += ".md5";
1504 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001505 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 -04001506 return false;
1507 }
1508 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001509 while (index < 1000) {
1510 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001511 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001512 return false;
1513 }
1514 index++;
1515 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001516 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001517 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001518 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001519 } else {
1520 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001521 md5file = Full_Filename + ".md5";
1522 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001523 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 -04001524 return false;
1525 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001526 md5sum.setfn(Full_Filename);
1527 if (md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001528 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001529 return false;
1530 } else
1531 return true;
1532 }
1533 return false;
1534}
1535
Ethan Yonker472f5062016-02-25 13:47:30 -06001536bool TWPartition::Restore(const string& restore_folder, ProgressTracking *progress) {
Gary Peck43acadf2012-11-21 21:19:01 -08001537 string Restore_File_System;
1538
Matt Mower3c366972015-12-25 19:28:31 -06001539 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Dees_Troy2673cec2013-04-02 20:22:16 +00001540 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001541
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001542 Restore_File_System = Get_Restore_File_System(restore_folder);
1543
1544 if (Is_File_System(Restore_File_System))
Ethan Yonker472f5062016-02-25 13:47:30 -06001545 return Restore_Tar(restore_folder, Restore_File_System, progress);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001546 else if (Is_Image(Restore_File_System)) {
Ethan Yonker472f5062016-02-25 13:47:30 -06001547 return Restore_Image(restore_folder, Restore_File_System, progress);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001548 }
1549
1550 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1551 return false;
1552}
1553
Ethan Yonker472f5062016-02-25 13:47:30 -06001554string TWPartition::Get_Restore_File_System(const string& restore_folder) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001555 size_t first_period, second_period;
1556 string Restore_File_System;
1557
Gary Peck43acadf2012-11-21 21:19:01 -08001558 // Parse backup filename to extract the file system before wiping
1559 first_period = Backup_FileName.find(".");
1560 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001561 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001562 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001563 }
1564 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1565 second_period = Restore_File_System.find(".");
1566 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001567 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001568 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001569 }
1570 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001571 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001572 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001573}
1574
1575string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001576 if (Backup_Method == NONE)
1577 return "none";
1578 else if (Backup_Method == FILES)
1579 return "files";
1580 else if (Backup_Method == DD)
1581 return "dd";
1582 else if (Backup_Method == FLASH_UTILS)
1583 return "flash_utils";
1584 else
1585 return "undefined";
1586 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001587}
1588
1589bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001590 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001591 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001592 return 1;
1593}
1594
1595bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001596 bool Save_Data_Media = Has_Data_Media;
1597
1598 if (!UnMount(true))
1599 return false;
1600
Dees_Troy38bd7602012-09-14 13:33:53 -04001601 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001602 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001603#ifdef TW_INCLUDE_CRYPTO
1604 if (Is_Decrypted) {
1605 if (!UnMount(true))
1606 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001607 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001608 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1609 }
1610 }
1611#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001612 Is_Decrypted = false;
1613 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001614 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001615 if (Crypto_Key_Location == "footer") {
1616 int newlen, fd;
1617 if (Length != 0) {
1618 newlen = Length;
1619 if (newlen < 0)
1620 newlen = newlen * -1;
1621 } else {
1622 newlen = CRYPT_FOOTER_OFFSET;
1623 }
1624 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1625 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1626 } else {
1627 unsigned int block_count;
1628 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1629 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1630 } else {
1631 off64_t offset = ((off64_t)block_count * 512) - newlen;
1632 if (lseek64(fd, offset, SEEK_SET) == -1) {
1633 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1634 } else {
1635 void* buffer = malloc(newlen);
1636 if (!buffer) {
1637 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1638 } else {
1639 memset(buffer, 0, newlen);
1640 int ret = write(fd, buffer, newlen);
1641 if (ret != newlen) {
1642 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1643 } else {
1644 LOGINFO("Successfully wiped crypto footer.\n");
1645 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001646 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001647 }
1648 }
1649 }
1650 close(fd);
1651 }
1652 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001653 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1654 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1655 TWFunc::Exec_Cmd(Command);
1656 } else {
1657 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1658 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001659 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001660 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001661 Has_Data_Media = Save_Data_Media;
1662 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1663 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001664 if (Mount(false))
1665 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001666 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001667 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001668#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001669 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 -05001670#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001671 return true;
1672 } else {
1673 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001674 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001675 if (Has_Data_Media && Mount(false))
1676 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001677 return false;
1678 }
1679 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001680}
1681
1682void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001683 const char* type;
1684 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001685
Dees_Troy68cab492012-12-12 19:29:35 +00001686 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1687 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001688
Dees_Troy38bd7602012-09-14 13:33:53 -04001689 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001690 if (!Is_Present)
1691 return;
Dees_Troy51127312012-09-08 13:08:49 -04001692
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001693 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1694 if (blkid_do_fullprobe(pr)) {
1695 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001696 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001697 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001698 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001699
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001700 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001701 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001702 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001703 return;
1704 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001705
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001706 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001707 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001708}
1709
Gary Peck43acadf2012-11-21 21:19:01 -08001710bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001711 if (!UnMount(true))
1712 return false;
1713
Dees_Troy43d8b002012-09-17 16:00:01 -04001714 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001715 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001716
Greg Wallace4b44fef2015-12-29 15:33:24 -05001717 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001718 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001719 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001720 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001721 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001722 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001723 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001724 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001725 return true;
1726 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001727 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001728 return false;
1729 }
1730 } else
1731 return Wipe_RMRF();
1732
1733 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001734}
1735
1736bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001737 Find_Actual_Block_Device();
1738 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001739 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1740 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001741 return false;
1742 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001743 if (!UnMount(true))
1744 return false;
1745
Dees_Troyb3265ab2013-08-30 02:59:14 +00001746#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001747 int ret;
1748 char *secontext = NULL;
1749
Greg Wallace4b44fef2015-12-29 15:33:24 -05001750 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001751
Dees Troy99c8dbf2014-03-10 16:53:58 +00001752 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001753 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1754 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1755 } else {
1756 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1757 }
1758 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001759 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001760 return false;
1761 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001762 string sedir = Mount_Point + "/lost+found";
1763 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1764 rmdir(sedir.c_str());
1765 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001766 return true;
1767 }
1768#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001769 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001770 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001771
Greg Wallace4b44fef2015-12-29 15:33:24 -05001772 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001773 Find_Actual_Block_Device();
1774 Command = "make_ext4fs";
1775 if (!Is_Decrypted && Length != 0) {
1776 // Only use length if we're not decrypted
1777 char len[32];
1778 sprintf(len, "%i", Length);
1779 Command += " -l ";
1780 Command += len;
1781 }
Dees_Troy5295d582013-09-06 15:51:08 +00001782 if (TWFunc::Path_Exists("/file_contexts")) {
1783 Command += " -S /file_contexts";
1784 }
1785 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001786 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001787 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001788 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001789 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001790 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001791 return true;
1792 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001793 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001794 return false;
1795 }
1796 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001797 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001798#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001799 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001800}
1801
1802bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001803 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001804
Matt Mower18794c82015-11-11 16:22:45 -06001805 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001806 if (!UnMount(true))
1807 return false;
1808
Greg Wallace4b44fef2015-12-29 15:33:24 -05001809 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001810 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001811 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001812 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001813 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001814 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001815 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001816 return true;
1817 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001818 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001819 return false;
1820 }
1821 return true;
1822 }
1823 else
1824 return Wipe_RMRF();
1825
1826 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001827}
1828
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001829bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001830 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001831
1832 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1833 if (!UnMount(true))
1834 return false;
1835
Greg Wallace4b44fef2015-12-29 15:33:24 -05001836 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001837 Find_Actual_Block_Device();
1838 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001839 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001840 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001841 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001842 return true;
1843 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001844 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001845 return false;
1846 }
1847 return true;
1848 }
1849 return false;
1850}
1851
Dees_Troy38bd7602012-09-14 13:33:53 -04001852bool TWPartition::Wipe_MTD() {
1853 if (!UnMount(true))
1854 return false;
1855
Greg Wallace4b44fef2015-12-29 15:33:24 -05001856 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001857
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001858 mtd_scan_partitions();
1859 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1860 if (mtd == NULL) {
1861 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1862 return false;
1863 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001864
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001865 MtdWriteContext* ctx = mtd_write_partition(mtd);
1866 if (ctx == NULL) {
1867 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1868 return false;
1869 }
1870 if (mtd_erase_blocks(ctx, -1) == -1) {
1871 mtd_write_close(ctx);
1872 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1873 return false;
1874 }
1875 if (mtd_write_close(ctx) != 0) {
1876 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1877 return false;
1878 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001879 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001880 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001881 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001882 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001883}
1884
1885bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001886 if (!Mount(true))
1887 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06001888 // This is the only wipe that leaves the partition mounted, so we
1889 // must manually remove the partition from MTP if it is a storage
1890 // partition.
1891 if (Is_Storage)
1892 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001893
Ethan Yonker74db1572015-10-28 12:44:49 -05001894 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001895 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001896 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001897 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001898}
1899
Dees_Troye5017042013-08-29 16:38:55 +00001900bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001901 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001902
1903 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1904 if (!UnMount(true))
1905 return false;
1906
Greg Wallace4b44fef2015-12-29 15:33:24 -05001907 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00001908 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05001909 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05001910 if (!Is_Decrypted && Length != 0) {
1911 // Only use length if we're not decrypted
1912 char len[32];
1913 int mod_length = Length;
1914 if (Length < 0)
1915 mod_length *= -1;
1916 sprintf(len, "%i", mod_length);
1917 command += " -r ";
1918 command += len;
1919 }
1920 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001921 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001922 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001923 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00001924 return true;
1925 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001926 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00001927 return false;
1928 }
1929 return true;
1930 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001931 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00001932 return Wipe_RMRF();
1933 }
1934 return false;
1935}
1936
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001937bool TWPartition::Wipe_NTFS() {
1938 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001939 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001940
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001941 if (TWFunc::Path_Exists("/sbin/mkntfs"))
1942 Ntfsmake_Binary = "mkntfs";
1943 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
1944 Ntfsmake_Binary = "mkfs.ntfs";
1945 else
1946 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001947
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001948 if (!UnMount(true))
1949 return false;
1950
Greg Wallace4b44fef2015-12-29 15:33:24 -05001951 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001952 Find_Actual_Block_Device();
1953 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
1954 if (TWFunc::Exec_Cmd(command) == 0) {
1955 Recreate_AndSec_Folder();
1956 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001957 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001958 } else {
1959 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
1960 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001961 }
1962 return false;
1963}
1964
Dees_Troy51a0e822012-09-05 15:24:24 -04001965bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001966#ifdef TW_OEM_BUILD
1967 // In an OEM Build we want to do a full format
1968 return Wipe_Encryption();
1969#else
Ethan Yonker66a19492015-12-10 10:19:45 -06001970 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001971
Dees_Troy38bd7602012-09-14 13:33:53 -04001972 if (!Mount(true))
1973 return false;
1974
Ethan Yonker74db1572015-10-28 12:44:49 -05001975 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06001976 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
1977 if (ret)
1978 gui_msg("done=Done.");
1979 return ret;
1980#endif // ifdef TW_OEM_BUILD
1981}
1982
1983bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
1984 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001985
1986 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06001987 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00001988 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001989 struct dirent* de;
1990 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001991 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001992
Ethan Yonker66a19492015-12-10 10:19:45 -06001993 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001994 dir.append(de->d_name);
Ethan Yonker66a19492015-12-10 10:19:45 -06001995 if (du.check_skip_dirs(dir)) {
1996 LOGINFO("skipped '%s'\n", dir.c_str());
1997 continue;
1998 }
Dees_Troyce675462013-01-09 19:48:21 +00001999 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002000 dir.append("/");
2001 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2002 closedir(d);
2003 return false;
2004 }
2005 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002006 } 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 +00002007 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00002008 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00002009 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002010 }
2011 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002012
Dees_Troy16b74352012-11-14 22:27:31 +00002013 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002014 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002015 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002016 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002017}
2018
Ethan Yonker472f5062016-02-25 13:47:30 -06002019bool TWPartition::Backup_Tar(const string& backup_folder, ProgressTracking *progress, pid_t &tar_fork_pid) {
2020 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002021 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002022
Dees_Troy43d8b002012-09-17 16:00:01 -04002023 if (!Mount(true))
2024 return false;
2025
Dees_Troya13d74f2013-03-24 08:54:55 -05002026 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002027 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002028
Ethan Yonker472f5062016-02-25 13:47:30 -06002029 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002030
Dees_Troy83bd4832013-05-04 12:39:56 +00002031#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002032 if (Can_Encrypt_Backup) {
2033 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2034 if (tar.use_encryption) {
2035 if (Use_Userdata_Encryption)
2036 tar.userdata_encryption = tar.use_encryption;
2037 string Password;
2038 DataManager::GetValue("tw_backup_password", Password);
2039 tar.setpassword(Password);
2040 } else {
2041 tar.use_encryption = 0;
2042 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002043 }
2044#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002045
Ethan Yonker472f5062016-02-25 13:47:30 -06002046 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002047 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00002048 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00002049 Full_FileName = backup_folder + "/" + Backup_FileName;
2050 tar.setdir(Backup_Path);
2051 tar.setfn(Full_FileName);
2052 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002053 tar.partition_name = Backup_Name;
2054 tar.backup_folder = backup_folder;
Ethan Yonker472f5062016-02-25 13:47:30 -06002055 if (tar.createTarFork(progress, tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002056 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002057 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002058}
2059
Ethan Yonker472f5062016-02-25 13:47:30 -06002060bool TWPartition::Backup_Image(const string& backup_folder, ProgressTracking *progress) {
2061 string Full_FileName;
igoriok87e3d932013-01-31 21:03:53 +02002062
Ethan Yonker74db1572015-10-28 12:44:49 -05002063 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2064 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002065
Ethan Yonker472f5062016-02-25 13:47:30 -06002066 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002067 Full_FileName = backup_folder + "/" + Backup_FileName;
2068
Ethan Yonker472f5062016-02-25 13:47:30 -06002069 if (!Raw_Read_Write(Actual_Block_Device, Full_FileName, Backup_Size, progress))
2070 return false;
2071
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002072 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04002073 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002074 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002075 return false;
2076 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002077 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002078}
2079
Ethan Yonker472f5062016-02-25 13:47:30 -06002080bool TWPartition::Raw_Read_Write(const string& input_file, const string& output_file, const unsigned long long input_size, ProgressTracking *progress) {
2081 unsigned long long RW_Block_Size, Remain;
2082 int src_fd = -1, dest_fd = -1, bs;
2083 bool ret = false;
2084 void* buffer = NULL;
2085 unsigned long long backedup_size = 0;
2086
2087 RW_Block_Size = 1048576LLU; // 1MB
2088 Remain = input_size;
2089
2090 src_fd = open(input_file.c_str(), O_RDONLY | O_LARGEFILE);
2091 if (src_fd < 0) {
2092 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(input_file)(strerror(errno)));
2093 return false;
2094 }
2095 dest_fd = open(output_file.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRUSR | S_IWUSR);
2096 if (dest_fd < 0) {
2097 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(output_file)(strerror(errno)));
2098 goto exit;
2099 }
2100 bs = (int)(RW_Block_Size);
2101 buffer = malloc((size_t)bs);
2102 if (!buffer) {
2103 LOGINFO("Raw_Read_Write failed to malloc\n");
2104 goto exit;
2105 }
2106 LOGINFO("Reading '%s', writing '%s'\n", input_file.c_str(), output_file.c_str());
2107 if (progress)
2108 progress->SetPartitionSize(input_size);
2109 while (Remain > 0) {
2110 if (Remain < RW_Block_Size)
2111 bs = (int)(Remain);
2112 if (read(src_fd, buffer, bs) != bs) {
2113 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2114 goto exit;
2115 }
2116 if (write(dest_fd, buffer, bs) != bs) {
2117 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2118 goto exit;
2119 }
2120 backedup_size += (unsigned long long)(bs);
2121 Remain -= (unsigned long long)(bs);
2122 if (progress)
2123 progress->UpdateSize(backedup_size);
2124 if (PartitionManager.Check_Backup_Cancel() != 0)
2125 goto exit;
2126 }
2127 if (progress)
2128 progress->UpdateDisplayDetails(true);
2129 fsync(dest_fd);
2130 ret = true;
2131exit:
2132 if (src_fd >= 0)
2133 close(src_fd);
2134 if (dest_fd >= 0)
2135 close(dest_fd);
2136 if (buffer)
2137 free(buffer);
2138 return ret;
2139}
2140
2141bool TWPartition::Backup_Dump_Image(const string& backup_folder, ProgressTracking *progress) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002142 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002143
Ethan Yonker74db1572015-10-28 12:44:49 -05002144 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2145 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002146
Ethan Yonker472f5062016-02-25 13:47:30 -06002147 if (progress)
2148 progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002149
Ethan Yonker472f5062016-02-25 13:47:30 -06002150 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002151 Full_FileName = backup_folder + "/" + Backup_FileName;
2152
2153 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00002154 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002155 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002156 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002157 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2158 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
Matt Mower3c366972015-12-25 19:28:31 -06002159 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002160 return false;
2161 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002162 if (progress)
2163 progress->UpdateSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002164 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002165}
2166
Ethan Yonker472f5062016-02-25 13:47:30 -06002167unsigned long long TWPartition::Get_Restore_Size(const string& restore_folder) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002168 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
2169 if (restore_info.LoadValues() == 0) {
2170 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2171 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2172 return Restore_Size;
2173 }
2174 }
2175 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
2176
2177 Full_FileName = restore_folder + "/" + Backup_FileName;
2178
2179 if (Is_Image(Restore_File_System)) {
2180 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2181 return Restore_Size;
2182 }
2183
2184 twrpTar tar;
2185 tar.setdir(Backup_Path);
2186 tar.setfn(Full_FileName);
2187 tar.backup_name = Backup_Name;
2188#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2189 string Password;
2190 DataManager::GetValue("tw_restore_password", Password);
2191 if (!Password.empty())
2192 tar.setpassword(Password);
2193#endif
2194 tar.partition_name = Backup_Name;
2195 tar.backup_folder = restore_folder;
2196 Restore_Size = tar.get_size();
2197 return Restore_Size;
2198}
2199
Ethan Yonker472f5062016-02-25 13:47:30 -06002200bool TWPartition::Restore_Tar(const string& restore_folder, const string& Restore_File_System, ProgressTracking *progress) {
2201 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002202 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002203
Dees_Troye58d5262012-09-21 12:27:57 -04002204 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002205 if (!Wipe_AndSec())
2206 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002207 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002208 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002209 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002210 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 -05002211 if (!Wipe_Data_Without_Wiping_Media())
2212 return false;
2213 } else {
2214 if (!Wipe(Restore_File_System))
2215 return false;
2216 }
Dees_Troye58d5262012-09-21 12:27:57 -04002217 }
Matt Mower3c366972015-12-25 19:28:31 -06002218 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002219 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002220
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002221 // Remount as read/write as needed so we can restore the backup
2222 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002223 return false;
2224
Dees_Troy4a2a1262012-09-18 09:33:47 -04002225 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002226 twrpTar tar;
2227 tar.setdir(Backup_Path);
2228 tar.setfn(Full_FileName);
2229 tar.backup_name = Backup_Name;
2230#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2231 string Password;
2232 DataManager::GetValue("tw_restore_password", Password);
2233 if (!Password.empty())
2234 tar.setpassword(Password);
2235#endif
Ethan Yonker472f5062016-02-25 13:47:30 -06002236 progress->SetPartitionSize(Get_Restore_Size(restore_folder));
2237 if (tar.extractTarFork(progress) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002238 ret = false;
2239 else
2240 ret = true;
2241#ifdef HAVE_CAPABILITIES
2242 // Restore capabilities to the run-as binary
2243 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2244 struct vfs_cap_data cap_data;
2245 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2246
2247 memset(&cap_data, 0, sizeof(cap_data));
2248 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2249 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2250 cap_data.data[0].inheritable = 0;
2251 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2252 cap_data.data[1].inheritable = 0;
2253 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2254 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2255 } else {
2256 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2257 }
2258 }
2259#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002260 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2261 // Remount as read only when restoration is complete
2262 ReMount(true);
2263
Dees Troy4159aed2014-02-28 17:24:43 +00002264 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002265}
2266
Ethan Yonker472f5062016-02-25 13:47:30 -06002267bool TWPartition::Restore_Image(const string& restore_folder, const string& Restore_File_System, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002268 string Full_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002269
Matt Mower3c366972015-12-25 19:28:31 -06002270 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002271 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002272 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002273
Ethan Yonker96af84a2015-01-05 14:58:36 -06002274 if (Restore_File_System == "emmc") {
Ethan Yonker472f5062016-02-25 13:47:30 -06002275 unsigned long long file_size = (unsigned long long)(TWFunc::Get_File_Size(Full_FileName));
2276 if (!Raw_Read_Write(Full_FileName, Actual_Block_Device, file_size, progress))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002277 return false;
2278 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
Ethan Yonker472f5062016-02-25 13:47:30 -06002279 if (!Flash_Image_FI(Full_FileName, progress))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002280 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002281 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002282 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002283}
Dees_Troy5bf43922012-09-07 16:07:55 -04002284
2285bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002286 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002287
Dees_Troyab10ee22012-09-21 14:27:30 -04002288 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002289 return false;
2290
Dees_Troy0550cfb2012-10-13 11:56:13 -04002291 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002292 if (Removable || Is_Encrypted) {
2293 if (!Mount(false))
2294 return true;
2295 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002296 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002297
2298 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002299 if (!ret || Size == 0) {
2300 if (!Get_Size_Via_df(Display_Error)) {
2301 if (!Was_Already_Mounted)
2302 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002303 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002304 }
2305 }
Dees_Troy51127312012-09-08 13:08:49 -04002306
Dees_Troy5bf43922012-09-07 16:07:55 -04002307 if (Has_Data_Media) {
2308 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002309 unsigned long long data_media_used, actual_data;
Ethan Yonker66a19492015-12-10 10:19:45 -06002310 Used = du.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002311 Backup_Size = Used;
2312 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002313 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002314 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002315 } else {
2316 if (!Was_Already_Mounted)
2317 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002318 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002319 }
Dees_Troye58d5262012-09-21 12:27:57 -04002320 } else if (Has_Android_Secure) {
2321 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002322 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002323 else {
2324 if (!Was_Already_Mounted)
2325 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002326 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002327 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002328 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002329 if (!Was_Already_Mounted)
2330 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002331 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002332}
Dees_Troy38bd7602012-09-14 13:33:53 -04002333
2334void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002335 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002336 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002337 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002338 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002339 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002340 Is_Present = true;
2341 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002342 return;
2343 }
2344 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002345 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002346 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002347 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002348 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002349 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002350 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002351}
2352
2353void TWPartition::Recreate_Media_Folder(void) {
2354 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002355 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002356
2357 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002358 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2359 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002360 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002361 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2362 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002363 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2364 if (!Internal_path.empty()) {
2365 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2366 mkdir(Internal_path.c_str(), 0770);
2367 }
2368#ifdef TW_INTERNAL_STORAGE_PATH
2369 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2370#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002371#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002372 // Afterwards, we will try to set the
2373 // default metadata that we were hopefully able to get during
2374 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002375 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002376 if (!Internal_path.empty())
2377 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002378#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002379 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002380 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002381 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002382 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002383}
Dees_Troye58d5262012-09-21 12:27:57 -04002384
2385void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002386 if (!Has_Android_Secure)
2387 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002388 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002389 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002390 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002391 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002392 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002393 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002394 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002395 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002396 }
2397}
bigbiff7cb4c332014-11-26 20:36:07 -05002398
2399uint64_t TWPartition::Get_Max_FileSize() {
2400 uint64_t maxFileSize = 0;
2401 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2402 const uint64_t constTB = (uint64_t) constGB * 1024;
2403 const uint64_t constPB = (uint64_t) constTB * 1024;
2404 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002405 if (Current_File_System == "ext4")
2406 maxFileSize = 16 * constTB; //16 TB
2407 else if (Current_File_System == "vfat")
2408 maxFileSize = 4 * constGB; //4 GB
2409 else if (Current_File_System == "ntfs")
2410 maxFileSize = 256 * constTB; //256 TB
2411 else if (Current_File_System == "exfat")
2412 maxFileSize = 16 * constPB; //16 PB
2413 else if (Current_File_System == "ext3")
2414 maxFileSize = 2 * constTB; //2 TB
2415 else if (Current_File_System == "f2fs")
2416 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002417 else
2418 maxFileSize = 100000000L;
2419 return maxFileSize - 1;
2420}
2421
Ethan Yonker472f5062016-02-25 13:47:30 -06002422bool TWPartition::Flash_Image(const string& Filename) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002423 string Restore_File_System;
2424
2425 LOGINFO("Image filename is: %s\n", Filename.c_str());
2426
2427 if (Backup_Method == FILES) {
2428 LOGERR("Cannot flash images to file systems\n");
2429 return false;
2430 } else if (!Can_Flash_Img) {
2431 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2432 return false;
2433 } else {
2434 if (!Find_Partition_Size()) {
2435 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2436 return false;
2437 }
2438 unsigned long long image_size = TWFunc::Get_File_Size(Filename);
2439 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002440 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonker96af84a2015-01-05 14:58:36 -06002441 image_size, Filename.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002442 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002443 return false;
2444 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002445 if (Backup_Method == DD) {
2446 if (Is_Sparse_Image(Filename)) {
2447 return Flash_Sparse_Image(Filename);
2448 }
2449 unsigned long long file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2450 ProgressTracking pt(file_size);
2451 return Raw_Read_Write(Filename, Actual_Block_Device, file_size, &pt);
2452 } else if (Backup_Method == FLASH_UTILS) {
2453 return Flash_Image_FI(Filename, NULL);
2454 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002455 }
2456
2457 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2458 return false;
2459}
2460
Ethan Yonker472f5062016-02-25 13:47:30 -06002461bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05002462 uint32_t magic = 0;
2463 int fd = open(Filename.c_str(), O_RDONLY);
2464 if (fd < 0) {
2465 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2466 return false;
2467 }
2468 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2469 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2470 close(fd);
2471 return false;
2472 }
2473 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06002474 if (magic == SPARSE_HEADER_MAGIC)
2475 return true;
2476 return false;
2477}
2478
2479bool TWPartition::Flash_Sparse_Image(const string& Filename) {
2480 string Command;
2481
2482 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
2483
2484 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06002485 LOGINFO("Flash command: '%s'\n", Command.c_str());
2486 TWFunc::Exec_Cmd(Command);
2487 return true;
2488}
2489
Ethan Yonker472f5062016-02-25 13:47:30 -06002490bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002491 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06002492 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002493
Ethan Yonker74db1572015-10-28 12:44:49 -05002494 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06002495 if (progress) {
2496 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2497 progress->SetPartitionSize(file_size);
2498 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002499 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2500 Command = "erase_image " + MTD_Name;
2501 LOGINFO("Erase command: '%s'\n", Command.c_str());
2502 TWFunc::Exec_Cmd(Command);
2503 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2504 LOGINFO("Flash command: '%s'\n", Command.c_str());
2505 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06002506 if (progress)
2507 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002508 return true;
2509}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002510
2511void TWPartition::Change_Mount_Read_Only(bool new_value) {
2512 Mount_Read_Only = new_value;
2513}
2514
2515int TWPartition::Check_Lifetime_Writes() {
2516 bool original_read_only = Mount_Read_Only;
2517 int ret = 1;
2518
2519 Mount_Read_Only = true;
2520 if (Mount(false)) {
2521 Find_Actual_Block_Device();
2522 string block = basename(Actual_Block_Device.c_str());
2523 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2524 string result;
2525 if (TWFunc::Path_Exists(file)) {
2526 if (TWFunc::read_file(file, result) != 0) {
2527 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2528 } else {
2529 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2530 if (result == "0") {
2531 ret = 0;
2532 }
2533 }
2534 } else {
2535 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2536 }
2537 UnMount(true);
2538 } else {
2539 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2540 }
2541 Mount_Read_Only = original_read_only;
2542 return ret;
2543}
Ethan Yonker66a19492015-12-10 10:19:45 -06002544
2545int TWPartition::Decrypt_Adopted() {
2546#ifdef TW_INCLUDE_CRYPTO
2547 int ret = 1;
2548 Is_Adopted_Storage = false;
2549 string Adopted_Key_File = "";
2550
2551 if (!Removable)
2552 return ret;
2553
2554 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
2555 if (fd < 0) {
2556 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
2557 return ret;
2558 }
2559 char type_guid[80];
2560 char part_guid[80];
2561
2562 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
2563 LOGINFO("type: '%s'\n", type_guid);
2564 LOGINFO("part: '%s'\n", part_guid);
2565 Adopted_GUID = part_guid;
2566 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
2567 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
2568 LOGINFO("android_expand found\n");
2569 Adopted_Key_File = "/data/misc/vold/expand_";
2570 Adopted_Key_File += part_guid;
2571 Adopted_Key_File += ".key";
2572 if (TWFunc::Path_Exists(Adopted_Key_File)) {
2573 Is_Adopted_Storage = true;
2574 /* Until we find a use case for this, I think it is safe
2575 * to disable USB Mass Storage whenever adopted storage
2576 * is present.
2577 */
2578 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
2579 DataManager::SetValue("tw_has_usb_storage", 0);
2580 }
2581 }
2582 }
2583
2584 if (Is_Adopted_Storage) {
2585 string Adopted_Block_Device = Alternate_Block_Device + "p2";
2586 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2587 Adopted_Block_Device = Alternate_Block_Device + "2";
2588 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2589 LOGINFO("Adopted block device does not exist\n");
2590 goto exit;
2591 }
2592 }
2593 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
2594 char crypto_blkdev[MAXPATHLEN];
2595 std::string thekey;
2596 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
2597 if (fdkey < 0) {
2598 LOGINFO("failed to open key file\n");
2599 goto exit;
2600 }
2601 char buf[512];
2602 ssize_t n;
2603 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
2604 thekey.append(buf, n);
2605 }
2606 close(fdkey);
2607 unsigned char* key = (unsigned char*) thekey.data();
2608 cryptfs_revert_ext_volume(part_guid);
2609
2610 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
2611 if (ret == 0) {
2612 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
2613 Decrypted_Block_Device = crypto_blkdev;
2614 Is_Decrypted = true;
2615 Is_Encrypted = true;
2616 Find_Actual_Block_Device();
2617 if (!Mount(false)) {
2618 LOGERR("Failed to mount decrypted adopted storage device\n");
2619 Is_Decrypted = false;
2620 Is_Encrypted = false;
2621 cryptfs_revert_ext_volume(part_guid);
2622 ret = 1;
2623 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002624 UnMount(false);
2625 Has_Android_Secure = false;
2626 Symlink_Path = "";
2627 Symlink_Mount_Point = "";
2628 Backup_Name = Mount_Point.substr(1);
2629 Backup_Path = Mount_Point;
2630 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
2631 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
2632 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
2633 PartitionManager.Remove_Partition_By_Path("/sd-ext");
2634 }
Ethan Yonker66a19492015-12-10 10:19:45 -06002635 Setup_Data_Media();
2636 Recreate_Media_Folder();
2637 Wipe_Available_in_GUI = true;
2638 Wipe_During_Factory_Reset = true;
2639 Can_Be_Backed_Up = true;
2640 Can_Encrypt_Backup = true;
2641 Use_Userdata_Encryption = true;
2642 Is_Storage = true;
2643 Storage_Name = "Adopted Storage";
2644 Is_SubPartition = true;
2645 SubPartition_Of = "/data";
2646 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2647 DataManager::SetValue("tw_has_adopted_storage", 1);
2648 }
2649 } else {
2650 LOGERR("Failed to setup adopted storage decryption\n");
2651 }
2652 }
2653exit:
2654 return ret;
2655#else
2656 LOGINFO("Decrypt_Adopted: no crypto support\n");
2657 return 1;
2658#endif
2659}
2660
2661void TWPartition::Revert_Adopted() {
2662#ifdef TW_INCLUDE_CRYPTO
2663 if (!Adopted_GUID.empty()) {
2664 PartitionManager.Remove_MTP_Storage(Mount_Point);
2665 UnMount(false);
2666 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
2667 Is_Adopted_Storage = false;
2668 Is_Encrypted = false;
2669 Is_Decrypted = false;
2670 Decrypted_Block_Device = "";
2671 Find_Actual_Block_Device();
2672 Wipe_During_Factory_Reset = false;
2673 Can_Be_Backed_Up = false;
2674 Can_Encrypt_Backup = false;
2675 Use_Userdata_Encryption = false;
2676 Is_SubPartition = false;
2677 SubPartition_Of = "";
2678 Has_Data_Media = false;
2679 Storage_Path = Mount_Point;
2680 if (!Symlink_Mount_Point.empty()) {
2681 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
2682 if (Dat) {
2683 Dat->UnMount(false);
2684 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
2685 }
2686 Symlink_Mount_Point = "";
2687 }
2688 }
2689#else
2690 LOGINFO("Revert_Adopted: no crypto support\n");
2691#endif
2692}