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