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