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