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