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