Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 2 | Copyright 2013 TeamWin |
Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/vfs.h> |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 24 | #include <sys/mount.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 25 | #include <unistd.h> |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 26 | #include <dirent.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 27 | #include <iostream> |
| 28 | #include <sstream> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 29 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 30 | #ifdef TW_INCLUDE_CRYPTO |
| 31 | #include "cutils/properties.h" |
| 32 | #endif |
| 33 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 34 | #include "libblkid/include/blkid.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 35 | #include "variables.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 36 | #include "twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 37 | #include "partitions.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 38 | #include "data.hpp" |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 39 | #include "twrp-functions.hpp" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 40 | #include "twrpDigest.hpp" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 41 | #include "twrpTar.hpp" |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 42 | #include "twrpDU.hpp" |
bigbiff bigbiff | 6b600f9 | 2014-01-05 18:13:43 -0500 | [diff] [blame] | 43 | #include "fixPermissions.hpp" |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 44 | #include "infomanager.hpp" |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 45 | #include "set_metadata.h" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 46 | extern "C" { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 47 | #include "mtdutils/mtdutils.h" |
| 48 | #include "mtdutils/mounts.h" |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 49 | #ifdef USE_EXT4 |
| 50 | #include "make_ext4fs.h" |
| 51 | #endif |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 52 | |
| 53 | #ifdef TW_INCLUDE_CRYPTO |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 54 | #include "crypto/lollipop/cryptfs.h" |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 55 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 56 | } |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 57 | #ifdef HAVE_SELINUX |
| 58 | #include "selinux/selinux.h" |
Ethan Yonker | f27497f | 2014-02-09 11:48:33 -0600 | [diff] [blame] | 59 | #include <selinux/label.h> |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 60 | #endif |
Dees Troy | 4159aed | 2014-02-28 17:24:43 +0000 | [diff] [blame] | 61 | #ifdef HAVE_CAPABILITIES |
| 62 | #include <sys/capability.h> |
| 63 | #include <sys/xattr.h> |
| 64 | #include <linux/xattr.h> |
| 65 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 66 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 67 | using namespace std; |
| 68 | |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 69 | extern struct selabel_handle *selinux_handle; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 70 | extern bool datamedia; |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 71 | |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 72 | struct flag_list { |
| 73 | const char *name; |
| 74 | unsigned flag; |
| 75 | }; |
| 76 | |
| 77 | static struct flag_list mount_flags[] = { |
| 78 | { "noatime", MS_NOATIME }, |
| 79 | { "noexec", MS_NOEXEC }, |
| 80 | { "nosuid", MS_NOSUID }, |
| 81 | { "nodev", MS_NODEV }, |
| 82 | { "nodiratime", MS_NODIRATIME }, |
| 83 | { "ro", MS_RDONLY }, |
| 84 | { "rw", 0 }, |
| 85 | { "remount", MS_REMOUNT }, |
| 86 | { "bind", MS_BIND }, |
| 87 | { "rec", MS_REC }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 88 | #ifdef MS_UNBINDABLE |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 89 | { "unbindable", MS_UNBINDABLE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 90 | #endif |
| 91 | #ifdef MS_PRIVATE |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 92 | { "private", MS_PRIVATE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 93 | #endif |
| 94 | #ifdef MS_SLAVE |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 95 | { "slave", MS_SLAVE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 96 | #endif |
| 97 | #ifdef MS_SHARED |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 98 | { "shared", MS_SHARED }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 99 | #endif |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 100 | { "sync", MS_SYNCHRONOUS }, |
| 101 | { "defaults", 0 }, |
| 102 | { 0, 0 }, |
| 103 | }; |
| 104 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 105 | TWPartition::TWPartition() { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 106 | Can_Be_Mounted = false; |
| 107 | Can_Be_Wiped = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 108 | Can_Be_Backed_Up = false; |
Vojtech Bocek | 1dc3098 | 2013-08-30 21:49:30 +0200 | [diff] [blame] | 109 | Use_Rm_Rf = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 110 | Wipe_During_Factory_Reset = false; |
| 111 | Wipe_Available_in_GUI = false; |
| 112 | Is_SubPartition = false; |
Dees_Troy | 2691f9d | 2012-09-24 11:15:49 -0400 | [diff] [blame] | 113 | Has_SubPartition = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 114 | SubPartition_Of = ""; |
| 115 | Symlink_Path = ""; |
| 116 | Symlink_Mount_Point = ""; |
| 117 | Mount_Point = ""; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 118 | Backup_Path = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 119 | Actual_Block_Device = ""; |
| 120 | Primary_Block_Device = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 121 | Alternate_Block_Device = ""; |
| 122 | Removable = false; |
| 123 | Is_Present = false; |
| 124 | Length = 0; |
| 125 | Size = 0; |
| 126 | Used = 0; |
| 127 | Free = 0; |
| 128 | Backup_Size = 0; |
| 129 | Can_Be_Encrypted = false; |
| 130 | Is_Encrypted = false; |
| 131 | Is_Decrypted = false; |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 132 | Mount_To_Decrypt = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 133 | Decrypted_Block_Device = ""; |
| 134 | Display_Name = ""; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 135 | Backup_Display_Name = ""; |
| 136 | Storage_Name = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 137 | Backup_Name = ""; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 138 | Backup_FileName = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 139 | MTD_Name = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 140 | Backup_Method = NONE; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 141 | Can_Encrypt_Backup = false; |
| 142 | Use_Userdata_Encryption = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 143 | Has_Data_Media = false; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 144 | Has_Android_Secure = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 145 | Is_Storage = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 146 | Is_Settings_Storage = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 147 | Storage_Path = ""; |
| 148 | Current_File_System = ""; |
| 149 | Fstab_File_System = ""; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 150 | Mount_Flags = 0; |
| 151 | Mount_Options = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 152 | Format_Block_Size = 0; |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 153 | Ignore_Blkid = false; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 154 | Retain_Layout_Version = false; |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 155 | Crypto_Key_Location = "footer"; |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 156 | MTP_Storage_ID = 0; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 157 | Can_Flash_Img = false; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 158 | Mount_Read_Only = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | TWPartition::~TWPartition(void) { |
| 162 | // Do nothing |
| 163 | } |
| 164 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 165 | bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) { |
| 166 | char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH]; |
| 167 | int line_len = Line.size(), index = 0, item_index = 0; |
| 168 | char* ptr; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 169 | string Flags; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 170 | strncpy(full_line, Line.c_str(), line_len); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 171 | bool skip = false; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 172 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 173 | for (index = 0; index < line_len; index++) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 174 | if (full_line[index] == 34) |
| 175 | skip = !skip; |
| 176 | if (!skip && full_line[index] <= 32) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 177 | full_line[index] = '\0'; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 178 | } |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 179 | Mount_Point = full_line; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 180 | LOGINFO("Processing '%s'\n", Mount_Point.c_str()); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 181 | Backup_Path = Mount_Point; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 182 | Storage_Path = Mount_Point; |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 183 | Display_Name = full_line + 1; |
| 184 | Backup_Display_Name = Display_Name; |
| 185 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 186 | index = Mount_Point.size(); |
| 187 | while (index < line_len) { |
| 188 | while (index < line_len && full_line[index] == '\0') |
| 189 | index++; |
| 190 | if (index >= line_len) |
| 191 | continue; |
| 192 | ptr = full_line + index; |
| 193 | if (item_index == 0) { |
| 194 | // File System |
| 195 | Fstab_File_System = ptr; |
| 196 | Current_File_System = ptr; |
| 197 | item_index++; |
| 198 | } else if (item_index == 1) { |
| 199 | // Primary Block Device |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 200 | if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") { |
Dees_Troy | 094207a | 2012-09-26 12:00:39 -0400 | [diff] [blame] | 201 | MTD_Name = ptr; |
| 202 | Find_MTD_Block_Device(MTD_Name); |
Dees_Troy | 5fcd8f9 | 2012-10-16 12:22:05 -0400 | [diff] [blame] | 203 | } else if (Fstab_File_System == "bml") { |
| 204 | if (Mount_Point == "/boot") |
| 205 | MTD_Name = "boot"; |
| 206 | else if (Mount_Point == "/recovery") |
| 207 | MTD_Name = "recovery"; |
| 208 | Primary_Block_Device = ptr; |
| 209 | if (*ptr != '/') |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 210 | 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] | 211 | } else if (*ptr != '/') { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 212 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 213 | 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] | 214 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 215 | 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] | 216 | return 0; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 217 | } else { |
| 218 | Primary_Block_Device = ptr; |
| 219 | Find_Real_Block_Device(Primary_Block_Device, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 220 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 221 | item_index++; |
| 222 | } else if (item_index > 1) { |
| 223 | if (*ptr == '/') { |
| 224 | // Alternate Block Device |
| 225 | Alternate_Block_Device = ptr; |
| 226 | Find_Real_Block_Device(Alternate_Block_Device, Display_Error); |
| 227 | } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) { |
| 228 | // Partition length |
| 229 | ptr += 7; |
| 230 | Length = atoi(ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 231 | } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) { |
| 232 | // Custom flags, save for later so that new values aren't overwritten by defaults |
| 233 | ptr += 6; |
| 234 | Flags = ptr; |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 235 | Process_Flags(Flags, Display_Error); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 236 | } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) { |
| 237 | // Do nothing |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 238 | } else { |
| 239 | // Unhandled data |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 240 | 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] | 241 | } |
| 242 | } |
| 243 | while (index < line_len && full_line[index] != '\0') |
| 244 | index++; |
| 245 | } |
| 246 | |
| 247 | if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) { |
| 248 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 249 | LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 250 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 251 | LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 252 | return 0; |
| 253 | } else if (Is_File_System(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 254 | Find_Actual_Block_Device(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 255 | Setup_File_System(Display_Error); |
| 256 | if (Mount_Point == "/system") { |
| 257 | Display_Name = "System"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 258 | Backup_Display_Name = Display_Name; |
| 259 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 260 | Wipe_Available_in_GUI = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 261 | Can_Be_Backed_Up = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 262 | Mount_Read_Only = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 263 | } else if (Mount_Point == "/data") { |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 264 | 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] | 265 | Display_Name = "Data"; |
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 | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 269 | Wipe_During_Factory_Reset = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 270 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 271 | Can_Encrypt_Backup = true; |
| 272 | Use_Userdata_Encryption = true; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 273 | if (datamedia) |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 274 | Setup_Data_Media(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 275 | #ifdef TW_INCLUDE_CRYPTO |
| 276 | Can_Be_Encrypted = true; |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 277 | char crypto_blkdev[255]; |
| 278 | property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error"); |
| 279 | if (strcmp(crypto_blkdev, "error") != 0) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 280 | DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device); |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 281 | DataManager::SetValue(TW_IS_DECRYPTED, 1); |
| 282 | Is_Encrypted = true; |
| 283 | Is_Decrypted = true; |
| 284 | Decrypted_Block_Device = crypto_blkdev; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 285 | LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev); |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 286 | } else if (!Mount(false)) { |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 287 | if (Is_Present) { |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 288 | 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] | 289 | if (cryptfs_check_footer() == 0) { |
| 290 | Is_Encrypted = true; |
| 291 | Is_Decrypted = false; |
| 292 | Can_Be_Mounted = false; |
| 293 | Current_File_System = "emmc"; |
| 294 | Setup_Image(Display_Error); |
| 295 | DataManager::SetValue(TW_IS_ENCRYPTED, 1); |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 296 | DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type()); |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 297 | DataManager::SetValue(TW_CRYPTO_PASSWORD, ""); |
| 298 | DataManager::SetValue("tw_crypto_display", ""); |
| 299 | } else { |
| 300 | LOGERR("Could not mount /data and unable to find crypto footer.\n"); |
| 301 | } |
| 302 | } else { |
| 303 | LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str()); |
| 304 | } |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 305 | } else { |
| 306 | // Filesystem is not encrypted and the mount |
| 307 | // succeeded, so get it back to the original |
| 308 | // unmounted state |
| 309 | UnMount(false); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 310 | } |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 311 | if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) |
Dees_Troy | 9b21af7 | 2012-10-01 15:51:46 -0400 | [diff] [blame] | 312 | Recreate_Media_Folder(); |
Dees_Troy | 9b21af7 | 2012-10-01 15:51:46 -0400 | [diff] [blame] | 313 | #else |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 314 | if (datamedia) |
| 315 | Recreate_Media_Folder(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 316 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 317 | } else if (Mount_Point == "/cache") { |
| 318 | Display_Name = "Cache"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 319 | Backup_Display_Name = Display_Name; |
| 320 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 321 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 322 | Wipe_During_Factory_Reset = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 323 | Can_Be_Backed_Up = true; |
Dees_Troy | ce2fe77 | 2012-09-28 12:34:33 -0400 | [diff] [blame] | 324 | if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 325 | LOGINFO("Recreating /cache/recovery folder.\n"); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 326 | 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] | 327 | return -1; |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 328 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 329 | } else if (Mount_Point == "/datadata") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 330 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 331 | Display_Name = "DataData"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 332 | Backup_Display_Name = Display_Name; |
| 333 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 334 | Is_SubPartition = true; |
| 335 | SubPartition_Of = "/data"; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 336 | DataManager::SetValue(TW_HAS_DATADATA, 1); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 337 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 338 | Can_Encrypt_Backup = true; |
| 339 | Use_Userdata_Encryption = false; // This whole partition should be encrypted |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 340 | } else if (Mount_Point == "/sd-ext") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 341 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 342 | Display_Name = "SD-Ext"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 343 | Backup_Display_Name = Display_Name; |
| 344 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 345 | Wipe_Available_in_GUI = true; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 346 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 347 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 348 | Can_Encrypt_Backup = true; |
| 349 | Use_Userdata_Encryption = true; |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 350 | } else if (Mount_Point == "/boot") { |
| 351 | Display_Name = "Boot"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 352 | Backup_Display_Name = Display_Name; |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 353 | DataManager::SetValue("tw_boot_is_mountable", 1); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 354 | Can_Be_Backed_Up = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 355 | } |
| 356 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 357 | if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) { |
| 358 | Is_Storage = true; |
| 359 | Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH); |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 360 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 361 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 362 | #else |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 363 | if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 364 | Is_Storage = true; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 365 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 366 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 367 | #endif |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 368 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 369 | #ifdef TW_INTERNAL_STORAGE_PATH |
| 370 | if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) { |
| 371 | Is_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 372 | Is_Settings_Storage = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 373 | Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH); |
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 | } |
| 376 | #else |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 377 | if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 378 | Is_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 379 | Is_Settings_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 380 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 381 | } |
| 382 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 383 | } else if (Is_Image(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 384 | Find_Actual_Block_Device(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 385 | Setup_Image(Display_Error); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 386 | if (Mount_Point == "/boot") { |
| 387 | Display_Name = "Boot"; |
| 388 | Backup_Display_Name = Display_Name; |
| 389 | Can_Be_Backed_Up = true; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 390 | Can_Flash_Img = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 391 | } else if (Mount_Point == "/recovery") { |
| 392 | Display_Name = "Recovery"; |
| 393 | Backup_Display_Name = Display_Name; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 394 | Can_Flash_Img = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 395 | } else if (Mount_Point == "/system_image") { |
| 396 | Display_Name = "System Image"; |
| 397 | Backup_Display_Name = Display_Name; |
| 398 | Can_Flash_Img = false; |
| 399 | Can_Be_Backed_Up = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 400 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 401 | } |
| 402 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 403 | // Process any custom flags |
| 404 | if (Flags.size() > 0) |
| 405 | Process_Flags(Flags, Display_Error); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 406 | return true; |
| 407 | } |
| 408 | |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 409 | bool TWPartition::Process_FS_Flags(string& Options, int Flags) { |
| 410 | int i; |
| 411 | char *p; |
| 412 | char *savep; |
| 413 | char fs_options[250]; |
| 414 | |
| 415 | strlcpy(fs_options, Options.c_str(), sizeof(fs_options)); |
| 416 | Options = ""; |
| 417 | |
| 418 | p = strtok_r(fs_options, ",", &savep); |
| 419 | while (p) { |
| 420 | /* Look for the flag "p" in the flag list "fl" |
| 421 | * If not found, the loop exits with fl[i].name being null. |
| 422 | */ |
| 423 | for (i = 0; mount_flags[i].name; i++) { |
| 424 | if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) { |
| 425 | Flags |= mount_flags[i].flag; |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | if (!mount_flags[i].name) { |
| 431 | if (Options.size() > 0) |
| 432 | Options += ","; |
| 433 | Options += p; |
| 434 | } |
| 435 | p = strtok_r(NULL, ",", &savep); |
| 436 | } |
| 437 | |
| 438 | return true; |
| 439 | } |
| 440 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 441 | bool TWPartition::Process_Flags(string Flags, bool Display_Error) { |
| 442 | char flags[MAX_FSTAB_LINE_LENGTH]; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 443 | int flags_len, index = 0, ptr_len; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 444 | char* ptr; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 445 | 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] | 446 | |
| 447 | strcpy(flags, Flags.c_str()); |
| 448 | flags_len = Flags.size(); |
| 449 | for (index = 0; index < flags_len; index++) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 450 | if (flags[index] == 34) |
| 451 | skip = !skip; |
| 452 | if (!skip && flags[index] == ';') |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 453 | flags[index] = '\0'; |
| 454 | } |
| 455 | |
| 456 | index = 0; |
| 457 | while (index < flags_len) { |
| 458 | while (index < flags_len && flags[index] == '\0') |
| 459 | index++; |
| 460 | if (index >= flags_len) |
| 461 | continue; |
| 462 | ptr = flags + index; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 463 | ptr_len = strlen(ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 464 | if (strcmp(ptr, "removable") == 0) { |
| 465 | Removable = true; |
Ethan Yonker | 06c3f93 | 2014-02-02 22:11:14 -0600 | [diff] [blame] | 466 | } else if (strncmp(ptr, "storage", 7) == 0) { |
| 467 | if (ptr_len == 7) { |
Ethan Yonker | 06c3f93 | 2014-02-02 22:11:14 -0600 | [diff] [blame] | 468 | Is_Storage = true; |
| 469 | } else if (ptr_len == 9) { |
| 470 | ptr += 9; |
| 471 | if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') { |
| 472 | LOGINFO("storage set to true\n"); |
| 473 | Is_Storage = true; |
| 474 | } else { |
| 475 | LOGINFO("storage set to false\n"); |
| 476 | Is_Storage = false; |
| 477 | } |
| 478 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 479 | } else if (strcmp(ptr, "settingsstorage") == 0) { |
| 480 | Is_Storage = true; |
Xuefer | bfce504 | 2015-04-14 02:02:05 +0800 | [diff] [blame] | 481 | Is_Settings_Storage = true; |
Matt Mower | bf4efa3 | 2014-04-14 23:25:26 -0500 | [diff] [blame] | 482 | } else if (strcmp(ptr, "andsec") == 0) { |
| 483 | Has_Android_Secure = true; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 484 | } else if (strcmp(ptr, "canbewiped") == 0) { |
| 485 | Can_Be_Wiped = true; |
Hashcode | dabfd49 | 2013-08-29 22:45:30 -0700 | [diff] [blame] | 486 | } else if (strcmp(ptr, "usermrf") == 0) { |
| 487 | Use_Rm_Rf = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 488 | } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) { |
| 489 | ptr += 7; |
| 490 | if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') |
| 491 | Can_Be_Backed_Up = true; |
| 492 | else |
| 493 | Can_Be_Backed_Up = false; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 494 | } else if (strcmp(ptr, "wipeingui") == 0) { |
| 495 | Can_Be_Wiped = true; |
| 496 | Wipe_Available_in_GUI = true; |
| 497 | } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) { |
| 498 | Can_Be_Wiped = true; |
| 499 | Wipe_Available_in_GUI = true; |
| 500 | Wipe_During_Factory_Reset = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 501 | } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) { |
Dees_Troy | 2c4c26f | 2013-01-28 15:26:43 +0000 | [diff] [blame] | 502 | ptr += 15; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 503 | Is_SubPartition = true; |
| 504 | SubPartition_Of = ptr; |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 505 | } else if (strcmp(ptr, "ignoreblkid") == 0) { |
| 506 | Ignore_Blkid = true; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 507 | } else if (strcmp(ptr, "retainlayoutversion") == 0) { |
| 508 | Retain_Layout_Version = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 509 | } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 510 | ptr += 8; |
| 511 | Symlink_Path = ptr; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 512 | } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) { |
| 513 | has_display_name = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 514 | ptr += 8; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 515 | if (*ptr == '\"') ptr++; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 516 | Display_Name = ptr; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 517 | if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") { |
| 518 | Display_Name.resize(Display_Name.size() - 1); |
| 519 | } |
| 520 | } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) { |
| 521 | has_storage_name = true; |
| 522 | ptr += 11; |
| 523 | if (*ptr == '\"') ptr++; |
| 524 | Storage_Name = ptr; |
| 525 | if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") { |
| 526 | Storage_Name.resize(Storage_Name.size() - 1); |
| 527 | } |
| 528 | } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) { |
| 529 | has_backup_name = true; |
| 530 | ptr += 10; |
| 531 | if (*ptr == '\"') ptr++; |
| 532 | Backup_Display_Name = ptr; |
| 533 | if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") { |
| 534 | Backup_Display_Name.resize(Backup_Display_Name.size() - 1); |
| 535 | } |
| 536 | } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 537 | ptr += 10; |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 538 | Format_Block_Size = (unsigned long)(atol(ptr)); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 539 | } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 540 | ptr += 7; |
| 541 | Length = atoi(ptr); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 542 | } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) { |
| 543 | ptr += 17; |
| 544 | if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') |
| 545 | Can_Encrypt_Backup = true; |
| 546 | else |
| 547 | Can_Encrypt_Backup = false; |
| 548 | } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) { |
| 549 | ptr += 21; |
| 550 | if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') { |
| 551 | Can_Encrypt_Backup = true; |
| 552 | Use_Userdata_Encryption = true; |
| 553 | } else { |
| 554 | Use_Userdata_Encryption = false; |
| 555 | } |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 556 | } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) { |
| 557 | ptr += 8; |
| 558 | if (*ptr == '\"') ptr++; |
| 559 | |
| 560 | Mount_Options = ptr; |
| 561 | if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") { |
| 562 | Mount_Options.resize(Mount_Options.size() - 1); |
| 563 | } |
| 564 | Process_FS_Flags(Mount_Options, Mount_Flags); |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 565 | } else if ((ptr_len > 12 && strncmp(ptr, "encryptable=", 12) == 0) || (ptr_len > 13 && strncmp(ptr, "forceencrypt=", 13) == 0)) { |
| 566 | ptr += 12; |
| 567 | if (*ptr == '=') ptr++; |
| 568 | if (*ptr == '\"') ptr++; |
| 569 | |
| 570 | Crypto_Key_Location = ptr; |
| 571 | if (Crypto_Key_Location.substr(Crypto_Key_Location.size() - 1, 1) == "\"") { |
| 572 | Crypto_Key_Location.resize(Crypto_Key_Location.size() - 1); |
| 573 | } |
| 574 | } else if (ptr_len > 8 && strncmp(ptr, "mounttodecrypt", 14) == 0) { |
| 575 | Mount_To_Decrypt = true; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 576 | } else if (strncmp(ptr, "flashimg", 8) == 0) { |
| 577 | if (ptr_len == 8) { |
| 578 | Can_Flash_Img = true; |
| 579 | } else if (ptr_len == 10) { |
| 580 | ptr += 9; |
| 581 | if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') { |
| 582 | Can_Flash_Img = true; |
| 583 | } else { |
| 584 | Can_Flash_Img = false; |
| 585 | } |
| 586 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 587 | } else { |
| 588 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 589 | LOGERR("Unhandled flag: '%s'\n", ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 590 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 591 | LOGINFO("Unhandled flag: '%s'\n", ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 592 | } |
| 593 | while (index < flags_len && flags[index] != '\0') |
| 594 | index++; |
| 595 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 596 | if (has_display_name && !has_storage_name) |
| 597 | Storage_Name = Display_Name; |
| 598 | if (!has_display_name && has_storage_name) |
| 599 | Display_Name = Storage_Name; |
Dees_Troy | 74fb2e9 | 2013-04-15 14:35:47 +0000 | [diff] [blame] | 600 | if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure") |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 601 | Backup_Display_Name = Display_Name; |
| 602 | if (!has_display_name && has_backup_name) |
| 603 | Display_Name = Backup_Display_Name; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 604 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 605 | } |
| 606 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 607 | bool TWPartition::Is_File_System(string File_System) { |
| 608 | if (File_System == "ext2" || |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 609 | File_System == "ext3" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 610 | File_System == "ext4" || |
| 611 | File_System == "vfat" || |
| 612 | File_System == "ntfs" || |
| 613 | File_System == "yaffs2" || |
bigbiff bigbiff | 3e14652 | 2012-11-14 14:32:59 -0500 | [diff] [blame] | 614 | File_System == "exfat" || |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 615 | File_System == "f2fs" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 616 | File_System == "auto") |
| 617 | return true; |
| 618 | else |
| 619 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 620 | } |
| 621 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 622 | bool TWPartition::Is_Image(string File_System) { |
Dees_Troy | 5fcd8f9 | 2012-10-16 12:22:05 -0400 | [diff] [blame] | 623 | if (File_System == "emmc" || File_System == "mtd" || File_System == "bml") |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 624 | return true; |
| 625 | else |
| 626 | return false; |
| 627 | } |
| 628 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 629 | bool TWPartition::Make_Dir(string Path, bool Display_Error) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 630 | if (!TWFunc::Path_Exists(Path)) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 631 | if (mkdir(Path.c_str(), 0777) == -1) { |
| 632 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 633 | LOGERR("Can not create '%s' folder.\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 634 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 635 | LOGINFO("Can not create '%s' folder.\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 636 | return false; |
| 637 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 638 | LOGINFO("Created '%s' folder.\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 639 | return true; |
| 640 | } |
| 641 | } |
| 642 | return true; |
| 643 | } |
| 644 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 645 | void TWPartition::Setup_File_System(bool Display_Error) { |
| 646 | struct statfs st; |
| 647 | |
| 648 | Can_Be_Mounted = true; |
| 649 | Can_Be_Wiped = true; |
| 650 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 651 | // Make the mount point folder if it doesn't exist |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 652 | Make_Dir(Mount_Point, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 653 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 654 | Backup_Name = Display_Name; |
| 655 | Backup_Method = FILES; |
| 656 | } |
| 657 | |
| 658 | void TWPartition::Setup_Image(bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 659 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 660 | Backup_Name = Display_Name; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 661 | if (Current_File_System == "emmc") |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 662 | Backup_Method = DD; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 663 | else if (Current_File_System == "mtd" || Current_File_System == "bml") |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 664 | Backup_Method = FLASH_UTILS; |
| 665 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 666 | 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] | 667 | if (Find_Partition_Size()) { |
| 668 | Used = Size; |
| 669 | Backup_Size = Size; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 670 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 671 | if (Display_Error) |
Dees Troy | d932ce1 | 2013-10-18 17:12:59 +0000 | [diff] [blame] | 672 | 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] | 673 | else |
Dees Troy | d932ce1 | 2013-10-18 17:12:59 +0000 | [diff] [blame] | 674 | 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] | 675 | } |
| 676 | } |
| 677 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 678 | void TWPartition::Setup_AndSec(void) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 679 | Backup_Display_Name = "Android Secure"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 680 | Backup_Name = "and-sec"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 681 | Can_Be_Backed_Up = true; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 682 | Has_Android_Secure = true; |
| 683 | Symlink_Path = Mount_Point + "/.android_secure"; |
| 684 | Symlink_Mount_Point = "/and-sec"; |
| 685 | Backup_Path = Symlink_Mount_Point; |
| 686 | Make_Dir("/and-sec", true); |
| 687 | Recreate_AndSec_Folder(); |
Ethan Yonker | d4d1073 | 2014-02-03 15:27:52 -0600 | [diff] [blame] | 688 | Mount_Storage_Retry(); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 689 | } |
| 690 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 691 | void TWPartition::Setup_Data_Media() { |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 692 | LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str()); |
| 693 | Storage_Name = "Internal Storage"; |
| 694 | Has_Data_Media = true; |
| 695 | Is_Storage = true; |
| 696 | Is_Settings_Storage = true; |
| 697 | Storage_Path = "/data/media"; |
| 698 | Symlink_Path = Storage_Path; |
| 699 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 700 | Make_Dir("/emmc", false); |
| 701 | Symlink_Mount_Point = "/emmc"; |
| 702 | } else { |
| 703 | Make_Dir("/sdcard", false); |
| 704 | Symlink_Mount_Point = "/sdcard"; |
| 705 | } |
| 706 | if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) { |
| 707 | Storage_Path = "/data/media/0"; |
| 708 | Symlink_Path = Storage_Path; |
| 709 | DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0"); |
| 710 | UnMount(true); |
| 711 | } |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 712 | DataManager::SetValue("tw_has_internal", 1); |
| 713 | DataManager::SetValue("tw_has_data_media", 1); |
| 714 | du.add_absolute_dir("/data/media"); |
| 715 | } |
| 716 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 717 | void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) { |
| 718 | char device[512], realDevice[512]; |
| 719 | |
| 720 | strcpy(device, Block.c_str()); |
| 721 | memset(realDevice, 0, sizeof(realDevice)); |
| 722 | while (readlink(device, realDevice, sizeof(realDevice)) > 0) |
| 723 | { |
| 724 | strcpy(device, realDevice); |
| 725 | memset(realDevice, 0, sizeof(realDevice)); |
| 726 | } |
| 727 | |
| 728 | if (device[0] != '/') { |
| 729 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 730 | 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] | 731 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 732 | 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] | 733 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 734 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 735 | Block = device; |
| 736 | return; |
| 737 | } |
| 738 | } |
| 739 | |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 740 | void TWPartition::Mount_Storage_Retry(void) { |
| 741 | // 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] | 742 | if (!Mount(true)) { |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 743 | int retry_count = 5; |
| 744 | while (retry_count > 0 && !Mount(false)) { |
| 745 | usleep(500000); |
| 746 | retry_count--; |
| 747 | } |
| 748 | Mount(true); |
| 749 | } |
| 750 | } |
| 751 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 752 | bool TWPartition::Find_MTD_Block_Device(string MTD_Name) { |
| 753 | FILE *fp = NULL; |
| 754 | char line[255]; |
| 755 | |
| 756 | fp = fopen("/proc/mtd", "rt"); |
| 757 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 758 | LOGERR("Device does not support /proc/mtd\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 759 | return false; |
| 760 | } |
| 761 | |
| 762 | while (fgets(line, sizeof(line), fp) != NULL) |
| 763 | { |
| 764 | char device[32], label[32]; |
| 765 | unsigned long size = 0; |
| 766 | char* fstype = NULL; |
| 767 | int deviceId; |
| 768 | |
| 769 | sscanf(line, "%s %lx %*s %*c%s", device, &size, label); |
| 770 | |
| 771 | // Skip header and blank lines |
| 772 | if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8)) |
| 773 | continue; |
| 774 | |
| 775 | // Strip off the trailing " from the label |
| 776 | label[strlen(label)-1] = '\0'; |
| 777 | |
| 778 | if (strcmp(label, MTD_Name.c_str()) == 0) { |
| 779 | // We found our device |
| 780 | // Strip off the trailing : from the device |
| 781 | device[strlen(device)-1] = '\0'; |
| 782 | if (sscanf(device,"mtd%d", &deviceId) == 1) { |
| 783 | sprintf(device, "/dev/block/mtdblock%d", deviceId); |
| 784 | Primary_Block_Device = device; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 785 | fclose(fp); |
| 786 | return true; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | } |
| 790 | fclose(fp); |
| 791 | |
| 792 | return false; |
| 793 | } |
| 794 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 795 | bool TWPartition::Get_Size_Via_statfs(bool Display_Error) { |
| 796 | struct statfs st; |
| 797 | string Local_Path = Mount_Point + "/."; |
| 798 | |
| 799 | if (!Mount(Display_Error)) |
| 800 | return false; |
| 801 | |
| 802 | if (statfs(Local_Path.c_str(), &st) != 0) { |
| 803 | if (!Removable) { |
| 804 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 805 | LOGERR("Unable to statfs '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 806 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 807 | LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 808 | } |
| 809 | return false; |
| 810 | } |
| 811 | Size = (st.f_blocks * st.f_bsize); |
| 812 | Used = ((st.f_blocks - st.f_bfree) * st.f_bsize); |
| 813 | Free = (st.f_bfree * st.f_bsize); |
| 814 | Backup_Size = Used; |
| 815 | return true; |
| 816 | } |
| 817 | |
| 818 | bool TWPartition::Get_Size_Via_df(bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 819 | FILE* fp; |
| 820 | char command[255], line[512]; |
| 821 | int include_block = 1; |
| 822 | unsigned int min_len; |
| 823 | |
| 824 | if (!Mount(Display_Error)) |
| 825 | return false; |
| 826 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 827 | min_len = Actual_Block_Device.size() + 2; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 828 | sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 829 | TWFunc::Exec_Cmd(command); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 830 | fp = fopen("/tmp/dfoutput.txt", "rt"); |
| 831 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 832 | LOGINFO("Unable to open /tmp/dfoutput.txt.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 833 | return false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 834 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 835 | |
| 836 | while (fgets(line, sizeof(line), fp) != NULL) |
| 837 | { |
| 838 | unsigned long blocks, used, available; |
| 839 | char device[64]; |
| 840 | char tmpString[64]; |
| 841 | |
| 842 | if (strncmp(line, "Filesystem", 10) == 0) |
| 843 | continue; |
| 844 | if (strlen(line) < min_len) { |
| 845 | include_block = 0; |
| 846 | continue; |
| 847 | } |
| 848 | if (include_block) { |
| 849 | sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available); |
| 850 | } else { |
| 851 | // The device block string is so long that the df information is on the next line |
| 852 | int space_count = 0; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 853 | sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 854 | while (tmpString[space_count] == 32) |
| 855 | space_count++; |
| 856 | sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available); |
| 857 | } |
| 858 | |
| 859 | // Adjust block size to byte size |
| 860 | Size = blocks * 1024ULL; |
| 861 | Used = used * 1024ULL; |
| 862 | Free = available * 1024ULL; |
| 863 | Backup_Size = Used; |
| 864 | } |
| 865 | fclose(fp); |
| 866 | return true; |
| 867 | } |
| 868 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 869 | bool TWPartition::Find_Partition_Size(void) { |
| 870 | FILE* fp; |
| 871 | char line[512]; |
| 872 | string tmpdevice; |
| 873 | |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 874 | fp = fopen("/proc/dumchar_info", "rt"); |
| 875 | if (fp != NULL) { |
| 876 | while (fgets(line, sizeof(line), fp) != NULL) |
| 877 | { |
| 878 | char label[32], device[32]; |
| 879 | unsigned long size = 0; |
| 880 | |
that | d43bf2d | 2014-09-21 23:13:02 +0200 | [diff] [blame] | 881 | sscanf(line, "%s %lx %*x %*u %s", label, &size, device); |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 882 | |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 883 | // Skip header, annotation and blank lines |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 884 | if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8)) |
| 885 | continue; |
| 886 | |
| 887 | tmpdevice = "/dev/"; |
| 888 | tmpdevice += label; |
| 889 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
| 890 | Size = size; |
| 891 | fclose(fp); |
| 892 | return true; |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 897 | // In this case, we'll first get the partitions we care about (with labels) |
| 898 | fp = fopen("/proc/partitions", "rt"); |
| 899 | if (fp == NULL) |
| 900 | return false; |
| 901 | |
| 902 | while (fgets(line, sizeof(line), fp) != NULL) |
| 903 | { |
| 904 | unsigned long major, minor, blocks; |
| 905 | char device[512]; |
| 906 | char tmpString[64]; |
| 907 | |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 908 | if (strlen(line) < 7 || line[0] == 'm') continue; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 909 | sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device); |
| 910 | |
| 911 | tmpdevice = "/dev/block/"; |
| 912 | tmpdevice += device; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 913 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 914 | // Adjust block size to byte size |
| 915 | Size = blocks * 1024ULL; |
| 916 | fclose(fp); |
| 917 | return true; |
| 918 | } |
| 919 | } |
| 920 | fclose(fp); |
| 921 | return false; |
| 922 | } |
| 923 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 924 | bool TWPartition::Is_Mounted(void) { |
| 925 | if (!Can_Be_Mounted) |
| 926 | return false; |
| 927 | |
| 928 | struct stat st1, st2; |
| 929 | string test_path; |
| 930 | |
| 931 | // Check to see if the mount point directory exists |
| 932 | test_path = Mount_Point + "/."; |
| 933 | if (stat(test_path.c_str(), &st1) != 0) return false; |
| 934 | |
| 935 | // Check to see if the directory above the mount point exists |
| 936 | test_path = Mount_Point + "/../."; |
| 937 | if (stat(test_path.c_str(), &st2) != 0) return false; |
| 938 | |
| 939 | // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device |
| 940 | int ret = (st1.st_dev != st2.st_dev) ? true : false; |
| 941 | |
| 942 | return ret; |
| 943 | } |
| 944 | |
| 945 | bool TWPartition::Mount(bool Display_Error) { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 946 | int exfat_mounted = 0; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 947 | unsigned long flags = Mount_Flags; |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 948 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 949 | if (Is_Mounted()) { |
| 950 | return true; |
| 951 | } else if (!Can_Be_Mounted) { |
| 952 | return false; |
| 953 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 954 | |
| 955 | Find_Actual_Block_Device(); |
| 956 | |
| 957 | // Check the current file system before mounting |
| 958 | Check_FS_Type(); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 959 | if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 960 | 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] | 961 | LOGINFO("cmd: %s\n", cmd.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 962 | string result; |
| 963 | if (TWFunc::Exec_Cmd(cmd, result) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 964 | 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] | 965 | Current_File_System = "vfat"; |
| 966 | } else { |
| 967 | #ifdef TW_NO_EXFAT_FUSE |
| 968 | UnMount(false); |
| 969 | // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat |
| 970 | // Some kernels let us mount vfat as exfat which doesn't work out too well |
| 971 | #else |
| 972 | exfat_mounted = 1; |
| 973 | #endif |
| 974 | } |
| 975 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 976 | |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame^] | 977 | if (Current_File_System == "ntfs" && TWFunc::Path_Exists("/sbin/ntfs-3g")) { |
| 978 | string cmd; |
| 979 | if (Mount_Read_Only) |
| 980 | cmd = "/sbin/ntfs-3g -o ro " + Actual_Block_Device + " " + Mount_Point; |
| 981 | else |
| 982 | cmd = "/sbin/ntfs-3g " + Actual_Block_Device + " " + Mount_Point; |
| 983 | LOGINFO("cmd: '%s'\n", cmd.c_str()); |
| 984 | if (TWFunc::Exec_Cmd(cmd) == 0) { |
| 985 | return true; |
| 986 | } else { |
| 987 | LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n"); |
| 988 | } |
| 989 | } |
| 990 | |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 991 | if (Mount_Read_Only) |
| 992 | flags |= MS_RDONLY; |
| 993 | |
Dees_Troy | 2204203 | 2012-12-18 21:23:08 +0000 | [diff] [blame] | 994 | if (Fstab_File_System == "yaffs2") { |
| 995 | // mount an MTD partition as a YAFFS2 filesystem. |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 996 | flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME; |
| 997 | if (Mount_Read_Only) |
| 998 | flags |= MS_RDONLY; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 999 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) { |
| 1000 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) { |
| 1001 | if (Display_Error) |
| 1002 | LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 1003 | else |
| 1004 | LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 1005 | return false; |
| 1006 | } else { |
| 1007 | LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str()); |
| 1008 | return true; |
| 1009 | } |
| 1010 | } else { |
| 1011 | struct stat st; |
| 1012 | string test_path = Mount_Point; |
| 1013 | if (stat(test_path.c_str(), &st) < 0) { |
| 1014 | if (Display_Error) |
| 1015 | LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 1016 | else |
| 1017 | LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 1018 | return false; |
| 1019 | } |
| 1020 | mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH; |
| 1021 | if (new_mode != st.st_mode) { |
| 1022 | LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str()); |
| 1023 | if (chmod(Mount_Point.c_str(), new_mode) < 0) { |
| 1024 | if (Display_Error) |
| 1025 | LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno)); |
| 1026 | else |
| 1027 | LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno)); |
| 1028 | return false; |
| 1029 | } |
| 1030 | } |
Dees_Troy | 2204203 | 2012-12-18 21:23:08 +0000 | [diff] [blame] | 1031 | return true; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1032 | } |
that | c7572eb | 2015-03-31 18:55:30 +0200 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | string mount_fs = Current_File_System; |
| 1036 | if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat")) |
| 1037 | mount_fs = "texfat"; |
| 1038 | |
| 1039 | if (!exfat_mounted && |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1040 | mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 && |
| 1041 | 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] | 1042 | #ifdef TW_NO_EXFAT_FUSE |
| 1043 | if (Current_File_System == "exfat") { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1044 | LOGINFO("Mounting exfat failed, trying vfat...\n"); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1045 | 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] | 1046 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1047 | LOGERR("Unable to mount '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1048 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1049 | LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str()); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1050 | 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] | 1051 | return false; |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1052 | } |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 1053 | } else { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1054 | #endif |
bigbiff bigbiff | 26774a0 | 2014-03-29 18:22:00 -0400 | [diff] [blame] | 1055 | if (!Removable && Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1056 | LOGERR("Unable to mount '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1057 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1058 | LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str()); |
| 1059 | 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] | 1060 | return false; |
| 1061 | #ifdef TW_NO_EXFAT_FUSE |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1062 | } |
| 1063 | #endif |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1064 | } |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 1065 | |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1066 | if (Removable) |
| 1067 | Update_Size(Display_Error); |
| 1068 | |
xiaolu | 9416f4f | 2015-06-04 08:22:23 +0800 | [diff] [blame] | 1069 | if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) { |
codelover | 2a3d4ce | 2015-03-14 20:26:49 +0800 | [diff] [blame] | 1070 | string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'"; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1071 | TWFunc::Exec_Cmd(Command); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1072 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1073 | return true; |
| 1074 | } |
| 1075 | |
| 1076 | bool TWPartition::UnMount(bool Display_Error) { |
| 1077 | if (Is_Mounted()) { |
| 1078 | int never_unmount_system; |
| 1079 | |
| 1080 | DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system); |
| 1081 | if (never_unmount_system == 1 && Mount_Point == "/system") |
| 1082 | return true; // Never unmount system if you're not supposed to unmount it |
| 1083 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1084 | if (Is_Storage) |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1085 | PartitionManager.Remove_MTP_Storage(MTP_Storage_ID); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1086 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1087 | if (!Symlink_Mount_Point.empty()) |
| 1088 | umount(Symlink_Mount_Point.c_str()); |
| 1089 | |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 1090 | umount(Mount_Point.c_str()); |
| 1091 | if (Is_Mounted()) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1092 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1093 | LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1094 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1095 | LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1096 | return false; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1097 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1098 | return true; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1099 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1100 | } else { |
| 1101 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1105 | bool TWPartition::Wipe(string New_File_System) { |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1106 | bool wiped = false, update_crypt = false, recreate_media = true; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1107 | int check; |
| 1108 | string Layout_Filename = Mount_Point + "/.layout_version"; |
| 1109 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1110 | if (!Can_Be_Wiped) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1111 | LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1112 | return false; |
| 1113 | } |
| 1114 | |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 1115 | if (Mount_Point == "/cache") |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1116 | Log_Offset = 0; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 1117 | |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1118 | if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename)) |
| 1119 | TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600); |
| 1120 | else |
| 1121 | unlink("/.layout_version"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1122 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1123 | if (Has_Data_Media && Current_File_System == New_File_System) { |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1124 | wiped = Wipe_Data_Without_Wiping_Media(); |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1125 | recreate_media = false; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1126 | } else { |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1127 | DataManager::GetValue(TW_RM_RF_VAR, check); |
| 1128 | |
Hashcode | dabfd49 | 2013-08-29 22:45:30 -0700 | [diff] [blame] | 1129 | if (check || Use_Rm_Rf) |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1130 | wiped = Wipe_RMRF(); |
| 1131 | else if (New_File_System == "ext4") |
| 1132 | wiped = Wipe_EXT4(); |
| 1133 | else if (New_File_System == "ext2" || New_File_System == "ext3") |
| 1134 | wiped = Wipe_EXT23(New_File_System); |
| 1135 | else if (New_File_System == "vfat") |
| 1136 | wiped = Wipe_FAT(); |
| 1137 | else if (New_File_System == "exfat") |
| 1138 | wiped = Wipe_EXFAT(); |
| 1139 | else if (New_File_System == "yaffs2") |
| 1140 | wiped = Wipe_MTD(); |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 1141 | else if (New_File_System == "f2fs") |
| 1142 | wiped = Wipe_F2FS(); |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame^] | 1143 | else if (New_File_System == "ntfs") |
| 1144 | wiped = Wipe_NTFS(); |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1145 | else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1146 | 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] | 1147 | unlink("/.layout_version"); |
| 1148 | return false; |
| 1149 | } |
| 1150 | update_crypt = wiped; |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1151 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1152 | |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1153 | if (wiped) { |
Dees_Troy | 1c1ac44 | 2013-01-17 21:42:14 +0000 | [diff] [blame] | 1154 | if (Mount_Point == "/cache") |
| 1155 | DataManager::Output_Version(); |
| 1156 | |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1157 | if (TWFunc::Path_Exists("/.layout_version") && Mount(false)) |
| 1158 | TWFunc::copy_file("/.layout_version", Layout_Filename, 0600); |
| 1159 | |
| 1160 | if (update_crypt) { |
| 1161 | Setup_File_System(false); |
| 1162 | if (Is_Encrypted && !Is_Decrypted) { |
| 1163 | // just wiped an encrypted partition back to its unencrypted state |
| 1164 | Is_Encrypted = false; |
| 1165 | Is_Decrypted = false; |
| 1166 | Decrypted_Block_Device = ""; |
| 1167 | if (Mount_Point == "/data") { |
| 1168 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
| 1169 | DataManager::SetValue(TW_IS_DECRYPTED, 0); |
| 1170 | } |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1171 | } |
| 1172 | } |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1173 | |
| 1174 | if (Mount_Point == "/data" && Has_Data_Media && recreate_media) { |
| 1175 | Recreate_Media_Folder(); |
| 1176 | } |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1177 | } |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1178 | if (Is_Storage) { |
| 1179 | PartitionManager.Add_MTP_Storage(MTP_Storage_ID); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1180 | } |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1181 | return wiped; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1182 | } |
| 1183 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1184 | bool TWPartition::Wipe() { |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1185 | if (Is_File_System(Current_File_System)) |
| 1186 | return Wipe(Current_File_System); |
| 1187 | else |
| 1188 | return Wipe(Fstab_File_System); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1189 | } |
| 1190 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1191 | bool TWPartition::Wipe_AndSec(void) { |
| 1192 | if (!Has_Android_Secure) |
| 1193 | return false; |
| 1194 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1195 | if (!Mount(true)) |
| 1196 | return false; |
| 1197 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1198 | gui_print("Wiping %s\n", Backup_Display_Name.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1199 | TWFunc::removeDir(Mount_Point + "/.android_secure/", true); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1200 | return true; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1201 | } |
| 1202 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1203 | bool TWPartition::Can_Repair() { |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1204 | if (Mount_Read_Only) |
| 1205 | return false; |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1206 | if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/dosfsck")) |
| 1207 | return true; |
| 1208 | else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck")) |
| 1209 | return true; |
| 1210 | else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat")) |
| 1211 | return true; |
| 1212 | else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs")) |
| 1213 | return true; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame^] | 1214 | else if (Current_File_System == "ntfs" && TWFunc::Path_Exists("/sbin/ntfsfix")) |
| 1215 | return true; |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1216 | return false; |
| 1217 | } |
| 1218 | |
| 1219 | bool TWPartition::Repair() { |
| 1220 | string command; |
| 1221 | |
| 1222 | if (Current_File_System == "vfat") { |
| 1223 | if (!TWFunc::Path_Exists("/sbin/dosfsck")) { |
| 1224 | gui_print("dosfsck does not exist! Cannot repair!\n"); |
| 1225 | return false; |
| 1226 | } |
| 1227 | if (!UnMount(true)) |
| 1228 | return false; |
| 1229 | gui_print("Repairing %s using dosfsck...\n", Display_Name.c_str()); |
| 1230 | Find_Actual_Block_Device(); |
| 1231 | command = "/sbin/dosfsck -y " + Actual_Block_Device; |
| 1232 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1233 | if (TWFunc::Exec_Cmd(command) == 0) { |
| 1234 | gui_print("Done.\n"); |
| 1235 | return true; |
| 1236 | } else { |
| 1237 | LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str()); |
| 1238 | return false; |
| 1239 | } |
| 1240 | } |
| 1241 | if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") { |
| 1242 | if (!TWFunc::Path_Exists("/sbin/e2fsck")) { |
| 1243 | gui_print("e2fsck does not exist! Cannot repair!\n"); |
| 1244 | return false; |
| 1245 | } |
| 1246 | if (!UnMount(true)) |
| 1247 | return false; |
| 1248 | gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str()); |
| 1249 | Find_Actual_Block_Device(); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1250 | command = "/sbin/e2fsck -fp " + Actual_Block_Device; |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1251 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1252 | if (TWFunc::Exec_Cmd(command) == 0) { |
| 1253 | gui_print("Done.\n"); |
| 1254 | return true; |
| 1255 | } else { |
| 1256 | LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str()); |
| 1257 | return false; |
| 1258 | } |
| 1259 | } |
| 1260 | if (Current_File_System == "exfat") { |
| 1261 | if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) { |
| 1262 | gui_print("fsck.exfat does not exist! Cannot repair!\n"); |
| 1263 | return false; |
| 1264 | } |
| 1265 | if (!UnMount(true)) |
| 1266 | return false; |
| 1267 | gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str()); |
| 1268 | Find_Actual_Block_Device(); |
| 1269 | command = "/sbin/fsck.exfat " + Actual_Block_Device; |
| 1270 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1271 | if (TWFunc::Exec_Cmd(command) == 0) { |
| 1272 | gui_print("Done.\n"); |
| 1273 | return true; |
| 1274 | } else { |
| 1275 | LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str()); |
| 1276 | return false; |
| 1277 | } |
| 1278 | } |
| 1279 | if (Current_File_System == "f2fs") { |
| 1280 | if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) { |
| 1281 | gui_print("fsck.f2fs does not exist! Cannot repair!\n"); |
| 1282 | return false; |
| 1283 | } |
| 1284 | if (!UnMount(true)) |
| 1285 | return false; |
| 1286 | gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str()); |
| 1287 | Find_Actual_Block_Device(); |
| 1288 | command = "/sbin/fsck.f2fs " + Actual_Block_Device; |
| 1289 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1290 | if (TWFunc::Exec_Cmd(command) == 0) { |
| 1291 | gui_print("Done.\n"); |
| 1292 | return true; |
| 1293 | } else { |
| 1294 | LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str()); |
| 1295 | return false; |
| 1296 | } |
| 1297 | } |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame^] | 1298 | if (Current_File_System == "ntfs") { |
| 1299 | if (!TWFunc::Path_Exists("/sbin/ntfsfix")) { |
| 1300 | gui_print("ntfsfix does not exist! Cannot repair!\n"); |
| 1301 | return false; |
| 1302 | } |
| 1303 | if (!UnMount(true)) |
| 1304 | return false; |
| 1305 | gui_print("Repairing %s using ntfsfix...\n", Display_Name.c_str()); |
| 1306 | Find_Actual_Block_Device(); |
| 1307 | command = "/sbin/ntfsfix " + Actual_Block_Device; |
| 1308 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1309 | if (TWFunc::Exec_Cmd(command) == 0) { |
| 1310 | gui_print("Done.\n"); |
| 1311 | return true; |
| 1312 | } else { |
| 1313 | LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str()); |
| 1314 | return false; |
| 1315 | } |
| 1316 | } |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1317 | return false; |
| 1318 | } |
| 1319 | |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1320 | bool TWPartition::Can_Resize() { |
| 1321 | if (Mount_Read_Only) |
| 1322 | return false; |
| 1323 | if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs")) |
| 1324 | return true; |
| 1325 | return false; |
| 1326 | } |
| 1327 | |
| 1328 | bool TWPartition::Resize() { |
| 1329 | string command; |
| 1330 | |
| 1331 | if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") { |
| 1332 | if (!Can_Repair()) { |
| 1333 | LOGERR("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str()); |
| 1334 | return false; |
| 1335 | } |
| 1336 | if (!TWFunc::Path_Exists("/sbin/resize2fs")) { |
| 1337 | gui_print("resize2fs does not exist! Cannot resize!\n"); |
| 1338 | return false; |
| 1339 | } |
| 1340 | // Repair will unmount so no need to do it twice |
| 1341 | gui_print("Repairing %s before resizing.\n", Display_Name.c_str()); |
| 1342 | if (!Repair()) |
| 1343 | return false; |
| 1344 | gui_print("Resizing %s using resize2fs...\n", Display_Name.c_str()); |
| 1345 | Find_Actual_Block_Device(); |
| 1346 | command = "/sbin/resize2fs " + Actual_Block_Device; |
| 1347 | if (Length != 0) { |
| 1348 | unsigned int block_device_size; |
| 1349 | int fd, ret; |
| 1350 | |
| 1351 | fd = open(Actual_Block_Device.c_str(), O_RDONLY); |
| 1352 | if (fd < 0) { |
| 1353 | LOGERR("Resize: Failed to open '%s'\n", Actual_Block_Device.c_str()); |
| 1354 | return false; |
| 1355 | } |
| 1356 | ret = ioctl(fd, BLKGETSIZE, &block_device_size); |
| 1357 | close(fd); |
| 1358 | if (ret) { |
| 1359 | LOGERR("Resize: ioctl error\n"); |
| 1360 | return false; |
| 1361 | } |
| 1362 | unsigned long long Actual_Size = (unsigned long long)(block_device_size) * 512LLU; |
| 1363 | unsigned long long Block_Count; |
| 1364 | if (Length < 0) { |
| 1365 | // Reduce overall size by this length |
| 1366 | Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU); |
| 1367 | } else { |
| 1368 | // This is the size, not a size reduction |
| 1369 | Block_Count = ((unsigned long long)(Length) / 1024LLU); |
| 1370 | } |
| 1371 | char temp[256]; |
| 1372 | sprintf(temp, "%llu", Block_Count); |
| 1373 | command += " "; |
| 1374 | command += temp; |
| 1375 | command += "K"; |
| 1376 | } |
| 1377 | LOGINFO("Resize command: %s\n", command.c_str()); |
| 1378 | if (TWFunc::Exec_Cmd(command) == 0) { |
| 1379 | Update_Size(true); |
| 1380 | gui_print("Done.\n"); |
| 1381 | return true; |
| 1382 | } else { |
| 1383 | Update_Size(true); |
| 1384 | LOGERR("Unable to resize '%s'.\n", Mount_Point.c_str()); |
| 1385 | return false; |
| 1386 | } |
| 1387 | } |
| 1388 | return false; |
| 1389 | } |
| 1390 | |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1391 | bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) { |
| 1392 | if (Backup_Method == FILES) { |
| 1393 | return Backup_Tar(backup_folder, overall_size, other_backups_size, tar_fork_pid); |
| 1394 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1395 | else if (Backup_Method == DD) |
| 1396 | return Backup_DD(backup_folder); |
| 1397 | else if (Backup_Method == FLASH_UTILS) |
| 1398 | return Backup_Dump_Image(backup_folder); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1399 | LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1400 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1401 | } |
| 1402 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1403 | bool TWPartition::Check_MD5(string restore_folder) { |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 1404 | string Full_Filename, md5file; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1405 | char split_filename[512]; |
| 1406 | int index = 0; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 1407 | twrpDigest md5sum; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1408 | |
Captain Throwback | ff5935f | 2014-09-23 16:08:34 -0400 | [diff] [blame] | 1409 | sync(); |
| 1410 | |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 1411 | memset(split_filename, 0, sizeof(split_filename)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1412 | Full_Filename = restore_folder + "/" + Backup_FileName; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1413 | if (!TWFunc::Path_Exists(Full_Filename)) { |
| 1414 | // This is a split archive, we presume |
| 1415 | sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1416 | LOGINFO("split_filename: %s\n", split_filename); |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 1417 | md5file = split_filename; |
| 1418 | md5file += ".md5"; |
| 1419 | if (!TWFunc::Path_Exists(md5file)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1420 | LOGERR("No md5 file found for '%s'.\n", split_filename); |
| 1421 | LOGERR("Please unselect Enable MD5 verification to restore.\n"); |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 1422 | return false; |
| 1423 | } |
| 1424 | md5sum.setfn(split_filename); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1425 | while (index < 1000) { |
| 1426 | if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1427 | LOGERR("MD5 failed to match on '%s'.\n", split_filename); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1428 | return false; |
| 1429 | } |
| 1430 | index++; |
| 1431 | sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index); |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 1432 | md5sum.setfn(split_filename); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1433 | } |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1434 | return true; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1435 | } else { |
| 1436 | // Single file archive |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 1437 | md5file = Full_Filename + ".md5"; |
| 1438 | if (!TWFunc::Path_Exists(md5file)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1439 | LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str()); |
| 1440 | LOGERR("Please unselect Enable MD5 verification to restore.\n"); |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 1441 | return false; |
| 1442 | } |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 1443 | md5sum.setfn(Full_Filename); |
| 1444 | if (md5sum.verify_md5digest() != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1445 | LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1446 | return false; |
| 1447 | } else |
| 1448 | return true; |
| 1449 | } |
| 1450 | return false; |
| 1451 | } |
| 1452 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1453 | 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] | 1454 | string Restore_File_System; |
| 1455 | |
| 1456 | TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1457 | LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str()); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1458 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1459 | Restore_File_System = Get_Restore_File_System(restore_folder); |
| 1460 | |
| 1461 | if (Is_File_System(Restore_File_System)) |
| 1462 | return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size); |
| 1463 | else if (Is_Image(Restore_File_System)) { |
Ethan Yonker | f66324f | 2015-01-09 16:46:57 -0600 | [diff] [blame] | 1464 | 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] | 1465 | } |
| 1466 | |
| 1467 | LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str()); |
| 1468 | return false; |
| 1469 | } |
| 1470 | |
| 1471 | string TWPartition::Get_Restore_File_System(string restore_folder) { |
| 1472 | size_t first_period, second_period; |
| 1473 | string Restore_File_System; |
| 1474 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1475 | // Parse backup filename to extract the file system before wiping |
| 1476 | first_period = Backup_FileName.find("."); |
| 1477 | if (first_period == string::npos) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1478 | LOGERR("Unable to find file system (first period).\n"); |
that | d43bf2d | 2014-09-21 23:13:02 +0200 | [diff] [blame] | 1479 | return string(); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1480 | } |
| 1481 | Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1); |
| 1482 | second_period = Restore_File_System.find("."); |
| 1483 | if (second_period == string::npos) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1484 | LOGERR("Unable to find file system (second period).\n"); |
that | d43bf2d | 2014-09-21 23:13:02 +0200 | [diff] [blame] | 1485 | return string(); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1486 | } |
| 1487 | Restore_File_System.resize(second_period); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1488 | LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str()); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1489 | return Restore_File_System; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | string TWPartition::Backup_Method_By_Name() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1493 | if (Backup_Method == NONE) |
| 1494 | return "none"; |
| 1495 | else if (Backup_Method == FILES) |
| 1496 | return "files"; |
| 1497 | else if (Backup_Method == DD) |
| 1498 | return "dd"; |
| 1499 | else if (Backup_Method == FLASH_UTILS) |
| 1500 | return "flash_utils"; |
| 1501 | else |
| 1502 | return "undefined"; |
| 1503 | return "ERROR!"; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | bool TWPartition::Decrypt(string Password) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1507 | LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1508 | // Is this needed? |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1509 | return 1; |
| 1510 | } |
| 1511 | |
| 1512 | bool TWPartition::Wipe_Encryption() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1513 | bool Save_Data_Media = Has_Data_Media; |
| 1514 | |
| 1515 | if (!UnMount(true)) |
| 1516 | return false; |
| 1517 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1518 | Has_Data_Media = false; |
Dees_Troy | 74fb2e9 | 2013-04-15 14:35:47 +0000 | [diff] [blame] | 1519 | Decrypted_Block_Device = ""; |
Ethan Yonker | d79d9bc | 2014-12-20 15:38:29 -0600 | [diff] [blame] | 1520 | #ifdef TW_INCLUDE_CRYPTO |
| 1521 | if (Is_Decrypted) { |
| 1522 | if (!UnMount(true)) |
| 1523 | return false; |
Matt Mower | 2b18a53 | 2015-02-20 16:58:05 -0600 | [diff] [blame] | 1524 | if (delete_crypto_blk_dev((char*)("userdata")) != 0) { |
Ethan Yonker | d79d9bc | 2014-12-20 15:38:29 -0600 | [diff] [blame] | 1525 | LOGERR("Error deleting crypto block device, continuing anyway.\n"); |
| 1526 | } |
| 1527 | } |
| 1528 | #endif |
Dees_Troy | 74fb2e9 | 2013-04-15 14:35:47 +0000 | [diff] [blame] | 1529 | Is_Decrypted = false; |
| 1530 | Is_Encrypted = false; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1531 | Find_Actual_Block_Device(); |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1532 | if (Wipe(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1533 | Has_Data_Media = Save_Data_Media; |
| 1534 | if (Has_Data_Media && !Symlink_Mount_Point.empty()) { |
| 1535 | Recreate_Media_Folder(); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1536 | if (Mount(false)) |
| 1537 | PartitionManager.Add_MTP_Storage(MTP_Storage_ID); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1538 | } |
Ethan Yonker | d79d9bc | 2014-12-20 15:38:29 -0600 | [diff] [blame] | 1539 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 1540 | #ifndef TW_OEM_BUILD |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1541 | gui_print("You may need to reboot recovery to be able to use /data again.\n"); |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 1542 | #endif |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1543 | return true; |
| 1544 | } else { |
| 1545 | Has_Data_Media = Save_Data_Media; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1546 | LOGERR("Unable to format to remove encryption.\n"); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1547 | if (Has_Data_Media && Mount(false)) |
| 1548 | PartitionManager.Add_MTP_Storage(MTP_Storage_ID); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1549 | return false; |
| 1550 | } |
| 1551 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | void TWPartition::Check_FS_Type() { |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1555 | const char* type; |
| 1556 | blkid_probe pr; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1557 | |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 1558 | if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid) |
| 1559 | 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] | 1560 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1561 | Find_Actual_Block_Device(); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1562 | if (!Is_Present) |
| 1563 | return; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1564 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1565 | pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str()); |
| 1566 | if (blkid_do_fullprobe(pr)) { |
| 1567 | blkid_free_probe(pr); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1568 | LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str()); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1569 | return; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1570 | } |
Vojtech Bocek | 4d4b336 | 2013-06-24 22:46:13 +0200 | [diff] [blame] | 1571 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 1572 | if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) { |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1573 | blkid_free_probe(pr); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1574 | 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] | 1575 | return; |
| 1576 | } |
Vojtech Bocek | 4d4b336 | 2013-06-24 22:46:13 +0200 | [diff] [blame] | 1577 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1578 | Current_File_System = type; |
Vojtech Bocek | 4d4b336 | 2013-06-24 22:46:13 +0200 | [diff] [blame] | 1579 | blkid_free_probe(pr); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1580 | } |
| 1581 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1582 | bool TWPartition::Wipe_EXT23(string File_System) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1583 | if (!UnMount(true)) |
| 1584 | return false; |
| 1585 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1586 | if (TWFunc::Path_Exists("/sbin/mke2fs")) { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1587 | string command; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1588 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1589 | gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1590 | Find_Actual_Block_Device(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1591 | command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1592 | LOGINFO("mke2fs command: %s\n", command.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1593 | if (TWFunc::Exec_Cmd(command) == 0) { |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1594 | Current_File_System = File_System; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1595 | Recreate_AndSec_Folder(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1596 | gui_print("Done.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1597 | return true; |
| 1598 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1599 | LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1600 | return false; |
| 1601 | } |
| 1602 | } else |
| 1603 | return Wipe_RMRF(); |
| 1604 | |
| 1605 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | bool TWPartition::Wipe_EXT4() { |
Ethan Yonker | 25f20c1 | 2014-10-14 09:04:43 -0500 | [diff] [blame] | 1609 | Find_Actual_Block_Device(); |
| 1610 | if (!Is_Present) { |
| 1611 | LOGERR("Block device not present, cannot wipe %s.\n", Display_Name.c_str()); |
| 1612 | return false; |
| 1613 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1614 | if (!UnMount(true)) |
| 1615 | return false; |
| 1616 | |
Dees_Troy | b3265ab | 2013-08-30 02:59:14 +0000 | [diff] [blame] | 1617 | #if defined(HAVE_SELINUX) && defined(USE_EXT4) |
Ethan Yonker | f27497f | 2014-02-09 11:48:33 -0600 | [diff] [blame] | 1618 | int ret; |
| 1619 | char *secontext = NULL; |
| 1620 | |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 1621 | gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str()); |
Ethan Yonker | f27497f | 2014-02-09 11:48:33 -0600 | [diff] [blame] | 1622 | |
Dees Troy | 99c8dbf | 2014-03-10 16:53:58 +0000 | [diff] [blame] | 1623 | 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] | 1624 | LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str()); |
| 1625 | ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL); |
| 1626 | } else { |
| 1627 | ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle); |
| 1628 | } |
| 1629 | if (ret != 0) { |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 1630 | LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str()); |
| 1631 | return false; |
| 1632 | } else { |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 1633 | string sedir = Mount_Point + "/lost+found"; |
| 1634 | PartitionManager.Mount_By_Path(sedir.c_str(), true); |
| 1635 | rmdir(sedir.c_str()); |
| 1636 | mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP); |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 1637 | return true; |
| 1638 | } |
| 1639 | #else |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1640 | if (TWFunc::Path_Exists("/sbin/make_ext4fs")) { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1641 | string Command; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1642 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1643 | gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1644 | Find_Actual_Block_Device(); |
| 1645 | Command = "make_ext4fs"; |
| 1646 | if (!Is_Decrypted && Length != 0) { |
| 1647 | // Only use length if we're not decrypted |
| 1648 | char len[32]; |
| 1649 | sprintf(len, "%i", Length); |
| 1650 | Command += " -l "; |
| 1651 | Command += len; |
| 1652 | } |
Dees_Troy | 5295d58 | 2013-09-06 15:51:08 +0000 | [diff] [blame] | 1653 | if (TWFunc::Path_Exists("/file_contexts")) { |
| 1654 | Command += " -S /file_contexts"; |
| 1655 | } |
| 1656 | Command += " -a " + Mount_Point + " " + Actual_Block_Device; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1657 | LOGINFO("make_ext4fs command: %s\n", Command.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1658 | if (TWFunc::Exec_Cmd(Command) == 0) { |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1659 | Current_File_System = "ext4"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1660 | Recreate_AndSec_Folder(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1661 | gui_print("Done.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1662 | return true; |
| 1663 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1664 | LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1665 | return false; |
| 1666 | } |
| 1667 | } else |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1668 | return Wipe_EXT23("ext4"); |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 1669 | #endif |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1670 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | bool TWPartition::Wipe_FAT() { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1674 | string command; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1675 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1676 | if (TWFunc::Path_Exists("/sbin/mkdosfs")) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1677 | if (!UnMount(true)) |
| 1678 | return false; |
| 1679 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1680 | gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1681 | Find_Actual_Block_Device(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1682 | command = "mkdosfs " + Actual_Block_Device; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1683 | if (TWFunc::Exec_Cmd(command) == 0) { |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1684 | Current_File_System = "vfat"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1685 | Recreate_AndSec_Folder(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1686 | gui_print("Done.\n"); |