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