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