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