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