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