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