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