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