Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 2 | Copyright 2013 TeamWin |
Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 3 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/vfs.h> |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 24 | #include <sys/mount.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 25 | #include <unistd.h> |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 26 | #include <dirent.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 27 | #include <iostream> |
| 28 | #include <sstream> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 29 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 30 | #ifdef TW_INCLUDE_CRYPTO |
| 31 | #include "cutils/properties.h" |
| 32 | #endif |
| 33 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 34 | #include "libblkid/blkid.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 35 | #include "variables.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 36 | #include "twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 37 | #include "partitions.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 38 | #include "data.hpp" |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 39 | #include "twrp-functions.hpp" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 40 | #include "twrpDigest.hpp" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 41 | #include "twrpTar.hpp" |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 42 | #include "twrpDU.hpp" |
bigbiff bigbiff | 6b600f9 | 2014-01-05 18:13:43 -0500 | [diff] [blame] | 43 | #include "fixPermissions.hpp" |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 44 | #include "infomanager.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 45 | extern "C" { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 46 | #include "mtdutils/mtdutils.h" |
| 47 | #include "mtdutils/mounts.h" |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 48 | #ifdef TW_INCLUDE_CRYPTO_SAMSUNG |
| 49 | #include "crypto/libcrypt_samsung/include/libcrypt_samsung.h" |
| 50 | #endif |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 51 | #ifdef USE_EXT4 |
| 52 | #include "make_ext4fs.h" |
| 53 | #endif |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 54 | |
| 55 | #ifdef TW_INCLUDE_CRYPTO |
| 56 | #ifdef TW_INCLUDE_JB_CRYPTO |
| 57 | #include "crypto/jb/cryptfs.h" |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 58 | #elif defined(TW_INCLUDE_L_CRYPTO) |
| 59 | #include "crypto/lollipop/cryptfs.h" |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 60 | #else |
| 61 | #include "crypto/ics/cryptfs.h" |
| 62 | #endif |
| 63 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 64 | } |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 65 | #ifdef HAVE_SELINUX |
| 66 | #include "selinux/selinux.h" |
Ethan Yonker | f27497f | 2014-02-09 11:48:33 -0600 | [diff] [blame] | 67 | #include <selinux/label.h> |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 68 | #endif |
Dees Troy | 4159aed | 2014-02-28 17:24:43 +0000 | [diff] [blame] | 69 | #ifdef HAVE_CAPABILITIES |
| 70 | #include <sys/capability.h> |
| 71 | #include <sys/xattr.h> |
| 72 | #include <linux/xattr.h> |
| 73 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 74 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 75 | using namespace std; |
| 76 | |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 77 | extern struct selabel_handle *selinux_handle; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 78 | extern bool datamedia; |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 79 | |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 80 | struct flag_list { |
| 81 | const char *name; |
| 82 | unsigned flag; |
| 83 | }; |
| 84 | |
| 85 | static struct flag_list mount_flags[] = { |
| 86 | { "noatime", MS_NOATIME }, |
| 87 | { "noexec", MS_NOEXEC }, |
| 88 | { "nosuid", MS_NOSUID }, |
| 89 | { "nodev", MS_NODEV }, |
| 90 | { "nodiratime", MS_NODIRATIME }, |
| 91 | { "ro", MS_RDONLY }, |
| 92 | { "rw", 0 }, |
| 93 | { "remount", MS_REMOUNT }, |
| 94 | { "bind", MS_BIND }, |
| 95 | { "rec", MS_REC }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 96 | #ifdef MS_UNBINDABLE |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 97 | { "unbindable", MS_UNBINDABLE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 98 | #endif |
| 99 | #ifdef MS_PRIVATE |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 100 | { "private", MS_PRIVATE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 101 | #endif |
| 102 | #ifdef MS_SLAVE |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 103 | { "slave", MS_SLAVE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 104 | #endif |
| 105 | #ifdef MS_SHARED |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 106 | { "shared", MS_SHARED }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 107 | #endif |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 108 | { "sync", MS_SYNCHRONOUS }, |
| 109 | { "defaults", 0 }, |
| 110 | { 0, 0 }, |
| 111 | }; |
| 112 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 113 | TWPartition::TWPartition() { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 114 | Can_Be_Mounted = false; |
| 115 | Can_Be_Wiped = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 116 | Can_Be_Backed_Up = false; |
Vojtech Bocek | 1dc3098 | 2013-08-30 21:49:30 +0200 | [diff] [blame] | 117 | Use_Rm_Rf = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 118 | Wipe_During_Factory_Reset = false; |
| 119 | Wipe_Available_in_GUI = false; |
| 120 | Is_SubPartition = false; |
Dees_Troy | 2691f9d | 2012-09-24 11:15:49 -0400 | [diff] [blame] | 121 | Has_SubPartition = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 122 | SubPartition_Of = ""; |
| 123 | Symlink_Path = ""; |
| 124 | Symlink_Mount_Point = ""; |
| 125 | Mount_Point = ""; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 126 | Backup_Path = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 127 | Actual_Block_Device = ""; |
| 128 | Primary_Block_Device = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 129 | Alternate_Block_Device = ""; |
| 130 | Removable = false; |
| 131 | Is_Present = false; |
| 132 | Length = 0; |
| 133 | Size = 0; |
| 134 | Used = 0; |
| 135 | Free = 0; |
| 136 | Backup_Size = 0; |
| 137 | Can_Be_Encrypted = false; |
| 138 | Is_Encrypted = false; |
| 139 | Is_Decrypted = false; |
| 140 | Decrypted_Block_Device = ""; |
| 141 | Display_Name = ""; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 142 | Backup_Display_Name = ""; |
| 143 | Storage_Name = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 144 | Backup_Name = ""; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 145 | Backup_FileName = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 146 | MTD_Name = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 147 | Backup_Method = NONE; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 148 | Can_Encrypt_Backup = false; |
| 149 | Use_Userdata_Encryption = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 150 | Has_Data_Media = false; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 151 | Has_Android_Secure = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 152 | Is_Storage = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 153 | Is_Settings_Storage = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 154 | Storage_Path = ""; |
| 155 | Current_File_System = ""; |
| 156 | Fstab_File_System = ""; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 157 | Mount_Flags = 0; |
| 158 | Mount_Options = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 159 | Format_Block_Size = 0; |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 160 | Ignore_Blkid = false; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 161 | Retain_Layout_Version = false; |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 162 | #ifdef TW_INCLUDE_CRYPTO_SAMSUNG |
| 163 | EcryptFS_Password = ""; |
| 164 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | TWPartition::~TWPartition(void) { |
| 168 | // Do nothing |
| 169 | } |
| 170 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 171 | bool 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_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 175 | string Flags; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 176 | strncpy(full_line, Line.c_str(), line_len); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 177 | bool skip = false; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 178 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 179 | for (index = 0; index < line_len; index++) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 180 | if (full_line[index] == 34) |
| 181 | skip = !skip; |
| 182 | if (!skip && full_line[index] <= 32) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 183 | full_line[index] = '\0'; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 184 | } |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 185 | Mount_Point = full_line; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 186 | LOGINFO("Processing '%s'\n", Mount_Point.c_str()); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 187 | Backup_Path = Mount_Point; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 188 | Storage_Path = Mount_Point; |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 189 | Display_Name = full_line + 1; |
| 190 | Backup_Display_Name = Display_Name; |
| 191 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 192 | 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_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 206 | if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") { |
Dees_Troy | 094207a | 2012-09-26 12:00:39 -0400 | [diff] [blame] | 207 | MTD_Name = ptr; |
| 208 | Find_MTD_Block_Device(MTD_Name); |
Dees_Troy | 5fcd8f9 | 2012-10-16 12:22:05 -0400 | [diff] [blame] | 209 | } 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_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 216 | 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_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 217 | } else if (*ptr != '/') { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 218 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 219 | LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 220 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 221 | LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 222 | return 0; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 223 | } else { |
| 224 | Primary_Block_Device = ptr; |
| 225 | Find_Real_Block_Device(Primary_Block_Device, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 226 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 227 | 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_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 237 | } 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_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 241 | Process_Flags(Flags, Display_Error); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 242 | } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) { |
| 243 | // Do nothing |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 244 | } else { |
| 245 | // Unhandled data |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 246 | LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 247 | } |
| 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_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 255 | LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 256 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 257 | LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 258 | return 0; |
| 259 | } else if (Is_File_System(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 260 | Find_Actual_Block_Device(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 261 | Setup_File_System(Display_Error); |
| 262 | if (Mount_Point == "/system") { |
| 263 | Display_Name = "System"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 264 | Backup_Display_Name = Display_Name; |
| 265 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 266 | Wipe_Available_in_GUI = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 267 | Can_Be_Backed_Up = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 268 | } else if (Mount_Point == "/data") { |
| 269 | Display_Name = "Data"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 270 | Backup_Display_Name = Display_Name; |
| 271 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 272 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 273 | Wipe_During_Factory_Reset = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 274 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 275 | Can_Encrypt_Backup = true; |
| 276 | Use_Userdata_Encryption = true; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 277 | if (datamedia) |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 278 | Setup_Data_Media(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 279 | #ifdef TW_INCLUDE_CRYPTO |
| 280 | Can_Be_Encrypted = true; |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 281 | char crypto_blkdev[255]; |
| 282 | property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error"); |
| 283 | if (strcmp(crypto_blkdev, "error") != 0) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 284 | DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device); |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 285 | DataManager::SetValue(TW_IS_DECRYPTED, 1); |
| 286 | Is_Encrypted = true; |
| 287 | Is_Decrypted = true; |
| 288 | Decrypted_Block_Device = crypto_blkdev; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 289 | LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev); |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 290 | } else if (!Mount(false)) { |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 291 | if (Is_Present) { |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 292 | #if defined(TW_INCLUDE_JB_CRYPTO) || defined(TW_INCLUDE_L_CRYPTO) |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 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 Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 323 | } 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_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 328 | } |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 329 | if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) |
Dees_Troy | 9b21af7 | 2012-10-01 15:51:46 -0400 | [diff] [blame] | 330 | Recreate_Media_Folder(); |
Dees_Troy | 9b21af7 | 2012-10-01 15:51:46 -0400 | [diff] [blame] | 331 | #else |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 332 | if (datamedia) |
| 333 | Recreate_Media_Folder(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 334 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 335 | } else if (Mount_Point == "/cache") { |
| 336 | Display_Name = "Cache"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 337 | Backup_Display_Name = Display_Name; |
| 338 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 339 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 340 | Wipe_During_Factory_Reset = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 341 | Can_Be_Backed_Up = true; |
Dees_Troy | ce2fe77 | 2012-09-28 12:34:33 -0400 | [diff] [blame] | 342 | if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 343 | LOGINFO("Recreating /cache/recovery folder.\n"); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 344 | if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 345 | return -1; |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 346 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 347 | } else if (Mount_Point == "/datadata") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 348 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 349 | Display_Name = "DataData"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 350 | Backup_Display_Name = Display_Name; |
| 351 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 352 | Is_SubPartition = true; |
| 353 | SubPartition_Of = "/data"; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 354 | DataManager::SetValue(TW_HAS_DATADATA, 1); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 355 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 356 | Can_Encrypt_Backup = true; |
| 357 | Use_Userdata_Encryption = false; // This whole partition should be encrypted |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 358 | } else if (Mount_Point == "/sd-ext") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 359 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 360 | Display_Name = "SD-Ext"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 361 | Backup_Display_Name = Display_Name; |
| 362 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 363 | Wipe_Available_in_GUI = true; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 364 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 365 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 366 | Can_Encrypt_Backup = true; |
| 367 | Use_Userdata_Encryption = true; |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 368 | } else if (Mount_Point == "/boot") { |
| 369 | Display_Name = "Boot"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 370 | Backup_Display_Name = Display_Name; |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 371 | DataManager::SetValue("tw_boot_is_mountable", 1); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 372 | Can_Be_Backed_Up = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 373 | } |
| 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_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 378 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 379 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 380 | #else |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 381 | if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 382 | Is_Storage = true; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 383 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 384 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 385 | #endif |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 386 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 387 | #ifdef TW_INTERNAL_STORAGE_PATH |
| 388 | if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) { |
| 389 | Is_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 390 | Is_Settings_Storage = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 391 | Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 392 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 393 | } |
| 394 | #else |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 395 | if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 396 | Is_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 397 | Is_Settings_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 398 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 399 | } |
| 400 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 401 | } else if (Is_Image(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 402 | Find_Actual_Block_Device(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 403 | Setup_Image(Display_Error); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 404 | 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_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 411 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 412 | } |
| 413 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 414 | // Process any custom flags |
| 415 | if (Flags.size() > 0) |
| 416 | Process_Flags(Flags, Display_Error); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 417 | return true; |
| 418 | } |
| 419 | |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 420 | bool TWPartition::Process_FS_Flags(string& Options, int Flags) { |
| 421 | int i; |
| 422 | char *p; |
| 423 | char *savep; |
| 424 | char fs_options[250]; |
| 425 | |
| 426 | strlcpy(fs_options, Options.c_str(), sizeof(fs_options)); |
| 427 | Options = ""; |
| 428 | |
| 429 | p = strtok_r(fs_options, ",", &savep); |
| 430 | while (p) { |
| 431 | /* Look for the flag "p" in the flag list "fl" |
| 432 | * If not found, the loop exits with fl[i].name being null. |
| 433 | */ |
| 434 | for (i = 0; mount_flags[i].name; i++) { |
| 435 | if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) { |
| 436 | Flags |= mount_flags[i].flag; |
| 437 | break; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | if (!mount_flags[i].name) { |
| 442 | if (Options.size() > 0) |
| 443 | Options += ","; |
| 444 | Options += p; |
| 445 | } |
| 446 | p = strtok_r(NULL, ",", &savep); |
| 447 | } |
| 448 | |
| 449 | return true; |
| 450 | } |
| 451 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 452 | bool TWPartition::Process_Flags(string Flags, bool Display_Error) { |
| 453 | char flags[MAX_FSTAB_LINE_LENGTH]; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 454 | int flags_len, index = 0, ptr_len; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 455 | char* ptr; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 456 | bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 457 | |
| 458 | strcpy(flags, Flags.c_str()); |
| 459 | flags_len = Flags.size(); |
| 460 | for (index = 0; index < flags_len; index++) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 461 | if (flags[index] == 34) |
| 462 | skip = !skip; |
| 463 | if (!skip && flags[index] == ';') |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 464 | flags[index] = '\0'; |
| 465 | } |
| 466 | |
| 467 | index = 0; |
| 468 | while (index < flags_len) { |
| 469 | while (index < flags_len && flags[index] == '\0') |
| 470 | index++; |
| 471 | if (index >= flags_len) |
| 472 | continue; |
| 473 | ptr = flags + index; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 474 | ptr_len = strlen(ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 475 | if (strcmp(ptr, "removable") == 0) { |
| 476 | Removable = true; |
Ethan Yonker | 06c3f93 | 2014-02-02 22:11:14 -0600 | [diff] [blame] | 477 | } else if (strncmp(ptr, "storage", 7) == 0) { |
| 478 | if (ptr_len == 7) { |
Ethan Yonker | 06c3f93 | 2014-02-02 22:11:14 -0600 | [diff] [blame] | 479 | Is_Storage = true; |
| 480 | } else if (ptr_len == 9) { |
| 481 | ptr += 9; |
| 482 | if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') { |
| 483 | LOGINFO("storage set to true\n"); |
| 484 | Is_Storage = true; |
| 485 | } else { |
| 486 | LOGINFO("storage set to false\n"); |
| 487 | Is_Storage = false; |
| 488 | } |
| 489 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 490 | } else if (strcmp(ptr, "settingsstorage") == 0) { |
| 491 | Is_Storage = true; |
Matt Mower | bf4efa3 | 2014-04-14 23:25:26 -0500 | [diff] [blame] | 492 | } else if (strcmp(ptr, "andsec") == 0) { |
| 493 | Has_Android_Secure = true; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 494 | } else if (strcmp(ptr, "canbewiped") == 0) { |
| 495 | Can_Be_Wiped = true; |
Hashcode | dabfd49 | 2013-08-29 22:45:30 -0700 | [diff] [blame] | 496 | } else if (strcmp(ptr, "usermrf") == 0) { |
| 497 | Use_Rm_Rf = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 498 | } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) { |
| 499 | ptr += 7; |
| 500 | if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') |
| 501 | Can_Be_Backed_Up = true; |
| 502 | else |
| 503 | Can_Be_Backed_Up = false; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 504 | } else if (strcmp(ptr, "wipeingui") == 0) { |
| 505 | Can_Be_Wiped = true; |
| 506 | Wipe_Available_in_GUI = true; |
| 507 | } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) { |
| 508 | Can_Be_Wiped = true; |
| 509 | Wipe_Available_in_GUI = true; |
| 510 | Wipe_During_Factory_Reset = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 511 | } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) { |
Dees_Troy | 2c4c26f | 2013-01-28 15:26:43 +0000 | [diff] [blame] | 512 | ptr += 15; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 513 | Is_SubPartition = true; |
| 514 | SubPartition_Of = ptr; |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 515 | } else if (strcmp(ptr, "ignoreblkid") == 0) { |
| 516 | Ignore_Blkid = true; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 517 | } else if (strcmp(ptr, "retainlayoutversion") == 0) { |
| 518 | Retain_Layout_Version = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 519 | } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 520 | ptr += 8; |
| 521 | Symlink_Path = ptr; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 522 | } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) { |
| 523 | has_display_name = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 524 | ptr += 8; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 525 | if (*ptr == '\"') ptr++; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 526 | Display_Name = ptr; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 527 | if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") { |
| 528 | Display_Name.resize(Display_Name.size() - 1); |
| 529 | } |
| 530 | } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) { |
| 531 | has_storage_name = true; |
| 532 | ptr += 11; |
| 533 | if (*ptr == '\"') ptr++; |
| 534 | Storage_Name = ptr; |
| 535 | if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") { |
| 536 | Storage_Name.resize(Storage_Name.size() - 1); |
| 537 | } |
| 538 | } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) { |
| 539 | has_backup_name = true; |
| 540 | ptr += 10; |
| 541 | if (*ptr == '\"') ptr++; |
| 542 | Backup_Display_Name = ptr; |
| 543 | if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") { |
| 544 | Backup_Display_Name.resize(Backup_Display_Name.size() - 1); |
| 545 | } |
| 546 | } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 547 | ptr += 10; |
| 548 | Format_Block_Size = atoi(ptr); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 549 | } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 550 | ptr += 7; |
| 551 | Length = atoi(ptr); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 552 | } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) { |
| 553 | ptr += 17; |
| 554 | if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') |
| 555 | Can_Encrypt_Backup = true; |
| 556 | else |
| 557 | Can_Encrypt_Backup = false; |
| 558 | } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) { |
| 559 | ptr += 21; |
| 560 | if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') { |
| 561 | Can_Encrypt_Backup = true; |
| 562 | Use_Userdata_Encryption = true; |
| 563 | } else { |
| 564 | Use_Userdata_Encryption = false; |
| 565 | } |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 566 | } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) { |
| 567 | ptr += 8; |
| 568 | if (*ptr == '\"') ptr++; |
| 569 | |
| 570 | Mount_Options = ptr; |
| 571 | if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") { |
| 572 | Mount_Options.resize(Mount_Options.size() - 1); |
| 573 | } |
| 574 | Process_FS_Flags(Mount_Options, Mount_Flags); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 575 | } else { |
| 576 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 577 | LOGERR("Unhandled flag: '%s'\n", ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 578 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 579 | LOGINFO("Unhandled flag: '%s'\n", ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 580 | } |
| 581 | while (index < flags_len && flags[index] != '\0') |
| 582 | index++; |
| 583 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 584 | if (has_display_name && !has_storage_name) |
| 585 | Storage_Name = Display_Name; |
| 586 | if (!has_display_name && has_storage_name) |
| 587 | Display_Name = Storage_Name; |
Dees_Troy | 74fb2e9 | 2013-04-15 14:35:47 +0000 | [diff] [blame] | 588 | if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure") |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 589 | Backup_Display_Name = Display_Name; |
| 590 | if (!has_display_name && has_backup_name) |
| 591 | Display_Name = Backup_Display_Name; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 592 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 593 | } |
| 594 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 595 | bool TWPartition::Is_File_System(string File_System) { |
| 596 | if (File_System == "ext2" || |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 597 | File_System == "ext3" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 598 | File_System == "ext4" || |
| 599 | File_System == "vfat" || |
| 600 | File_System == "ntfs" || |
| 601 | File_System == "yaffs2" || |
bigbiff bigbiff | 3e14652 | 2012-11-14 14:32:59 -0500 | [diff] [blame] | 602 | File_System == "exfat" || |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 603 | File_System == "f2fs" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 604 | File_System == "auto") |
| 605 | return true; |
| 606 | else |
| 607 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 608 | } |
| 609 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 610 | bool TWPartition::Is_Image(string File_System) { |
Dees_Troy | 5fcd8f9 | 2012-10-16 12:22:05 -0400 | [diff] [blame] | 611 | if (File_System == "emmc" || File_System == "mtd" || File_System == "bml") |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 612 | return true; |
| 613 | else |
| 614 | return false; |
| 615 | } |
| 616 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 617 | bool TWPartition::Make_Dir(string Path, bool Display_Error) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 618 | if (!TWFunc::Path_Exists(Path)) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 619 | if (mkdir(Path.c_str(), 0777) == -1) { |
| 620 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 621 | LOGERR("Can not create '%s' folder.\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 622 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 623 | LOGINFO("Can not create '%s' folder.\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 624 | return false; |
| 625 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 626 | LOGINFO("Created '%s' folder.\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 627 | return true; |
| 628 | } |
| 629 | } |
| 630 | return true; |
| 631 | } |
| 632 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 633 | void TWPartition::Setup_File_System(bool Display_Error) { |
| 634 | struct statfs st; |
| 635 | |
| 636 | Can_Be_Mounted = true; |
| 637 | Can_Be_Wiped = true; |
| 638 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 639 | // Make the mount point folder if it doesn't exist |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 640 | Make_Dir(Mount_Point, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 641 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 642 | Backup_Name = Display_Name; |
| 643 | Backup_Method = FILES; |
| 644 | } |
| 645 | |
| 646 | void TWPartition::Setup_Image(bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 647 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 648 | Backup_Name = Display_Name; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 649 | if (Current_File_System == "emmc") |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 650 | Backup_Method = DD; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 651 | else if (Current_File_System == "mtd" || Current_File_System == "bml") |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 652 | Backup_Method = FLASH_UTILS; |
| 653 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 654 | LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 655 | if (Find_Partition_Size()) { |
| 656 | Used = Size; |
| 657 | Backup_Size = Size; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 658 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 659 | if (Display_Error) |
Dees Troy | d932ce1 | 2013-10-18 17:12:59 +0000 | [diff] [blame] | 660 | LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 661 | else |
Dees Troy | d932ce1 | 2013-10-18 17:12:59 +0000 | [diff] [blame] | 662 | LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 666 | void TWPartition::Setup_AndSec(void) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 667 | Backup_Display_Name = "Android Secure"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 668 | Backup_Name = "and-sec"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 669 | Can_Be_Backed_Up = true; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 670 | Has_Android_Secure = true; |
| 671 | Symlink_Path = Mount_Point + "/.android_secure"; |
| 672 | Symlink_Mount_Point = "/and-sec"; |
| 673 | Backup_Path = Symlink_Mount_Point; |
| 674 | Make_Dir("/and-sec", true); |
| 675 | Recreate_AndSec_Folder(); |
Ethan Yonker | d4d1073 | 2014-02-03 15:27:52 -0600 | [diff] [blame] | 676 | Mount_Storage_Retry(); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 677 | } |
| 678 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 679 | void TWPartition::Setup_Data_Media() { |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 680 | LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str()); |
| 681 | Storage_Name = "Internal Storage"; |
| 682 | Has_Data_Media = true; |
| 683 | Is_Storage = true; |
| 684 | Is_Settings_Storage = true; |
| 685 | Storage_Path = "/data/media"; |
| 686 | Symlink_Path = Storage_Path; |
| 687 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 688 | Make_Dir("/emmc", false); |
| 689 | Symlink_Mount_Point = "/emmc"; |
| 690 | } else { |
| 691 | Make_Dir("/sdcard", false); |
| 692 | Symlink_Mount_Point = "/sdcard"; |
| 693 | } |
| 694 | if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) { |
| 695 | Storage_Path = "/data/media/0"; |
| 696 | Symlink_Path = Storage_Path; |
| 697 | DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0"); |
| 698 | UnMount(true); |
| 699 | } |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 700 | DataManager::SetValue("tw_has_internal", 1); |
| 701 | DataManager::SetValue("tw_has_data_media", 1); |
| 702 | du.add_absolute_dir("/data/media"); |
| 703 | } |
| 704 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 705 | void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) { |
| 706 | char device[512], realDevice[512]; |
| 707 | |
| 708 | strcpy(device, Block.c_str()); |
| 709 | memset(realDevice, 0, sizeof(realDevice)); |
| 710 | while (readlink(device, realDevice, sizeof(realDevice)) > 0) |
| 711 | { |
| 712 | strcpy(device, realDevice); |
| 713 | memset(realDevice, 0, sizeof(realDevice)); |
| 714 | } |
| 715 | |
| 716 | if (device[0] != '/') { |
| 717 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 718 | LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 719 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 720 | LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 721 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 722 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 723 | Block = device; |
| 724 | return; |
| 725 | } |
| 726 | } |
| 727 | |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 728 | void TWPartition::Mount_Storage_Retry(void) { |
| 729 | // On some devices, storage doesn't want to mount right away, retry and sleep |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 730 | if (!Mount(true)) { |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 731 | int retry_count = 5; |
| 732 | while (retry_count > 0 && !Mount(false)) { |
| 733 | usleep(500000); |
| 734 | retry_count--; |
| 735 | } |
| 736 | Mount(true); |
| 737 | } |
| 738 | } |
| 739 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 740 | bool TWPartition::Find_MTD_Block_Device(string MTD_Name) { |
| 741 | FILE *fp = NULL; |
| 742 | char line[255]; |
| 743 | |
| 744 | fp = fopen("/proc/mtd", "rt"); |
| 745 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 746 | LOGERR("Device does not support /proc/mtd\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 747 | return false; |
| 748 | } |
| 749 | |
| 750 | while (fgets(line, sizeof(line), fp) != NULL) |
| 751 | { |
| 752 | char device[32], label[32]; |
| 753 | unsigned long size = 0; |
| 754 | char* fstype = NULL; |
| 755 | int deviceId; |
| 756 | |
| 757 | sscanf(line, "%s %lx %*s %*c%s", device, &size, label); |
| 758 | |
| 759 | // Skip header and blank lines |
| 760 | if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8)) |
| 761 | continue; |
| 762 | |
| 763 | // Strip off the trailing " from the label |
| 764 | label[strlen(label)-1] = '\0'; |
| 765 | |
| 766 | if (strcmp(label, MTD_Name.c_str()) == 0) { |
| 767 | // We found our device |
| 768 | // Strip off the trailing : from the device |
| 769 | device[strlen(device)-1] = '\0'; |
| 770 | if (sscanf(device,"mtd%d", &deviceId) == 1) { |
| 771 | sprintf(device, "/dev/block/mtdblock%d", deviceId); |
| 772 | Primary_Block_Device = device; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 773 | fclose(fp); |
| 774 | return true; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | } |
| 778 | fclose(fp); |
| 779 | |
| 780 | return false; |
| 781 | } |
| 782 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 783 | bool TWPartition::Get_Size_Via_statfs(bool Display_Error) { |
| 784 | struct statfs st; |
| 785 | string Local_Path = Mount_Point + "/."; |
| 786 | |
| 787 | if (!Mount(Display_Error)) |
| 788 | return false; |
| 789 | |
| 790 | if (statfs(Local_Path.c_str(), &st) != 0) { |
| 791 | if (!Removable) { |
| 792 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 793 | LOGERR("Unable to statfs '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 794 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 795 | LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 796 | } |
| 797 | return false; |
| 798 | } |
| 799 | Size = (st.f_blocks * st.f_bsize); |
| 800 | Used = ((st.f_blocks - st.f_bfree) * st.f_bsize); |
| 801 | Free = (st.f_bfree * st.f_bsize); |
| 802 | Backup_Size = Used; |
| 803 | return true; |
| 804 | } |
| 805 | |
| 806 | bool TWPartition::Get_Size_Via_df(bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 807 | FILE* fp; |
| 808 | char command[255], line[512]; |
| 809 | int include_block = 1; |
| 810 | unsigned int min_len; |
| 811 | |
| 812 | if (!Mount(Display_Error)) |
| 813 | return false; |
| 814 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 815 | min_len = Actual_Block_Device.size() + 2; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 816 | sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 817 | TWFunc::Exec_Cmd(command); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 818 | fp = fopen("/tmp/dfoutput.txt", "rt"); |
| 819 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 820 | LOGINFO("Unable to open /tmp/dfoutput.txt.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 821 | return false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 822 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 823 | |
| 824 | while (fgets(line, sizeof(line), fp) != NULL) |
| 825 | { |
| 826 | unsigned long blocks, used, available; |
| 827 | char device[64]; |
| 828 | char tmpString[64]; |
| 829 | |
| 830 | if (strncmp(line, "Filesystem", 10) == 0) |
| 831 | continue; |
| 832 | if (strlen(line) < min_len) { |
| 833 | include_block = 0; |
| 834 | continue; |
| 835 | } |
| 836 | if (include_block) { |
| 837 | sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available); |
| 838 | } else { |
| 839 | // The device block string is so long that the df information is on the next line |
| 840 | int space_count = 0; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 841 | sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 842 | while (tmpString[space_count] == 32) |
| 843 | space_count++; |
| 844 | sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available); |
| 845 | } |
| 846 | |
| 847 | // Adjust block size to byte size |
| 848 | Size = blocks * 1024ULL; |
| 849 | Used = used * 1024ULL; |
| 850 | Free = available * 1024ULL; |
| 851 | Backup_Size = Used; |
| 852 | } |
| 853 | fclose(fp); |
| 854 | return true; |
| 855 | } |
| 856 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 857 | bool TWPartition::Find_Partition_Size(void) { |
| 858 | FILE* fp; |
| 859 | char line[512]; |
| 860 | string tmpdevice; |
| 861 | |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 862 | fp = fopen("/proc/dumchar_info", "rt"); |
| 863 | if (fp != NULL) { |
| 864 | while (fgets(line, sizeof(line), fp) != NULL) |
| 865 | { |
| 866 | char label[32], device[32]; |
| 867 | unsigned long size = 0; |
| 868 | |
that | d43bf2d | 2014-09-21 23:13:02 +0200 | [diff] [blame] | 869 | sscanf(line, "%s %lx %*x %*u %s", label, &size, device); |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 870 | |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 871 | // Skip header, annotation and blank lines |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 872 | if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8)) |
| 873 | continue; |
| 874 | |
| 875 | tmpdevice = "/dev/"; |
| 876 | tmpdevice += label; |
| 877 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
| 878 | Size = size; |
| 879 | fclose(fp); |
| 880 | return true; |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 885 | // In this case, we'll first get the partitions we care about (with labels) |
| 886 | fp = fopen("/proc/partitions", "rt"); |
| 887 | if (fp == NULL) |
| 888 | return false; |
| 889 | |
| 890 | while (fgets(line, sizeof(line), fp) != NULL) |
| 891 | { |
| 892 | unsigned long major, minor, blocks; |
| 893 | char device[512]; |
| 894 | char tmpString[64]; |
| 895 | |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 896 | if (strlen(line) < 7 || line[0] == 'm') continue; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 897 | sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device); |
| 898 | |
| 899 | tmpdevice = "/dev/block/"; |
| 900 | tmpdevice += device; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 901 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 902 | // Adjust block size to byte size |
| 903 | Size = blocks * 1024ULL; |
| 904 | fclose(fp); |
| 905 | return true; |
| 906 | } |
| 907 | } |
| 908 | fclose(fp); |
| 909 | return false; |
| 910 | } |
| 911 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 912 | bool TWPartition::Is_Mounted(void) { |
| 913 | if (!Can_Be_Mounted) |
| 914 | return false; |
| 915 | |
| 916 | struct stat st1, st2; |
| 917 | string test_path; |
| 918 | |
| 919 | // Check to see if the mount point directory exists |
| 920 | test_path = Mount_Point + "/."; |
| 921 | if (stat(test_path.c_str(), &st1) != 0) return false; |
| 922 | |
| 923 | // Check to see if the directory above the mount point exists |
| 924 | test_path = Mount_Point + "/../."; |
| 925 | if (stat(test_path.c_str(), &st2) != 0) return false; |
| 926 | |
| 927 | // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device |
| 928 | int ret = (st1.st_dev != st2.st_dev) ? true : false; |
| 929 | |
| 930 | return ret; |
| 931 | } |
| 932 | |
| 933 | bool TWPartition::Mount(bool Display_Error) { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 934 | int exfat_mounted = 0; |
| 935 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 936 | if (Is_Mounted()) { |
| 937 | return true; |
| 938 | } else if (!Can_Be_Mounted) { |
| 939 | return false; |
| 940 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 941 | |
| 942 | Find_Actual_Block_Device(); |
| 943 | |
| 944 | // Check the current file system before mounting |
| 945 | Check_FS_Type(); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 946 | if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 947 | string cmd = "/sbin/exfat-fuse -o big_writes,max_read=131072,max_write=131072 " + Actual_Block_Device + " " + Mount_Point; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 948 | LOGINFO("cmd: %s\n", cmd.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 949 | string result; |
| 950 | if (TWFunc::Exec_Cmd(cmd, result) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 951 | LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 952 | Current_File_System = "vfat"; |
| 953 | } else { |
| 954 | #ifdef TW_NO_EXFAT_FUSE |
| 955 | UnMount(false); |
| 956 | // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat |
| 957 | // Some kernels let us mount vfat as exfat which doesn't work out too well |
| 958 | #else |
| 959 | exfat_mounted = 1; |
| 960 | #endif |
| 961 | } |
| 962 | } |
Dees_Troy | 2204203 | 2012-12-18 21:23:08 +0000 | [diff] [blame] | 963 | if (Fstab_File_System == "yaffs2") { |
| 964 | // mount an MTD partition as a YAFFS2 filesystem. |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 965 | const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME; |
| 966 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) { |
| 967 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) { |
| 968 | if (Display_Error) |
| 969 | LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 970 | else |
| 971 | LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 972 | return false; |
| 973 | } else { |
| 974 | LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str()); |
| 975 | return true; |
| 976 | } |
| 977 | } else { |
| 978 | struct stat st; |
| 979 | string test_path = Mount_Point; |
| 980 | if (stat(test_path.c_str(), &st) < 0) { |
| 981 | if (Display_Error) |
| 982 | LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 983 | else |
| 984 | LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 985 | return false; |
| 986 | } |
| 987 | mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH; |
| 988 | if (new_mode != st.st_mode) { |
| 989 | LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str()); |
| 990 | if (chmod(Mount_Point.c_str(), new_mode) < 0) { |
| 991 | if (Display_Error) |
| 992 | LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno)); |
| 993 | else |
| 994 | LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno)); |
| 995 | return false; |
| 996 | } |
| 997 | } |
Dees_Troy | 2204203 | 2012-12-18 21:23:08 +0000 | [diff] [blame] | 998 | return true; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 999 | } |
Dees Troy | 216e042 | 2014-02-07 03:46:42 +0000 | [diff] [blame] | 1000 | } 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_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1001 | #ifdef TW_NO_EXFAT_FUSE |
| 1002 | if (Current_File_System == "exfat") { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1003 | LOGINFO("Mounting exfat failed, trying vfat...\n"); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1004 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) { |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1005 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1006 | LOGERR("Unable to mount '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1007 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1008 | LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str()); |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 1009 | 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_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1010 | return false; |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1011 | } |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 1012 | } else { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1013 | #endif |
bigbiff bigbiff | 26774a0 | 2014-03-29 18:22:00 -0400 | [diff] [blame] | 1014 | if (!Removable && Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1015 | LOGERR("Unable to mount '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1016 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1017 | LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str()); |
| 1018 | LOGINFO("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1019 | return false; |
| 1020 | #ifdef TW_NO_EXFAT_FUSE |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1021 | } |
| 1022 | #endif |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1023 | } |
| 1024 | #ifdef TW_INCLUDE_CRYPTO_SAMSUNG |
| 1025 | string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH); |
| 1026 | MetaEcfsFile += "/.MetaEcfsFile"; |
| 1027 | if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) { |
| 1028 | if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) { |
| 1029 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1030 | LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1031 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1032 | LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1033 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1034 | LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1035 | Is_Decrypted = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1036 | } |
Dees_Troy | 066eb30 | 2013-08-23 17:20:32 +0000 | [diff] [blame] | 1037 | } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) { |
| 1038 | if (Is_Decrypted) |
| 1039 | LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1040 | Is_Decrypted = false; |
| 1041 | } |
| 1042 | #endif |
| 1043 | if (Removable) |
| 1044 | Update_Size(Display_Error); |
| 1045 | |
| 1046 | if (!Symlink_Mount_Point.empty()) { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1047 | string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'"; |
| 1048 | TWFunc::Exec_Cmd(Command); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1049 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1050 | return true; |
| 1051 | } |
| 1052 | |
| 1053 | bool TWPartition::UnMount(bool Display_Error) { |
| 1054 | if (Is_Mounted()) { |
| 1055 | int never_unmount_system; |
| 1056 | |
| 1057 | DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system); |
| 1058 | if (never_unmount_system == 1 && Mount_Point == "/system") |
| 1059 | return true; // Never unmount system if you're not supposed to unmount it |
| 1060 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1061 | if (Is_Storage) |
| 1062 | TWFunc::Toggle_MTP(false); |
| 1063 | |
Dees_Troy | c8bafa1 | 2013-01-10 15:43:00 +0000 | [diff] [blame] | 1064 | #ifdef TW_INCLUDE_CRYPTO_SAMSUNG |
| 1065 | if (EcryptFS_Password.size() > 0) { |
| 1066 | if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) { |
| 1067 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1068 | LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str()); |
Dees_Troy | c8bafa1 | 2013-01-10 15:43:00 +0000 | [diff] [blame] | 1069 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1070 | LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str()); |
Dees_Troy | c8bafa1 | 2013-01-10 15:43:00 +0000 | [diff] [blame] | 1071 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1072 | LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str()); |
Dees_Troy | c8bafa1 | 2013-01-10 15:43:00 +0000 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | #endif |
| 1076 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1077 | if (!Symlink_Mount_Point.empty()) |
| 1078 | umount(Symlink_Mount_Point.c_str()); |
| 1079 | |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 1080 | umount(Mount_Point.c_str()); |
| 1081 | if (Is_Mounted()) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1082 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1083 | LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1084 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1085 | LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1086 | return false; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1087 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1088 | return true; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1089 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1090 | } else { |
| 1091 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1095 | bool TWPartition::Wipe(string New_File_System) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1096 | bool wiped = false, update_crypt = false, recreate_media = true, mtp_toggle = true; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1097 | int check; |
| 1098 | string Layout_Filename = Mount_Point + "/.layout_version"; |
| 1099 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1100 | if (!Can_Be_Wiped) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1101 | LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1102 | return false; |
| 1103 | } |
| 1104 | |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 1105 | if (Mount_Point == "/cache") |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1106 | Log_Offset = 0; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 1107 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 1108 | #ifdef TW_INCLUDE_CRYPTO_SAMSUNG |
| 1109 | if (Mount_Point == "/data" && Mount(false)) { |
| 1110 | if (TWFunc::Path_Exists("/data/system/edk_p_sd")) |
| 1111 | TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600); |
| 1112 | } |
| 1113 | #endif |
| 1114 | |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1115 | if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename)) |
| 1116 | TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600); |
| 1117 | else |
| 1118 | unlink("/.layout_version"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1119 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1120 | if (Has_Data_Media && Current_File_System == New_File_System) { |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1121 | wiped = Wipe_Data_Without_Wiping_Media(); |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1122 | recreate_media = false; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1123 | mtp_toggle = false; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1124 | } else { |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1125 | DataManager::GetValue(TW_RM_RF_VAR, check); |
| 1126 | |
Hashcode | dabfd49 | 2013-08-29 22:45:30 -0700 | [diff] [blame] | 1127 | if (check || Use_Rm_Rf) |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1128 | wiped = Wipe_RMRF(); |
| 1129 | else if (New_File_System == "ext4") |
| 1130 | wiped = Wipe_EXT4(); |
| 1131 | else if (New_File_System == "ext2" || New_File_System == "ext3") |
| 1132 | wiped = Wipe_EXT23(New_File_System); |
| 1133 | else if (New_File_System == "vfat") |
| 1134 | wiped = Wipe_FAT(); |
| 1135 | else if (New_File_System == "exfat") |
| 1136 | wiped = Wipe_EXFAT(); |
| 1137 | else if (New_File_System == "yaffs2") |
| 1138 | wiped = Wipe_MTD(); |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 1139 | else if (New_File_System == "f2fs") |
| 1140 | wiped = Wipe_F2FS(); |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1141 | else { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1142 | if (Is_Storage) { |
| 1143 | TWFunc::Toggle_MTP(true); |
| 1144 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1145 | LOGERR("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str()); |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1146 | unlink("/.layout_version"); |
| 1147 | return false; |
| 1148 | } |
| 1149 | update_crypt = wiped; |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1150 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1151 | |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1152 | if (wiped) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 1153 | #ifdef TW_INCLUDE_CRYPTO_SAMSUNG |
| 1154 | if (Mount_Point == "/data" && Mount(false)) { |
| 1155 | if (TWFunc::Path_Exists("/tmp/edk_p_sd")) { |
| 1156 | Make_Dir("/data/system", true); |
| 1157 | TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600); |
| 1158 | } |
| 1159 | } |
| 1160 | #endif |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1161 | |
Dees_Troy | 1c1ac44 | 2013-01-17 21:42:14 +0000 | [diff] [blame] | 1162 | if (Mount_Point == "/cache") |
| 1163 | DataManager::Output_Version(); |
| 1164 | |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1165 | if (TWFunc::Path_Exists("/.layout_version") && Mount(false)) |
| 1166 | TWFunc::copy_file("/.layout_version", Layout_Filename, 0600); |
| 1167 | |
| 1168 | if (update_crypt) { |
| 1169 | Setup_File_System(false); |
| 1170 | if (Is_Encrypted && !Is_Decrypted) { |
| 1171 | // just wiped an encrypted partition back to its unencrypted state |
| 1172 | Is_Encrypted = false; |
| 1173 | Is_Decrypted = false; |
| 1174 | Decrypted_Block_Device = ""; |
| 1175 | if (Mount_Point == "/data") { |
| 1176 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
| 1177 | DataManager::SetValue(TW_IS_DECRYPTED, 0); |
| 1178 | } |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1179 | } |
| 1180 | } |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1181 | |
| 1182 | if (Mount_Point == "/data" && Has_Data_Media && recreate_media) { |
| 1183 | Recreate_Media_Folder(); |
| 1184 | } |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1185 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1186 | if (Is_Storage && mtp_toggle) { |
| 1187 | TWFunc::Toggle_MTP(true); |
| 1188 | } |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1189 | return wiped; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1190 | } |
| 1191 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1192 | bool TWPartition::Wipe() { |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1193 | if (Is_File_System(Current_File_System)) |
| 1194 | return Wipe(Current_File_System); |
| 1195 | else |
| 1196 | return Wipe(Fstab_File_System); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1197 | } |
| 1198 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1199 | bool TWPartition::Wipe_AndSec(void) { |
| 1200 | if (!Has_Android_Secure) |
| 1201 | return false; |
| 1202 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1203 | if (!Mount(true)) |
| 1204 | return false; |
| 1205 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1206 | gui_print("Wiping %s\n", Backup_Display_Name.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1207 | TWFunc::removeDir(Mount_Point + "/.android_secure/", true); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1208 | return true; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1209 | } |
| 1210 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1211 | bool TWPartition::Can_Repair() { |
| 1212 | if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/dosfsck")) |
| 1213 | return true; |
| 1214 | else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck")) |
| 1215 | return true; |
| 1216 | else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat")) |
| 1217 | return true; |
| 1218 | else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs")) |
| 1219 | return true; |
| 1220 | return false; |
| 1221 | } |
| 1222 | |
| 1223 | bool TWPartition::Repair() { |
| 1224 | string command; |
| 1225 | |
| 1226 | if (Current_File_System == "vfat") { |
| 1227 | if (!TWFunc::Path_Exists("/sbin/dosfsck")) { |
| 1228 | gui_print("dosfsck does not exist! Cannot repair!\n"); |
| 1229 | return false; |
| 1230 | } |
| 1231 | if (!UnMount(true)) |
| 1232 | return false; |
| 1233 | gui_print("Repairing %s using dosfsck...\n", Display_Name.c_str()); |
| 1234 | Find_Actual_Block_Device(); |
| 1235 | command = "/sbin/dosfsck -y " + Actual_Block_Device; |
| 1236 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1237 | if (TWFunc::Exec_Cmd(command) == 0) { |
| 1238 | gui_print("Done.\n"); |
| 1239 | return true; |
| 1240 | } else { |
| 1241 | LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str()); |
| 1242 | return false; |
| 1243 | } |
| 1244 | } |
| 1245 | if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") { |
| 1246 | if (!TWFunc::Path_Exists("/sbin/e2fsck")) { |
| 1247 | gui_print("e2fsck does not exist! Cannot repair!\n"); |
| 1248 | return false; |
| 1249 | } |
| 1250 | if (!UnMount(true)) |
| 1251 | return false; |
| 1252 | gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str()); |
| 1253 | Find_Actual_Block_Device(); |
| 1254 | command = "/sbin/e2fsck -p " + Actual_Block_Device; |
| 1255 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1256 | if (TWFunc::Exec_Cmd(command) == 0) { |
| 1257 | gui_print("Done.\n"); |
| 1258 | return true; |
| 1259 | } else { |
| 1260 | LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str()); |
| 1261 | return false; |
| 1262 | } |
| 1263 | } |
| 1264 | if (Current_File_System == "exfat") { |
| 1265 | if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) { |
| 1266 | gui_print("fsck.exfat does not exist! Cannot repair!\n"); |
| 1267 | return false; |
| 1268 | } |
| 1269 | if (!UnMount(true)) |
| 1270 | return false; |
| 1271 | gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str()); |
| 1272 | Find_Actual_Block_Device(); |
| 1273 | command = "/sbin/fsck.exfat " + Actual_Block_Device; |
| 1274 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1275 | if (TWFunc::Exec_Cmd(command) == 0) { |
| 1276 | gui_print("Done.\n"); |
| 1277 | return true; |
| 1278 | } else { |
| 1279 | LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str()); |
| 1280 | return false; |
| 1281 | } |
| 1282 | } |
| 1283 | if (Current_File_System == "f2fs") { |
| 1284 | if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) { |
| 1285 | gui_print("fsck.f2fs does not exist! Cannot repair!\n"); |
| 1286 | return false; |
| 1287 | } |
| 1288 | if (!UnMount(true)) |
| 1289 | return false; |
| 1290 | gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str()); |
| 1291 | Find_Actual_Block_Device(); |
| 1292 | command = "/sbin/fsck.f2fs " + Actual_Block_Device; |
| 1293 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1294 | if (TWFunc::Exec_Cmd(command) == 0) { |
| 1295 | gui_print("Done.\n"); |
| 1296 | return true; |
| 1297 | } else { |
| 1298 | LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str()); |
| 1299 | return false; |
| 1300 | } |
| 1301 | } |
| 1302 | return false; |
| 1303 | } |
| 1304 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1305 | bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1306 | if (Backup_Method == FILES) |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1307 | return Backup_Tar(backup_folder, overall_size, other_backups_size); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1308 | else if (Backup_Method == DD) |
| 1309 | return Backup_DD(backup_folder); |
| 1310 | else if (Backup_Method == FLASH_UTILS) |
| 1311 | return Backup_Dump_Image(backup_folder); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1312 | LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1313 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1314 | } |
| 1315 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1316 | bool TWPartition::Check_MD5(string restore_folder) { |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 1317 | string Full_Filename, md5file; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1318 | char split_filename[512]; |
| 1319 | int index = 0; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 1320 | twrpDigest md5sum; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1321 | |
Captain Throwback | ff5935f | 2014-09-23 16:08:34 -0400 | [diff] [blame] | 1322 | sync(); |
| 1323 | |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 1324 | memset(split_filename, 0, sizeof(split_filename)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1325 | Full_Filename = restore_folder + "/" + Backup_FileName; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1326 | if (!TWFunc::Path_Exists(Full_Filename)) { |
| 1327 | // This is a split archive, we presume |
| 1328 | sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1329 | LOGINFO("split_filename: %s\n", split_filename); |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 1330 | md5file = split_filename; |
| 1331 | md5file += ".md5"; |
| 1332 | if (!TWFunc::Path_Exists(md5file)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1333 | LOGERR("No md5 file found for '%s'.\n", split_filename); |
| 1334 | LOGERR("Please unselect Enable MD5 verification to restore.\n"); |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 1335 | return false; |
| 1336 | } |
| 1337 | md5sum.setfn(split_filename); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1338 | while (index < 1000) { |
| 1339 | if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1340 | LOGERR("MD5 failed to match on '%s'.\n", split_filename); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1341 | return false; |
| 1342 | } |
| 1343 | index++; |
| 1344 | sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index); |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 1345 | md5sum.setfn(split_filename); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1346 | } |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1347 | return true; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1348 | } else { |
| 1349 | // Single file archive |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 1350 | md5file = Full_Filename + ".md5"; |
| 1351 | if (!TWFunc::Path_Exists(md5file)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1352 | LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str()); |
| 1353 | LOGERR("Please unselect Enable MD5 verification to restore.\n"); |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 1354 | return false; |
| 1355 | } |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 1356 | md5sum.setfn(Full_Filename); |
| 1357 | if (md5sum.verify_md5digest() != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1358 | LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1359 | return false; |
| 1360 | } else |
| 1361 | return true; |
| 1362 | } |
| 1363 | return false; |
| 1364 | } |
| 1365 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1366 | bool TWPartition::Restore(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) { |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1367 | string Restore_File_System; |
| 1368 | |
| 1369 | TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1370 | LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str()); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1371 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1372 | Restore_File_System = Get_Restore_File_System(restore_folder); |
| 1373 | |
| 1374 | if (Is_File_System(Restore_File_System)) |
| 1375 | return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size); |
| 1376 | else if (Is_Image(Restore_File_System)) { |
| 1377 | *already_restored_size += TWFunc::Get_File_Size(Backup_Name); |
| 1378 | if (Restore_File_System == "emmc") |
| 1379 | return Restore_DD(restore_folder, total_restore_size, already_restored_size); |
| 1380 | else if (Restore_File_System == "mtd" || Restore_File_System == "bml") |
| 1381 | return Restore_Flash_Image(restore_folder, total_restore_size, already_restored_size); |
| 1382 | } |
| 1383 | |
| 1384 | LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str()); |
| 1385 | return false; |
| 1386 | } |
| 1387 | |
| 1388 | string TWPartition::Get_Restore_File_System(string restore_folder) { |
| 1389 | size_t first_period, second_period; |
| 1390 | string Restore_File_System; |
| 1391 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1392 | // Parse backup filename to extract the file system before wiping |
| 1393 | first_period = Backup_FileName.find("."); |
| 1394 | if (first_period == string::npos) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1395 | LOGERR("Unable to find file system (first period).\n"); |
that | d43bf2d | 2014-09-21 23:13:02 +0200 | [diff] [blame] | 1396 | return string(); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1397 | } |
| 1398 | Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1); |
| 1399 | second_period = Restore_File_System.find("."); |
| 1400 | if (second_period == string::npos) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1401 | LOGERR("Unable to find file system (second period).\n"); |
that | d43bf2d | 2014-09-21 23:13:02 +0200 | [diff] [blame] | 1402 | return string(); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1403 | } |
| 1404 | Restore_File_System.resize(second_period); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1405 | LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str()); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1406 | return Restore_File_System; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | string TWPartition::Backup_Method_By_Name() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1410 | if (Backup_Method == NONE) |
| 1411 | return "none"; |
| 1412 | else if (Backup_Method == FILES) |
| 1413 | return "files"; |
| 1414 | else if (Backup_Method == DD) |
| 1415 | return "dd"; |
| 1416 | else if (Backup_Method == FLASH_UTILS) |
| 1417 | return "flash_utils"; |
| 1418 | else |
| 1419 | return "undefined"; |
| 1420 | return "ERROR!"; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | bool TWPartition::Decrypt(string Password) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1424 | LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1425 | // Is this needed? |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1426 | return 1; |
| 1427 | } |
| 1428 | |
| 1429 | bool TWPartition::Wipe_Encryption() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1430 | bool Save_Data_Media = Has_Data_Media; |
| 1431 | |
| 1432 | if (!UnMount(true)) |
| 1433 | return false; |
| 1434 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1435 | Has_Data_Media = false; |
Dees_Troy | 74fb2e9 | 2013-04-15 14:35:47 +0000 | [diff] [blame] | 1436 | Decrypted_Block_Device = ""; |
| 1437 | Is_Decrypted = false; |
| 1438 | Is_Encrypted = false; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1439 | Find_Actual_Block_Device(); |
| 1440 | bool mtp_was_enabled = TWFunc::Toggle_MTP(false); |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1441 | if (Wipe(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1442 | Has_Data_Media = Save_Data_Media; |
| 1443 | if (Has_Data_Media && !Symlink_Mount_Point.empty()) { |
| 1444 | Recreate_Media_Folder(); |
| 1445 | } |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 1446 | #ifndef TW_OEM_BUILD |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1447 | gui_print("You may need to reboot recovery to be able to use /data again.\n"); |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 1448 | #endif |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1449 | TWFunc::Toggle_MTP(mtp_was_enabled); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1450 | return true; |
| 1451 | } else { |
| 1452 | Has_Data_Media = Save_Data_Media; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1453 | LOGERR("Unable to format to remove encryption.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1454 | return false; |
| 1455 | } |
| 1456 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | void TWPartition::Check_FS_Type() { |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1460 | const char* type; |
| 1461 | blkid_probe pr; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1462 | |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 1463 | if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid) |
| 1464 | return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1465 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1466 | Find_Actual_Block_Device(); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1467 | if (!Is_Present) |
| 1468 | return; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1469 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1470 | pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str()); |
| 1471 | if (blkid_do_fullprobe(pr)) { |
| 1472 | blkid_free_probe(pr); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1473 | LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str()); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1474 | return; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1475 | } |
Vojtech Bocek | 4d4b336 | 2013-06-24 22:46:13 +0200 | [diff] [blame] | 1476 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 1477 | if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) { |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1478 | blkid_free_probe(pr); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1479 | LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str()); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1480 | return; |
| 1481 | } |
Vojtech Bocek | 4d4b336 | 2013-06-24 22:46:13 +0200 | [diff] [blame] | 1482 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1483 | Current_File_System = type; |
Vojtech Bocek | 4d4b336 | 2013-06-24 22:46:13 +0200 | [diff] [blame] | 1484 | blkid_free_probe(pr); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1485 | } |
| 1486 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1487 | bool TWPartition::Wipe_EXT23(string File_System) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1488 | if (!UnMount(true)) |
| 1489 | return false; |
| 1490 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1491 | if (TWFunc::Path_Exists("/sbin/mke2fs")) { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1492 | string command; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1493 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1494 | gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1495 | Find_Actual_Block_Device(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1496 | command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1497 | LOGINFO("mke2fs command: %s\n", command.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1498 | if (TWFunc::Exec_Cmd(command) == 0) { |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1499 | Current_File_System = File_System; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1500 | Recreate_AndSec_Folder(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1501 | gui_print("Done.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1502 | return true; |
| 1503 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1504 | LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1505 | return false; |
| 1506 | } |
| 1507 | } else |
| 1508 | return Wipe_RMRF(); |
| 1509 | |
| 1510 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | bool TWPartition::Wipe_EXT4() { |
Ethan Yonker | 25f20c1 | 2014-10-14 09:04:43 -0500 | [diff] [blame] | 1514 | Find_Actual_Block_Device(); |
| 1515 | if (!Is_Present) { |
| 1516 | LOGERR("Block device not present, cannot wipe %s.\n", Display_Name.c_str()); |
| 1517 | return false; |
| 1518 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1519 | if (!UnMount(true)) |
| 1520 | return false; |
| 1521 | |
Dees_Troy | b3265ab | 2013-08-30 02:59:14 +0000 | [diff] [blame] | 1522 | #if defined(HAVE_SELINUX) && defined(USE_EXT4) |
Ethan Yonker | f27497f | 2014-02-09 11:48:33 -0600 | [diff] [blame] | 1523 | int ret; |
| 1524 | char *secontext = NULL; |
| 1525 | |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 1526 | gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str()); |
Ethan Yonker | f27497f | 2014-02-09 11:48:33 -0600 | [diff] [blame] | 1527 | |
Dees Troy | 99c8dbf | 2014-03-10 16:53:58 +0000 | [diff] [blame] | 1528 | if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) { |
Ethan Yonker | f27497f | 2014-02-09 11:48:33 -0600 | [diff] [blame] | 1529 | LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str()); |
| 1530 | ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL); |
| 1531 | } else { |
| 1532 | ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle); |
| 1533 | } |
| 1534 | if (ret != 0) { |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 1535 | LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str()); |
| 1536 | return false; |
| 1537 | } else { |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 1538 | string sedir = Mount_Point + "/lost+found"; |
| 1539 | PartitionManager.Mount_By_Path(sedir.c_str(), true); |
| 1540 | rmdir(sedir.c_str()); |
| 1541 | mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP); |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 1542 | return true; |
| 1543 | } |
| 1544 | #else |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1545 | if (TWFunc::Path_Exists("/sbin/make_ext4fs")) { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1546 | string Command; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1547 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1548 | gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1549 | Find_Actual_Block_Device(); |
| 1550 | Command = "make_ext4fs"; |
| 1551 | if (!Is_Decrypted && Length != 0) { |
| 1552 | // Only use length if we're not decrypted |
| 1553 | char len[32]; |
| 1554 | sprintf(len, "%i", Length); |
| 1555 | Command += " -l "; |
| 1556 | Command += len; |
| 1557 | } |
Dees_Troy | 5295d58 | 2013-09-06 15:51:08 +0000 | [diff] [blame] | 1558 | if (TWFunc::Path_Exists("/file_contexts")) { |
| 1559 | Command += " -S /file_contexts"; |
| 1560 | } |
| 1561 | Command += " -a " + Mount_Point + " " + Actual_Block_Device; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1562 | LOGINFO("make_ext4fs command: %s\n", Command.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1563 | if (TWFunc::Exec_Cmd(Command) == 0) { |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1564 | Current_File_System = "ext4"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1565 | Recreate_AndSec_Folder(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1566 | gui_print("Done.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1567 | return true; |
| 1568 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1569 | LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1570 | return false; |
| 1571 | } |
| 1572 | } else |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1573 | return Wipe_EXT23("ext4"); |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 1574 | #endif |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1575 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | bool TWPartition::Wipe_FAT() { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1579 | string command; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1580 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1581 | if (TWFunc::Path_Exists("/sbin/mkdosfs")) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1582 | if (!UnMount(true)) |
| 1583 | return false; |
| 1584 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1585 | gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1586 | Find_Actual_Block_Device(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1587 | command = "mkdosfs " + Actual_Block_Device; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1588 | if (TWFunc::Exec_Cmd(command) == 0) { |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1589 | Current_File_System = "vfat"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1590 | Recreate_AndSec_Folder(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1591 | gui_print("Done.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1592 | return true; |
| 1593 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1594 | LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1595 | return false; |
| 1596 | } |
| 1597 | return true; |
| 1598 | } |
| 1599 | else |
| 1600 | return Wipe_RMRF(); |
| 1601 | |
| 1602 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1603 | } |
| 1604 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1605 | bool TWPartition::Wipe_EXFAT() { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1606 | string command; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1607 | |
| 1608 | if (TWFunc::Path_Exists("/sbin/mkexfatfs")) { |
| 1609 | if (!UnMount(true)) |
| 1610 | return false; |
| 1611 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1612 | gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1613 | Find_Actual_Block_Device(); |
| 1614 | command = "mkexfatfs " + Actual_Block_Device; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1615 | if (TWFunc::Exec_Cmd(command) == 0) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1616 | Recreate_AndSec_Folder(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1617 | gui_print("Done.\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1618 | return true; |
| 1619 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1620 | LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1621 | return false; |
| 1622 | } |
| 1623 | return true; |
| 1624 | } |
| 1625 | return false; |
| 1626 | } |
| 1627 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1628 | bool TWPartition::Wipe_MTD() { |
| 1629 | if (!UnMount(true)) |
| 1630 | return false; |
| 1631 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1632 | gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1633 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1634 | mtd_scan_partitions(); |
| 1635 | const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str()); |
| 1636 | if (mtd == NULL) { |
| 1637 | LOGERR("No mtd partition named '%s'", MTD_Name.c_str()); |
| 1638 | return false; |
| 1639 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1640 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1641 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 1642 | if (ctx == NULL) { |
| 1643 | LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str()); |
| 1644 | return false; |
| 1645 | } |
| 1646 | if (mtd_erase_blocks(ctx, -1) == -1) { |
| 1647 | mtd_write_close(ctx); |
| 1648 | LOGERR("Failed to format '%s'", MTD_Name.c_str()); |
| 1649 | return false; |
| 1650 | } |
| 1651 | if (mtd_write_close(ctx) != 0) { |
| 1652 | LOGERR("Failed to close '%s'", MTD_Name.c_str()); |
| 1653 | return false; |
| 1654 | } |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1655 | Current_File_System = "yaffs2"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1656 | Recreate_AndSec_Folder(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1657 | gui_print("Done.\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1658 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | bool TWPartition::Wipe_RMRF() { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1662 | if (Is_Storage) |
| 1663 | TWFunc::Toggle_MTP(false); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1664 | if (!Mount(true)) |
| 1665 | return false; |
| 1666 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1667 | gui_print("Removing all files under '%s'\n", Mount_Point.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1668 | TWFunc::removeDir(Mount_Point, true); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1669 | Recreate_AndSec_Folder(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1670 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1671 | } |
| 1672 | |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 1673 | bool TWPartition::Wipe_F2FS() { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1674 | string command; |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 1675 | |
| 1676 | if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) { |
| 1677 | if (!UnMount(true)) |
| 1678 | return false; |
| 1679 | |
| 1680 | gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str()); |
| 1681 | Find_Actual_Block_Device(); |
| 1682 | command = "mkfs.f2fs " + Actual_Block_Device; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1683 | if (TWFunc::Exec_Cmd(command) == 0) { |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 1684 | Recreate_AndSec_Folder(); |
| 1685 | gui_print("Done.\n"); |
| 1686 | return true; |
| 1687 | } else { |
| 1688 | LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str()); |
| 1689 | return false; |
| 1690 | } |
| 1691 | return true; |
| 1692 | } else { |
| 1693 | gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n"); |
| 1694 | return Wipe_RMRF(); |
| 1695 | } |
| 1696 | return false; |
| 1697 | } |
| 1698 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1699 | bool TWPartition::Wipe_Data_Without_Wiping_Media() { |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 1700 | #ifdef TW_OEM_BUILD |
| 1701 | // In an OEM Build we want to do a full format |
| 1702 | return Wipe_Encryption(); |
| 1703 | #else |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1704 | string dir; |
bigbiff bigbiff | 6b600f9 | 2014-01-05 18:13:43 -0500 | [diff] [blame] | 1705 | #ifdef HAVE_SELINUX |
| 1706 | fixPermissions perms; |
| 1707 | #endif |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1708 | |
| 1709 | // This handles wiping data on devices with "sdcard" in /data/media |
| 1710 | if (!Mount(true)) |
| 1711 | return false; |
| 1712 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1713 | gui_print("Wiping data without wiping /data/media ...\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1714 | |
| 1715 | DIR* d; |
| 1716 | d = opendir("/data"); |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 1717 | if (d != NULL) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1718 | struct dirent* de; |
| 1719 | while ((de = readdir(d)) != NULL) { |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 1720 | if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue; |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 1721 | // The media folder is the "internal sdcard" |
| 1722 | // The .layout_version file is responsible for determining whether 4.2 decides up upgrade |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1723 | // the media folder for multi-user. |
bigbiff bigbiff | c7360dd | 2014-01-25 15:02:57 -0500 | [diff] [blame] | 1724 | //TODO: convert this to use twrpDU.cpp |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 1725 | if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue; |
bigbiff bigbiff | c7360dd | 2014-01-25 15:02:57 -0500 | [diff] [blame] | 1726 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1727 | dir = "/data/"; |
| 1728 | dir.append(de->d_name); |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 1729 | if (de->d_type == DT_DIR) { |
| 1730 | TWFunc::removeDir(dir, false); |
bigbiff bigbiff | 98f1f90 | 2013-01-19 18:46:13 -0500 | [diff] [blame] | 1731 | } else if (de->d_type == DT_REG || de->d_type == DT_LNK || de->d_type == DT_FIFO || de->d_type == DT_SOCK) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 1732 | if (!unlink(dir.c_str())) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1733 | LOGINFO("Unable to unlink '%s'\n", dir.c_str()); |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 1734 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1735 | } |
| 1736 | closedir(d); |
bigbiff bigbiff | c7360dd | 2014-01-25 15:02:57 -0500 | [diff] [blame] | 1737 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1738 | gui_print("Done.\n"); |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 1739 | return true; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1740 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1741 | gui_print("Dirent failed to open /data, error!\n"); |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 1742 | return false; |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 1743 | #endif // ifdef TW_OEM_BUILD |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1744 | } |
| 1745 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1746 | bool TWPartition::Backup_Tar(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size) { |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1747 | char back_name[255], split_index[5]; |
| 1748 | string Full_FileName, Split_FileName, Tar_Args, Command; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1749 | int use_compression, use_encryption = 0, index, backup_count; |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1750 | struct stat st; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 1751 | unsigned long long total_bsize = 0, file_size; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1752 | twrpTar tar; |
| 1753 | vector <string> files; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 1754 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1755 | if (!Mount(true)) |
| 1756 | return false; |
| 1757 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1758 | TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1759 | gui_print("Backing up %s...\n", Backup_Display_Name.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1760 | |
| 1761 | DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1762 | tar.use_compression = use_compression; |
Matt Mower | bb81e5d | 2014-03-20 18:05:41 -0500 | [diff] [blame] | 1763 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1764 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 1765 | DataManager::GetValue("tw_encrypt_backup", use_encryption); |
| 1766 | if (use_encryption && Can_Encrypt_Backup) { |
| 1767 | tar.use_encryption = use_encryption; |
| 1768 | if (Use_Userdata_Encryption) |
| 1769 | tar.userdata_encryption = use_encryption; |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 1770 | string Password; |
| 1771 | DataManager::GetValue("tw_backup_password", Password); |
| 1772 | tar.setpassword(Password); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1773 | } else { |
| 1774 | use_encryption = false; |
| 1775 | } |
| 1776 | #endif |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1777 | |
| 1778 | sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str()); |
| 1779 | Backup_FileName = back_name; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1780 | Full_FileName = backup_folder + "/" + Backup_FileName; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1781 | tar.has_data_media = Has_Data_Media; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 1782 | Full_FileName = backup_folder + "/" + Backup_FileName; |
| 1783 | tar.setdir(Backup_Path); |
| 1784 | tar.setfn(Full_FileName); |
| 1785 | tar.setsize(Backup_Size); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1786 | tar.partition_name = Backup_Name; |
| 1787 | tar.backup_folder = backup_folder; |
| 1788 | if (tar.createTarFork(overall_size, other_backups_size) != 0) |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 1789 | return false; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1790 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | bool TWPartition::Backup_DD(string backup_folder) { |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1794 | char back_name[255], backup_size[32]; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1795 | string Full_FileName, Command, DD_BS; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1796 | int use_compression; |
| 1797 | |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1798 | sprintf(backup_size, "%llu", Backup_Size); |
| 1799 | DD_BS = backup_size; |
| 1800 | |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 1801 | TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1802 | gui_print("Backing up %s...\n", Display_Name.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1803 | |
| 1804 | sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str()); |
| 1805 | Backup_FileName = back_name; |
| 1806 | |
| 1807 | Full_FileName = backup_folder + "/" + Backup_FileName; |
| 1808 | |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1809 | Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1"; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1810 | LOGINFO("Backup command: '%s'\n", Command.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1811 | TWFunc::Exec_Cmd(Command); |
Dees_Troy | c154ac2 | 2012-10-12 15:36:47 -0400 | [diff] [blame] | 1812 | if (TWFunc::Get_File_Size(Full_FileName) == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1813 | LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str()); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 1814 | return false; |
| 1815 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1816 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | bool TWPartition::Backup_Dump_Image(string backup_folder) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1820 | char back_name[255]; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1821 | string Full_FileName, Command; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1822 | int use_compression; |
| 1823 | |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 1824 | TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1825 | gui_print("Backing up %s...\n", Display_Name.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1826 | |
| 1827 | sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str()); |
| 1828 | Backup_FileName = back_name; |
| 1829 | |
| 1830 | Full_FileName = backup_folder + "/" + Backup_FileName; |
| 1831 | |
| 1832 | Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'"; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1833 | LOGINFO("Backup command: '%s'\n", Command.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1834 | TWFunc::Exec_Cmd(Command); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 1835 | if (TWFunc::Get_File_Size(Full_FileName) == 0) { |
| 1836 | // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1837 | LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str()); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 1838 | return false; |
| 1839 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1840 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1841 | } |
| 1842 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1843 | unsigned long long TWPartition::Get_Restore_Size(string restore_folder) { |
| 1844 | InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info"); |
| 1845 | if (restore_info.LoadValues() == 0) { |
| 1846 | if (restore_info.GetValue("backup_size", Restore_Size) == 0) { |
| 1847 | LOGINFO("Read info file, restore size is %llu\n", Restore_Size); |
| 1848 | return Restore_Size; |
| 1849 | } |
| 1850 | } |
| 1851 | string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder); |
| 1852 | |
| 1853 | Full_FileName = restore_folder + "/" + Backup_FileName; |
| 1854 | |
| 1855 | if (Is_Image(Restore_File_System)) { |
| 1856 | Restore_Size = TWFunc::Get_File_Size(Full_FileName); |
| 1857 | return Restore_Size; |
| 1858 | } |
| 1859 | |
| 1860 | twrpTar tar; |
| 1861 | tar.setdir(Backup_Path); |
| 1862 | tar.setfn(Full_FileName); |
| 1863 | tar.backup_name = Backup_Name; |
| 1864 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 1865 | string Password; |
| 1866 | DataManager::GetValue("tw_restore_password", Password); |
| 1867 | if (!Password.empty()) |
| 1868 | tar.setpassword(Password); |
| 1869 | #endif |
| 1870 | tar.partition_name = Backup_Name; |
| 1871 | tar.backup_folder = restore_folder; |
| 1872 | Restore_Size = tar.get_size(); |
| 1873 | return Restore_Size; |
| 1874 | } |
| 1875 | |
| 1876 | bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) { |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1877 | string Full_FileName, Command; |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1878 | int index = 0; |
| 1879 | char split_index[5]; |
Dees Troy | 4159aed | 2014-02-28 17:24:43 +0000 | [diff] [blame] | 1880 | bool ret = false; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1881 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1882 | if (Has_Android_Secure) { |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1883 | if (!Wipe_AndSec()) |
| 1884 | return false; |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1885 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1886 | gui_print("Wiping %s...\n", Display_Name.c_str()); |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1887 | if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) { |
| 1888 | gui_print("WARNING: This /data backup was made with %s file system!\n", Restore_File_System.c_str()); |
| 1889 | gui_print("The backup may not boot unless you change back to %s.\n", Restore_File_System.c_str()); |
| 1890 | if (!Wipe_Data_Without_Wiping_Media()) |
| 1891 | return false; |
| 1892 | } else { |
| 1893 | if (!Wipe(Restore_File_System)) |
| 1894 | return false; |
| 1895 | } |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1896 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1897 | TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1898 | gui_print("Restoring %s...\n", Backup_Display_Name.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1899 | |
| 1900 | if (!Mount(true)) |
| 1901 | return false; |
| 1902 | |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1903 | Full_FileName = restore_folder + "/" + Backup_FileName; |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 1904 | twrpTar tar; |
| 1905 | tar.setdir(Backup_Path); |
| 1906 | tar.setfn(Full_FileName); |
| 1907 | tar.backup_name = Backup_Name; |
| 1908 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 1909 | string Password; |
| 1910 | DataManager::GetValue("tw_restore_password", Password); |
| 1911 | if (!Password.empty()) |
| 1912 | tar.setpassword(Password); |
| 1913 | #endif |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1914 | if (tar.extractTarFork(total_restore_size, already_restored_size) != 0) |
Dees Troy | 4159aed | 2014-02-28 17:24:43 +0000 | [diff] [blame] | 1915 | ret = false; |
| 1916 | else |
| 1917 | ret = true; |
| 1918 | #ifdef HAVE_CAPABILITIES |
| 1919 | // Restore capabilities to the run-as binary |
| 1920 | if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) { |
| 1921 | struct vfs_cap_data cap_data; |
| 1922 | uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID); |
| 1923 | |
| 1924 | memset(&cap_data, 0, sizeof(cap_data)); |
| 1925 | cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE; |
| 1926 | cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff); |
| 1927 | cap_data.data[0].inheritable = 0; |
| 1928 | cap_data.data[1].permitted = (uint32_t) (capabilities >> 32); |
| 1929 | cap_data.data[1].inheritable = 0; |
| 1930 | if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) { |
| 1931 | LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n"); |
| 1932 | } else { |
| 1933 | LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n"); |
| 1934 | } |
| 1935 | } |
| 1936 | #endif |
| 1937 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1938 | } |
| 1939 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1940 | bool TWPartition::Restore_DD(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1941 | string Full_FileName, Command; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1942 | double display_percent, progress_percent; |
| 1943 | char size_progress[1024]; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1944 | |
Dees_Troy | da8b55a | 2012-12-12 19:18:30 +0000 | [diff] [blame] | 1945 | TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1946 | Full_FileName = restore_folder + "/" + Backup_FileName; |
Gary Peck | 15e623d | 2012-11-21 21:07:58 -0800 | [diff] [blame] | 1947 | |
| 1948 | if (!Find_Partition_Size()) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1949 | LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str()); |
Gary Peck | 15e623d | 2012-11-21 21:07:58 -0800 | [diff] [blame] | 1950 | return false; |
| 1951 | } |
| 1952 | unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName); |
| 1953 | if (backup_size > Size) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1954 | LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n", |
Gary Peck | 15e623d | 2012-11-21 21:07:58 -0800 | [diff] [blame] | 1955 | (int)(backup_size / 1048576LLU), Full_FileName.c_str(), |
| 1956 | Actual_Block_Device.c_str(), (int)(Size / 1048576LLU)); |
| 1957 | return false; |
| 1958 | } |
| 1959 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1960 | gui_print("Restoring %s...\n", Display_Name.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1961 | Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1962 | LOGINFO("Restore command: '%s'\n", Command.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1963 | TWFunc::Exec_Cmd(Command); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1964 | display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100; |
| 1965 | sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent)); |
| 1966 | DataManager::SetValue("tw_size_progress", size_progress); |
| 1967 | progress_percent = (display_percent / 100); |
| 1968 | DataManager::SetProgress((float)(progress_percent)); |
| 1969 | *already_restored_size += Restore_Size; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1970 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1971 | } |
| 1972 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1973 | bool TWPartition::Restore_Flash_Image(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1974 | string Full_FileName, Command; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1975 | double display_percent, progress_percent; |
| 1976 | char size_progress[1024]; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1977 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1978 | gui_print("Restoring %s...\n", Display_Name.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1979 | Full_FileName = restore_folder + "/" + Backup_FileName; |
| 1980 | // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes |
| 1981 | Command = "erase_image " + MTD_Name; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1982 | LOGINFO("Erase command: '%s'\n", Command.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1983 | TWFunc::Exec_Cmd(Command); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1984 | Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'"; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1985 | LOGINFO("Restore command: '%s'\n", Command.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1986 | TWFunc::Exec_Cmd(Command); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1987 | display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100; |
| 1988 | sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent)); |
| 1989 | DataManager::SetValue("tw_size_progress", size_progress); |
| 1990 | progress_percent = (display_percent / 100); |
| 1991 | DataManager::SetProgress((float)(progress_percent)); |
| 1992 | *already_restored_size += Restore_Size; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1993 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1994 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1995 | |
| 1996 | bool TWPartition::Update_Size(bool Display_Error) { |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 1997 | bool ret = false, Was_Already_Mounted = false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1998 | |
Dees_Troy | ab10ee2 | 2012-09-21 14:27:30 -0400 | [diff] [blame] | 1999 | if (!Can_Be_Mounted && !Is_Encrypted) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 2000 | return false; |
| 2001 | |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2002 | Was_Already_Mounted = Is_Mounted(); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2003 | if (Removable || Is_Encrypted) { |
| 2004 | if (!Mount(false)) |
| 2005 | return true; |
| 2006 | } else if (!Mount(Display_Error)) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 2007 | return false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 2008 | |
| 2009 | ret = Get_Size_Via_statfs(Display_Error); |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2010 | if (!ret || Size == 0) { |
| 2011 | if (!Get_Size_Via_df(Display_Error)) { |
| 2012 | if (!Was_Already_Mounted) |
| 2013 | UnMount(false); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 2014 | return false; |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2015 | } |
| 2016 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 2017 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 2018 | if (Has_Data_Media) { |
| 2019 | if (Mount(Display_Error)) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 2020 | unsigned long long data_media_used, actual_data; |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 2021 | Used = du.Get_Folder_Size("/data"); |
| 2022 | Backup_Size = Used; |
| 2023 | int bak = (int)(Used / 1048576LLU); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 2024 | int fre = (int)(Free / 1048576LLU); |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 2025 | LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre); |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2026 | } else { |
| 2027 | if (!Was_Already_Mounted) |
| 2028 | UnMount(false); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 2029 | return false; |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2030 | } |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2031 | } else if (Has_Android_Secure) { |
| 2032 | if (Mount(Display_Error)) |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 2033 | Backup_Size = du.Get_Folder_Size(Backup_Path); |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2034 | else { |
| 2035 | if (!Was_Already_Mounted) |
| 2036 | UnMount(false); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2037 | return false; |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2038 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 2039 | } |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2040 | if (!Was_Already_Mounted) |
| 2041 | UnMount(false); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 2042 | return true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 2043 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2044 | |
| 2045 | void TWPartition::Find_Actual_Block_Device(void) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 2046 | if (Is_Decrypted && !Decrypted_Block_Device.empty()) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2047 | Actual_Block_Device = Decrypted_Block_Device; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 2048 | if (TWFunc::Path_Exists(Decrypted_Block_Device)) |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2049 | Is_Present = true; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2050 | } else if (TWFunc::Path_Exists(Primary_Block_Device)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2051 | Is_Present = true; |
| 2052 | Actual_Block_Device = Primary_Block_Device; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2053 | return; |
| 2054 | } |
| 2055 | if (Is_Decrypted) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2056 | } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) { |
Dees_Troy | 3f04d03 | 2012-10-07 18:20:09 -0400 | [diff] [blame] | 2057 | Actual_Block_Device = Alternate_Block_Device; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2058 | Is_Present = true; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2059 | } else { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2060 | Is_Present = false; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2061 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | void TWPartition::Recreate_Media_Folder(void) { |
| 2065 | string Command; |
| 2066 | |
bigbiff bigbiff | 6b600f9 | 2014-01-05 18:13:43 -0500 | [diff] [blame] | 2067 | #ifdef HAVE_SELINUX |
| 2068 | fixPermissions perms; |
| 2069 | #endif |
| 2070 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2071 | if (!Mount(true)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 2072 | LOGERR("Unable to recreate /data/media folder.\n"); |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 2073 | } else if (!TWFunc::Path_Exists("/data/media")) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2074 | PartitionManager.Mount_By_Path(Symlink_Mount_Point, true); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 2075 | LOGINFO("Recreating /data/media folder.\n"); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 2076 | mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
Ethan Yonker | 5ef301e | 2014-10-14 10:04:44 -0500 | [diff] [blame] | 2077 | #ifdef HAVE_SELINUX |
bigbiff bigbiff | 6b600f9 | 2014-01-05 18:13:43 -0500 | [diff] [blame] | 2078 | perms.fixDataInternalContexts(); |
Ethan Yonker | 5ef301e | 2014-10-14 10:04:44 -0500 | [diff] [blame] | 2079 | #endif |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 2080 | // Toggle mount to ensure that "internal sdcard" gets mounted |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2081 | PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true); |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 2082 | PartitionManager.Mount_By_Path(Symlink_Mount_Point, true); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2083 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2084 | } |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2085 | |
| 2086 | void TWPartition::Recreate_AndSec_Folder(void) { |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2087 | if (!Has_Android_Secure) |
| 2088 | return; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 2089 | LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str()); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2090 | if (!Mount(true)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 2091 | LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str()); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2092 | } else if (!TWFunc::Path_Exists(Symlink_Path)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 2093 | LOGINFO("Recreating %s folder.\n", Backup_Name.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2094 | PartitionManager.Mount_By_Path(Symlink_Mount_Point, true); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 2095 | mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2096 | PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2097 | } |
| 2098 | } |