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