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