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