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