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