Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2 | Copyright 2014 to 2016 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> |
| 24 | #include <unistd.h> |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 25 | #include <vector> |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 26 | #include <dirent.h> |
| 27 | #include <time.h> |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 30 | #include <zlib.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 31 | #include <iostream> |
| 32 | #include <iomanip> |
Ethan Yonker | 8613dc0 | 2014-09-11 09:28:20 -0500 | [diff] [blame] | 33 | #include <sys/wait.h> |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 34 | #include <linux/fs.h> |
| 35 | #include <sys/mount.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 36 | #include "variables.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 37 | #include "twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 38 | #include "partitions.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 39 | #include "data.hpp" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 40 | #include "twrp-functions.hpp" |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 41 | #include "fixContexts.hpp" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 42 | #include "twrpDigest.hpp" |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 43 | #include "exclude.hpp" |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 44 | #include "set_metadata.h" |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 45 | #include "tw_atomic.hpp" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 46 | #include "gui/gui.hpp" |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 47 | #include "progresstracking.hpp" |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 48 | #include "adbbu/libtwadbbu.hpp" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 49 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 50 | #ifdef TW_HAS_MTP |
| 51 | #include "mtp/mtp_MtpServer.hpp" |
| 52 | #include "mtp/twrpMtp.hpp" |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 53 | #include "mtp/MtpMessage.hpp" |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 54 | #endif |
| 55 | |
Dees Troy | 6f6441d | 2014-01-23 02:07:03 +0000 | [diff] [blame] | 56 | extern "C" { |
| 57 | #include "cutils/properties.h" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 58 | #include "gui/gui.h" |
Dees Troy | 6f6441d | 2014-01-23 02:07:03 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 61 | #ifdef TW_INCLUDE_CRYPTO |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 62 | #include "crypto/lollipop/cryptfs.h" |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 63 | #include "gui/rapidxml.hpp" |
| 64 | #include "gui/pages.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 65 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 66 | |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame^] | 67 | #ifdef AB_OTA_UPDATER |
| 68 | #include <hardware/hardware.h> |
| 69 | #include <hardware/boot_control.h> |
| 70 | #endif |
| 71 | |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 72 | extern bool datamedia; |
| 73 | |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 74 | TWPartitionManager::TWPartitionManager(void) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 75 | mtp_was_enabled = false; |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 76 | mtp_write_fd = -1; |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 77 | stop_backup.set_value(0); |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame^] | 78 | #ifdef AB_OTA_UPDATER |
| 79 | char slot_suffix[PROPERTY_VALUE_MAX]; |
| 80 | property_get("ro.boot.slot_suffix", slot_suffix, "_a"); |
| 81 | Active_Slot_Display = ""; |
| 82 | if (strcmp(slot_suffix, "_a") == 0) |
| 83 | Set_Active_Slot("A"); |
| 84 | else |
| 85 | Set_Active_Slot("B"); |
| 86 | #endif |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 87 | } |
| 88 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 89 | int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 90 | FILE *fstabFile; |
| 91 | char fstab_line[MAX_FSTAB_LINE_LENGTH]; |
Dees Troy | 02a6453 | 2014-03-19 15:23:32 +0000 | [diff] [blame] | 92 | TWPartition* settings_partition = NULL; |
Matt Mower | bf4efa3 | 2014-04-14 23:25:26 -0500 | [diff] [blame] | 93 | TWPartition* andsec_partition = NULL; |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 94 | unsigned int storageid = 1 << 16; // upper 16 bits are for physical storage device, we pretend to have only one |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 95 | |
| 96 | fstabFile = fopen(Fstab_Filename.c_str(), "rt"); |
| 97 | if (fstabFile == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 98 | LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | |
| 102 | while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) { |
| 103 | if (fstab_line[0] != '/') |
| 104 | continue; |
| 105 | |
Dees_Troy | 2a92358 | 2012-09-20 12:13:34 -0400 | [diff] [blame] | 106 | if (fstab_line[strlen(fstab_line) - 1] != '\n') |
| 107 | fstab_line[strlen(fstab_line)] = '\n'; |
Dees_Troy | 2a92358 | 2012-09-20 12:13:34 -0400 | [diff] [blame] | 108 | |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 109 | TWPartition* partition = new TWPartition(); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 110 | if (partition->Process_Fstab_Line(fstab_line, Display_Error)) |
Ethan Yonker | c05c598 | 2014-03-13 09:19:56 -0500 | [diff] [blame] | 111 | Partitions.push_back(partition); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 112 | else |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 113 | delete partition; |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 114 | |
| 115 | memset(fstab_line, 0, sizeof(fstab_line)); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 116 | } |
| 117 | fclose(fstabFile); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 118 | |
| 119 | std::vector<TWPartition*>::iterator iter; |
| 120 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 121 | (*iter)->Partition_Post_Processing(Display_Error); |
| 122 | |
| 123 | if ((*iter)->Is_Storage) { |
| 124 | ++storageid; |
| 125 | (*iter)->MTP_Storage_ID = storageid; |
| 126 | } |
| 127 | |
| 128 | if (!settings_partition && (*iter)->Is_Settings_Storage && (*iter)->Is_Present) |
| 129 | settings_partition = (*iter); |
| 130 | else |
| 131 | (*iter)->Is_Settings_Storage = false; |
| 132 | |
| 133 | if (!andsec_partition && (*iter)->Has_Android_Secure && (*iter)->Is_Present) |
| 134 | andsec_partition = (*iter); |
| 135 | else |
| 136 | (*iter)->Has_Android_Secure = false; |
| 137 | } |
| 138 | |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 139 | if (!datamedia && !settings_partition && Find_Partition_By_Path("/sdcard") == NULL && Find_Partition_By_Path("/internal_sd") == NULL && Find_Partition_By_Path("/internal_sdcard") == NULL && Find_Partition_By_Path("/emmc") == NULL) { |
| 140 | // Attempt to automatically identify /data/media emulated storage devices |
| 141 | TWPartition* Dat = Find_Partition_By_Path("/data"); |
| 142 | if (Dat) { |
| 143 | LOGINFO("Using automatic handling for /data/media emulated storage device.\n"); |
| 144 | datamedia = true; |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 145 | Dat->Setup_Data_Media(); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 146 | settings_partition = Dat; |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 147 | // Since /data was not considered a storage partition earlier, we still need to assign an MTP ID |
| 148 | ++storageid; |
| 149 | Dat->MTP_Storage_ID = storageid; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 150 | } |
| 151 | } |
Dees Troy | 02a6453 | 2014-03-19 15:23:32 +0000 | [diff] [blame] | 152 | if (!settings_partition) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 153 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 154 | if ((*iter)->Is_Storage) { |
Dees Troy | 02a6453 | 2014-03-19 15:23:32 +0000 | [diff] [blame] | 155 | settings_partition = (*iter); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 156 | break; |
| 157 | } |
| 158 | } |
Dees Troy | 02a6453 | 2014-03-19 15:23:32 +0000 | [diff] [blame] | 159 | if (!settings_partition) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 160 | LOGERR("Unable to locate storage partition for storing settings file.\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 161 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 162 | if (!Write_Fstab()) { |
| 163 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 164 | LOGERR("Error creating fstab\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 165 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 166 | LOGINFO("Error creating fstab\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 167 | } |
Matt Mower | ed71fa3 | 2014-04-16 13:21:47 -0500 | [diff] [blame] | 168 | |
Matt Mower | bf4efa3 | 2014-04-14 23:25:26 -0500 | [diff] [blame] | 169 | if (andsec_partition) { |
| 170 | Setup_Android_Secure_Location(andsec_partition); |
Matt Mower | ed71fa3 | 2014-04-16 13:21:47 -0500 | [diff] [blame] | 171 | } else if (settings_partition) { |
Matt Mower | bf4efa3 | 2014-04-14 23:25:26 -0500 | [diff] [blame] | 172 | Setup_Android_Secure_Location(settings_partition); |
| 173 | } |
Matt Mower | ed71fa3 | 2014-04-16 13:21:47 -0500 | [diff] [blame] | 174 | if (settings_partition) { |
| 175 | Setup_Settings_Storage_Partition(settings_partition); |
| 176 | } |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 177 | #ifdef TW_INCLUDE_CRYPTO |
Ethan Yonker | cceebb8 | 2014-11-18 10:17:59 -0600 | [diff] [blame] | 178 | TWPartition* Decrypt_Data = Find_Partition_By_Path("/data"); |
| 179 | if (Decrypt_Data && Decrypt_Data->Is_Encrypted && !Decrypt_Data->Is_Decrypted) { |
| 180 | int password_type = cryptfs_get_password_type(); |
| 181 | if (password_type == CRYPT_TYPE_DEFAULT) { |
| 182 | LOGINFO("Device is encrypted with the default password, attempting to decrypt.\n"); |
| 183 | if (Decrypt_Device("default_password") == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 184 | gui_msg("decrypt_success=Successfully decrypted with default password."); |
Ethan Yonker | cceebb8 | 2014-11-18 10:17:59 -0600 | [diff] [blame] | 185 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
| 186 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 187 | gui_err("unable_to_decrypt=Unable to decrypt with default password."); |
Ethan Yonker | cceebb8 | 2014-11-18 10:17:59 -0600 | [diff] [blame] | 188 | } |
| 189 | } else { |
| 190 | DataManager::SetValue("TW_CRYPTO_TYPE", password_type); |
| 191 | } |
| 192 | } |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 193 | if (Decrypt_Data && (!Decrypt_Data->Is_Encrypted || Decrypt_Data->Is_Decrypted) && Decrypt_Data->Mount(false)) { |
| 194 | Decrypt_Adopted(); |
| 195 | } |
Ethan Yonker | cceebb8 | 2014-11-18 10:17:59 -0600 | [diff] [blame] | 196 | #endif |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 197 | Update_System_Details(); |
Dees_Troy | d0384ef | 2012-10-12 12:15:42 -0400 | [diff] [blame] | 198 | UnMount_Main_Partitions(); |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame^] | 199 | #ifdef AB_OTA_UPDATER |
| 200 | DataManager::SetValue("tw_active_slot", Get_Active_Slot_Display()); |
| 201 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 202 | return true; |
| 203 | } |
| 204 | |
| 205 | int TWPartitionManager::Write_Fstab(void) { |
| 206 | FILE *fp; |
| 207 | std::vector<TWPartition*>::iterator iter; |
| 208 | string Line; |
| 209 | |
| 210 | fp = fopen("/etc/fstab", "w"); |
| 211 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 212 | LOGINFO("Can not open /etc/fstab.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 213 | return false; |
| 214 | } |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 215 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 216 | if ((*iter)->Can_Be_Mounted) { |
dianlujitao | 7d304c7 | 2016-01-02 12:15:50 +0800 | [diff] [blame] | 217 | Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw 0 0\n"; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 218 | fputs(Line.c_str(), fp); |
Dees_Troy | 91862e6 | 2013-04-04 23:48:21 +0000 | [diff] [blame] | 219 | } |
| 220 | // Handle subpartition tracking |
| 221 | if ((*iter)->Is_SubPartition) { |
| 222 | TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of); |
| 223 | if (ParentPartition) |
| 224 | ParentPartition->Has_SubPartition = true; |
| 225 | else |
| 226 | LOGERR("Unable to locate parent partition '%s' of '%s'\n", (*iter)->SubPartition_Of.c_str(), (*iter)->Mount_Point.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | fclose(fp); |
| 230 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 231 | } |
| 232 | |
Ethan Yonker | c05c598 | 2014-03-13 09:19:56 -0500 | [diff] [blame] | 233 | void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) { |
Ethan Yonker | c05c598 | 2014-03-13 09:19:56 -0500 | [diff] [blame] | 234 | DataManager::SetValue("tw_settings_path", Part->Storage_Path); |
| 235 | DataManager::SetValue("tw_storage_path", Part->Storage_Path); |
| 236 | LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str()); |
| 237 | } |
| 238 | |
Matt Mower | bf4efa3 | 2014-04-14 23:25:26 -0500 | [diff] [blame] | 239 | void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) { |
| 240 | if (Part->Has_Android_Secure) |
| 241 | Part->Setup_AndSec(); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 242 | else if (!datamedia) |
Matt Mower | bf4efa3 | 2014-04-14 23:25:26 -0500 | [diff] [blame] | 243 | Part->Setup_AndSec(); |
Matt Mower | bf4efa3 | 2014-04-14 23:25:26 -0500 | [diff] [blame] | 244 | } |
| 245 | |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 246 | void TWPartitionManager::Output_Partition_Logging(void) { |
| 247 | std::vector<TWPartition*>::iterator iter; |
| 248 | |
| 249 | printf("\n\nPartition Logs:\n"); |
| 250 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) |
| 251 | Output_Partition((*iter)); |
| 252 | } |
| 253 | |
| 254 | void TWPartitionManager::Output_Partition(TWPartition* Part) { |
| 255 | unsigned long long mb = 1048576; |
| 256 | |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 257 | printf("%s | %s | Size: %iMB", Part->Mount_Point.c_str(), Part->Actual_Block_Device.c_str(), (int)(Part->Size / mb)); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 258 | if (Part->Can_Be_Mounted) { |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 259 | printf(" Used: %iMB Free: %iMB Backup Size: %iMB", (int)(Part->Used / mb), (int)(Part->Free / mb), (int)(Part->Backup_Size / mb)); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 260 | } |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 261 | printf("\n Flags: "); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 262 | if (Part->Can_Be_Mounted) |
| 263 | printf("Can_Be_Mounted "); |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 264 | if (Part->Can_Be_Wiped) |
| 265 | printf("Can_Be_Wiped "); |
Hashcode | dabfd49 | 2013-08-29 22:45:30 -0700 | [diff] [blame] | 266 | if (Part->Use_Rm_Rf) |
| 267 | printf("Use_Rm_Rf "); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 268 | if (Part->Can_Be_Backed_Up) |
| 269 | printf("Can_Be_Backed_Up "); |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 270 | if (Part->Wipe_During_Factory_Reset) |
| 271 | printf("Wipe_During_Factory_Reset "); |
| 272 | if (Part->Wipe_Available_in_GUI) |
| 273 | printf("Wipe_Available_in_GUI "); |
| 274 | if (Part->Is_SubPartition) |
| 275 | printf("Is_SubPartition "); |
| 276 | if (Part->Has_SubPartition) |
| 277 | printf("Has_SubPartition "); |
| 278 | if (Part->Removable) |
| 279 | printf("Removable "); |
| 280 | if (Part->Is_Present) |
| 281 | printf("IsPresent "); |
| 282 | if (Part->Can_Be_Encrypted) |
| 283 | printf("Can_Be_Encrypted "); |
| 284 | if (Part->Is_Encrypted) |
| 285 | printf("Is_Encrypted "); |
| 286 | if (Part->Is_Decrypted) |
| 287 | printf("Is_Decrypted "); |
| 288 | if (Part->Has_Data_Media) |
| 289 | printf("Has_Data_Media "); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 290 | if (Part->Can_Encrypt_Backup) |
| 291 | printf("Can_Encrypt_Backup "); |
| 292 | if (Part->Use_Userdata_Encryption) |
| 293 | printf("Use_Userdata_Encryption "); |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 294 | if (Part->Has_Android_Secure) |
| 295 | printf("Has_Android_Secure "); |
| 296 | if (Part->Is_Storage) |
| 297 | printf("Is_Storage "); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 298 | if (Part->Is_Settings_Storage) |
| 299 | printf("Is_Settings_Storage "); |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 300 | if (Part->Ignore_Blkid) |
| 301 | printf("Ignore_Blkid "); |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 302 | if (Part->Retain_Layout_Version) |
| 303 | printf("Retain_Layout_Version "); |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 304 | if (Part->Mount_To_Decrypt) |
| 305 | printf("Mount_To_Decrypt "); |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 306 | if (Part->Can_Flash_Img) |
| 307 | printf("Can_Flash_Img "); |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 308 | if (Part->Is_Adopted_Storage) |
| 309 | printf("Is_Adopted_Storage "); |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame^] | 310 | if (Part->SlotSelect) |
| 311 | printf("SlotSelect "); |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 312 | printf("\n"); |
| 313 | if (!Part->SubPartition_Of.empty()) |
| 314 | printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str()); |
| 315 | if (!Part->Symlink_Path.empty()) |
| 316 | printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str()); |
| 317 | if (!Part->Symlink_Mount_Point.empty()) |
| 318 | printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str()); |
| 319 | if (!Part->Primary_Block_Device.empty()) |
| 320 | printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str()); |
| 321 | if (!Part->Alternate_Block_Device.empty()) |
| 322 | printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str()); |
| 323 | if (!Part->Decrypted_Block_Device.empty()) |
| 324 | printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str()); |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 325 | if (!Part->Crypto_Key_Location.empty() && Part->Crypto_Key_Location != "footer") |
| 326 | printf(" Crypto_Key_Location: %s\n", Part->Crypto_Key_Location.c_str()); |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 327 | if (Part->Length != 0) |
| 328 | printf(" Length: %i\n", Part->Length); |
| 329 | if (!Part->Display_Name.empty()) |
| 330 | printf(" Display_Name: %s\n", Part->Display_Name.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 331 | if (!Part->Storage_Name.empty()) |
| 332 | printf(" Storage_Name: %s\n", Part->Storage_Name.c_str()); |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 333 | if (!Part->Backup_Path.empty()) |
| 334 | printf(" Backup_Path: %s\n", Part->Backup_Path.c_str()); |
| 335 | if (!Part->Backup_Name.empty()) |
| 336 | printf(" Backup_Name: %s\n", Part->Backup_Name.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 337 | if (!Part->Backup_Display_Name.empty()) |
| 338 | printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str()); |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 339 | if (!Part->Backup_FileName.empty()) |
| 340 | printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str()); |
| 341 | if (!Part->Storage_Path.empty()) |
| 342 | printf(" Storage_Path: %s\n", Part->Storage_Path.c_str()); |
| 343 | if (!Part->Current_File_System.empty()) |
| 344 | printf(" Current_File_System: %s\n", Part->Current_File_System.c_str()); |
| 345 | if (!Part->Fstab_File_System.empty()) |
| 346 | printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str()); |
| 347 | if (Part->Format_Block_Size != 0) |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 348 | printf(" Format_Block_Size: %lu\n", Part->Format_Block_Size); |
Dees_Troy | 094207a | 2012-09-26 12:00:39 -0400 | [diff] [blame] | 349 | if (!Part->MTD_Name.empty()) |
| 350 | printf(" MTD_Name: %s\n", Part->MTD_Name.c_str()); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 351 | string back_meth = Part->Backup_Method_By_Name(); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 352 | printf(" Backup_Method: %s\n", back_meth.c_str()); |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 353 | if (Part->Mount_Flags || !Part->Mount_Options.empty()) |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 354 | printf(" Mount_Options: %s\n", Part->Mount_Options.c_str()); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 355 | if (Part->MTP_Storage_ID) |
| 356 | printf(" MTP_Storage_ID: %i\n", Part->MTP_Storage_ID); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 357 | printf("\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 358 | } |
| 359 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 360 | int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 361 | std::vector<TWPartition*>::iterator iter; |
| 362 | int ret = false; |
| 363 | bool found = false; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 364 | string Local_Path = TWFunc::Get_Root_Path(Path); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 365 | |
Dees_Troy | d93bda5 | 2013-07-03 19:55:19 +0000 | [diff] [blame] | 366 | if (Local_Path == "/tmp" || Local_Path == "/") |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 367 | return true; |
| 368 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 369 | // Iterate through all partitions |
| 370 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 371 | if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 372 | ret = (*iter)->Mount(Display_Error); |
| 373 | found = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 374 | } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 375 | (*iter)->Mount(Display_Error); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 376 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 377 | } |
| 378 | if (found) { |
| 379 | return ret; |
| 380 | } else if (Display_Error) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 381 | gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path)); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 382 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 383 | LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 384 | } |
| 385 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 386 | } |
| 387 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 388 | int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 389 | std::vector<TWPartition*>::iterator iter; |
| 390 | int ret = false; |
| 391 | bool found = false; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 392 | string Local_Path = TWFunc::Get_Root_Path(Path); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 393 | |
| 394 | // Iterate through all partitions |
| 395 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 396 | if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 397 | ret = (*iter)->UnMount(Display_Error); |
| 398 | found = true; |
| 399 | } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) { |
| 400 | (*iter)->UnMount(Display_Error); |
| 401 | } |
| 402 | } |
| 403 | if (found) { |
| 404 | return ret; |
| 405 | } else if (Display_Error) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 406 | gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path)); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 407 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 408 | LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 409 | } |
| 410 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 411 | } |
| 412 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 413 | int TWPartitionManager::Is_Mounted_By_Path(string Path) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 414 | TWPartition* Part = Find_Partition_By_Path(Path); |
| 415 | |
| 416 | if (Part) |
| 417 | return Part->Is_Mounted(); |
| 418 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 419 | LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 420 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 421 | } |
| 422 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 423 | int TWPartitionManager::Mount_Current_Storage(bool Display_Error) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 424 | string current_storage_path = DataManager::GetCurrentStoragePath(); |
| 425 | |
| 426 | if (Mount_By_Path(current_storage_path, Display_Error)) { |
| 427 | TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path); |
| 428 | if (FreeStorage) |
| 429 | DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU)); |
| 430 | return true; |
| 431 | } |
| 432 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 433 | } |
| 434 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 435 | int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) { |
| 436 | return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error); |
| 437 | } |
| 438 | |
| 439 | TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) { |
| 440 | std::vector<TWPartition*>::iterator iter; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 441 | string Local_Path = TWFunc::Get_Root_Path(Path); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 442 | |
| 443 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 444 | if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 445 | return (*iter); |
| 446 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 447 | return NULL; |
| 448 | } |
| 449 | |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 450 | int TWPartitionManager::Check_Backup_Name(bool Display_Error) { |
| 451 | // Check the backup name to ensure that it is the correct size and contains only valid characters |
| 452 | // and that a backup with that name doesn't already exist |
| 453 | char backup_name[MAX_BACKUP_NAME_LEN]; |
| 454 | char backup_loc[255], tw_image_dir[255]; |
| 455 | int copy_size; |
| 456 | int index, cur_char; |
| 457 | string Backup_Name, Backup_Loc; |
| 458 | |
| 459 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
| 460 | copy_size = Backup_Name.size(); |
| 461 | // Check size |
| 462 | if (copy_size > MAX_BACKUP_NAME_LEN) { |
| 463 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 464 | gui_err("backup_name_len=Backup name is too long."); |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 465 | return -2; |
| 466 | } |
| 467 | |
| 468 | // Check each character |
| 469 | strncpy(backup_name, Backup_Name.c_str(), copy_size); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 470 | if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0) |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 471 | return 0; // A "0" (zero) means to use the current timestamp for the backup name |
| 472 | for (index=0; index<copy_size; index++) { |
| 473 | cur_char = (int)backup_name[index]; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 474 | if (cur_char == 32 || (cur_char >= 48 && cur_char <= 57) || (cur_char >= 65 && cur_char <= 91) || cur_char == 93 || cur_char == 95 || (cur_char >= 97 && cur_char <= 123) || cur_char == 125 || cur_char == 45 || cur_char == 46) { |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 475 | // These are valid characters |
| 476 | // Numbers |
| 477 | // Upper case letters |
| 478 | // Lower case letters |
| 479 | // Space |
| 480 | // and -_.{}[] |
| 481 | } else { |
| 482 | if (Display_Error) |
Ethan Yonker | 4adc33e | 2016-01-22 16:07:11 -0600 | [diff] [blame] | 483 | gui_msg(Msg(msg::kError, "backup_name_invalid=Backup name '{1}' contains invalid character: '{1}'")(Backup_Name)((char)cur_char)); |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 484 | return -3; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // Check to make sure that a backup with this name doesn't already exist |
| 489 | DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc); |
| 490 | strcpy(backup_loc, Backup_Loc.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 491 | sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str()); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 492 | if (TWFunc::Path_Exists(tw_image_dir)) { |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 493 | if (Display_Error) |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 494 | gui_err("backup_name_exists=A backup with that name already exists!"); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 495 | |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 496 | return -4; |
| 497 | } |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 498 | // No problems found, return 0 |
| 499 | return 0; |
| 500 | } |
| 501 | |
bigbiff bigbiff | a481d36 | 2016-08-08 20:36:10 -0400 | [diff] [blame] | 502 | bool TWPartitionManager::Make_MD5(PartitionSettings *part_settings) |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 503 | { |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 504 | string command, result; |
| 505 | |
| 506 | if (part_settings->Part == NULL) |
| 507 | return false; |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 508 | string Full_File = part_settings->Backup_Folder + "/" + part_settings->Part->Backup_FileName; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 509 | twrpDigest md5sum; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 510 | |
bigbiff bigbiff | a481d36 | 2016-08-08 20:36:10 -0400 | [diff] [blame] | 511 | if (!part_settings->generate_md5) |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 512 | return true; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 513 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 514 | TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, gui_parse_text("{@generating_md51}")); |
| 515 | gui_msg("generating_md52= * Generating md5..."); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 516 | if (TWFunc::Path_Exists(Full_File)) { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 517 | md5sum.setfn(Full_File); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 518 | if (md5sum.computeMD5() == 0) |
| 519 | if (md5sum.write_md5digest() == 0) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 520 | gui_msg("md5_created= * MD5 Created."); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 521 | else |
| 522 | return -1; |
| 523 | else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 524 | gui_err("md5_error= * MD5 Error!"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 525 | } else { |
| 526 | char filename[512]; |
| 527 | int index = 0; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 528 | string strfn; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 529 | sprintf(filename, "%s%03i", Full_File.c_str(), index); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 530 | strfn = filename; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 531 | while (index < 1000) { |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 532 | md5sum.setfn(filename); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 533 | if (TWFunc::Path_Exists(filename)) { |
| 534 | if (md5sum.computeMD5() == 0) { |
| 535 | if (md5sum.write_md5digest() != 0) |
| 536 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 537 | gui_err("md5_error= * MD5 Error!"); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 538 | return false; |
| 539 | } |
| 540 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 541 | gui_err("md5_compute_error= * Error computing MD5."); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 542 | return false; |
| 543 | } |
| 544 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 545 | index++; |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 546 | sprintf(filename, "%s%03i", Full_File.c_str(), index); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 547 | strfn = filename; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 548 | } |
| 549 | if (index == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 550 | LOGERR("Backup file: '%s' not found!\n", filename); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 551 | return false; |
| 552 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 553 | gui_msg("md5_created= * MD5 Created."); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 554 | } |
| 555 | return true; |
| 556 | } |
| 557 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 558 | |
| 559 | bool TWPartitionManager::Backup_Partition(PartitionSettings *part_settings) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 560 | time_t start, stop; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 561 | int use_compression, adb_control_bu_fd; |
| 562 | string backup_log = part_settings->Backup_Folder + "/recovery.log"; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 563 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 564 | if (part_settings->Part == NULL) |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 565 | return true; |
| 566 | |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 567 | DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression); |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 568 | |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 569 | TWFunc::SetPerformanceMode(true); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 570 | time(&start); |
| 571 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 572 | if (part_settings->Part->Backup(part_settings, &tar_fork_pid)) { |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 573 | bool md5Success = false; |
bigbiff bigbiff | 8fd4b09 | 2016-08-30 20:48:53 -0400 | [diff] [blame] | 574 | if (part_settings->adbbackup) { |
| 575 | md5Success = true; |
| 576 | } |
| 577 | else |
| 578 | md5Success = Make_MD5(part_settings); |
| 579 | |
| 580 | TWFunc::SetPerformanceMode(false); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 581 | if (part_settings->Part->Has_SubPartition) { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 582 | std::vector<TWPartition*>::iterator subpart; |
bigbiff bigbiff | 8fd4b09 | 2016-08-30 20:48:53 -0400 | [diff] [blame] | 583 | TWPartition *parentPart = part_settings->Part; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 584 | |
| 585 | for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) { |
bigbiff bigbiff | 8fd4b09 | 2016-08-30 20:48:53 -0400 | [diff] [blame] | 586 | if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == parentPart->Mount_Point) { |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 587 | part_settings->Part = *subpart; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 588 | if (!(*subpart)->Backup(part_settings, &tar_fork_pid)) { |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 589 | TWFunc::SetPerformanceMode(false); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 590 | Clean_Backup_Folder(part_settings->Backup_Folder); |
bigbiff | bf1d672 | 2015-02-14 16:25:59 -0500 | [diff] [blame] | 591 | TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644); |
| 592 | tw_set_default_metadata(backup_log.c_str()); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 593 | return false; |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 594 | } |
Dees_Troy | 2727b99 | 2013-08-14 20:09:30 +0000 | [diff] [blame] | 595 | sync(); |
| 596 | sync(); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 597 | if (!part_settings->adbbackup) { |
bigbiff bigbiff | a481d36 | 2016-08-08 20:36:10 -0400 | [diff] [blame] | 598 | if (!Make_MD5(part_settings)) { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 599 | TWFunc::SetPerformanceMode(false); |
| 600 | return false; |
| 601 | } |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 602 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 606 | time(&stop); |
that | 203bcc9 | 2015-05-09 17:27:33 +0200 | [diff] [blame] | 607 | int backup_time = (int) difftime(stop, start); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 608 | LOGINFO("Partition Backup time: %d\n", backup_time); |
bigbiff bigbiff | 8fd4b09 | 2016-08-30 20:48:53 -0400 | [diff] [blame] | 609 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 610 | if (part_settings->Part->Backup_Method == BM_FILES) { |
| 611 | part_settings->file_time += backup_time; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 612 | } else { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 613 | part_settings->img_time += backup_time; |
| 614 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 615 | } |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 616 | |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 617 | return md5Success; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 618 | } else { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 619 | Clean_Backup_Folder(part_settings->Backup_Folder); |
bigbiff | bf1d672 | 2015-02-14 16:25:59 -0500 | [diff] [blame] | 620 | TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644); |
| 621 | tw_set_default_metadata(backup_log.c_str()); |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 622 | TWFunc::SetPerformanceMode(false); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 623 | return false; |
| 624 | } |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 625 | return 0; |
| 626 | } |
| 627 | |
bigbiff | bf1d672 | 2015-02-14 16:25:59 -0500 | [diff] [blame] | 628 | void TWPartitionManager::Clean_Backup_Folder(string Backup_Folder) { |
| 629 | DIR *d = opendir(Backup_Folder.c_str()); |
| 630 | struct dirent *p; |
| 631 | int r; |
| 632 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 633 | gui_msg("backup_clean=Backup Failed. Cleaning Backup Folder."); |
bigbiff | bf1d672 | 2015-02-14 16:25:59 -0500 | [diff] [blame] | 634 | |
| 635 | if (d == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 636 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Backup_Folder)(strerror(errno))); |
bigbiff | bf1d672 | 2015-02-14 16:25:59 -0500 | [diff] [blame] | 637 | return; |
| 638 | } |
| 639 | |
Matt Mower | 2b18a53 | 2015-02-20 16:58:05 -0600 | [diff] [blame] | 640 | while ((p = readdir(d))) { |
bigbiff | bf1d672 | 2015-02-14 16:25:59 -0500 | [diff] [blame] | 641 | if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) |
| 642 | continue; |
| 643 | |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 644 | string path = Backup_Folder + "/" + p->d_name; |
bigbiff | bf1d672 | 2015-02-14 16:25:59 -0500 | [diff] [blame] | 645 | |
| 646 | size_t dot = path.find_last_of(".") + 1; |
| 647 | if (path.substr(dot) == "win" || path.substr(dot) == "md5" || path.substr(dot) == "info") { |
| 648 | r = unlink(path.c_str()); |
| 649 | if (r != 0) { |
| 650 | LOGINFO("Unable to unlink '%s: %s'\n", path.c_str(), strerror(errno)); |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | closedir(d); |
| 655 | } |
| 656 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 657 | int TWPartitionManager::Check_Backup_Cancel() { |
| 658 | return stop_backup.get_value(); |
| 659 | } |
| 660 | |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 661 | int TWPartitionManager::Cancel_Backup() { |
| 662 | string Backup_Folder, Backup_Name, Full_Backup_Path; |
| 663 | |
| 664 | stop_backup.set_value(1); |
| 665 | |
| 666 | if (tar_fork_pid != 0) { |
| 667 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
| 668 | DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder); |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 669 | Full_Backup_Path = Backup_Folder + "/" + Backup_Name; |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 670 | LOGINFO("Killing pid: %d\n", tar_fork_pid); |
| 671 | kill(tar_fork_pid, SIGUSR2); |
| 672 | while (kill(tar_fork_pid, 0) == 0) { |
| 673 | usleep(1000); |
| 674 | } |
| 675 | LOGINFO("Backup_Run stopped and returning false, backup cancelled.\n"); |
| 676 | LOGINFO("Removing directory %s\n", Full_Backup_Path.c_str()); |
| 677 | TWFunc::removeDir(Full_Backup_Path, false); |
| 678 | tar_fork_pid = 0; |
| 679 | } |
| 680 | |
| 681 | return 0; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 682 | } |
| 683 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 684 | int TWPartitionManager::Run_Backup(bool adbbackup) { |
| 685 | PartitionSettings part_settings; |
| 686 | int check, partition_count = 0, disable_free_space_check = 0, do_md5 = 0; |
| 687 | int gui_adb_backup; |
| 688 | string Backup_Name, Backup_List, backup_path; |
| 689 | unsigned long long total_bytes = 0, free_space = 0, subpart_size; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 690 | TWPartition* storage = NULL; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 691 | std::vector<TWPartition*>::iterator subpart; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 692 | struct tm *t; |
| 693 | time_t start, stop, seconds, total_start, total_stop; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 694 | size_t start_pos = 0, end_pos = 0; |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 695 | stop_backup.set_value(0); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 696 | seconds = time(0); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 697 | t = localtime(&seconds); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 698 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 699 | part_settings.img_bytes_remaining = 0; |
| 700 | part_settings.file_bytes_remaining = 0; |
| 701 | part_settings.img_time = 0; |
| 702 | part_settings.file_time = 0; |
| 703 | part_settings.img_bytes = 0; |
| 704 | part_settings.file_bytes = 0; |
| 705 | part_settings.PM_Method = PM_BACKUP; |
| 706 | |
| 707 | DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup); |
| 708 | if (gui_adb_backup == true) |
| 709 | adbbackup = true; |
| 710 | |
| 711 | part_settings.adbbackup = adbbackup; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 712 | time(&total_start); |
| 713 | |
| 714 | Update_System_Details(); |
| 715 | |
| 716 | if (!Mount_Current_Storage(true)) |
| 717 | return false; |
| 718 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 719 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 720 | DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5); |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 721 | if (do_md5 == 0) |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 722 | part_settings.generate_md5 = true; |
Dees_Troy | c5865ab | 2012-09-24 15:08:04 -0400 | [diff] [blame] | 723 | else |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 724 | part_settings.generate_md5 = false; |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 725 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 726 | DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, part_settings.Backup_Folder); |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 727 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
| 728 | if (Backup_Name == gui_lookup("curr_date", "(Current Date)")) { |
| 729 | Backup_Name = TWFunc::Get_Current_Date(); |
| 730 | } else if (Backup_Name == gui_lookup("auto_generate", "(Auto Generate)") || Backup_Name == "0" || Backup_Name.empty()) { |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 731 | TWFunc::Auto_Generate_Backup_Name(); |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 732 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 733 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 734 | |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 735 | LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str()); |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 736 | part_settings.Backup_Folder = part_settings.Backup_Folder + "/" + Backup_Name; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 737 | |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 738 | LOGINFO("Backup_Folder is: '%s'\n", part_settings.Backup_Folder.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 739 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 740 | LOGINFO("Calculating backup details...\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 741 | DataManager::GetValue("tw_backup_list", Backup_List); |
| 742 | if (!Backup_List.empty()) { |
| 743 | end_pos = Backup_List.find(";", start_pos); |
| 744 | while (end_pos != string::npos && start_pos < Backup_List.size()) { |
| 745 | backup_path = Backup_List.substr(start_pos, end_pos - start_pos); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 746 | part_settings.Part = Find_Partition_By_Path(backup_path); |
| 747 | if (part_settings.Part != NULL) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 748 | partition_count++; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 749 | if (part_settings.Part->Backup_Method == BM_FILES) |
| 750 | part_settings.file_bytes += part_settings.Part->Backup_Size; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 751 | else |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 752 | part_settings.img_bytes += part_settings.Part->Backup_Size; |
| 753 | if (part_settings.Part->Has_SubPartition) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 754 | std::vector<TWPartition*>::iterator subpart; |
| 755 | |
| 756 | for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 757 | if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == part_settings.Part->Mount_Point) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 758 | partition_count++; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 759 | if ((*subpart)->Backup_Method == BM_FILES) |
| 760 | part_settings.file_bytes += (*subpart)->Backup_Size; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 761 | else |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 762 | part_settings.img_bytes += (*subpart)->Backup_Size; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 763 | } |
| 764 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 765 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 766 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 767 | gui_msg(Msg(msg::kError, "unable_to_locate_partition=Unable to locate '{1}' partition for backup calculations.")(backup_path)); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 768 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 769 | start_pos = end_pos + 1; |
| 770 | end_pos = Backup_List.find(";", start_pos); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 771 | } |
| 772 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 773 | |
| 774 | if (partition_count == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 775 | gui_msg("no_partition_selected=No partitions selected for backup."); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 776 | return false; |
| 777 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 778 | if (adbbackup) { |
| 779 | if (twadbbu::Write_ADB_Stream_Header(partition_count) == false) { |
| 780 | return false; |
| 781 | } |
| 782 | } |
| 783 | total_bytes = part_settings.file_bytes + part_settings.img_bytes; |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 784 | ProgressTracking progress(total_bytes); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 785 | part_settings.progress = &progress; |
| 786 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 787 | gui_msg(Msg("total_partitions_backup= * Total number of partitions to back up: {1}")(partition_count)); |
| 788 | gui_msg(Msg("total_backup_size= * Total size of all data: {1}MB")(total_bytes / 1024 / 1024)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 789 | storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath()); |
| 790 | if (storage != NULL) { |
| 791 | free_space = storage->Free; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 792 | gui_msg(Msg("available_space= * Available space: {1}MB")(free_space / 1024 / 1024)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 793 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 794 | gui_err("unable_locate_storage=Unable to locate storage device."); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 795 | return false; |
| 796 | } |
bigbiff | bf1d672 | 2015-02-14 16:25:59 -0500 | [diff] [blame] | 797 | |
Matt Mower | bfccfb8 | 2016-04-25 23:22:31 -0500 | [diff] [blame] | 798 | DataManager::GetValue(TW_DISABLE_FREE_SPACE_VAR, disable_free_space_check); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 799 | |
| 800 | if (adbbackup) |
| 801 | disable_free_space_check = true; |
| 802 | |
bigbiff | bf1d672 | 2015-02-14 16:25:59 -0500 | [diff] [blame] | 803 | if (!disable_free_space_check) { |
| 804 | if (free_space - (32 * 1024 * 1024) < total_bytes) { |
| 805 | // We require an extra 32MB just in case |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 806 | gui_err("no_space=Not enough free space on storage."); |
bigbiff | bf1d672 | 2015-02-14 16:25:59 -0500 | [diff] [blame] | 807 | return false; |
| 808 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 809 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 810 | part_settings.img_bytes_remaining = part_settings.img_bytes; |
| 811 | part_settings.file_bytes_remaining = part_settings.file_bytes; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 812 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 813 | gui_msg("backup_started=[BACKUP STARTED]"); |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 814 | gui_msg(Msg("backup_folder= * Backup Folder: {1}")(part_settings.Backup_Folder)); |
| 815 | if (!TWFunc::Recursive_Mkdir(part_settings.Backup_Folder)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 816 | gui_err("fail_backup_folder=Failed to make backup folder."); |
Dees_Troy | d4b22b0 | 2013-01-18 17:17:58 +0000 | [diff] [blame] | 817 | return false; |
| 818 | } |
| 819 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 820 | DataManager::SetProgress(0.0); |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 821 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 822 | start_pos = 0; |
| 823 | end_pos = Backup_List.find(";", start_pos); |
| 824 | while (end_pos != string::npos && start_pos < Backup_List.size()) { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 825 | if (stop_backup.get_value() != 0) |
| 826 | return -1; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 827 | backup_path = Backup_List.substr(start_pos, end_pos - start_pos); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 828 | part_settings.Part = Find_Partition_By_Path(backup_path); |
| 829 | if (part_settings.Part != NULL) { |
| 830 | if (!Backup_Partition(&part_settings)) |
| 831 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 832 | return false; |
| 833 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 834 | gui_msg(Msg(msg::kError, "unable_to_locate_partition=Unable to locate '{1}' partition for backup calculations.")(backup_path)); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 835 | } |
| 836 | start_pos = end_pos + 1; |
| 837 | end_pos = Backup_List.find(";", start_pos); |
| 838 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 839 | |
| 840 | // Average BPS |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 841 | if (part_settings.img_time == 0) |
| 842 | part_settings.img_time = 1; |
| 843 | if (part_settings.file_time == 0) |
| 844 | part_settings.file_time = 1; |
| 845 | int img_bps = (int)part_settings.img_bytes / (int)part_settings.img_time; |
| 846 | unsigned long long file_bps = part_settings.file_bytes / (int)part_settings.file_time; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 847 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 848 | if (part_settings.file_bytes != 0) |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 849 | gui_msg(Msg("avg_backup_fs=Average backup rate for file systems: {1} MB/sec")(file_bps / (1024 * 1024))); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 850 | if (part_settings.img_bytes != 0) |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 851 | gui_msg(Msg("avg_backup_img=Average backup rate for imaged drives: {1} MB/sec")(img_bps / (1024 * 1024))); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 852 | |
| 853 | time(&total_stop); |
| 854 | int total_time = (int) difftime(total_stop, total_start); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 855 | |
| 856 | uint64_t actual_backup_size; |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 857 | if (!adbbackup) { |
| 858 | TWExclude twe; |
| 859 | actual_backup_size = twe.Get_Folder_Size(part_settings.Backup_Folder); |
| 860 | } else |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 861 | actual_backup_size = part_settings.file_bytes + part_settings.img_bytes; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 862 | actual_backup_size /= (1024LLU * 1024LLU); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 863 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 864 | int prev_img_bps = 0, use_compression = 0; |
| 865 | unsigned long long prev_file_bps = 0; |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 866 | img_bps += (prev_img_bps * 4); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 867 | img_bps /= 5; |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 868 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 869 | DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression); |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 870 | if (use_compression) |
| 871 | DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 872 | else |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 873 | DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps); |
| 874 | file_bps += (prev_file_bps * 4); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 875 | file_bps /= 5; |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 876 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 877 | DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps); |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 878 | if (use_compression) |
| 879 | DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps); |
| 880 | else |
| 881 | DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps); |
| 882 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 883 | gui_msg(Msg("total_backed_size=[{1} MB TOTAL BACKED UP]")(actual_backup_size)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 884 | Update_System_Details(); |
Dees_Troy | d0384ef | 2012-10-12 12:15:42 -0400 | [diff] [blame] | 885 | UnMount_Main_Partitions(); |
Ethan Yonker | 56db1c4 | 2015-12-20 22:49:00 -0600 | [diff] [blame] | 886 | gui_msg(Msg(msg::kHighlight, "backup_completed=[BACKUP COMPLETED IN {1} SECONDS]")(total_time)); // the end |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 887 | string backup_log = part_settings.Backup_Folder + "/recovery.log"; |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 888 | TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644); |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 889 | tw_set_default_metadata(backup_log.c_str()); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 890 | |
| 891 | if (part_settings.adbbackup) { |
| 892 | if (twadbbu::Write_ADB_Stream_Trailer() == false) { |
| 893 | return false; |
| 894 | } |
| 895 | } |
| 896 | part_settings.adbbackup = false; |
| 897 | DataManager::SetValue("tw_enable_adb_backup", 0); |
| 898 | |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 899 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 900 | } |
| 901 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 902 | bool TWPartitionManager::Restore_Partition(PartitionSettings *part_settings) { |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 903 | time_t Start, Stop; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 904 | |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 905 | TWFunc::SetPerformanceMode(true); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 906 | |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 907 | time(&Start); |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 908 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 909 | |
| 910 | if (!part_settings->Part->Restore(part_settings)) { |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 911 | TWFunc::SetPerformanceMode(false); |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 912 | return false; |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 913 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 914 | if (part_settings->Part->Has_SubPartition) { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 915 | std::vector<TWPartition*>::iterator subpart; |
bigbiff bigbiff | 8fd4b09 | 2016-08-30 20:48:53 -0400 | [diff] [blame] | 916 | TWPartition *parentPart = part_settings->Part; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 917 | |
| 918 | for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) { |
bigbiff bigbiff | 8fd4b09 | 2016-08-30 20:48:53 -0400 | [diff] [blame] | 919 | part_settings->Part = *subpart; |
| 920 | if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == parentPart->Mount_Point) { |
| 921 | part_settings->Part = (*subpart); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 922 | if (!(*subpart)->Restore(part_settings)) { |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 923 | TWFunc::SetPerformanceMode(false); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 924 | return false; |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 925 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | } |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 929 | time(&Stop); |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 930 | TWFunc::SetPerformanceMode(false); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 931 | gui_msg(Msg("restore_part_done=[{1} done ({2} seconds)]")(part_settings->Part->Backup_Display_Name)((int)difftime(Stop, Start))); |
| 932 | |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 933 | return true; |
| 934 | } |
| 935 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 936 | int TWPartitionManager::Run_Restore(const string& Restore_Name) { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 937 | PartitionSettings part_settings; |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 938 | int check_md5, check, partition_count = 0; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 939 | TWPartition* restore_part = NULL; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 940 | |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 941 | time_t rStart, rStop; |
| 942 | time(&rStart); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 943 | string Restore_List, restore_path; |
| 944 | size_t start_pos = 0, end_pos; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 945 | |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 946 | part_settings.Backup_Folder = Restore_Name; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 947 | part_settings.Part = NULL; |
| 948 | part_settings.partition_count = 0; |
| 949 | part_settings.total_restore_size = 0; |
| 950 | part_settings.adbbackup = false; |
| 951 | part_settings.PM_Method = PM_RESTORE; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 952 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 953 | gui_msg("restore_started=[RESTORE STARTED]"); |
| 954 | gui_msg(Msg("restore_folder=Restore folder: '{1}'")(Restore_Name)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 955 | |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 956 | if (!Mount_Current_Storage(true)) |
| 957 | return false; |
| 958 | |
| 959 | DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 960 | if (check_md5 > 0) { |
| 961 | // Check MD5 files first before restoring to ensure that all of them match before starting a restore |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 962 | TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, gui_parse_text("{@verifying_md5}")); |
| 963 | gui_msg("verifying_md5=Verifying MD5"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 964 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 965 | gui_msg("skip_md5=Skipping MD5 check based on user setting."); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 966 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 967 | gui_msg("calc_restore=Calculating restore details..."); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 968 | DataManager::GetValue("tw_restore_selected", Restore_List); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 969 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 970 | if (!Restore_List.empty()) { |
| 971 | end_pos = Restore_List.find(";", start_pos); |
| 972 | while (end_pos != string::npos && start_pos < Restore_List.size()) { |
| 973 | restore_path = Restore_List.substr(start_pos, end_pos - start_pos); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 974 | part_settings.Part = Find_Partition_By_Path(restore_path); |
| 975 | if (part_settings.Part != NULL) { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 976 | if (part_settings.Part->Mount_Read_Only) { |
| 977 | gui_msg(Msg(msg::kError, "restore_read_only=Cannot restore {1} -- mounted read only.")(part_settings.Part->Backup_Display_Name)); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 978 | return false; |
| 979 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 980 | |
bigbiff bigbiff | 8fd4b09 | 2016-08-30 20:48:53 -0400 | [diff] [blame] | 981 | if (check_md5 > 0 && !part_settings.Part->Check_MD5(&part_settings)) |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 982 | return false; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 983 | part_settings.partition_count++; |
| 984 | part_settings.total_restore_size += part_settings.Part->Get_Restore_Size(&part_settings); |
| 985 | if (part_settings.Part->Has_SubPartition) { |
bigbiff bigbiff | 8fd4b09 | 2016-08-30 20:48:53 -0400 | [diff] [blame] | 986 | TWPartition *parentPart = part_settings.Part; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 987 | std::vector<TWPartition*>::iterator subpart; |
| 988 | |
| 989 | for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) { |
bigbiff bigbiff | 8fd4b09 | 2016-08-30 20:48:53 -0400 | [diff] [blame] | 990 | part_settings.Part = *subpart; |
| 991 | if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == parentPart->Mount_Point) { |
| 992 | if (check_md5 > 0 && !(*subpart)->Check_MD5(&part_settings)) |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 993 | return false; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 994 | part_settings.total_restore_size += (*subpart)->Get_Restore_Size(&part_settings); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | } |
| 998 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 999 | gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path)); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1000 | } |
| 1001 | start_pos = end_pos + 1; |
| 1002 | end_pos = Restore_List.find(";", start_pos); |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1003 | } |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1004 | } |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1005 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1006 | if (part_settings.partition_count == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1007 | gui_err("no_part_restore=No partitions selected for restore."); |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1008 | return false; |
| 1009 | } |
| 1010 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1011 | gui_msg(Msg("restore_part_count=Restoring {1} partitions...")(part_settings.partition_count)); |
| 1012 | gui_msg(Msg("total_restore_size=Total restore size is {1}MB")(part_settings.total_restore_size / 1048576)); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1013 | DataManager::SetProgress(0.0); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1014 | ProgressTracking progress(part_settings.total_restore_size); |
| 1015 | part_settings.progress = &progress; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1016 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1017 | start_pos = 0; |
| 1018 | if (!Restore_List.empty()) { |
| 1019 | end_pos = Restore_List.find(";", start_pos); |
| 1020 | while (end_pos != string::npos && start_pos < Restore_List.size()) { |
| 1021 | restore_path = Restore_List.substr(start_pos, end_pos - start_pos); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1022 | |
| 1023 | part_settings.Part = Find_Partition_By_Path(restore_path); |
| 1024 | if (part_settings.Part != NULL) { |
| 1025 | part_settings.partition_count++; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1026 | if (!Restore_Partition(&part_settings)) |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1027 | return false; |
| 1028 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1029 | gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path)); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1030 | } |
| 1031 | start_pos = end_pos + 1; |
| 1032 | end_pos = Restore_List.find(";", start_pos); |
| 1033 | } |
| 1034 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1035 | TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, gui_parse_text("{@updating_system_details}")); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1036 | Update_System_Details(); |
Dees_Troy | d0384ef | 2012-10-12 12:15:42 -0400 | [diff] [blame] | 1037 | UnMount_Main_Partitions(); |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1038 | time(&rStop); |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 1039 | gui_msg(Msg(msg::kHighlight, "restore_completed=[RESTORE COMPLETED IN {1} SECONDS]")((int)difftime(rStop,rStart))); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1040 | DataManager::SetValue("tw_file_progress", ""); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1041 | |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1042 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | void TWPartitionManager::Set_Restore_Files(string Restore_Name) { |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1046 | // Start with the default values |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1047 | string Restore_List; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1048 | bool get_date = true, check_encryption = true; |
| 1049 | |
| 1050 | DataManager::SetValue("tw_restore_encrypted", 0); |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1051 | |
| 1052 | DIR* d; |
| 1053 | d = opendir(Restore_Name.c_str()); |
| 1054 | if (d == NULL) |
| 1055 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1056 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Restore_Name)(strerror(errno))); |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1057 | return; |
| 1058 | } |
| 1059 | |
| 1060 | struct dirent* de; |
| 1061 | while ((de = readdir(d)) != NULL) |
| 1062 | { |
| 1063 | // Strip off three components |
| 1064 | char str[256]; |
| 1065 | char* label; |
| 1066 | char* fstype = NULL; |
| 1067 | char* extn = NULL; |
| 1068 | char* ptr; |
| 1069 | |
| 1070 | strcpy(str, de->d_name); |
| 1071 | if (strlen(str) <= 2) |
| 1072 | continue; |
| 1073 | |
| 1074 | if (get_date) { |
| 1075 | char file_path[255]; |
| 1076 | struct stat st; |
| 1077 | |
| 1078 | strcpy(file_path, Restore_Name.c_str()); |
| 1079 | strcat(file_path, "/"); |
| 1080 | strcat(file_path, str); |
| 1081 | stat(file_path, &st); |
| 1082 | string backup_date = ctime((const time_t*)(&st.st_mtime)); |
| 1083 | DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date); |
| 1084 | get_date = false; |
| 1085 | } |
| 1086 | |
| 1087 | label = str; |
| 1088 | ptr = label; |
| 1089 | while (*ptr && *ptr != '.') ptr++; |
| 1090 | if (*ptr == '.') |
| 1091 | { |
| 1092 | *ptr = 0x00; |
| 1093 | ptr++; |
| 1094 | fstype = ptr; |
| 1095 | } |
| 1096 | while (*ptr && *ptr != '.') ptr++; |
| 1097 | if (*ptr == '.') |
| 1098 | { |
| 1099 | *ptr = 0x00; |
| 1100 | ptr++; |
| 1101 | extn = ptr; |
| 1102 | } |
| 1103 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1104 | if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1105 | int extnlength = strlen(extn); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1106 | if (extnlength != 3 && extnlength != 6) continue; |
| 1107 | if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue; |
| 1108 | //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue; |
| 1109 | |
| 1110 | if (check_encryption) { |
| 1111 | string filename = Restore_Name + "/"; |
| 1112 | filename += de->d_name; |
| 1113 | if (TWFunc::Get_File_Type(filename) == 2) { |
| 1114 | LOGINFO("'%s' is encrypted\n", filename.c_str()); |
| 1115 | DataManager::SetValue("tw_restore_encrypted", 1); |
| 1116 | } |
| 1117 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1118 | if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1119 | |
| 1120 | TWPartition* Part = Find_Partition_By_Path(label); |
| 1121 | if (Part == NULL) |
| 1122 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1123 | gui_msg(Msg(msg::kError, "unable_locate_part_backup_name=Unable to locate partition by backup name: '{1}'")(label)); |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1124 | continue; |
| 1125 | } |
| 1126 | |
| 1127 | Part->Backup_FileName = de->d_name; |
| 1128 | if (strlen(extn) > 3) { |
| 1129 | Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3); |
| 1130 | } |
| 1131 | |
James Christopher Adduono | fddbdfa | 2016-02-29 15:08:11 -0500 | [diff] [blame] | 1132 | if (!Part->Is_SubPartition) |
| 1133 | Restore_List += Part->Backup_Path + ";"; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1134 | } |
| 1135 | closedir(d); |
| 1136 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1137 | // Set the final value |
| 1138 | DataManager::SetValue("tw_restore_list", Restore_List); |
| 1139 | DataManager::SetValue("tw_restore_selected", Restore_List); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1140 | return; |
| 1141 | } |
| 1142 | |
| 1143 | int TWPartitionManager::Wipe_By_Path(string Path) { |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1144 | std::vector<TWPartition*>::iterator iter; |
| 1145 | int ret = false; |
| 1146 | bool found = false; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1147 | string Local_Path = TWFunc::Get_Root_Path(Path); |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1148 | |
| 1149 | // Iterate through all partitions |
| 1150 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 1151 | if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) { |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1152 | if (Path == "/and-sec") |
| 1153 | ret = (*iter)->Wipe_AndSec(); |
| 1154 | else |
| 1155 | ret = (*iter)->Wipe(); |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1156 | found = true; |
| 1157 | } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) { |
| 1158 | (*iter)->Wipe(); |
| 1159 | } |
| 1160 | } |
| 1161 | if (found) { |
| 1162 | return ret; |
| 1163 | } else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1164 | gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path)); |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1165 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1166 | } |
| 1167 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1168 | int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) { |
| 1169 | std::vector<TWPartition*>::iterator iter; |
| 1170 | int ret = false; |
| 1171 | bool found = false; |
| 1172 | string Local_Path = TWFunc::Get_Root_Path(Path); |
| 1173 | |
| 1174 | // Iterate through all partitions |
| 1175 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 1176 | if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) { |
| 1177 | if (Path == "/and-sec") |
| 1178 | ret = (*iter)->Wipe_AndSec(); |
| 1179 | else |
| 1180 | ret = (*iter)->Wipe(New_File_System); |
| 1181 | found = true; |
| 1182 | } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) { |
| 1183 | (*iter)->Wipe(New_File_System); |
| 1184 | } |
| 1185 | } |
| 1186 | if (found) { |
| 1187 | return ret; |
| 1188 | } else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1189 | gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path)); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1190 | return false; |
| 1191 | } |
| 1192 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1193 | int TWPartitionManager::Factory_Reset(void) { |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1194 | std::vector<TWPartition*>::iterator iter; |
| 1195 | int ret = true; |
| 1196 | |
| 1197 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1198 | if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) { |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 1199 | #ifdef TW_OEM_BUILD |
| 1200 | if ((*iter)->Mount_Point == "/data") { |
| 1201 | if (!(*iter)->Wipe_Encryption()) |
| 1202 | ret = false; |
| 1203 | } else { |
| 1204 | #endif |
| 1205 | if (!(*iter)->Wipe()) |
| 1206 | ret = false; |
| 1207 | #ifdef TW_OEM_BUILD |
| 1208 | } |
| 1209 | #endif |
Dees_Troy | 094207a | 2012-09-26 12:00:39 -0400 | [diff] [blame] | 1210 | } else if ((*iter)->Has_Android_Secure) { |
| 1211 | if (!(*iter)->Wipe_AndSec()) |
| 1212 | ret = false; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1213 | } |
| 1214 | } |
Ethan Yonker | 9fa76ba | 2016-06-30 14:05:43 -0500 | [diff] [blame] | 1215 | TWFunc::check_and_run_script("/sbin/factoryreset.sh", "Factory Reset Script"); |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1216 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1217 | } |
| 1218 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1219 | int TWPartitionManager::Wipe_Dalvik_Cache(void) { |
| 1220 | struct stat st; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1221 | vector <string> dir; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1222 | |
| 1223 | if (!Mount_By_Path("/data", true)) |
| 1224 | return false; |
| 1225 | |
| 1226 | if (!Mount_By_Path("/cache", true)) |
| 1227 | return false; |
| 1228 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1229 | dir.push_back("/data/dalvik-cache"); |
| 1230 | dir.push_back("/cache/dalvik-cache"); |
| 1231 | dir.push_back("/cache/dc"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1232 | gui_msg("wiping_dalvik=Wiping Dalvik Cache Directories..."); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1233 | for (unsigned i = 0; i < dir.size(); ++i) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1234 | if (stat(dir.at(i).c_str(), &st) == 0) { |
| 1235 | TWFunc::removeDir(dir.at(i), false); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1236 | gui_msg(Msg("cleaned=Cleaned: {1}...")(dir.at(i))); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1237 | } |
| 1238 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1239 | TWPartition* sdext = Find_Partition_By_Path("/sd-ext"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1240 | if (sdext && sdext->Is_Present && sdext->Mount(false)) |
| 1241 | { |
| 1242 | if (stat("/sd-ext/dalvik-cache", &st) == 0) |
| 1243 | { |
| 1244 | TWFunc::removeDir("/sd-ext/dalvik-cache", false); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1245 | gui_msg(Msg("cleaned=Cleaned: {1}...")("/sd-ext/dalvik-cache")); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1246 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1247 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1248 | gui_msg("dalvik_done=-- Dalvik Cache Directories Wipe Complete!"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1249 | return true; |
| 1250 | } |
| 1251 | |
| 1252 | int TWPartitionManager::Wipe_Rotate_Data(void) { |
| 1253 | if (!Mount_By_Path("/data", true)) |
| 1254 | return false; |
| 1255 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1256 | unlink("/data/misc/akmd*"); |
| 1257 | unlink("/data/misc/rild*"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1258 | gui_print("Rotation data wiped.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1259 | return true; |
| 1260 | } |
| 1261 | |
| 1262 | int TWPartitionManager::Wipe_Battery_Stats(void) { |
| 1263 | struct stat st; |
| 1264 | |
| 1265 | if (!Mount_By_Path("/data", true)) |
| 1266 | return false; |
| 1267 | |
| 1268 | if (0 != stat("/data/system/batterystats.bin", &st)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1269 | gui_print("No Battery Stats Found. No Need To Wipe.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1270 | } else { |
| 1271 | remove("/data/system/batterystats.bin"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1272 | gui_print("Cleared battery stats.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1273 | } |
| 1274 | return true; |
| 1275 | } |
| 1276 | |
Dees_Troy | 2ff5a8d | 2012-09-26 14:53:02 -0400 | [diff] [blame] | 1277 | int TWPartitionManager::Wipe_Android_Secure(void) { |
| 1278 | std::vector<TWPartition*>::iterator iter; |
| 1279 | int ret = false; |
| 1280 | bool found = false; |
| 1281 | |
| 1282 | // Iterate through all partitions |
| 1283 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 1284 | if ((*iter)->Has_Android_Secure) { |
| 1285 | ret = (*iter)->Wipe_AndSec(); |
| 1286 | found = true; |
| 1287 | } |
| 1288 | } |
| 1289 | if (found) { |
| 1290 | return ret; |
| 1291 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1292 | gui_err("no_andsec=No android secure partitions found."); |
Dees_Troy | 2ff5a8d | 2012-09-26 14:53:02 -0400 | [diff] [blame] | 1293 | } |
| 1294 | return false; |
| 1295 | } |
| 1296 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1297 | int TWPartitionManager::Format_Data(void) { |
| 1298 | TWPartition* dat = Find_Partition_By_Path("/data"); |
| 1299 | |
| 1300 | if (dat != NULL) { |
| 1301 | if (!dat->UnMount(true)) |
| 1302 | return false; |
| 1303 | |
| 1304 | return dat->Wipe_Encryption(); |
| 1305 | } else { |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 1306 | gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/data")); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1307 | return false; |
| 1308 | } |
| 1309 | return false; |
| 1310 | } |
| 1311 | |
| 1312 | int TWPartitionManager::Wipe_Media_From_Data(void) { |
| 1313 | TWPartition* dat = Find_Partition_By_Path("/data"); |
| 1314 | |
| 1315 | if (dat != NULL) { |
| 1316 | if (!dat->Has_Data_Media) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1317 | LOGERR("This device does not have /data/media\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1318 | return false; |
| 1319 | } |
| 1320 | if (!dat->Mount(true)) |
| 1321 | return false; |
| 1322 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1323 | gui_msg("wiping_datamedia=Wiping internal storage -- /data/media..."); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1324 | Remove_MTP_Storage(dat->MTP_Storage_ID); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1325 | TWFunc::removeDir("/data/media", false); |
xiaolu | 9416f4f | 2015-06-04 08:22:23 +0800 | [diff] [blame] | 1326 | dat->Recreate_Media_Folder(); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1327 | Add_MTP_Storage(dat->MTP_Storage_ID); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1328 | return true; |
| 1329 | } else { |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 1330 | gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/data")); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1331 | return false; |
| 1332 | } |
| 1333 | return false; |
| 1334 | } |
| 1335 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1336 | int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) { |
| 1337 | std::vector<TWPartition*>::iterator iter; |
| 1338 | int ret = false; |
| 1339 | bool found = false; |
| 1340 | string Local_Path = TWFunc::Get_Root_Path(Path); |
| 1341 | |
| 1342 | if (Local_Path == "/tmp" || Local_Path == "/") |
| 1343 | return true; |
| 1344 | |
| 1345 | // Iterate through all partitions |
| 1346 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 1347 | if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) { |
| 1348 | ret = (*iter)->Repair(); |
| 1349 | found = true; |
| 1350 | } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) { |
| 1351 | (*iter)->Repair(); |
| 1352 | } |
| 1353 | } |
| 1354 | if (found) { |
| 1355 | return ret; |
| 1356 | } else if (Display_Error) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1357 | gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path)); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1358 | } else { |
| 1359 | LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str()); |
| 1360 | } |
| 1361 | return false; |
| 1362 | } |
| 1363 | |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1364 | int TWPartitionManager::Resize_By_Path(string Path, bool Display_Error) { |
| 1365 | std::vector<TWPartition*>::iterator iter; |
| 1366 | int ret = false; |
| 1367 | bool found = false; |
| 1368 | string Local_Path = TWFunc::Get_Root_Path(Path); |
| 1369 | |
| 1370 | if (Local_Path == "/tmp" || Local_Path == "/") |
| 1371 | return true; |
| 1372 | |
| 1373 | // Iterate through all partitions |
| 1374 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 1375 | if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) { |
| 1376 | ret = (*iter)->Resize(); |
| 1377 | found = true; |
| 1378 | } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) { |
| 1379 | (*iter)->Resize(); |
| 1380 | } |
| 1381 | } |
| 1382 | if (found) { |
| 1383 | return ret; |
| 1384 | } else if (Display_Error) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1385 | gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path)); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1386 | } else { |
| 1387 | LOGINFO("Resize: Unable to find partition for path '%s'\n", Local_Path.c_str()); |
| 1388 | } |
| 1389 | return false; |
| 1390 | } |
| 1391 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1392 | void TWPartitionManager::Update_System_Details(void) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1393 | std::vector<TWPartition*>::iterator iter; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1394 | int data_size = 0; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1395 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1396 | gui_msg("update_part_details=Updating partition details..."); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1397 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame^] | 1398 | (*iter)->Update_Size(true); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1399 | if ((*iter)->Can_Be_Mounted) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1400 | if ((*iter)->Mount_Point == "/system") { |
| 1401 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1402 | DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size); |
| 1403 | } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") { |
| 1404 | data_size += (int)((*iter)->Backup_Size / 1048576LLU); |
| 1405 | } else if ((*iter)->Mount_Point == "/cache") { |
| 1406 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1407 | DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size); |
| 1408 | } else if ((*iter)->Mount_Point == "/sd-ext") { |
| 1409 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1410 | DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size); |
| 1411 | if ((*iter)->Backup_Size == 0) { |
| 1412 | DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0); |
| 1413 | DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0); |
| 1414 | } else |
| 1415 | DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1416 | } else if ((*iter)->Has_Android_Secure) { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1417 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1418 | DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1419 | if ((*iter)->Backup_Size == 0) { |
| 1420 | DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0); |
| 1421 | DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0); |
| 1422 | } else |
| 1423 | DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1); |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 1424 | } else if ((*iter)->Mount_Point == "/boot") { |
| 1425 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1426 | DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size); |
| 1427 | if ((*iter)->Backup_Size == 0) { |
| 1428 | DataManager::SetValue("tw_has_boot_partition", 0); |
| 1429 | DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0); |
| 1430 | } else |
| 1431 | DataManager::SetValue("tw_has_boot_partition", 1); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1432 | } |
Dees_Troy | aa9cc40 | 2012-10-13 12:14:05 -0400 | [diff] [blame] | 1433 | } else { |
| 1434 | // Handle unmountable partitions in case we reset defaults |
| 1435 | if ((*iter)->Mount_Point == "/boot") { |
| 1436 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1437 | DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size); |
| 1438 | if ((*iter)->Backup_Size == 0) { |
| 1439 | DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0); |
| 1440 | DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0); |
| 1441 | } else |
| 1442 | DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1); |
| 1443 | } else if ((*iter)->Mount_Point == "/recovery") { |
| 1444 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1445 | DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size); |
| 1446 | if ((*iter)->Backup_Size == 0) { |
| 1447 | DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0); |
| 1448 | DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0); |
| 1449 | } else |
| 1450 | DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1); |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1451 | } else if ((*iter)->Mount_Point == "/data") { |
| 1452 | data_size += (int)((*iter)->Backup_Size / 1048576LLU); |
Dees_Troy | aa9cc40 | 2012-10-13 12:14:05 -0400 | [diff] [blame] | 1453 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1454 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1455 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1456 | gui_msg("update_part_details_done=...done"); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1457 | DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size); |
| 1458 | string current_storage_path = DataManager::GetCurrentStoragePath(); |
| 1459 | TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1460 | if (FreeStorage != NULL) { |
| 1461 | // Attempt to mount storage |
| 1462 | if (!FreeStorage->Mount(false)) { |
Matt Mower | 2d50cad | 2015-12-03 22:26:55 -0600 | [diff] [blame] | 1463 | gui_msg(Msg(msg::kError, "unable_to_mount_storage=Unable to mount storage")); |
| 1464 | DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1465 | } else { |
| 1466 | DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU)); |
| 1467 | } |
| 1468 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1469 | LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str()); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1470 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1471 | if (!Write_Fstab()) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1472 | LOGERR("Error creating fstab\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1473 | return; |
| 1474 | } |
| 1475 | |
| 1476 | int TWPartitionManager::Decrypt_Device(string Password) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1477 | #ifdef TW_INCLUDE_CRYPTO |
| 1478 | int ret_val, password_len; |
Ethan Yonker | a1de149 | 2015-11-04 11:58:27 -0600 | [diff] [blame] | 1479 | char crypto_state[PROPERTY_VALUE_MAX], crypto_blkdev[PROPERTY_VALUE_MAX], cPassword[255]; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1480 | size_t result; |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 1481 | std::vector<TWPartition*>::iterator iter; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1482 | |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 1483 | // Mount any partitions that need to be mounted for decrypt |
| 1484 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 1485 | if ((*iter)->Mount_To_Decrypt) { |
| 1486 | (*iter)->Mount(true); |
| 1487 | } |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1488 | } |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1489 | |
Ethan Yonker | a1de149 | 2015-11-04 11:58:27 -0600 | [diff] [blame] | 1490 | property_get("ro.crypto.state", crypto_state, "error"); |
| 1491 | if (strcmp(crypto_state, "error") == 0) { |
| 1492 | property_set("ro.crypto.state", "encrypted"); |
| 1493 | // Sleep for a bit so that services can start if needed |
| 1494 | sleep(1); |
| 1495 | } |
| 1496 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1497 | strcpy(cPassword, Password.c_str()); |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1498 | int pwret = cryptfs_check_passwd(cPassword); |
| 1499 | |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 1500 | // Unmount any partitions that were needed for decrypt |
| 1501 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 1502 | if ((*iter)->Mount_To_Decrypt) { |
| 1503 | (*iter)->UnMount(false); |
| 1504 | } |
| 1505 | } |
| 1506 | |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1507 | if (pwret != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1508 | gui_err("fail_decrypt=Failed to decrypt data."); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1509 | return -1; |
| 1510 | } |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1511 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1512 | property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error"); |
| 1513 | if (strcmp(crypto_blkdev, "error") == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1514 | LOGERR("Error retrieving decrypted data block device.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1515 | } else { |
| 1516 | TWPartition* dat = Find_Partition_By_Path("/data"); |
| 1517 | if (dat != NULL) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1518 | DataManager::SetValue(TW_IS_DECRYPTED, 1); |
| 1519 | dat->Is_Decrypted = true; |
| 1520 | dat->Decrypted_Block_Device = crypto_blkdev; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1521 | dat->Setup_File_System(false); |
Dees_Troy | 74fb2e9 | 2013-04-15 14:35:47 +0000 | [diff] [blame] | 1522 | dat->Current_File_System = dat->Fstab_File_System; // Needed if we're ignoring blkid because encrypted devices start out as emmc |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 1523 | gui_msg(Msg("decrypt_success_dev=Data successfully decrypted, new block device: '{1}'")(crypto_blkdev)); |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1524 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1525 | // Sleep for a bit so that the device will be ready |
| 1526 | sleep(1); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1527 | if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) { |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 1528 | dat->Storage_Path = "/data/media/0"; |
| 1529 | dat->Symlink_Path = dat->Storage_Path; |
Dees Troy | 98fb46c | 2013-12-04 16:56:45 +0000 | [diff] [blame] | 1530 | DataManager::SetValue("tw_storage_path", "/data/media/0"); |
Ethan Yonker | ec0fa4a | 2014-11-20 09:51:03 -0600 | [diff] [blame] | 1531 | DataManager::SetValue("tw_settings_path", "/data/media/0"); |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 1532 | dat->UnMount(false); |
Dees_Troy | dc8bc1b | 2013-01-17 01:39:28 +0000 | [diff] [blame] | 1533 | Output_Partition(dat); |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 1534 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1535 | Update_System_Details(); |
Dees_Troy | d0384ef | 2012-10-12 12:15:42 -0400 | [diff] [blame] | 1536 | UnMount_Main_Partitions(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1537 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1538 | LOGERR("Unable to locate data partition.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1539 | } |
| 1540 | return 0; |
| 1541 | #else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1542 | gui_err("no_crypto_support=No crypto support was compiled into this build."); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1543 | return -1; |
| 1544 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1545 | return 1; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1546 | } |
| 1547 | |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1548 | int TWPartitionManager::Fix_Contexts(void) { |
that | a3d31fb | 2014-12-21 22:27:40 +0100 | [diff] [blame] | 1549 | #ifdef HAVE_SELINUX |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1550 | std::vector<TWPartition*>::iterator iter; |
| 1551 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 1552 | if ((*iter)->Has_Data_Media) { |
| 1553 | if ((*iter)->Mount(true)) { |
| 1554 | if (fixContexts::fixDataMediaContexts((*iter)->Mount_Point) != 0) |
| 1555 | return -1; |
| 1556 | } |
| 1557 | } |
| 1558 | } |
Dees_Troy | 1a650e6 | 2012-10-19 20:54:32 -0400 | [diff] [blame] | 1559 | UnMount_Main_Partitions(); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1560 | gui_msg("done=Done."); |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1561 | return 0; |
| 1562 | #else |
| 1563 | LOGERR("Cannot fix contexts, no selinux support present.\n"); |
| 1564 | return -1; |
| 1565 | #endif |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 1566 | } |
| 1567 | |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1568 | TWPartition* TWPartitionManager::Find_Next_Storage(string Path, bool Exclude_Data_Media) { |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1569 | std::vector<TWPartition*>::iterator iter = Partitions.begin(); |
| 1570 | |
| 1571 | if (!Path.empty()) { |
| 1572 | string Search_Path = TWFunc::Get_Root_Path(Path); |
| 1573 | for (; iter != Partitions.end(); iter++) { |
Matt Mower | 9a561dd | 2016-02-22 21:25:51 -0600 | [diff] [blame] | 1574 | if ((*iter)->Mount_Point == Search_Path) { |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1575 | iter++; |
| 1576 | break; |
| 1577 | } |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | for (; iter != Partitions.end(); iter++) { |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1582 | if (Exclude_Data_Media && (*iter)->Has_Data_Media) { |
| 1583 | // do nothing, do not return this type of partition |
| 1584 | } else if ((*iter)->Is_Storage && (*iter)->Is_Present) { |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1585 | return (*iter); |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | return NULL; |
| 1590 | } |
| 1591 | |
Dees_Troy | d21618c | 2012-10-14 18:48:49 -0400 | [diff] [blame] | 1592 | int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) { |
Dees_Troy | d21618c | 2012-10-14 18:48:49 -0400 | [diff] [blame] | 1593 | TWPartition* Part = Find_Partition_By_Path(Partition_Path); |
| 1594 | |
| 1595 | if (Part == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1596 | LOGINFO("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str()); |
| 1597 | gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Partition_Path)); |
Dees_Troy | d21618c | 2012-10-14 18:48:49 -0400 | [diff] [blame] | 1598 | return false; |
| 1599 | } |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1600 | LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str()); |
| 1601 | if (!Part->UnMount(true) || !Part->Is_Present) |
Dees_Troy | d21618c | 2012-10-14 18:48:49 -0400 | [diff] [blame] | 1602 | return false; |
| 1603 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1604 | if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) { |
| 1605 | LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno)); |
Dees_Troy | d21618c | 2012-10-14 18:48:49 -0400 | [diff] [blame] | 1606 | return false; |
| 1607 | } |
Dees_Troy | d21618c | 2012-10-14 18:48:49 -0400 | [diff] [blame] | 1608 | return true; |
| 1609 | } |
| 1610 | |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1611 | int TWPartitionManager::usb_storage_enable(void) { |
Dees_Troy | d21618c | 2012-10-14 18:48:49 -0400 | [diff] [blame] | 1612 | int has_dual, has_data_media; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1613 | char lun_file[255]; |
Dees_Troy | d21618c | 2012-10-14 18:48:49 -0400 | [diff] [blame] | 1614 | bool has_multiple_lun = false; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1615 | |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1616 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media); |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1617 | string Lun_File_str = CUSTOM_LUN_FILE; |
| 1618 | size_t found = Lun_File_str.find("%"); |
| 1619 | if (found != string::npos) { |
| 1620 | sprintf(lun_file, CUSTOM_LUN_FILE, 1); |
| 1621 | if (TWFunc::Path_Exists(lun_file)) |
| 1622 | has_multiple_lun = true; |
| 1623 | } |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1624 | mtp_was_enabled = TWFunc::Toggle_MTP(false); // Must disable MTP for USB Storage |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1625 | if (!has_multiple_lun) { |
| 1626 | LOGINFO("Device doesn't have multiple lun files, mount current storage\n"); |
| 1627 | sprintf(lun_file, CUSTOM_LUN_FILE, 0); |
| 1628 | if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") { |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1629 | TWPartition* Mount = Find_Next_Storage("", true); |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1630 | if (Mount) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1631 | if (!Open_Lun_File(Mount->Mount_Point, lun_file)) { |
| 1632 | goto error_handle; |
| 1633 | } |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1634 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1635 | gui_err("unable_locate_storage=Unable to locate storage device."); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1636 | goto error_handle; |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1637 | } |
| 1638 | } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1639 | goto error_handle; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1640 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1641 | } else { |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1642 | LOGINFO("Device has multiple lun files\n"); |
| 1643 | TWPartition* Mount1; |
| 1644 | TWPartition* Mount2; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1645 | sprintf(lun_file, CUSTOM_LUN_FILE, 0); |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1646 | Mount1 = Find_Next_Storage("", true); |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1647 | if (Mount1) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1648 | if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) { |
| 1649 | goto error_handle; |
| 1650 | } |
Matt Mower | ee71706 | 2014-04-26 01:46:46 -0500 | [diff] [blame] | 1651 | sprintf(lun_file, CUSTOM_LUN_FILE, 1); |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1652 | Mount2 = Find_Next_Storage(Mount1->Mount_Point, true); |
Matt Mower | 1fdcdb7 | 2016-01-25 20:53:47 -0600 | [diff] [blame] | 1653 | if (Mount2 && Mount2->Mount_Point != Mount1->Mount_Point) { |
Matt Mower | ee71706 | 2014-04-26 01:46:46 -0500 | [diff] [blame] | 1654 | Open_Lun_File(Mount2->Mount_Point, lun_file); |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1655 | } |
| 1656 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1657 | gui_err("unable_locate_storage=Unable to locate storage device."); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1658 | goto error_handle; |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1659 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1660 | } |
Matt Mower | b6ef478 | 2014-11-06 02:24:36 -0600 | [diff] [blame] | 1661 | property_set("sys.storage.ums_enabled", "1"); |
HandyMenny | 37d4299 | 2014-10-26 22:15:59 +0100 | [diff] [blame] | 1662 | property_set("sys.usb.config", "mass_storage,adb"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1663 | return true; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1664 | error_handle: |
| 1665 | if (mtp_was_enabled) |
| 1666 | if (!Enable_MTP()) |
| 1667 | Disable_MTP(); |
| 1668 | return false; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | int TWPartitionManager::usb_storage_disable(void) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1672 | int index, ret; |
| 1673 | char lun_file[255], ch[2] = {0, 0}; |
| 1674 | string str = ch; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1675 | |
| 1676 | for (index=0; index<2; index++) { |
| 1677 | sprintf(lun_file, CUSTOM_LUN_FILE, index); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1678 | ret = TWFunc::write_file(lun_file, str); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1679 | if (ret < 0) { |
| 1680 | break; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1681 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1682 | } |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1683 | Mount_All_Storage(); |
| 1684 | Update_System_Details(); |
Dees_Troy | cfd73ef | 2012-10-12 16:52:00 -0400 | [diff] [blame] | 1685 | UnMount_Main_Partitions(); |
Matt Mower | d9cb906 | 2013-12-14 20:34:45 -0600 | [diff] [blame] | 1686 | property_set("sys.storage.ums_enabled", "0"); |
HandyMenny | 37d4299 | 2014-10-26 22:15:59 +0100 | [diff] [blame] | 1687 | property_set("sys.usb.config", "adb"); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1688 | if (mtp_was_enabled) |
| 1689 | if (!Enable_MTP()) |
| 1690 | Disable_MTP(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1691 | if (ret < 0 && index == 0) { |
| 1692 | LOGERR("Unable to write to ums lunfile '%s'.", lun_file); |
| 1693 | return false; |
| 1694 | } else { |
| 1695 | return true; |
| 1696 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1697 | return true; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | void TWPartitionManager::Mount_All_Storage(void) { |
| 1701 | std::vector<TWPartition*>::iterator iter; |
| 1702 | |
| 1703 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 1704 | if ((*iter)->Is_Storage) |
| 1705 | (*iter)->Mount(false); |
| 1706 | } |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 1707 | } |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1708 | |
Dees_Troy | d0384ef | 2012-10-12 12:15:42 -0400 | [diff] [blame] | 1709 | void TWPartitionManager::UnMount_Main_Partitions(void) { |
| 1710 | // Unmounts system and data if data is not data/media |
| 1711 | // Also unmounts boot if boot is mountable |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1712 | LOGINFO("Unmounting main partitions...\n"); |
Dees_Troy | d0384ef | 2012-10-12 12:15:42 -0400 | [diff] [blame] | 1713 | |
| 1714 | TWPartition* Boot_Partition = Find_Partition_By_Path("/boot"); |
| 1715 | |
| 1716 | UnMount_By_Path("/system", true); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1717 | if (!datamedia) |
| 1718 | UnMount_By_Path("/data", true); |
| 1719 | |
Dees_Troy | d0384ef | 2012-10-12 12:15:42 -0400 | [diff] [blame] | 1720 | if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted) |
| 1721 | Boot_Partition->UnMount(true); |
| 1722 | } |
| 1723 | |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1724 | int TWPartitionManager::Partition_SDCard(void) { |
| 1725 | char mkdir_path[255], temp[255], line[512]; |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1726 | string Storage_Path, Command, Device, fat_str, ext_str, start_loc, end_loc, ext_format, sd_path, tmpdevice; |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1727 | int ext, swap, total_size = 0, fat_size; |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1728 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1729 | gui_msg("start_partition_sd=Partitioning SD Card..."); |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1730 | |
| 1731 | // Locate and validate device to partition |
| 1732 | TWPartition* SDCard = Find_Partition_By_Path(DataManager::GetCurrentStoragePath()); |
| 1733 | |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1734 | if (SDCard->Is_Adopted_Storage) |
| 1735 | SDCard->Revert_Adopted(); |
| 1736 | |
Ethan Yonker | 1eff6cd | 2014-09-15 13:30:42 -0500 | [diff] [blame] | 1737 | if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1738 | gui_err("partition_sd_locate=Unable to locate device to partition."); |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1739 | return false; |
| 1740 | } |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1741 | |
| 1742 | // Unmount everything |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1743 | if (!SDCard->UnMount(true)) |
| 1744 | return false; |
| 1745 | TWPartition* SDext = Find_Partition_By_Path("/sd-ext"); |
| 1746 | if (SDext != NULL) { |
| 1747 | if (!SDext->UnMount(true)) |
| 1748 | return false; |
| 1749 | } |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1750 | char* swappath = getenv("SWAPPATH"); |
| 1751 | if (swappath != NULL) { |
| 1752 | LOGINFO("Unmounting swap at '%s'\n", swappath); |
| 1753 | umount(swappath); |
| 1754 | } |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1755 | |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1756 | // Determine block device |
| 1757 | if (SDCard->Alternate_Block_Device.empty()) { |
| 1758 | SDCard->Find_Actual_Block_Device(); |
| 1759 | Device = SDCard->Actual_Block_Device; |
| 1760 | // Just use the root block device |
| 1761 | Device.resize(strlen("/dev/block/mmcblkX")); |
| 1762 | } else { |
| 1763 | Device = SDCard->Alternate_Block_Device; |
| 1764 | } |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1765 | |
| 1766 | // Find the size of the block device: |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1767 | total_size = (int)(TWFunc::IOCTL_Get_Block_Size(Device.c_str()) / (1048576)); |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1768 | |
| 1769 | DataManager::GetValue("tw_sdext_size", ext); |
| 1770 | DataManager::GetValue("tw_swap_size", swap); |
| 1771 | DataManager::GetValue("tw_sdpart_file_system", ext_format); |
| 1772 | fat_size = total_size - ext - swap; |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1773 | LOGINFO("sd card mount point %s block device is '%s', sdcard size is: %iMB, fat size: %iMB, ext size: %iMB, ext system: '%s', swap size: %iMB\n", DataManager::GetCurrentStoragePath().c_str(), Device.c_str(), total_size, fat_size, ext, ext_format.c_str(), swap); |
| 1774 | |
| 1775 | // Determine partition sizes |
| 1776 | if (swap == 0 && ext == 0) { |
| 1777 | fat_str = "-0"; |
| 1778 | } else { |
| 1779 | memset(temp, 0, sizeof(temp)); |
| 1780 | sprintf(temp, "%i", fat_size); |
| 1781 | fat_str = temp; |
| 1782 | fat_str += "MB"; |
| 1783 | } |
| 1784 | if (swap == 0) { |
| 1785 | ext_str = "-0"; |
| 1786 | } else { |
| 1787 | memset(temp, 0, sizeof(temp)); |
| 1788 | sprintf(temp, "%i", ext); |
| 1789 | ext_str = "+"; |
| 1790 | ext_str += temp; |
| 1791 | ext_str += "MB"; |
| 1792 | } |
| 1793 | |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1794 | if (ext + swap > total_size) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1795 | gui_err("ext_swap_size=EXT + Swap size is larger than sdcard size."); |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1796 | return false; |
| 1797 | } |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1798 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1799 | gui_msg("remove_part_table=Removing partition table..."); |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1800 | Command = "sgdisk --zap-all " + Device; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1801 | LOGINFO("Command is: '%s'\n", Command.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1802 | if (TWFunc::Exec_Cmd(Command) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1803 | gui_err("unable_rm_part=Unable to remove partition table."); |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1804 | Update_System_Details(); |
| 1805 | return false; |
| 1806 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1807 | gui_msg(Msg("create_part=Creating {1} partition...")("FAT32")); |
Captain Throwback | 9643a80 | 2016-05-27 11:44:51 -0400 | [diff] [blame] | 1808 | Command = "sgdisk --new=0:0:" + fat_str + " --change-name=0:\"Microsoft basic data\" --typecode=0:EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 " + Device; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1809 | LOGINFO("Command is: '%s'\n", Command.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1810 | if (TWFunc::Exec_Cmd(Command) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1811 | gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("FAT32")); |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1812 | return false; |
| 1813 | } |
| 1814 | if (ext > 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1815 | gui_msg(Msg("create_part=Creating {1} partition...")("EXT")); |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1816 | Command = "sgdisk --new=0:0:" + ext_str + " --change-name=0:\"Linux filesystem\" " + Device; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1817 | LOGINFO("Command is: '%s'\n", Command.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1818 | if (TWFunc::Exec_Cmd(Command) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1819 | gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("EXT")); |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1820 | Update_System_Details(); |
| 1821 | return false; |
| 1822 | } |
| 1823 | } |
| 1824 | if (swap > 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1825 | gui_msg(Msg("create_part=Creating {1} partition...")("swap")); |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1826 | Command = "sgdisk --new=0:0:-0 --change-name=0:\"Linux swap\" --typecode=0:0657FD6D-A4AB-43C4-84E5-0933C84B4F4F " + Device; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1827 | LOGINFO("Command is: '%s'\n", Command.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1828 | if (TWFunc::Exec_Cmd(Command) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1829 | gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("swap")); |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1830 | Update_System_Details(); |
| 1831 | return false; |
| 1832 | } |
| 1833 | } |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1834 | |
| 1835 | // Convert GPT to MBR |
| 1836 | Command = "sgdisk --gpttombr " + Device; |
| 1837 | if (TWFunc::Exec_Cmd(Command) != 0) |
| 1838 | LOGINFO("Failed to covert partition GPT to MBR\n"); |
| 1839 | |
| 1840 | // Tell the kernel to rescan the partition table |
| 1841 | int fd = open(Device.c_str(), O_RDONLY); |
| 1842 | ioctl(fd, BLKRRPART, 0); |
| 1843 | close(fd); |
| 1844 | |
| 1845 | string format_device = Device; |
| 1846 | if (Device.substr(0, 17) == "/dev/block/mmcblk") |
| 1847 | format_device += "p"; |
| 1848 | |
| 1849 | // Format new partitions to proper file system |
| 1850 | if (fat_size > 0) { |
| 1851 | Command = "mkfs.fat " + format_device + "1"; |
| 1852 | TWFunc::Exec_Cmd(Command); |
| 1853 | } |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1854 | if (ext > 0) { |
| 1855 | if (SDext == NULL) { |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1856 | Command = "mke2fs -t " + ext_format + " -m 0 " + format_device + "2"; |
| 1857 | gui_msg(Msg("format_sdext_as=Formatting sd-ext as {1}...")(ext_format)); |
| 1858 | LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str()); |
| 1859 | TWFunc::Exec_Cmd(Command); |
| 1860 | } else { |
| 1861 | SDext->Wipe(ext_format); |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1862 | } |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1863 | } |
| 1864 | if (swap > 0) { |
| 1865 | Command = "mkswap " + format_device; |
| 1866 | if (ext > 0) |
| 1867 | Command += "3"; |
| 1868 | else |
| 1869 | Command += "2"; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1870 | TWFunc::Exec_Cmd(Command); |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1871 | } |
| 1872 | |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1873 | // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned |
| 1874 | if (SDCard->Mount(true)) { |
| 1875 | string TWRP_Folder = SDCard->Mount_Point + "/TWRP"; |
| 1876 | mkdir(TWRP_Folder.c_str(), 0777); |
| 1877 | DataManager::Flush(); |
| 1878 | } |
| 1879 | |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1880 | Update_System_Details(); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1881 | gui_msg("part_complete=Partitioning complete."); |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1882 | return true; |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 1883 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1884 | |
| 1885 | void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) { |
| 1886 | std::vector<TWPartition*>::iterator iter; |
| 1887 | if (ListType == "mount") { |
| 1888 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 1889 | if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) { |
| 1890 | struct PartitionList part; |
| 1891 | part.Display_Name = (*iter)->Display_Name; |
| 1892 | part.Mount_Point = (*iter)->Mount_Point; |
| 1893 | part.selected = (*iter)->Is_Mounted(); |
| 1894 | Partition_List->push_back(part); |
| 1895 | } |
| 1896 | } |
| 1897 | } else if (ListType == "storage") { |
| 1898 | char free_space[255]; |
| 1899 | string Current_Storage = DataManager::GetCurrentStoragePath(); |
| 1900 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 1901 | if ((*iter)->Is_Storage) { |
| 1902 | struct PartitionList part; |
| 1903 | sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024); |
| 1904 | part.Display_Name = (*iter)->Storage_Name + " ("; |
| 1905 | part.Display_Name += free_space; |
| 1906 | part.Display_Name += "MB)"; |
| 1907 | part.Mount_Point = (*iter)->Storage_Path; |
| 1908 | if ((*iter)->Storage_Path == Current_Storage) |
| 1909 | part.selected = 1; |
| 1910 | else |
| 1911 | part.selected = 0; |
| 1912 | Partition_List->push_back(part); |
| 1913 | } |
| 1914 | } |
| 1915 | } else if (ListType == "backup") { |
| 1916 | char backup_size[255]; |
Dees_Troy | 9e0b71c | 2013-04-08 13:35:37 +0000 | [diff] [blame] | 1917 | unsigned long long Backup_Size; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1918 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
Dees_Troy | 9e0b71c | 2013-04-08 13:35:37 +0000 | [diff] [blame] | 1919 | if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1920 | struct PartitionList part; |
Dees_Troy | 9e0b71c | 2013-04-08 13:35:37 +0000 | [diff] [blame] | 1921 | Backup_Size = (*iter)->Backup_Size; |
| 1922 | if ((*iter)->Has_SubPartition) { |
| 1923 | std::vector<TWPartition*>::iterator subpart; |
| 1924 | |
| 1925 | for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) { |
| 1926 | if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point) |
| 1927 | Backup_Size += (*subpart)->Backup_Size; |
| 1928 | } |
| 1929 | } |
| 1930 | sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1931 | part.Display_Name = (*iter)->Backup_Display_Name + " ("; |
| 1932 | part.Display_Name += backup_size; |
| 1933 | part.Display_Name += "MB)"; |
| 1934 | part.Mount_Point = (*iter)->Backup_Path; |
| 1935 | part.selected = 0; |
| 1936 | Partition_List->push_back(part); |
| 1937 | } |
| 1938 | } |
| 1939 | } else if (ListType == "restore") { |
| 1940 | string Restore_List, restore_path; |
| 1941 | TWPartition* restore_part = NULL; |
| 1942 | |
| 1943 | DataManager::GetValue("tw_restore_list", Restore_List); |
| 1944 | if (!Restore_List.empty()) { |
| 1945 | size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos); |
| 1946 | while (end_pos != string::npos && start_pos < Restore_List.size()) { |
| 1947 | restore_path = Restore_List.substr(start_pos, end_pos - start_pos); |
Dees_Troy | 59df926 | 2013-06-19 14:53:57 -0500 | [diff] [blame] | 1948 | if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) { |
Dees Troy | 4159aed | 2014-02-28 17:24:43 +0000 | [diff] [blame] | 1949 | if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1950 | // Don't allow restore of recovery (causes problems on some devices) |
Dees_Troy | 59df926 | 2013-06-19 14:53:57 -0500 | [diff] [blame] | 1951 | // Don't add subpartitions to the list of items |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1952 | } else { |
| 1953 | struct PartitionList part; |
| 1954 | part.Display_Name = restore_part->Backup_Display_Name; |
| 1955 | part.Mount_Point = restore_part->Backup_Path; |
| 1956 | part.selected = 1; |
| 1957 | Partition_List->push_back(part); |
| 1958 | } |
| 1959 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1960 | gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path)); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1961 | } |
| 1962 | start_pos = end_pos + 1; |
| 1963 | end_pos = Restore_List.find(";", start_pos); |
| 1964 | } |
| 1965 | } |
| 1966 | } else if (ListType == "wipe") { |
| 1967 | struct PartitionList dalvik; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1968 | dalvik.Display_Name = gui_parse_text("{@dalvik}"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1969 | dalvik.Mount_Point = "DALVIK"; |
| 1970 | dalvik.selected = 0; |
| 1971 | Partition_List->push_back(dalvik); |
| 1972 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 1973 | if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) { |
| 1974 | struct PartitionList part; |
| 1975 | part.Display_Name = (*iter)->Display_Name; |
| 1976 | part.Mount_Point = (*iter)->Mount_Point; |
| 1977 | part.selected = 0; |
| 1978 | Partition_List->push_back(part); |
| 1979 | } |
| 1980 | if ((*iter)->Has_Android_Secure) { |
| 1981 | struct PartitionList part; |
| 1982 | part.Display_Name = (*iter)->Backup_Display_Name; |
| 1983 | part.Mount_Point = (*iter)->Backup_Path; |
| 1984 | part.selected = 0; |
| 1985 | Partition_List->push_back(part); |
| 1986 | } |
Dees_Troy | 74fb2e9 | 2013-04-15 14:35:47 +0000 | [diff] [blame] | 1987 | if ((*iter)->Has_Data_Media) { |
| 1988 | struct PartitionList datamedia; |
| 1989 | datamedia.Display_Name = (*iter)->Storage_Name; |
| 1990 | datamedia.Mount_Point = "INTERNAL"; |
| 1991 | datamedia.selected = 0; |
| 1992 | Partition_List->push_back(datamedia); |
| 1993 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1994 | } |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 1995 | } else if (ListType == "flashimg") { |
| 1996 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 1997 | if ((*iter)->Can_Flash_Img && (*iter)->Is_Present) { |
| 1998 | struct PartitionList part; |
| 1999 | part.Display_Name = (*iter)->Backup_Display_Name; |
| 2000 | part.Mount_Point = (*iter)->Backup_Path; |
| 2001 | part.selected = 0; |
| 2002 | Partition_List->push_back(part); |
| 2003 | } |
| 2004 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 2005 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 2006 | LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | int TWPartitionManager::Fstab_Processed(void) { |
| 2011 | return Partitions.size(); |
| 2012 | } |
Dees_Troy | d93bda5 | 2013-07-03 19:55:19 +0000 | [diff] [blame] | 2013 | |
| 2014 | void TWPartitionManager::Output_Storage_Fstab(void) { |
| 2015 | std::vector<TWPartition*>::iterator iter; |
| 2016 | char storage_partition[255]; |
| 2017 | string Temp; |
| 2018 | FILE *fp = fopen("/cache/recovery/storage.fstab", "w"); |
| 2019 | |
| 2020 | if (fp == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2021 | gui_msg(Msg(msg::kError, "unable_to_open=Unable to open '{1}'.")("/cache/recovery/storage.fstab")); |
Dees_Troy | d93bda5 | 2013-07-03 19:55:19 +0000 | [diff] [blame] | 2022 | return; |
| 2023 | } |
| 2024 | |
| 2025 | // Iterate through all partitions |
| 2026 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 2027 | if ((*iter)->Is_Storage) { |
| 2028 | Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n"; |
| 2029 | strcpy(storage_partition, Temp.c_str()); |
| 2030 | fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp); |
| 2031 | } |
| 2032 | } |
| 2033 | fclose(fp); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 2034 | } |
Vojtech Bocek | 93cb1ef | 2014-05-12 15:41:52 +0200 | [diff] [blame] | 2035 | |
| 2036 | TWPartition *TWPartitionManager::Get_Default_Storage_Partition() |
| 2037 | { |
| 2038 | TWPartition *res = NULL; |
| 2039 | for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) { |
| 2040 | if(!(*iter)->Is_Storage) |
| 2041 | continue; |
| 2042 | |
| 2043 | if((*iter)->Is_Settings_Storage) |
| 2044 | return *iter; |
| 2045 | |
| 2046 | if(!res) |
| 2047 | res = *iter; |
| 2048 | } |
| 2049 | return res; |
| 2050 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 2051 | |
| 2052 | bool TWPartitionManager::Enable_MTP(void) { |
| 2053 | #ifdef TW_HAS_MTP |
Ethan Yonker | 8dfa777 | 2014-09-04 21:48:41 -0500 | [diff] [blame] | 2054 | if (mtppid) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2055 | gui_err("mtp_already_enabled=MTP already enabled"); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 2056 | return true; |
| 2057 | } |
| 2058 | //Launch MTP Responder |
| 2059 | LOGINFO("Starting MTP\n"); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 2060 | |
| 2061 | int mtppipe[2]; |
| 2062 | |
| 2063 | if (pipe(mtppipe) < 0) { |
| 2064 | LOGERR("Error creating MTP pipe\n"); |
| 2065 | return false; |
| 2066 | } |
| 2067 | |
Ethan Yonker | df7abac | 2014-12-29 10:48:17 -0600 | [diff] [blame] | 2068 | char old_value[PROPERTY_VALUE_MAX]; |
| 2069 | property_get("sys.usb.config", old_value, "error"); |
| 2070 | if (strcmp(old_value, "error") != 0 && strcmp(old_value, "mtp,adb") != 0) { |
| 2071 | char vendor[PROPERTY_VALUE_MAX]; |
| 2072 | char product[PROPERTY_VALUE_MAX]; |
| 2073 | property_set("sys.usb.config", "none"); |
| 2074 | property_get("usb.vendor", vendor, "18D1"); |
| 2075 | property_get("usb.product.mtpadb", product, "4EE2"); |
| 2076 | string vendorstr = vendor; |
| 2077 | string productstr = product; |
| 2078 | TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr); |
| 2079 | TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr); |
| 2080 | property_set("sys.usb.config", "mtp,adb"); |
| 2081 | } |
Ethan Yonker | 6d154c4 | 2014-09-03 14:19:43 -0500 | [diff] [blame] | 2082 | /* To enable MTP debug, use the twrp command line feature to |
| 2083 | * twrp set tw_mtp_debug 1 |
| 2084 | */ |
| 2085 | twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug")); |
Ethan Yonker | 1b03920 | 2015-01-30 10:08:48 -0600 | [diff] [blame] | 2086 | mtppid = mtp->forkserver(mtppipe); |
| 2087 | if (mtppid) { |
| 2088 | close(mtppipe[0]); // Host closes read side |
| 2089 | mtp_write_fd = mtppipe[1]; |
| 2090 | DataManager::SetValue("tw_mtp_enabled", 1); |
| 2091 | Add_All_MTP_Storage(); |
| 2092 | return true; |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 2093 | } else { |
| 2094 | close(mtppipe[0]); |
| 2095 | close(mtppipe[1]); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2096 | gui_err("mtp_fail=Failed to enable MTP"); |
Ethan Yonker | 1b03920 | 2015-01-30 10:08:48 -0600 | [diff] [blame] | 2097 | return false; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 2098 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 2099 | #else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2100 | gui_err("no_mtp=MTP support not included"); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 2101 | #endif |
| 2102 | DataManager::SetValue("tw_mtp_enabled", 0); |
| 2103 | return false; |
| 2104 | } |
| 2105 | |
Ethan Yonker | 1b03920 | 2015-01-30 10:08:48 -0600 | [diff] [blame] | 2106 | void TWPartitionManager::Add_All_MTP_Storage(void) { |
| 2107 | #ifdef TW_HAS_MTP |
| 2108 | std::vector<TWPartition*>::iterator iter; |
| 2109 | |
| 2110 | if (!mtppid) |
| 2111 | return; // MTP is not enabled |
| 2112 | |
| 2113 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 2114 | if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false)) |
| 2115 | Add_Remove_MTP_Storage((*iter), MTP_MESSAGE_ADD_STORAGE); |
| 2116 | } |
| 2117 | #else |
| 2118 | return; |
| 2119 | #endif |
| 2120 | } |
| 2121 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 2122 | bool TWPartitionManager::Disable_MTP(void) { |
Ethan Yonker | df7abac | 2014-12-29 10:48:17 -0600 | [diff] [blame] | 2123 | char old_value[PROPERTY_VALUE_MAX]; |
| 2124 | property_get("sys.usb.config", old_value, "error"); |
| 2125 | if (strcmp(old_value, "adb") != 0) { |
| 2126 | char vendor[PROPERTY_VALUE_MAX]; |
| 2127 | char product[PROPERTY_VALUE_MAX]; |
| 2128 | property_set("sys.usb.config", "none"); |
| 2129 | property_get("usb.vendor", vendor, "18D1"); |
| 2130 | property_get("usb.product.adb", product, "D002"); |
| 2131 | string vendorstr = vendor; |
| 2132 | string productstr = product; |
| 2133 | TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr); |
| 2134 | TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr); |
| 2135 | usleep(2000); |
| 2136 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 2137 | #ifdef TW_HAS_MTP |
Ethan Yonker | 8dfa777 | 2014-09-04 21:48:41 -0500 | [diff] [blame] | 2138 | if (mtppid) { |
Ethan Yonker | 8613dc0 | 2014-09-11 09:28:20 -0500 | [diff] [blame] | 2139 | LOGINFO("Disabling MTP\n"); |
| 2140 | int status; |
| 2141 | kill(mtppid, SIGKILL); |
Ethan Yonker | 8dfa777 | 2014-09-04 21:48:41 -0500 | [diff] [blame] | 2142 | mtppid = 0; |
Ethan Yonker | 8613dc0 | 2014-09-11 09:28:20 -0500 | [diff] [blame] | 2143 | // We don't care about the exit value, but this prevents a zombie process |
| 2144 | waitpid(mtppid, &status, 0); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 2145 | close(mtp_write_fd); |
| 2146 | mtp_write_fd = -1; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 2147 | } |
Ethan Yonker | df7abac | 2014-12-29 10:48:17 -0600 | [diff] [blame] | 2148 | #endif |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 2149 | property_set("sys.usb.config", "adb"); |
Ethan Yonker | df7abac | 2014-12-29 10:48:17 -0600 | [diff] [blame] | 2150 | #ifdef TW_HAS_MTP |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 2151 | DataManager::SetValue("tw_mtp_enabled", 0); |
| 2152 | return true; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 2153 | #endif |
Ethan Yonker | df7abac | 2014-12-29 10:48:17 -0600 | [diff] [blame] | 2154 | return false; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 2155 | } |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 2156 | |
| 2157 | TWPartition* TWPartitionManager::Find_Partition_By_MTP_Storage_ID(unsigned int Storage_ID) { |
| 2158 | std::vector<TWPartition*>::iterator iter; |
| 2159 | |
| 2160 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 2161 | if ((*iter)->MTP_Storage_ID == Storage_ID) |
| 2162 | return (*iter); |
| 2163 | } |
| 2164 | return NULL; |
| 2165 | } |
| 2166 | |
| 2167 | bool TWPartitionManager::Add_Remove_MTP_Storage(TWPartition* Part, int message_type) { |
| 2168 | #ifdef TW_HAS_MTP |
| 2169 | struct mtpmsg mtp_message; |
| 2170 | |
| 2171 | if (!mtppid) |
| 2172 | return false; // MTP is disabled |
| 2173 | |
| 2174 | if (mtp_write_fd < 0) { |
Ethan Yonker | d9ff3c5 | 2015-01-21 21:51:20 -0600 | [diff] [blame] | 2175 | LOGINFO("MTP: mtp_write_fd is not set\n"); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 2176 | return false; |
| 2177 | } |
| 2178 | |
| 2179 | if (Part) { |
Ethan Yonker | 4bfabab | 2014-12-29 09:15:37 -0600 | [diff] [blame] | 2180 | if (Part->MTP_Storage_ID == 0) |
| 2181 | return false; |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 2182 | if (message_type == MTP_MESSAGE_REMOVE_STORAGE) { |
| 2183 | mtp_message.message_type = MTP_MESSAGE_REMOVE_STORAGE; // Remove |
| 2184 | LOGINFO("sending message to remove %i\n", Part->MTP_Storage_ID); |
| 2185 | mtp_message.storage_id = Part->MTP_Storage_ID; |
| 2186 | if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) { |
Ethan Yonker | d9ff3c5 | 2015-01-21 21:51:20 -0600 | [diff] [blame] | 2187 | LOGINFO("error sending message to remove storage %i\n", Part->MTP_Storage_ID); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 2188 | return false; |
| 2189 | } else { |
| 2190 | LOGINFO("Message sent, remove storage ID: %i\n", Part->MTP_Storage_ID); |
| 2191 | return true; |
| 2192 | } |
| 2193 | } else if (message_type == MTP_MESSAGE_ADD_STORAGE && Part->Is_Mounted()) { |
| 2194 | mtp_message.message_type = MTP_MESSAGE_ADD_STORAGE; // Add |
| 2195 | mtp_message.storage_id = Part->MTP_Storage_ID; |
| 2196 | mtp_message.path = Part->Storage_Path.c_str(); |
| 2197 | mtp_message.display = Part->Storage_Name.c_str(); |
| 2198 | mtp_message.maxFileSize = Part->Get_Max_FileSize(); |
Ethan Yonker | 1b03920 | 2015-01-30 10:08:48 -0600 | [diff] [blame] | 2199 | LOGINFO("sending message to add %i '%s' '%s'\n", mtp_message.storage_id, mtp_message.path, mtp_message.display); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 2200 | if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) { |
Ethan Yonker | d9ff3c5 | 2015-01-21 21:51:20 -0600 | [diff] [blame] | 2201 | LOGINFO("error sending message to add storage %i\n", Part->MTP_Storage_ID); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 2202 | return false; |
| 2203 | } else { |
| 2204 | LOGINFO("Message sent, add storage ID: %i\n", Part->MTP_Storage_ID); |
| 2205 | return true; |
| 2206 | } |
| 2207 | } else { |
| 2208 | LOGERR("Unknown MTP message type: %i\n", message_type); |
| 2209 | } |
| 2210 | } else { |
| 2211 | // This hopefully never happens as the error handling should |
| 2212 | // occur in the calling function. |
Ethan Yonker | d9ff3c5 | 2015-01-21 21:51:20 -0600 | [diff] [blame] | 2213 | LOGINFO("TWPartitionManager::Add_Remove_MTP_Storage NULL partition given\n"); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 2214 | } |
| 2215 | return true; |
| 2216 | #else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2217 | gui_err("no_mtp=MTP support not included"); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 2218 | DataManager::SetValue("tw_mtp_enabled", 0); |
| 2219 | return false; |
| 2220 | #endif |
| 2221 | } |
| 2222 | |
| 2223 | bool TWPartitionManager::Add_MTP_Storage(string Mount_Point) { |
| 2224 | #ifdef TW_HAS_MTP |
| 2225 | TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point); |
| 2226 | if (Part) { |
| 2227 | return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE); |
| 2228 | } else { |
Ethan Yonker | d9ff3c5 | 2015-01-21 21:51:20 -0600 | [diff] [blame] | 2229 | LOGINFO("TWFunc::Add_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str()); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 2230 | } |
| 2231 | #endif |
| 2232 | return false; |
| 2233 | } |
| 2234 | |
| 2235 | bool TWPartitionManager::Add_MTP_Storage(unsigned int Storage_ID) { |
| 2236 | #ifdef TW_HAS_MTP |
| 2237 | TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID); |
| 2238 | if (Part) { |
| 2239 | return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE); |
| 2240 | } else { |
Ethan Yonker | d9ff3c5 | 2015-01-21 21:51:20 -0600 | [diff] [blame] | 2241 | LOGINFO("TWFunc::Add_MTP_Storage unable to locate partition for %i\n", Storage_ID); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 2242 | } |
| 2243 | #endif |
| 2244 | return false; |
| 2245 | } |
| 2246 | |
| 2247 | bool TWPartitionManager::Remove_MTP_Storage(string Mount_Point) { |
| 2248 | #ifdef TW_HAS_MTP |
| 2249 | TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point); |
| 2250 | if (Part) { |
| 2251 | return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE); |
| 2252 | } else { |
Ethan Yonker | d9ff3c5 | 2015-01-21 21:51:20 -0600 | [diff] [blame] | 2253 | LOGINFO("TWFunc::Remove_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str()); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 2254 | } |
| 2255 | #endif |
| 2256 | return false; |
| 2257 | } |
| 2258 | |
| 2259 | bool TWPartitionManager::Remove_MTP_Storage(unsigned int Storage_ID) { |
| 2260 | #ifdef TW_HAS_MTP |
| 2261 | TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID); |
| 2262 | if (Part) { |
| 2263 | return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE); |
| 2264 | } else { |
Ethan Yonker | d9ff3c5 | 2015-01-21 21:51:20 -0600 | [diff] [blame] | 2265 | LOGINFO("TWFunc::Remove_MTP_Storage unable to locate partition for %i\n", Storage_ID); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 2266 | } |
| 2267 | #endif |
| 2268 | return false; |
| 2269 | } |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2270 | |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 2271 | bool TWPartitionManager::Flash_Image(string& path, string& filename) { |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2272 | int check, partition_count = 0; |
| 2273 | TWPartition* flash_part = NULL; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2274 | string Flash_List, flash_path, full_filename; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2275 | size_t start_pos = 0, end_pos = 0; |
| 2276 | |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 2277 | full_filename = path + "/" + filename; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2278 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2279 | gui_msg("image_flash_start=[IMAGE FLASH STARTED]"); |
| 2280 | gui_msg(Msg("img_to_flash=Image to flash: '{1}'")(full_filename)); |
| 2281 | |
| 2282 | if (!TWFunc::Path_Exists(full_filename)) { |
| 2283 | if (!Mount_By_Path(full_filename, true)) { |
Ethan Yonker | b78fbdf | 2016-01-15 16:46:35 -0600 | [diff] [blame] | 2284 | return false; |
| 2285 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2286 | if (!TWFunc::Path_Exists(full_filename)) { |
| 2287 | gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(full_filename)); |
Ethan Yonker | b78fbdf | 2016-01-15 16:46:35 -0600 | [diff] [blame] | 2288 | return false; |
| 2289 | } |
| 2290 | } |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2291 | |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 2292 | PartitionSettings part_settings; |
| 2293 | part_settings.Backup_Folder = path; |
| 2294 | unsigned long long total_bytes = TWFunc::Get_File_Size(full_filename); |
| 2295 | ProgressTracking progress(total_bytes); |
| 2296 | part_settings.progress = &progress; |
| 2297 | part_settings.adbbackup = false; |
| 2298 | part_settings.PM_Method = PM_RESTORE; |
| 2299 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2300 | gui_msg("calc_restore=Calculating restore details..."); |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2301 | DataManager::GetValue("tw_flash_partition", Flash_List); |
| 2302 | if (!Flash_List.empty()) { |
| 2303 | end_pos = Flash_List.find(";", start_pos); |
| 2304 | while (end_pos != string::npos && start_pos < Flash_List.size()) { |
| 2305 | flash_path = Flash_List.substr(start_pos, end_pos - start_pos); |
| 2306 | flash_part = Find_Partition_By_Path(flash_path); |
| 2307 | if (flash_part != NULL) { |
| 2308 | partition_count++; |
| 2309 | if (partition_count > 1) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2310 | gui_err("too_many_flash=Too many partitions selected for flashing."); |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2311 | return false; |
| 2312 | } |
| 2313 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2314 | gui_msg(Msg(msg::kError, "flash_unable_locate=Unable to locate '{1}' partition for flashing.")(flash_path)); |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2315 | return false; |
| 2316 | } |
| 2317 | start_pos = end_pos + 1; |
| 2318 | end_pos = Flash_List.find(";", start_pos); |
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | if (partition_count == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2323 | gui_err("no_part_flash=No partitions selected for flashing."); |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2324 | return false; |
| 2325 | } |
| 2326 | |
| 2327 | DataManager::SetProgress(0.0); |
| 2328 | if (flash_part) { |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 2329 | flash_part->Backup_FileName = filename; |
| 2330 | if (!flash_part->Flash_Image(&part_settings)) |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2331 | return false; |
| 2332 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2333 | gui_err("invalid_flash=Invalid flash partition specified."); |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2334 | return false; |
| 2335 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2336 | gui_highlight("flash_done=IMAGE FLASH COMPLETED]"); |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2337 | return true; |
| 2338 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2339 | |
| 2340 | void TWPartitionManager::Translate_Partition(const char* path, const char* resource_name, const char* default_value) { |
| 2341 | TWPartition* part = PartitionManager.Find_Partition_By_Path(path); |
| 2342 | if (part) { |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2343 | if (part->Is_Adopted_Storage) { |
| 2344 | part->Display_Name = part->Display_Name + " - " + gui_lookup("data", "Data"); |
| 2345 | part->Backup_Display_Name = part->Display_Name; |
| 2346 | part->Storage_Name = part->Storage_Name + " - " + gui_lookup("adopted_storage", "Adopted Storage"); |
| 2347 | } else { |
| 2348 | part->Display_Name = gui_lookup(resource_name, default_value); |
| 2349 | part->Backup_Display_Name = part->Display_Name; |
| 2350 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2351 | } |
| 2352 | } |
| 2353 | |
| 2354 | void TWPartitionManager::Translate_Partition(const char* path, const char* resource_name, const char* default_value, const char* storage_resource_name, const char* storage_default_value) { |
| 2355 | TWPartition* part = PartitionManager.Find_Partition_By_Path(path); |
| 2356 | if (part) { |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2357 | if (part->Is_Adopted_Storage) { |
| 2358 | part->Display_Name = part->Display_Name + " - " + gui_lookup("data", "Data"); |
| 2359 | part->Backup_Display_Name = part->Display_Name; |
| 2360 | part->Storage_Name = part->Storage_Name + " - " + gui_lookup("adopted_storage", "Adopted Storage"); |
| 2361 | } else { |
| 2362 | part->Display_Name = gui_lookup(resource_name, default_value); |
| 2363 | part->Backup_Display_Name = part->Display_Name; |
| 2364 | if (part->Is_Storage) |
| 2365 | part->Storage_Name = gui_lookup(storage_resource_name, storage_default_value); |
| 2366 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2367 | } |
| 2368 | } |
| 2369 | |
| 2370 | void TWPartitionManager::Translate_Partition_Display_Names() { |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2371 | LOGINFO("Translating partition display names\n"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2372 | Translate_Partition("/system", "system", "System"); |
| 2373 | Translate_Partition("/system_image", "system_image", "System Image"); |
| 2374 | Translate_Partition("/vendor", "vendor", "Vendor"); |
| 2375 | Translate_Partition("/vendor_image", "vendor_image", "Vendor Image"); |
| 2376 | Translate_Partition("/cache", "cache", "Cache"); |
| 2377 | Translate_Partition("/data", "data", "Data", "internal", "Internal Storage"); |
| 2378 | Translate_Partition("/boot", "boot", "Boot"); |
| 2379 | Translate_Partition("/recovery", "recovery", "Recovery"); |
| 2380 | if (!datamedia) { |
| 2381 | Translate_Partition("/sdcard", "sdcard", "SDCard", "sdcard", "SDCard"); |
| 2382 | Translate_Partition("/internal_sd", "sdcard", "SDCard", "sdcard", "SDCard"); |
| 2383 | Translate_Partition("/internal_sdcard", "sdcard", "SDCard", "sdcard", "SDCard"); |
| 2384 | Translate_Partition("/emmc", "sdcard", "SDCard", "sdcard", "SDCard"); |
| 2385 | } |
| 2386 | Translate_Partition("/external_sd", "microsd", "Micro SDCard", "microsd", "Micro SDCard"); |
| 2387 | Translate_Partition("/external_sdcard", "microsd", "Micro SDCard", "microsd", "Micro SDCard"); |
| 2388 | Translate_Partition("/usb-otg", "usbotg", "USB OTG", "usbotg", "USB OTG"); |
| 2389 | Translate_Partition("/sd-ext", "sdext", "SD-EXT"); |
| 2390 | |
| 2391 | // Android secure is a special case |
| 2392 | TWPartition* part = PartitionManager.Find_Partition_By_Path("/and-sec"); |
| 2393 | if (part) |
| 2394 | part->Backup_Display_Name = gui_lookup("android_secure", "Android Secure"); |
| 2395 | |
| 2396 | // This updates the text on all of the storage selection buttons in the GUI |
| 2397 | DataManager::SetBackupFolder(); |
| 2398 | } |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2399 | |
| 2400 | void TWPartitionManager::Decrypt_Adopted() { |
| 2401 | #ifdef TW_INCLUDE_CRYPTO |
| 2402 | if (!Mount_By_Path("/data", false)) { |
| 2403 | LOGERR("Cannot decrypt adopted storage because /data will not mount\n"); |
| 2404 | return; |
| 2405 | } |
| 2406 | LOGINFO("Decrypt adopted storage starting\n"); |
| 2407 | char* xmlFile = PageManager::LoadFileToBuffer("/data/system/storage.xml", NULL); |
| 2408 | xml_document<> *doc = NULL; |
| 2409 | xml_node<>* volumes = NULL; |
| 2410 | xml_node<>* volume = NULL; |
| 2411 | string Primary_Storage_UUID = ""; |
| 2412 | if (xmlFile != NULL) { |
| 2413 | LOGINFO("successfully loaded storage.xml\n"); |
| 2414 | doc = new xml_document<>(); |
| 2415 | doc->parse<0>(xmlFile); |
| 2416 | volumes = doc->first_node("volumes"); |
| 2417 | if (volumes) { |
| 2418 | xml_attribute<>* psuuid = volumes->first_attribute("primaryStorageUuid"); |
| 2419 | if (psuuid) { |
| 2420 | Primary_Storage_UUID = psuuid->value(); |
| 2421 | } |
| 2422 | } |
| 2423 | } |
| 2424 | std::vector<TWPartition*>::iterator adopt; |
| 2425 | for (adopt = Partitions.begin(); adopt != Partitions.end(); adopt++) { |
| 2426 | if ((*adopt)->Removable && (*adopt)->Is_Present) { |
| 2427 | if ((*adopt)->Decrypt_Adopted() == 0) { |
| 2428 | if (volumes) { |
| 2429 | xml_node<>* volume = volumes->first_node("volume"); |
| 2430 | while (volume) { |
| 2431 | xml_attribute<>* guid = volume->first_attribute("partGuid"); |
| 2432 | if (guid) { |
| 2433 | string GUID = (*adopt)->Adopted_GUID.c_str(); |
| 2434 | GUID.insert(8, "-"); |
| 2435 | GUID.insert(13, "-"); |
| 2436 | GUID.insert(18, "-"); |
| 2437 | GUID.insert(23, "-"); |
| 2438 | |
| 2439 | if (strcasecmp(GUID.c_str(), guid->value()) == 0) { |
| 2440 | xml_attribute<>* attr = volume->first_attribute("nickname"); |
| 2441 | if (attr) { |
| 2442 | (*adopt)->Storage_Name = attr->value(); |
| 2443 | (*adopt)->Display_Name = (*adopt)->Storage_Name; |
| 2444 | (*adopt)->Backup_Display_Name = (*adopt)->Storage_Name; |
| 2445 | LOGINFO("storage name from storage.xml is '%s'\n", attr->value()); |
| 2446 | } |
| 2447 | attr = volume->first_attribute("fsUuid"); |
| 2448 | if (attr && !Primary_Storage_UUID.empty() && strcmp(Primary_Storage_UUID.c_str(), attr->value()) == 0) { |
| 2449 | TWPartition* Dat = Find_Partition_By_Path("/data"); |
| 2450 | if (Dat) { |
| 2451 | LOGINFO("Internal storage is found on adopted storage '%s'\n", (*adopt)->Display_Name.c_str()); |
| 2452 | LOGINFO("Changing '%s' to point to '%s'\n", Dat->Symlink_Mount_Point.c_str(), (*adopt)->Storage_Path.c_str()); |
| 2453 | (*adopt)->Symlink_Mount_Point = Dat->Symlink_Mount_Point; |
| 2454 | Dat->Symlink_Mount_Point = ""; |
| 2455 | // Toggle mounts to ensure that the symlink mount point (probably /sdcard) is mounted to the right location |
| 2456 | Dat->UnMount(false); |
| 2457 | Dat->Mount(false); |
| 2458 | (*adopt)->UnMount(false); |
| 2459 | (*adopt)->Mount(false); |
| 2460 | Output_Partition((*adopt)); |
| 2461 | } |
| 2462 | } |
| 2463 | break; |
| 2464 | } |
| 2465 | } |
| 2466 | volume = volume->next_sibling("volume"); |
| 2467 | } |
| 2468 | } |
| 2469 | } |
| 2470 | } |
| 2471 | } |
| 2472 | if (xmlFile) { |
| 2473 | doc->clear(); |
| 2474 | delete doc; |
| 2475 | free(xmlFile); |
| 2476 | } |
| 2477 | #else |
| 2478 | LOGINFO("Decrypt_Adopted: no crypto support\n"); |
| 2479 | return; |
| 2480 | #endif |
| 2481 | } |
Ethan Yonker | fcf3f24 | 2016-02-16 12:30:26 -0600 | [diff] [blame] | 2482 | |
| 2483 | void TWPartitionManager::Remove_Partition_By_Path(string Path) { |
| 2484 | std::vector<TWPartition*>::iterator iter; |
| 2485 | string Local_Path = TWFunc::Get_Root_Path(Path); |
| 2486 | |
| 2487 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 2488 | if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) { |
| 2489 | LOGINFO("Found and erasing '%s' from partition list\n", Local_Path.c_str()); |
| 2490 | Partitions.erase(iter); |
| 2491 | return; |
| 2492 | } |
| 2493 | } |
| 2494 | } |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame^] | 2495 | |
| 2496 | void TWPartitionManager::Set_Active_Slot(const string& Slot) { |
| 2497 | if (Slot != "A" && Slot != "B") { |
| 2498 | LOGERR("Set_Active_Slot invalid slot '%s'\n", Slot.c_str()); |
| 2499 | return; |
| 2500 | } |
| 2501 | if (Active_Slot_Display == Slot) |
| 2502 | return; |
| 2503 | LOGINFO("Setting active slot %s\n", Slot.c_str()); |
| 2504 | #ifdef AB_OTA_UPDATER |
| 2505 | if (!Active_Slot_Display.empty()) { |
| 2506 | const hw_module_t *hw_module; |
| 2507 | boot_control_module_t *module; |
| 2508 | int ret; |
| 2509 | ret = hw_get_module("bootctrl", &hw_module); |
| 2510 | if (ret != 0) { |
| 2511 | LOGERR("Error getting bootctrl module.\n"); |
| 2512 | } else { |
| 2513 | module = (boot_control_module_t*) hw_module; |
| 2514 | module->init(module); |
| 2515 | int slot_number = 0; |
| 2516 | if (Slot == "B") |
| 2517 | slot_number = 1; |
| 2518 | if (module->setActiveBootSlot(module, slot_number)) |
| 2519 | gui_msg(Msg(msg::kError, "unable_set_boot_slot=Error changing bootloader boot slot to {1}")(Slot)); |
| 2520 | } |
| 2521 | DataManager::SetValue("tw_active_slot", Slot); // Doing this outside of this if block may result in a seg fault because the DataManager may not be ready yet |
| 2522 | } |
| 2523 | #else |
| 2524 | LOGERR("Boot slot feature not present\n"); |
| 2525 | #endif |
| 2526 | Active_Slot_Display = Slot; |
| 2527 | if (Fstab_Processed()) |
| 2528 | Update_System_Details(); |
| 2529 | } |
| 2530 | string TWPartitionManager::Get_Active_Slot_Suffix() { |
| 2531 | if (Active_Slot_Display == "A") |
| 2532 | return "_a"; |
| 2533 | return "_b"; |
| 2534 | } |
| 2535 | string TWPartitionManager::Get_Active_Slot_Display() { |
| 2536 | return Active_Slot_Display; |
| 2537 | } |