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