blob: 00f84a1287c77cececf04cf845c989a55b2b02b9 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002 Copyright 2013 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050027#include <iostream>
28#include <sstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040029
Dees_Troy657c3092012-09-10 20:32:10 -040030#ifdef TW_INCLUDE_CRYPTO
31 #include "cutils/properties.h"
32#endif
33
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050034#include "libblkid/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000036#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040037#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040038#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040039#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050040#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041#include "twrpTar.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050042#include "twrpDU.hpp"
bigbiff bigbiff6b600f92014-01-05 18:13:43 -050043#include "fixPermissions.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050044#include "infomanager.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040045extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040046 #include "mtdutils/mtdutils.h"
47 #include "mtdutils/mounts.h"
Dees_Troy85f44ed2013-01-09 18:42:36 +000048#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
49 #include "crypto/libcrypt_samsung/include/libcrypt_samsung.h"
50#endif
Dees_Troya95f55c2013-08-17 13:14:43 +000051#ifdef USE_EXT4
52 #include "make_ext4fs.h"
53#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060054
55#ifdef TW_INCLUDE_CRYPTO
56 #ifdef TW_INCLUDE_JB_CRYPTO
57 #include "crypto/jb/cryptfs.h"
58 #else
59 #include "crypto/ics/cryptfs.h"
60 #endif
61#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
Dees_Troy51a0e822012-09-05 15:24:24 -040072
bigbiff bigbiff9c754052013-01-09 09:09:08 -050073using namespace std;
74
Dees_Troya95f55c2013-08-17 13:14:43 +000075extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050076extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000077
Hashcode62bd9e02013-11-19 21:59:42 -080078struct flag_list {
79 const char *name;
80 unsigned flag;
81};
82
83static struct flag_list mount_flags[] = {
84 { "noatime", MS_NOATIME },
85 { "noexec", MS_NOEXEC },
86 { "nosuid", MS_NOSUID },
87 { "nodev", MS_NODEV },
88 { "nodiratime", MS_NODIRATIME },
89 { "ro", MS_RDONLY },
90 { "rw", 0 },
91 { "remount", MS_REMOUNT },
92 { "bind", MS_BIND },
93 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000094#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080095 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000096#endif
97#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080098 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000099#endif
100#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800101 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000102#endif
103#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800104 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000105#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800106 { "sync", MS_SYNCHRONOUS },
107 { "defaults", 0 },
108 { 0, 0 },
109};
110
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400111TWPartition::TWPartition(int *id) {
112 initmtpid = id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400113 Can_Be_Mounted = false;
114 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500115 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200116 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400117 Wipe_During_Factory_Reset = false;
118 Wipe_Available_in_GUI = false;
119 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400120 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400121 SubPartition_Of = "";
122 Symlink_Path = "";
123 Symlink_Mount_Point = "";
124 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400125 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400126 Actual_Block_Device = "";
127 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400128 Alternate_Block_Device = "";
129 Removable = false;
130 Is_Present = false;
131 Length = 0;
132 Size = 0;
133 Used = 0;
134 Free = 0;
135 Backup_Size = 0;
136 Can_Be_Encrypted = false;
137 Is_Encrypted = false;
138 Is_Decrypted = false;
139 Decrypted_Block_Device = "";
140 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500141 Backup_Display_Name = "";
142 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400143 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400144 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400145 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400146 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000147 Can_Encrypt_Backup = false;
148 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400149 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400150 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400151 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500152 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400153 Storage_Path = "";
154 Current_File_System = "";
155 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800156 Mount_Flags = 0;
157 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400158 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000159 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000160 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000161#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
162 EcryptFS_Password = "";
163#endif
Ethan Yonker6277c792014-09-15 14:54:30 -0500164 mtpid = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400165}
166
167TWPartition::~TWPartition(void) {
168 // Do nothing
169}
170
Dees_Troy5bf43922012-09-07 16:07:55 -0400171bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
172 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
173 int line_len = Line.size(), index = 0, item_index = 0;
174 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400175 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400176 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500177 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400178
Dees_Troy51127312012-09-08 13:08:49 -0400179 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500180 if (full_line[index] == 34)
181 skip = !skip;
182 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400183 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400184 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400185 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000186 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400187 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500188 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000189 Display_Name = full_line + 1;
190 Backup_Display_Name = Display_Name;
191 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400192 index = Mount_Point.size();
193 while (index < line_len) {
194 while (index < line_len && full_line[index] == '\0')
195 index++;
196 if (index >= line_len)
197 continue;
198 ptr = full_line + index;
199 if (item_index == 0) {
200 // File System
201 Fstab_File_System = ptr;
202 Current_File_System = ptr;
203 item_index++;
204 } else if (item_index == 1) {
205 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400206 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400207 MTD_Name = ptr;
208 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400209 } else if (Fstab_File_System == "bml") {
210 if (Mount_Point == "/boot")
211 MTD_Name = "boot";
212 else if (Mount_Point == "/recovery")
213 MTD_Name = "recovery";
214 Primary_Block_Device = ptr;
215 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000216 LOGERR("Until we get better BML support, you will have to find and provide the full block device path to the BML devices e.g. /dev/block/bml9 instead of the partition name\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400217 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400218 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000219 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400220 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000221 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400222 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400223 } else {
224 Primary_Block_Device = ptr;
225 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400226 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400227 item_index++;
228 } else if (item_index > 1) {
229 if (*ptr == '/') {
230 // Alternate Block Device
231 Alternate_Block_Device = ptr;
232 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
233 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
234 // Partition length
235 ptr += 7;
236 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400237 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
238 // Custom flags, save for later so that new values aren't overwritten by defaults
239 ptr += 6;
240 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000241 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400242 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
243 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400244 } else {
245 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000246 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400247 }
248 }
249 while (index < line_len && full_line[index] != '\0')
250 index++;
251 }
252
253 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
254 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000255 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400256 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000257 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400258 return 0;
259 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400260 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 Setup_File_System(Display_Error);
262 if (Mount_Point == "/system") {
263 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500264 Backup_Display_Name = Display_Name;
265 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400266 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500267 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400268 } else if (Mount_Point == "/data") {
269 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500270 Backup_Display_Name = Display_Name;
271 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400272 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400273 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500274 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000275 Can_Encrypt_Backup = true;
276 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500277 if (datamedia)
278 Setup_Data_Media(0);
Dees_Troy5bf43922012-09-07 16:07:55 -0400279#ifdef TW_INCLUDE_CRYPTO
280 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400281 char crypto_blkdev[255];
282 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
283 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400284 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400285 DataManager::SetValue(TW_IS_DECRYPTED, 1);
286 Is_Encrypted = true;
287 Is_Decrypted = true;
288 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000289 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400290 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600291 if (Is_Present) {
292#ifdef TW_INCLUDE_JB_CRYPTO
293 // No extra flags needed
294#else
295 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
296 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
297 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
298 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
299 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
300 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
301#ifdef CRYPTO_SD_FS_TYPE
302 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
303 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
304 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
305#endif
306 property_set("rw.km_fips_status", "ready");
307#endif
308 if (cryptfs_check_footer() == 0) {
309 Is_Encrypted = true;
310 Is_Decrypted = false;
311 Can_Be_Mounted = false;
312 Current_File_System = "emmc";
313 Setup_Image(Display_Error);
314 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
315 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
316 DataManager::SetValue("tw_crypto_display", "");
317 } else {
318 LOGERR("Could not mount /data and unable to find crypto footer.\n");
319 }
320 } else {
321 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
322 }
Gary Peck82599a82012-11-21 16:23:12 -0800323 } else {
324 // Filesystem is not encrypted and the mount
325 // succeeded, so get it back to the original
326 // unmounted state
327 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400328 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500329 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400330 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400331#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500332 if (datamedia)
333 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400334#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400335 } else if (Mount_Point == "/cache") {
336 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500337 Backup_Display_Name = Display_Name;
338 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400339 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400340 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500341 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400342 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000343 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500344 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500345 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400346 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400347 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400348 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400349 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500350 Backup_Display_Name = Display_Name;
351 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400352 Is_SubPartition = true;
353 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400354 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500355 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000356 Can_Encrypt_Backup = true;
357 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400358 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400359 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400360 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500361 Backup_Display_Name = Display_Name;
362 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400363 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400364 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500365 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000366 Can_Encrypt_Backup = true;
367 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400368 } else if (Mount_Point == "/boot") {
369 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500370 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400371 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500372 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400373 }
374#ifdef TW_EXTERNAL_STORAGE_PATH
375 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
376 Is_Storage = true;
377 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400378 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500379 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400380#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000381 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400382 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400383 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500384 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400385#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000386 }
Dees_Troy8170a922012-09-18 15:40:25 -0400387#ifdef TW_INTERNAL_STORAGE_PATH
388 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
389 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500390 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400391 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500392 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400393 }
394#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000395 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400396 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500397 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500398 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400399 }
400#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400401 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400402 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400403 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500404 if (Mount_Point == "/boot") {
405 Display_Name = "Boot";
406 Backup_Display_Name = Display_Name;
407 Can_Be_Backed_Up = true;
408 } else if (Mount_Point == "/recovery") {
409 Display_Name = "Recovery";
410 Backup_Display_Name = Display_Name;
Dees_Troya13d74f2013-03-24 08:54:55 -0500411 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400412 }
413
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400414 // Generate MTP ID
415 if (Is_Storage) {
416 (*initmtpid)++;
417 mtpid = *initmtpid;
418 }
419
Dees_Troy51127312012-09-08 13:08:49 -0400420 // Process any custom flags
421 if (Flags.size() > 0)
422 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400423 return true;
424}
425
Hashcode62bd9e02013-11-19 21:59:42 -0800426bool TWPartition::Process_FS_Flags(string& Options, int Flags) {
427 int i;
428 char *p;
429 char *savep;
430 char fs_options[250];
431
432 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
433 Options = "";
434
435 p = strtok_r(fs_options, ",", &savep);
436 while (p) {
437 /* Look for the flag "p" in the flag list "fl"
438 * If not found, the loop exits with fl[i].name being null.
439 */
440 for (i = 0; mount_flags[i].name; i++) {
441 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
442 Flags |= mount_flags[i].flag;
443 break;
444 }
445 }
446
447 if (!mount_flags[i].name) {
448 if (Options.size() > 0)
449 Options += ",";
450 Options += p;
451 }
452 p = strtok_r(NULL, ",", &savep);
453 }
454
455 return true;
456}
457
Dees_Troy51127312012-09-08 13:08:49 -0400458bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
459 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500460 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400461 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500462 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400463
464 strcpy(flags, Flags.c_str());
465 flags_len = Flags.size();
466 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500467 if (flags[index] == 34)
468 skip = !skip;
469 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400470 flags[index] = '\0';
471 }
472
473 index = 0;
474 while (index < flags_len) {
475 while (index < flags_len && flags[index] == '\0')
476 index++;
477 if (index >= flags_len)
478 continue;
479 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500480 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400481 if (strcmp(ptr, "removable") == 0) {
482 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600483 } else if (strncmp(ptr, "storage", 7) == 0) {
484 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600485 Is_Storage = true;
486 } else if (ptr_len == 9) {
487 ptr += 9;
488 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
489 LOGINFO("storage set to true\n");
490 Is_Storage = true;
491 } else {
492 LOGINFO("storage set to false\n");
493 Is_Storage = false;
494 }
495 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500496 } else if (strcmp(ptr, "settingsstorage") == 0) {
497 Is_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500498 } else if (strcmp(ptr, "andsec") == 0) {
499 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400500 } else if (strcmp(ptr, "canbewiped") == 0) {
501 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700502 } else if (strcmp(ptr, "usermrf") == 0) {
503 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500504 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
505 ptr += 7;
506 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
507 Can_Be_Backed_Up = true;
508 else
509 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400510 } else if (strcmp(ptr, "wipeingui") == 0) {
511 Can_Be_Wiped = true;
512 Wipe_Available_in_GUI = true;
513 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
514 Can_Be_Wiped = true;
515 Wipe_Available_in_GUI = true;
516 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500517 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000518 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400519 Is_SubPartition = true;
520 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000521 } else if (strcmp(ptr, "ignoreblkid") == 0) {
522 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000523 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
524 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500525 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400526 ptr += 8;
527 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500528 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
529 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400530 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500531 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400532 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500533 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
534 Display_Name.resize(Display_Name.size() - 1);
535 }
536 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
537 has_storage_name = true;
538 ptr += 11;
539 if (*ptr == '\"') ptr++;
540 Storage_Name = ptr;
541 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
542 Storage_Name.resize(Storage_Name.size() - 1);
543 }
544 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
545 has_backup_name = true;
546 ptr += 10;
547 if (*ptr == '\"') ptr++;
548 Backup_Display_Name = ptr;
549 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
550 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
551 }
552 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400553 ptr += 10;
554 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500555 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400556 ptr += 7;
557 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000558 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
559 ptr += 17;
560 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
561 Can_Encrypt_Backup = true;
562 else
563 Can_Encrypt_Backup = false;
564 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
565 ptr += 21;
566 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
567 Can_Encrypt_Backup = true;
568 Use_Userdata_Encryption = true;
569 } else {
570 Use_Userdata_Encryption = false;
571 }
Hashcode62bd9e02013-11-19 21:59:42 -0800572 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
573 ptr += 8;
574 if (*ptr == '\"') ptr++;
575
576 Mount_Options = ptr;
577 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
578 Mount_Options.resize(Mount_Options.size() - 1);
579 }
580 Process_FS_Flags(Mount_Options, Mount_Flags);
Dees_Troy51127312012-09-08 13:08:49 -0400581 } else {
582 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000583 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400584 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000585 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400586 }
587 while (index < flags_len && flags[index] != '\0')
588 index++;
589 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500590 if (has_display_name && !has_storage_name)
591 Storage_Name = Display_Name;
592 if (!has_display_name && has_storage_name)
593 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000594 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500595 Backup_Display_Name = Display_Name;
596 if (!has_display_name && has_backup_name)
597 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400598 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400599}
600
Dees_Troy5bf43922012-09-07 16:07:55 -0400601bool TWPartition::Is_File_System(string File_System) {
602 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400603 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400604 File_System == "ext4" ||
605 File_System == "vfat" ||
606 File_System == "ntfs" ||
607 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500608 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000609 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400610 File_System == "auto")
611 return true;
612 else
613 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400614}
615
Dees_Troy5bf43922012-09-07 16:07:55 -0400616bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400617 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400618 return true;
619 else
620 return false;
621}
622
Dees_Troy51127312012-09-08 13:08:49 -0400623bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400624 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400625 if (mkdir(Path.c_str(), 0777) == -1) {
626 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000627 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400628 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000629 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400630 return false;
631 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000632 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400633 return true;
634 }
635 }
636 return true;
637}
638
Dees_Troy5bf43922012-09-07 16:07:55 -0400639void TWPartition::Setup_File_System(bool Display_Error) {
640 struct statfs st;
641
642 Can_Be_Mounted = true;
643 Can_Be_Wiped = true;
644
Dees_Troy5bf43922012-09-07 16:07:55 -0400645 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400646 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400647 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
648 Backup_Name = Display_Name;
649 Backup_Method = FILES;
650}
651
652void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400653 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
654 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800655 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400656 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800657 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400658 Backup_Method = FLASH_UTILS;
659 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000660 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 -0400661 if (Find_Partition_Size()) {
662 Used = Size;
663 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400664 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400665 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000666 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400667 else
Dees Troyd932ce12013-10-18 17:12:59 +0000668 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400669 }
670}
671
Dees_Troye58d5262012-09-21 12:27:57 -0400672void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500673 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400674 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500675 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400676 Has_Android_Secure = true;
677 Symlink_Path = Mount_Point + "/.android_secure";
678 Symlink_Mount_Point = "/and-sec";
679 Backup_Path = Symlink_Mount_Point;
680 Make_Dir("/and-sec", true);
681 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600682 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400683}
684
Ethan Yonker6277c792014-09-15 14:54:30 -0500685void TWPartition::Setup_Data_Media(int mtp) {
686 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
687 Storage_Name = "Internal Storage";
688 Has_Data_Media = true;
689 Is_Storage = true;
690 Is_Settings_Storage = true;
691 Storage_Path = "/data/media";
692 Symlink_Path = Storage_Path;
693 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
694 Make_Dir("/emmc", false);
695 Symlink_Mount_Point = "/emmc";
696 } else {
697 Make_Dir("/sdcard", false);
698 Symlink_Mount_Point = "/sdcard";
699 }
700 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
701 Storage_Path = "/data/media/0";
702 Symlink_Path = Storage_Path;
703 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
704 UnMount(true);
705 }
706 if (mtp)
707 mtpid = mtp;
708 DataManager::SetValue("tw_has_internal", 1);
709 DataManager::SetValue("tw_has_data_media", 1);
710 du.add_absolute_dir("/data/media");
711}
712
Dees_Troy5bf43922012-09-07 16:07:55 -0400713void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
714 char device[512], realDevice[512];
715
716 strcpy(device, Block.c_str());
717 memset(realDevice, 0, sizeof(realDevice));
718 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
719 {
720 strcpy(device, realDevice);
721 memset(realDevice, 0, sizeof(realDevice));
722 }
723
724 if (device[0] != '/') {
725 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000726 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400727 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000728 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400729 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400730 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400731 Block = device;
732 return;
733 }
734}
735
Dees_Troy8e337f32012-10-13 22:07:49 -0400736void TWPartition::Mount_Storage_Retry(void) {
737 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500738 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400739 int retry_count = 5;
740 while (retry_count > 0 && !Mount(false)) {
741 usleep(500000);
742 retry_count--;
743 }
744 Mount(true);
745 }
746}
747
Dees_Troy38bd7602012-09-14 13:33:53 -0400748bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
749 FILE *fp = NULL;
750 char line[255];
751
752 fp = fopen("/proc/mtd", "rt");
753 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000754 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400755 return false;
756 }
757
758 while (fgets(line, sizeof(line), fp) != NULL)
759 {
760 char device[32], label[32];
761 unsigned long size = 0;
762 char* fstype = NULL;
763 int deviceId;
764
765 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
766
767 // Skip header and blank lines
768 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
769 continue;
770
771 // Strip off the trailing " from the label
772 label[strlen(label)-1] = '\0';
773
774 if (strcmp(label, MTD_Name.c_str()) == 0) {
775 // We found our device
776 // Strip off the trailing : from the device
777 device[strlen(device)-1] = '\0';
778 if (sscanf(device,"mtd%d", &deviceId) == 1) {
779 sprintf(device, "/dev/block/mtdblock%d", deviceId);
780 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000781 fclose(fp);
782 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400783 }
784 }
785 }
786 fclose(fp);
787
788 return false;
789}
790
Dees_Troy51127312012-09-08 13:08:49 -0400791bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
792 struct statfs st;
793 string Local_Path = Mount_Point + "/.";
794
795 if (!Mount(Display_Error))
796 return false;
797
798 if (statfs(Local_Path.c_str(), &st) != 0) {
799 if (!Removable) {
800 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000801 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400802 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000803 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400804 }
805 return false;
806 }
807 Size = (st.f_blocks * st.f_bsize);
808 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
809 Free = (st.f_bfree * st.f_bsize);
810 Backup_Size = Used;
811 return true;
812}
813
814bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400815 FILE* fp;
816 char command[255], line[512];
817 int include_block = 1;
818 unsigned int min_len;
819
820 if (!Mount(Display_Error))
821 return false;
822
Dees_Troy38bd7602012-09-14 13:33:53 -0400823 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400824 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200825 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400826 fp = fopen("/tmp/dfoutput.txt", "rt");
827 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000828 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400829 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400830 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400831
832 while (fgets(line, sizeof(line), fp) != NULL)
833 {
834 unsigned long blocks, used, available;
835 char device[64];
836 char tmpString[64];
837
838 if (strncmp(line, "Filesystem", 10) == 0)
839 continue;
840 if (strlen(line) < min_len) {
841 include_block = 0;
842 continue;
843 }
844 if (include_block) {
845 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
846 } else {
847 // The device block string is so long that the df information is on the next line
848 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400849 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400850 while (tmpString[space_count] == 32)
851 space_count++;
852 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
853 }
854
855 // Adjust block size to byte size
856 Size = blocks * 1024ULL;
857 Used = used * 1024ULL;
858 Free = available * 1024ULL;
859 Backup_Size = Used;
860 }
861 fclose(fp);
862 return true;
863}
864
Dees_Troy5bf43922012-09-07 16:07:55 -0400865bool TWPartition::Find_Partition_Size(void) {
866 FILE* fp;
867 char line[512];
868 string tmpdevice;
869
igoriok87e3d932013-01-31 21:03:53 +0200870 fp = fopen("/proc/dumchar_info", "rt");
871 if (fp != NULL) {
872 while (fgets(line, sizeof(line), fp) != NULL)
873 {
874 char label[32], device[32];
875 unsigned long size = 0;
876
877 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
878
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500879 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200880 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
881 continue;
882
883 tmpdevice = "/dev/";
884 tmpdevice += label;
885 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
886 Size = size;
887 fclose(fp);
888 return true;
889 }
890 }
891 }
892
Dees_Troy5bf43922012-09-07 16:07:55 -0400893 // In this case, we'll first get the partitions we care about (with labels)
894 fp = fopen("/proc/partitions", "rt");
895 if (fp == NULL)
896 return false;
897
898 while (fgets(line, sizeof(line), fp) != NULL)
899 {
900 unsigned long major, minor, blocks;
901 char device[512];
902 char tmpString[64];
903
Dees_Troy63c8df72012-09-10 14:02:05 -0400904 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400905 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
906
907 tmpdevice = "/dev/block/";
908 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400909 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400910 // Adjust block size to byte size
911 Size = blocks * 1024ULL;
912 fclose(fp);
913 return true;
914 }
915 }
916 fclose(fp);
917 return false;
918}
919
Dees_Troy5bf43922012-09-07 16:07:55 -0400920bool TWPartition::Is_Mounted(void) {
921 if (!Can_Be_Mounted)
922 return false;
923
924 struct stat st1, st2;
925 string test_path;
926
927 // Check to see if the mount point directory exists
928 test_path = Mount_Point + "/.";
929 if (stat(test_path.c_str(), &st1) != 0) return false;
930
931 // Check to see if the directory above the mount point exists
932 test_path = Mount_Point + "/../.";
933 if (stat(test_path.c_str(), &st2) != 0) return false;
934
935 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
936 int ret = (st1.st_dev != st2.st_dev) ? true : false;
937
938 return ret;
939}
940
941bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000942 int exfat_mounted = 0;
943
Dees_Troy5bf43922012-09-07 16:07:55 -0400944 if (Is_Mounted()) {
945 return true;
946 } else if (!Can_Be_Mounted) {
947 return false;
948 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400949
950 Find_Actual_Block_Device();
951
952 // Check the current file system before mounting
953 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000954 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000955 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 +0000956 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000957 string result;
958 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000959 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000960 Current_File_System = "vfat";
961 } else {
962#ifdef TW_NO_EXFAT_FUSE
963 UnMount(false);
964 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
965 // Some kernels let us mount vfat as exfat which doesn't work out too well
966#else
967 exfat_mounted = 1;
968#endif
969 }
970 }
Dees_Troy22042032012-12-18 21:23:08 +0000971 if (Fstab_File_System == "yaffs2") {
972 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000973 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
974 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
975 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
976 if (Display_Error)
977 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
978 else
979 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
980 return false;
981 } else {
982 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
983 return true;
984 }
985 } else {
986 struct stat st;
987 string test_path = Mount_Point;
988 if (stat(test_path.c_str(), &st) < 0) {
989 if (Display_Error)
990 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
991 else
992 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
993 return false;
994 }
995 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
996 if (new_mode != st.st_mode) {
997 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
998 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
999 if (Display_Error)
1000 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1001 else
1002 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1003 return false;
1004 }
1005 }
Dees_Troy22042032012-12-18 21:23:08 +00001006 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001007 }
Dees Troy216e0422014-02-07 03:46:42 +00001008 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), Mount_Flags, Mount_Options.c_str()) != 0 && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), Mount_Flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001009#ifdef TW_NO_EXFAT_FUSE
1010 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001011 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001012 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001013 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001014 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001015 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001016 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -08001017 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(), Mount_Flags, Mount_Options.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001018 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001019 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001020 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001021#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001022 if (!Removable && Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001023 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001024 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001025 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1026 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 +00001027 return false;
1028#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001029 }
1030#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001031 }
1032#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1033 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1034 MetaEcfsFile += "/.MetaEcfsFile";
1035 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
1036 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
1037 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001038 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001039 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001040 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001041 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001042 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001043 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -04001044 }
Dees_Troy066eb302013-08-23 17:20:32 +00001045 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
1046 if (Is_Decrypted)
1047 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001048 Is_Decrypted = false;
1049 }
1050#endif
1051 if (Removable)
1052 Update_Size(Display_Error);
1053
1054 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001055 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
1056 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001057 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001058 return true;
1059}
1060
1061bool TWPartition::UnMount(bool Display_Error) {
1062 if (Is_Mounted()) {
1063 int never_unmount_system;
1064
1065 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1066 if (never_unmount_system == 1 && Mount_Point == "/system")
1067 return true; // Never unmount system if you're not supposed to unmount it
1068
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001069 if (Is_Storage)
1070 TWFunc::Toggle_MTP(false);
1071
Dees_Troyc8bafa12013-01-10 15:43:00 +00001072#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1073 if (EcryptFS_Password.size() > 0) {
1074 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
1075 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001076 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001077 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001078 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001079 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001080 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001081 }
1082 }
1083#endif
1084
Dees_Troy38bd7602012-09-14 13:33:53 -04001085 if (!Symlink_Mount_Point.empty())
1086 umount(Symlink_Mount_Point.c_str());
1087
Dees_Troyb05ddee2013-01-28 20:24:50 +00001088 umount(Mount_Point.c_str());
1089 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001090 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001091 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001092 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001093 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001094 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001095 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001096 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001097 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001098 } else {
1099 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001100 }
1101}
1102
Gary Peck43acadf2012-11-21 21:19:01 -08001103bool TWPartition::Wipe(string New_File_System) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001104 bool wiped = false, update_crypt = false, recreate_media = true, mtp_toggle = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001105 int check;
1106 string Layout_Filename = Mount_Point + "/.layout_version";
1107
Dees_Troy38bd7602012-09-14 13:33:53 -04001108 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001109 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001110 return false;
1111 }
1112
Dees_Troyc51f1f92012-09-20 15:32:13 -04001113 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001114 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001115
Dees_Troyce675462013-01-09 19:48:21 +00001116#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1117 if (Mount_Point == "/data" && Mount(false)) {
1118 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
1119 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
1120 }
1121#endif
1122
Dees_Troy16c2b312013-01-15 16:51:18 +00001123 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1124 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1125 else
1126 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001127
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001128 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001129 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001130 recreate_media = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001131 mtp_toggle = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001132 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001133 DataManager::GetValue(TW_RM_RF_VAR, check);
1134
Hashcodedabfd492013-08-29 22:45:30 -07001135 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001136 wiped = Wipe_RMRF();
1137 else if (New_File_System == "ext4")
1138 wiped = Wipe_EXT4();
1139 else if (New_File_System == "ext2" || New_File_System == "ext3")
1140 wiped = Wipe_EXT23(New_File_System);
1141 else if (New_File_System == "vfat")
1142 wiped = Wipe_FAT();
1143 else if (New_File_System == "exfat")
1144 wiped = Wipe_EXFAT();
1145 else if (New_File_System == "yaffs2")
1146 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001147 else if (New_File_System == "f2fs")
1148 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001149 else {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001150 if (Is_Storage) {
1151 TWFunc::Toggle_MTP(true);
1152 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001153 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 +00001154 unlink("/.layout_version");
1155 return false;
1156 }
1157 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001158 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001159
Gary Pecke8bc5d72012-12-21 06:45:25 -08001160 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001161#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1162 if (Mount_Point == "/data" && Mount(false)) {
1163 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1164 Make_Dir("/data/system", true);
1165 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1166 }
1167 }
1168#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001169
Dees_Troy1c1ac442013-01-17 21:42:14 +00001170 if (Mount_Point == "/cache")
1171 DataManager::Output_Version();
1172
Dees_Troy16c2b312013-01-15 16:51:18 +00001173 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1174 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1175
1176 if (update_crypt) {
1177 Setup_File_System(false);
1178 if (Is_Encrypted && !Is_Decrypted) {
1179 // just wiped an encrypted partition back to its unencrypted state
1180 Is_Encrypted = false;
1181 Is_Decrypted = false;
1182 Decrypted_Block_Device = "";
1183 if (Mount_Point == "/data") {
1184 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1185 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1186 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001187 }
1188 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001189
1190 if (Mount_Point == "/data" && Has_Data_Media && recreate_media) {
1191 Recreate_Media_Folder();
1192 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001193 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001194 if (Is_Storage && mtp_toggle) {
1195 TWFunc::Toggle_MTP(true);
1196 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001197 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001198}
1199
Gary Peck43acadf2012-11-21 21:19:01 -08001200bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001201 if (Is_File_System(Current_File_System))
1202 return Wipe(Current_File_System);
1203 else
1204 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001205}
1206
Dees_Troye58d5262012-09-21 12:27:57 -04001207bool TWPartition::Wipe_AndSec(void) {
1208 if (!Has_Android_Secure)
1209 return false;
1210
Dees_Troye58d5262012-09-21 12:27:57 -04001211 if (!Mount(true))
1212 return false;
1213
Dees_Troy2673cec2013-04-02 20:22:16 +00001214 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001215 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001216 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001217}
1218
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001219bool TWPartition::Can_Repair() {
1220 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/dosfsck"))
1221 return true;
1222 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1223 return true;
1224 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1225 return true;
1226 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1227 return true;
1228 return false;
1229}
1230
1231bool TWPartition::Repair() {
1232 string command;
1233
1234 if (Current_File_System == "vfat") {
1235 if (!TWFunc::Path_Exists("/sbin/dosfsck")) {
1236 gui_print("dosfsck does not exist! Cannot repair!\n");
1237 return false;
1238 }
1239 if (!UnMount(true))
1240 return false;
1241 gui_print("Repairing %s using dosfsck...\n", Display_Name.c_str());
1242 Find_Actual_Block_Device();
1243 command = "/sbin/dosfsck -y " + Actual_Block_Device;
1244 LOGINFO("Repair command: %s\n", command.c_str());
1245 if (TWFunc::Exec_Cmd(command) == 0) {
1246 gui_print("Done.\n");
1247 return true;
1248 } else {
1249 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1250 return false;
1251 }
1252 }
1253 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1254 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
1255 gui_print("e2fsck does not exist! Cannot repair!\n");
1256 return false;
1257 }
1258 if (!UnMount(true))
1259 return false;
1260 gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str());
1261 Find_Actual_Block_Device();
1262 command = "/sbin/e2fsck -p " + Actual_Block_Device;
1263 LOGINFO("Repair command: %s\n", command.c_str());
1264 if (TWFunc::Exec_Cmd(command) == 0) {
1265 gui_print("Done.\n");
1266 return true;
1267 } else {
1268 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1269 return false;
1270 }
1271 }
1272 if (Current_File_System == "exfat") {
1273 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
1274 gui_print("fsck.exfat does not exist! Cannot repair!\n");
1275 return false;
1276 }
1277 if (!UnMount(true))
1278 return false;
1279 gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str());
1280 Find_Actual_Block_Device();
1281 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1282 LOGINFO("Repair command: %s\n", command.c_str());
1283 if (TWFunc::Exec_Cmd(command) == 0) {
1284 gui_print("Done.\n");
1285 return true;
1286 } else {
1287 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1288 return false;
1289 }
1290 }
1291 if (Current_File_System == "f2fs") {
1292 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
1293 gui_print("fsck.f2fs does not exist! Cannot repair!\n");
1294 return false;
1295 }
1296 if (!UnMount(true))
1297 return false;
1298 gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str());
1299 Find_Actual_Block_Device();
1300 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1301 LOGINFO("Repair command: %s\n", command.c_str());
1302 if (TWFunc::Exec_Cmd(command) == 0) {
1303 gui_print("Done.\n");
1304 return true;
1305 } else {
1306 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1307 return false;
1308 }
1309 }
1310 return false;
1311}
1312
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001313bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001314 if (Backup_Method == FILES)
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001315 return Backup_Tar(backup_folder, overall_size, other_backups_size);
Dees_Troy38bd7602012-09-14 13:33:53 -04001316 else if (Backup_Method == DD)
1317 return Backup_DD(backup_folder);
1318 else if (Backup_Method == FLASH_UTILS)
1319 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001320 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001321 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001322}
1323
Dees_Troy43d8b002012-09-17 16:00:01 -04001324bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001325 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001326 char split_filename[512];
1327 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001328 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001329
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001330 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001331 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001332 if (!TWFunc::Path_Exists(Full_Filename)) {
1333 // This is a split archive, we presume
1334 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001335 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001336 md5file = split_filename;
1337 md5file += ".md5";
1338 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001339 LOGERR("No md5 file found for '%s'.\n", split_filename);
1340 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001341 return false;
1342 }
1343 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001344 while (index < 1000) {
1345 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001346 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001347 return false;
1348 }
1349 index++;
1350 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001351 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001352 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001353 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001354 } else {
1355 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001356 md5file = Full_Filename + ".md5";
1357 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001358 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1359 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001360 return false;
1361 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001362 md5sum.setfn(Full_Filename);
1363 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001364 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001365 return false;
1366 } else
1367 return true;
1368 }
1369 return false;
1370}
1371
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001372bool TWPartition::Restore(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Gary Peck43acadf2012-11-21 21:19:01 -08001373 string Restore_File_System;
1374
1375 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001376 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001377
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001378 Restore_File_System = Get_Restore_File_System(restore_folder);
1379
1380 if (Is_File_System(Restore_File_System))
1381 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1382 else if (Is_Image(Restore_File_System)) {
1383 *already_restored_size += TWFunc::Get_File_Size(Backup_Name);
1384 if (Restore_File_System == "emmc")
1385 return Restore_DD(restore_folder, total_restore_size, already_restored_size);
1386 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1387 return Restore_Flash_Image(restore_folder, total_restore_size, already_restored_size);
1388 }
1389
1390 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1391 return false;
1392}
1393
1394string TWPartition::Get_Restore_File_System(string restore_folder) {
1395 size_t first_period, second_period;
1396 string Restore_File_System;
1397
Gary Peck43acadf2012-11-21 21:19:01 -08001398 // Parse backup filename to extract the file system before wiping
1399 first_period = Backup_FileName.find(".");
1400 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001401 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001402 return false;
1403 }
1404 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1405 second_period = Restore_File_System.find(".");
1406 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001407 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001408 return false;
1409 }
1410 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001411 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001412 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001413}
1414
1415string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001416 if (Backup_Method == NONE)
1417 return "none";
1418 else if (Backup_Method == FILES)
1419 return "files";
1420 else if (Backup_Method == DD)
1421 return "dd";
1422 else if (Backup_Method == FLASH_UTILS)
1423 return "flash_utils";
1424 else
1425 return "undefined";
1426 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001427}
1428
1429bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001430 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001431 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001432 return 1;
1433}
1434
1435bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001436 bool Save_Data_Media = Has_Data_Media;
1437
1438 if (!UnMount(true))
1439 return false;
1440
Dees_Troy38bd7602012-09-14 13:33:53 -04001441 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001442 Decrypted_Block_Device = "";
1443 Is_Decrypted = false;
1444 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001445 Find_Actual_Block_Device();
1446 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001447 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001448 Has_Data_Media = Save_Data_Media;
1449 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1450 Recreate_Media_Folder();
1451 }
Ethan Yonker83e82572014-04-04 10:59:28 -05001452#ifndef TW_OEM_BUILD
Dees_Troy2673cec2013-04-02 20:22:16 +00001453 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Ethan Yonker83e82572014-04-04 10:59:28 -05001454#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001455 TWFunc::Toggle_MTP(mtp_was_enabled);
Dees_Troy38bd7602012-09-14 13:33:53 -04001456 return true;
1457 } else {
1458 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001459 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001460 return false;
1461 }
1462 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001463}
1464
1465void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001466 const char* type;
1467 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001468
Dees_Troy68cab492012-12-12 19:29:35 +00001469 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1470 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001471
Dees_Troy38bd7602012-09-14 13:33:53 -04001472 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001473 if (!Is_Present)
1474 return;
Dees_Troy51127312012-09-08 13:08:49 -04001475
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001476 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1477 if (blkid_do_fullprobe(pr)) {
1478 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001479 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001480 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001481 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001482
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001483 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001484 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001485 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001486 return;
1487 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001488
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001489 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001490 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001491}
1492
Gary Peck43acadf2012-11-21 21:19:01 -08001493bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001494 if (!UnMount(true))
1495 return false;
1496
Dees_Troy43d8b002012-09-17 16:00:01 -04001497 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001498 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001499
Dees_Troy2673cec2013-04-02 20:22:16 +00001500 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001501 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001502 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001503 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001504 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001505 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001506 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001507 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001508 return true;
1509 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001510 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001511 return false;
1512 }
1513 } else
1514 return Wipe_RMRF();
1515
1516 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001517}
1518
1519bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001520 if (!UnMount(true))
1521 return false;
1522
Dees_Troyb3265ab2013-08-30 02:59:14 +00001523#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001524 int ret;
1525 char *secontext = NULL;
1526
Dees_Troya95f55c2013-08-17 13:14:43 +00001527 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001528
Dees Troy99c8dbf2014-03-10 16:53:58 +00001529 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001530 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1531 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1532 } else {
1533 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1534 }
1535 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001536 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1537 return false;
1538 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001539 string sedir = Mount_Point + "/lost+found";
1540 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1541 rmdir(sedir.c_str());
1542 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001543 return true;
1544 }
1545#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001546 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001547 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001548
Dees_Troy2673cec2013-04-02 20:22:16 +00001549 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001550 Find_Actual_Block_Device();
1551 Command = "make_ext4fs";
1552 if (!Is_Decrypted && Length != 0) {
1553 // Only use length if we're not decrypted
1554 char len[32];
1555 sprintf(len, "%i", Length);
1556 Command += " -l ";
1557 Command += len;
1558 }
Dees_Troy5295d582013-09-06 15:51:08 +00001559 if (TWFunc::Path_Exists("/file_contexts")) {
1560 Command += " -S /file_contexts";
1561 }
1562 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001563 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001564 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001565 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001566 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001567 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001568 return true;
1569 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001570 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001571 return false;
1572 }
1573 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001574 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001575#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001576 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001577}
1578
1579bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001580 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001581
Dees_Troy43d8b002012-09-17 16:00:01 -04001582 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001583 if (!UnMount(true))
1584 return false;
1585
Dees_Troy2673cec2013-04-02 20:22:16 +00001586 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001587 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001588 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001589 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001590 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001591 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001592 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001593 return true;
1594 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001595 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001596 return false;
1597 }
1598 return true;
1599 }
1600 else
1601 return Wipe_RMRF();
1602
1603 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001604}
1605
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001606bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001607 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001608
1609 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1610 if (!UnMount(true))
1611 return false;
1612
Dees_Troy2673cec2013-04-02 20:22:16 +00001613 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001614 Find_Actual_Block_Device();
1615 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001616 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001617 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001618 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001619 return true;
1620 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001621 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001622 return false;
1623 }
1624 return true;
1625 }
1626 return false;
1627}
1628
Dees_Troy38bd7602012-09-14 13:33:53 -04001629bool TWPartition::Wipe_MTD() {
1630 if (!UnMount(true))
1631 return false;
1632
Dees_Troy2673cec2013-04-02 20:22:16 +00001633 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001634
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001635 mtd_scan_partitions();
1636 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1637 if (mtd == NULL) {
1638 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1639 return false;
1640 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001641
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001642 MtdWriteContext* ctx = mtd_write_partition(mtd);
1643 if (ctx == NULL) {
1644 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1645 return false;
1646 }
1647 if (mtd_erase_blocks(ctx, -1) == -1) {
1648 mtd_write_close(ctx);
1649 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1650 return false;
1651 }
1652 if (mtd_write_close(ctx) != 0) {
1653 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1654 return false;
1655 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001656 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001657 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001658 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001659 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001660}
1661
1662bool TWPartition::Wipe_RMRF() {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001663 if (Is_Storage)
1664 TWFunc::Toggle_MTP(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001665 if (!Mount(true))
1666 return false;
1667
Dees_Troy2673cec2013-04-02 20:22:16 +00001668 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001669 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001670 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001671 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001672}
1673
Dees_Troye5017042013-08-29 16:38:55 +00001674bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001675 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001676
1677 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1678 if (!UnMount(true))
1679 return false;
1680
1681 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1682 Find_Actual_Block_Device();
1683 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001684 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001685 Recreate_AndSec_Folder();
1686 gui_print("Done.\n");
1687 return true;
1688 } else {
1689 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1690 return false;
1691 }
1692 return true;
1693 } else {
1694 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1695 return Wipe_RMRF();
1696 }
1697 return false;
1698}
1699
Dees_Troy51a0e822012-09-05 15:24:24 -04001700bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001701#ifdef TW_OEM_BUILD
1702 // In an OEM Build we want to do a full format
1703 return Wipe_Encryption();
1704#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001705 string dir;
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001706 #ifdef HAVE_SELINUX
1707 fixPermissions perms;
1708 #endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001709
1710 // This handles wiping data on devices with "sdcard" in /data/media
1711 if (!Mount(true))
1712 return false;
1713
Dees_Troy2673cec2013-04-02 20:22:16 +00001714 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001715
1716 DIR* d;
1717 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001718 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001719 struct dirent* de;
1720 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001721 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001722 // The media folder is the "internal sdcard"
1723 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001724 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001725 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001726 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001727
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001728 dir = "/data/";
1729 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001730 if (de->d_type == DT_DIR) {
1731 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001732 } 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 +00001733 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001734 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001735 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001736 }
1737 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001738
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001739 #ifdef HAVE_SELINUX
1740 perms.fixDataInternalContexts();
1741 #endif
1742
Dees_Troy2673cec2013-04-02 20:22:16 +00001743 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001744 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001745 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001746 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001747 return false;
Ethan Yonker83e82572014-04-04 10:59:28 -05001748#endif // ifdef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001749}
1750
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001751bool TWPartition::Backup_Tar(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001752 char back_name[255], split_index[5];
1753 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001754 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001755 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001756 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001757 twrpTar tar;
1758 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001759
Dees_Troy43d8b002012-09-17 16:00:01 -04001760 if (!Mount(true))
1761 return false;
1762
Dees_Troya13d74f2013-03-24 08:54:55 -05001763 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001764 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001765
1766 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001767 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001768
Dees_Troy83bd4832013-05-04 12:39:56 +00001769#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1770 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1771 if (use_encryption && Can_Encrypt_Backup) {
1772 tar.use_encryption = use_encryption;
1773 if (Use_Userdata_Encryption)
1774 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001775 string Password;
1776 DataManager::GetValue("tw_backup_password", Password);
1777 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001778 } else {
1779 use_encryption = false;
1780 }
1781#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001782
1783 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1784 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001785 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001786 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001787 Full_FileName = backup_folder + "/" + Backup_FileName;
1788 tar.setdir(Backup_Path);
1789 tar.setfn(Full_FileName);
1790 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001791 tar.partition_name = Backup_Name;
1792 tar.backup_folder = backup_folder;
1793 if (tar.createTarFork(overall_size, other_backups_size) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00001794 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001795 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001796}
1797
1798bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001799 char back_name[255], backup_size[32];
Vojtech Bocek05534202013-09-11 08:11:56 +02001800 string Full_FileName, Command, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001801 int use_compression;
1802
igoriok87e3d932013-01-31 21:03:53 +02001803 sprintf(backup_size, "%llu", Backup_Size);
1804 DD_BS = backup_size;
1805
Dees_Troyb46a6842012-09-25 11:06:46 -04001806 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001807 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001808
1809 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1810 Backup_FileName = back_name;
1811
1812 Full_FileName = backup_folder + "/" + Backup_FileName;
1813
igoriok87e3d932013-01-31 21:03:53 +02001814 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001815 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001816 TWFunc::Exec_Cmd(Command);
Dees_Troyc154ac22012-10-12 15:36:47 -04001817 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001818 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001819 return false;
1820 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001821 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001822}
1823
1824bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001825 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001826 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001827 int use_compression;
1828
Dees_Troyb46a6842012-09-25 11:06:46 -04001829 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001830 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001831
1832 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1833 Backup_FileName = back_name;
1834
1835 Full_FileName = backup_folder + "/" + Backup_FileName;
1836
1837 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001838 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001839 TWFunc::Exec_Cmd(Command);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001840 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1841 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
Dees_Troy2673cec2013-04-02 20:22:16 +00001842 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001843 return false;
1844 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001845 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001846}
1847
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001848unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
1849 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
1850 if (restore_info.LoadValues() == 0) {
1851 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
1852 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
1853 return Restore_Size;
1854 }
1855 }
1856 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
1857
1858 Full_FileName = restore_folder + "/" + Backup_FileName;
1859
1860 if (Is_Image(Restore_File_System)) {
1861 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
1862 return Restore_Size;
1863 }
1864
1865 twrpTar tar;
1866 tar.setdir(Backup_Path);
1867 tar.setfn(Full_FileName);
1868 tar.backup_name = Backup_Name;
1869#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1870 string Password;
1871 DataManager::GetValue("tw_restore_password", Password);
1872 if (!Password.empty())
1873 tar.setpassword(Password);
1874#endif
1875 tar.partition_name = Backup_Name;
1876 tar.backup_folder = restore_folder;
1877 Restore_Size = tar.get_size();
1878 return Restore_Size;
1879}
1880
1881bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Gary Peck43acadf2012-11-21 21:19:01 -08001882 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001883 int index = 0;
1884 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00001885 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001886
Dees_Troye58d5262012-09-21 12:27:57 -04001887 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001888 if (!Wipe_AndSec())
1889 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001890 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001891 gui_print("Wiping %s...\n", Display_Name.c_str());
Ethan Yonker5eac2222014-06-11 12:22:55 -05001892 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
1893 gui_print("WARNING: This /data backup was made with %s file system!\n", Restore_File_System.c_str());
1894 gui_print("The backup may not boot unless you change back to %s.\n", Restore_File_System.c_str());
1895 if (!Wipe_Data_Without_Wiping_Media())
1896 return false;
1897 } else {
1898 if (!Wipe(Restore_File_System))
1899 return false;
1900 }
Dees_Troye58d5262012-09-21 12:27:57 -04001901 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001902 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001903 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001904
1905 if (!Mount(true))
1906 return false;
1907
Dees_Troy4a2a1262012-09-18 09:33:47 -04001908 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06001909 twrpTar tar;
1910 tar.setdir(Backup_Path);
1911 tar.setfn(Full_FileName);
1912 tar.backup_name = Backup_Name;
1913#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1914 string Password;
1915 DataManager::GetValue("tw_restore_password", Password);
1916 if (!Password.empty())
1917 tar.setpassword(Password);
1918#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001919 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00001920 ret = false;
1921 else
1922 ret = true;
1923#ifdef HAVE_CAPABILITIES
1924 // Restore capabilities to the run-as binary
1925 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
1926 struct vfs_cap_data cap_data;
1927 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
1928
1929 memset(&cap_data, 0, sizeof(cap_data));
1930 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
1931 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
1932 cap_data.data[0].inheritable = 0;
1933 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
1934 cap_data.data[1].inheritable = 0;
1935 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
1936 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
1937 } else {
1938 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
1939 }
1940 }
1941#endif
1942 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001943}
1944
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001945bool TWPartition::Restore_DD(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001946 string Full_FileName, Command;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001947 double display_percent, progress_percent;
1948 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04001949
Dees_Troyda8b55a2012-12-12 19:18:30 +00001950 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001951 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001952
1953 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001954 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001955 return false;
1956 }
1957 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1958 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001959 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001960 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1961 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1962 return false;
1963 }
1964
Dees_Troy2673cec2013-04-02 20:22:16 +00001965 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001966 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001967 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001968 TWFunc::Exec_Cmd(Command);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001969 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
1970 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
1971 DataManager::SetValue("tw_size_progress", size_progress);
1972 progress_percent = (display_percent / 100);
1973 DataManager::SetProgress((float)(progress_percent));
1974 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001975 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001976}
1977
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001978bool TWPartition::Restore_Flash_Image(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001979 string Full_FileName, Command;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001980 double display_percent, progress_percent;
1981 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04001982
Dees_Troy2673cec2013-04-02 20:22:16 +00001983 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001984 Full_FileName = restore_folder + "/" + Backup_FileName;
1985 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1986 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001987 LOGINFO("Erase command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001988 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001989 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001990 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001991 TWFunc::Exec_Cmd(Command);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001992 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
1993 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
1994 DataManager::SetValue("tw_size_progress", size_progress);
1995 progress_percent = (display_percent / 100);
1996 DataManager::SetProgress((float)(progress_percent));
1997 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001998 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001999}
Dees_Troy5bf43922012-09-07 16:07:55 -04002000
2001bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002002 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002003
Dees_Troyab10ee22012-09-21 14:27:30 -04002004 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002005 return false;
2006
Dees_Troy0550cfb2012-10-13 11:56:13 -04002007 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002008 if (Removable || Is_Encrypted) {
2009 if (!Mount(false))
2010 return true;
2011 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002012 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002013
2014 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002015 if (!ret || Size == 0) {
2016 if (!Get_Size_Via_df(Display_Error)) {
2017 if (!Was_Already_Mounted)
2018 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002019 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002020 }
2021 }
Dees_Troy51127312012-09-08 13:08:49 -04002022
Dees_Troy5bf43922012-09-07 16:07:55 -04002023 if (Has_Data_Media) {
2024 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002025 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002026 Used = du.Get_Folder_Size("/data");
2027 Backup_Size = Used;
2028 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002029 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002030 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002031 } else {
2032 if (!Was_Already_Mounted)
2033 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002034 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002035 }
Dees_Troye58d5262012-09-21 12:27:57 -04002036 } else if (Has_Android_Secure) {
2037 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002038 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002039 else {
2040 if (!Was_Already_Mounted)
2041 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002042 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002043 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002044 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002045 if (!Was_Already_Mounted)
2046 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002047 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002048}
Dees_Troy38bd7602012-09-14 13:33:53 -04002049
2050void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002051 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002052 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002053 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002054 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002055 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002056 Is_Present = true;
2057 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002058 return;
2059 }
2060 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002061 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002062 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002063 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002064 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002065 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002066 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002067}
2068
2069void TWPartition::Recreate_Media_Folder(void) {
2070 string Command;
2071
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05002072 #ifdef HAVE_SELINUX
2073 fixPermissions perms;
2074 #endif
2075
Dees_Troy38bd7602012-09-14 13:33:53 -04002076 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002077 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04002078 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002079 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00002080 LOGINFO("Recreating /data/media folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002081 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05002082 #ifdef HAVE_SELINUX
2083 perms.fixDataInternalContexts();
2084 #endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002085 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002086 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002087 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002088 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002089}
Dees_Troye58d5262012-09-21 12:27:57 -04002090
2091void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002092 if (!Has_Android_Secure)
2093 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002094 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002095 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002096 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002097 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002098 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002099 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002100 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002101 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002102 }
2103}