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