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