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