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