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