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