blob: 9beb2575f8324749492db3b60e5ad855f5fb342e [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/* 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_Troy5bf43922012-09-07 16:07:55 -040029#include <vector>
Dees_Troy63c8df72012-09-10 14:02:05 -040030#include <dirent.h>
31#include <time.h>
Dees_Troy8170a922012-09-18 15:40:25 -040032#include <errno.h>
33#include <fcntl.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050034#include <iostream>
35#include <iomanip>
Dees_Troy51a0e822012-09-05 15:24:24 -040036#include "variables.h"
37#include "common.h"
Dees_Troy093b7642012-09-21 15:59:38 -040038#include "ui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040040#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040041#include "twrp-functions.hpp"
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040042#include "fixPermissions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050043#include "twrpDigest.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040044
Dees_Troy5bf43922012-09-07 16:07:55 -040045#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_Troy51a0e822012-09-05 15:24:24 -040053
Dees_Troy093b7642012-09-21 15:59:38 -040054extern RecoveryUI* ui;
55
Dees_Troy51a0e822012-09-05 15:24:24 -040056int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040057 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_Troy2a923582012-09-20 12:13:34 -040070 if (fstab_line[strlen(fstab_line) - 1] != '\n')
71 fstab_line[strlen(fstab_line)] = '\n';
72
Dees_Troy5bf43922012-09-07 16:07:55 -040073 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040074 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040075 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040076
Dees_Troy5bf43922012-09-07 16:07:55 -040077 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_Troy51127312012-09-08 13:08:49 -040090 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -040091 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -040092 return true;
93}
94
95int 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_Troy63c8df72012-09-10 14:02:05 -0400102 LOGI("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400103 return false;
104 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400105 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400106 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400107 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400108 fputs(Line.c_str(), fp);
Dees_Troy51127312012-09-08 13:08:49 -0400109 // 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_Troy5bf43922012-09-07 16:07:55 -0400117 }
118 }
119 fclose(fp);
120 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400121}
122
Dees_Troy8170a922012-09-18 15:40:25 -0400123void 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
131void TWPartitionManager::Output_Partition(TWPartition* Part) {
132 unsigned long long mb = 1048576;
133
Gary Peck004d48b2012-11-21 16:28:18 -0800134 printf("%s | %s | Size: %iMB", Part->Mount_Point.c_str(), Part->Actual_Block_Device.c_str(), (int)(Part->Size / mb));
Dees_Troy8170a922012-09-18 15:40:25 -0400135 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800136 printf(" Used: %iMB Free: %iMB Backup Size: %iMB", (int)(Part->Used / mb), (int)(Part->Free / mb), (int)(Part->Backup_Size / mb));
Dees_Troy8170a922012-09-18 15:40:25 -0400137 }
Gary Peck004d48b2012-11-21 16:28:18 -0800138 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_Troy68cab492012-12-12 19:29:35 +0000165 if (Part->Ignore_Blkid)
166 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000167 if (Part->Retain_Layout_Version)
168 printf("Retain_Layout_Version ");
Gary Peck004d48b2012-11-21 16:28:18 -0800169 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_Troy094207a2012-09-26 12:00:39 -0400200 if (!Part->MTD_Name.empty())
201 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400202 string back_meth = Part->Backup_Method_By_Name();
203 printf(" Backup_Method: %s\n\n", back_meth.c_str());
204}
205
Dees_Troy51a0e822012-09-05 15:24:24 -0400206int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400207 std::vector<TWPartition*>::iterator iter;
208 int ret = false;
209 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400210 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400211
Dees_Troy43d8b002012-09-17 16:00:01 -0400212 if (Local_Path == "/tmp")
213 return true;
214
Dees_Troy5bf43922012-09-07 16:07:55 -0400215 // Iterate through all partitions
216 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400217 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400218 ret = (*iter)->Mount(Display_Error);
219 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400220 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400221 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400222 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400223 }
224 if (found) {
225 return ret;
226 } else if (Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400227 LOGE("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400228 } else {
Dees_Troy51127312012-09-08 13:08:49 -0400229 LOGI("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400230 }
231 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400232}
233
234int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400235 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400236
Dees_Troy51127312012-09-08 13:08:49 -0400237 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_Troy5bf43922012-09-07 16:07:55 -0400248 }
249 if (Display_Error)
Dees_Troy51127312012-09-08 13:08:49 -0400250 LOGE("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400251 else
Dees_Troy51127312012-09-08 13:08:49 -0400252 LOGI("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400253 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400254}
255
256int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400257 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400258
Dees_Troy51127312012-09-08 13:08:49 -0400259 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_Troy5bf43922012-09-07 16:07:55 -0400270 }
271 if (Display_Error)
Dees_Troy51127312012-09-08 13:08:49 -0400272 LOGE("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400273 else
Dees_Troy51127312012-09-08 13:08:49 -0400274 LOGI("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400275 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400276}
277
278int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400279 std::vector<TWPartition*>::iterator iter;
280 int ret = false;
281 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400282 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400283
284 // Iterate through all partitions
285 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400286 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400287 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_Troy51a0e822012-09-05 15:24:24 -0400301}
302
303int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400304 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_Troy51a0e822012-09-05 15:24:24 -0400323}
324
325int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400326 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_Troy51a0e822012-09-05 15:24:24 -0400345}
346
347int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400348 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_Troy51a0e822012-09-05 15:24:24 -0400355}
356
357int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400358 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_Troy51a0e822012-09-05 15:24:24 -0400365}
366
367int TWPartitionManager::Is_Mounted_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400368 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_Troy51a0e822012-09-05 15:24:24 -0400375}
376
Dees_Troy5bf43922012-09-07 16:07:55 -0400377int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400378 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_Troy51a0e822012-09-05 15:24:24 -0400387}
388
Dees_Troy5bf43922012-09-07 16:07:55 -0400389int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
390 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
391}
392
393TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
394 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400395 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400396
397 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400398 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400399 return (*iter);
400 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400401 return NULL;
402}
403
Dees_Troy5bf43922012-09-07 16:07:55 -0400404TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400405 std::vector<TWPartition*>::iterator iter;
406
407 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400408 if ((*iter)->Primary_Block_Device == Block || (*iter)->Alternate_Block_Device == Block || ((*iter)->Is_Decrypted && (*iter)->Decrypted_Block_Device == Block))
Dees_Troy51127312012-09-08 13:08:49 -0400409 return (*iter);
410 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400411 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400412}
413
414TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400415 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_Troy5bf43922012-09-07 16:07:55 -0400421 return NULL;
422}
Dees_Troy51a0e822012-09-05 15:24:24 -0400423
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400424int 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_Troy43d8b002012-09-17 16:00:01 -0400476bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
477{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500478 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400479 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500480 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500481 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400482
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500483 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400484 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400485
Dees_Troyb46a6842012-09-25 11:06:46 -0400486 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troyc51f1f92012-09-20 15:32:13 -0400487 ui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400488
489 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500490 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_Troyc5865ab2012-09-24 15:08:04 -0400497 ui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400498 } else {
499 char filename[512];
500 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500501 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400502 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500503 strfn = filename;
504 ostringstream intToStr;
505 ostringstream fn;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400506 while (TWFunc::Path_Exists(filename) == true) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500507 intToStr << index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500508 fn << setw(3) << setfill('0') << intToStr.str();
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500509 md5sum.setfn(strfn);
510 if (md5sum.computeMD5() == 0) {
511 if (md5sum.write_md5digest() != 0)
512 {
513 ui_print(" * MD5 Error.\n");
514 return false;
515 }
516 }
517 else {
518 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400519 }
520 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400521 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500522 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400523 }
524 if (index == 0) {
525 LOGE("Backup file: '%s' not found!\n", filename);
526 return false;
527 }
Dees_Troyc5865ab2012-09-24 15:08:04 -0400528 ui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400529 }
530 return true;
531}
532
Dees_Troy093b7642012-09-21 15:59:38 -0400533bool 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_Troy43d8b002012-09-17 16:00:01 -0400534 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500535 int img_bps;
536 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400537 unsigned long total_time, remain_time, section_time;
538 int use_compression, backup_time;
539 float pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400540
541 if (Part == NULL)
542 return true;
543
Dees_Troy093b7642012-09-21 15:59:38 -0400544 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
545
546 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
547 if (use_compression)
548 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
549 else
550 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
551
552 // We know the speed for both, how far into the whole backup are we, based on time
553 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
554 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
555
556 pos = (total_time - remain_time) / (float) total_time;
557 ui->SetProgress(pos);
558
559 LOGI("Estimated Total time: %lu Estimated remaining time: %lu\n", total_time, remain_time);
560
561 // And get the time
562 if (Part->Backup_Method == 1)
563 section_time = Part->Backup_Size / file_bps;
564 else
565 section_time = Part->Backup_Size / img_bps;
566
567 // Set the position
568 pos = section_time / (float) total_time;
569 ui->ShowProgress(pos, section_time);
570
Dees_Troy43d8b002012-09-17 16:00:01 -0400571 time(&start);
572
573 if (Part->Backup(Backup_Folder)) {
Dees_Troy8170a922012-09-18 15:40:25 -0400574 if (Part->Has_SubPartition) {
575 std::vector<TWPartition*>::iterator subpart;
576
577 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
578 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
579 if (!(*subpart)->Backup(Backup_Folder))
580 return false;
581 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
582 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400583 if (Part->Backup_Method == 1) {
584 *file_bytes_remaining -= (*subpart)->Backup_Size;
585 } else {
586 *img_bytes_remaining -= (*subpart)->Backup_Size;
587 }
Dees_Troy8170a922012-09-18 15:40:25 -0400588 }
589 }
590 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400591 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400592 backup_time = (int) difftime(stop, start);
593 LOGI("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400594 if (Part->Backup_Method == 1) {
595 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400596 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400597 } else {
598 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400599 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400600 }
601 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
602 } else {
603 return false;
604 }
605}
606
607int TWPartitionManager::Run_Backup(void) {
608 int check, do_md5, partition_count = 0;
609 string Backup_Folder, Backup_Name, Full_Backup_Path;
Dees_Troy8170a922012-09-18 15:40:25 -0400610 unsigned long long total_bytes = 0, file_bytes = 0, img_bytes = 0, free_space = 0, img_bytes_remaining, file_bytes_remaining, subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400611 unsigned long img_time = 0, file_time = 0;
612 TWPartition* backup_sys = NULL;
613 TWPartition* backup_data = NULL;
614 TWPartition* backup_cache = NULL;
615 TWPartition* backup_recovery = NULL;
616 TWPartition* backup_boot = NULL;
617 TWPartition* backup_andsec = NULL;
618 TWPartition* backup_sdext = NULL;
619 TWPartition* backup_sp1 = NULL;
620 TWPartition* backup_sp2 = NULL;
621 TWPartition* backup_sp3 = NULL;
622 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400623 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400624 struct tm *t;
625 time_t start, stop, seconds, total_start, total_stop;
626 seconds = time(0);
627 t = localtime(&seconds);
628
629 time(&total_start);
630
631 Update_System_Details();
632
633 if (!Mount_Current_Storage(true))
634 return false;
635
636 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400637 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400638 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400639 else
640 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400641
Dees_Troy43d8b002012-09-17 16:00:01 -0400642 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
643 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400644 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name.empty()) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400645 char timestamp[255];
646 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);
647 Backup_Name = timestamp;
648 }
649 LOGI("Backup Name is: '%s'\n", Backup_Name.c_str());
650 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
651 LOGI("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
652
Dees_Troy43d8b002012-09-17 16:00:01 -0400653 LOGI("Calculating backup details...\n");
654 DataManager::GetValue(TW_BACKUP_SYSTEM_VAR, check);
655 if (check) {
656 backup_sys = Find_Partition_By_Path("/system");
657 if (backup_sys != NULL) {
658 partition_count++;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500659 if (backup_sys->Backup_Method == 1) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400660 file_bytes += backup_sys->Backup_Size;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500661 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400662 else
663 img_bytes += backup_sys->Backup_Size;
664 } else {
665 LOGE("Unable to locate system partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000666 DataManager::SetValue(TW_BACKUP_SYSTEM_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400667 }
668 }
669 DataManager::GetValue(TW_BACKUP_DATA_VAR, check);
670 if (check) {
671 backup_data = Find_Partition_By_Path("/data");
672 if (backup_data != NULL) {
673 partition_count++;
Dees_Troy8170a922012-09-18 15:40:25 -0400674 subpart_size = 0;
675 if (backup_data->Has_SubPartition) {
676 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
677 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == backup_data->Mount_Point)
678 subpart_size += (*subpart)->Backup_Size;
679 }
680 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400681 if (backup_data->Backup_Method == 1)
Dees_Troy8170a922012-09-18 15:40:25 -0400682 file_bytes += backup_data->Backup_Size + subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400683 else
Dees_Troy8170a922012-09-18 15:40:25 -0400684 img_bytes += backup_data->Backup_Size + subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400685 } else {
686 LOGE("Unable to locate data partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000687 DataManager::SetValue(TW_BACKUP_DATA_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400688 }
689 }
690 DataManager::GetValue(TW_BACKUP_CACHE_VAR, check);
691 if (check) {
692 backup_cache = Find_Partition_By_Path("/cache");
693 if (backup_cache != NULL) {
694 partition_count++;
695 if (backup_cache->Backup_Method == 1)
696 file_bytes += backup_cache->Backup_Size;
697 else
698 img_bytes += backup_cache->Backup_Size;
699 } else {
700 LOGE("Unable to locate cache partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000701 DataManager::SetValue(TW_BACKUP_CACHE_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400702 }
703 }
704 DataManager::GetValue(TW_BACKUP_RECOVERY_VAR, check);
705 if (check) {
706 backup_recovery = Find_Partition_By_Path("/recovery");
707 if (backup_recovery != NULL) {
708 partition_count++;
709 if (backup_recovery->Backup_Method == 1)
710 file_bytes += backup_recovery->Backup_Size;
711 else
712 img_bytes += backup_recovery->Backup_Size;
713 } else {
714 LOGE("Unable to locate recovery partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000715 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400716 }
717 }
ChampionSwimmercf33e4d2013-02-03 13:59:22 +0530718#ifndef TW_HAS_NO_BOOT_PARTITION
Dees_Troy43d8b002012-09-17 16:00:01 -0400719 DataManager::GetValue(TW_BACKUP_BOOT_VAR, check);
720 if (check) {
721 backup_boot = Find_Partition_By_Path("/boot");
722 if (backup_boot != NULL) {
723 partition_count++;
724 if (backup_boot->Backup_Method == 1)
725 file_bytes += backup_boot->Backup_Size;
726 else
727 img_bytes += backup_boot->Backup_Size;
728 } else {
729 LOGE("Unable to locate boot partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000730 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400731 }
732 }
ChampionSwimmercf33e4d2013-02-03 13:59:22 +0530733#endif
Dees_Troy43d8b002012-09-17 16:00:01 -0400734 DataManager::GetValue(TW_BACKUP_ANDSEC_VAR, check);
735 if (check) {
736 backup_andsec = Find_Partition_By_Path("/and-sec");
737 if (backup_andsec != NULL) {
738 partition_count++;
739 if (backup_andsec->Backup_Method == 1)
740 file_bytes += backup_andsec->Backup_Size;
741 else
742 img_bytes += backup_andsec->Backup_Size;
743 } else {
744 LOGE("Unable to locate android secure partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000745 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400746 }
747 }
748 DataManager::GetValue(TW_BACKUP_SDEXT_VAR, check);
749 if (check) {
750 backup_sdext = Find_Partition_By_Path("/sd-ext");
751 if (backup_sdext != NULL) {
752 partition_count++;
753 if (backup_sdext->Backup_Method == 1)
754 file_bytes += backup_sdext->Backup_Size;
755 else
756 img_bytes += backup_sdext->Backup_Size;
757 } else {
758 LOGE("Unable to locate sd-ext partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000759 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400760 }
761 }
762#ifdef SP1_NAME
763 DataManager::GetValue(TW_BACKUP_SP1_VAR, check);
764 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400765 backup_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400766 if (backup_sp1 != NULL) {
767 partition_count++;
768 if (backup_sp1->Backup_Method == 1)
769 file_bytes += backup_sp1->Backup_Size;
770 else
771 img_bytes += backup_sp1->Backup_Size;
772 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400773 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000774 DataManager::SetValue(TW_BACKUP_SP1_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400775 }
776 }
777#endif
778#ifdef SP2_NAME
779 DataManager::GetValue(TW_BACKUP_SP2_VAR, check);
780 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400781 backup_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400782 if (backup_sp2 != NULL) {
783 partition_count++;
784 if (backup_sp2->Backup_Method == 1)
785 file_bytes += backup_sp2->Backup_Size;
786 else
787 img_bytes += backup_sp2->Backup_Size;
788 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400789 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000790 DataManager::SetValue(TW_BACKUP_SP2_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400791 }
792 }
793#endif
794#ifdef SP3_NAME
795 DataManager::GetValue(TW_BACKUP_SP3_VAR, check);
796 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400797 backup_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400798 if (backup_sp3 != NULL) {
799 partition_count++;
800 if (backup_sp3->Backup_Method == 1)
801 file_bytes += backup_sp3->Backup_Size;
802 else
803 img_bytes += backup_sp3->Backup_Size;
804 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400805 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000806 DataManager::SetValue(TW_BACKUP_SP3_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400807 }
808 }
809#endif
810
811 if (partition_count == 0) {
812 ui_print("No partitions selected for backup.\n");
813 return false;
814 }
815 total_bytes = file_bytes + img_bytes;
816 ui_print(" * Total number of partitions to back up: %d\n", partition_count);
817 ui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
818 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
819 if (storage != NULL) {
820 free_space = storage->Free;
821 ui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
822 } else {
823 LOGE("Unable to locate storage device.\n");
824 return false;
825 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000826 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400827 // We require an extra 32MB just in case
828 LOGE("Not enough free space on storage.\n");
829 return false;
830 }
831 img_bytes_remaining = img_bytes;
832 file_bytes_remaining = file_bytes;
833
Dees_Troyd4b22b02013-01-18 17:17:58 +0000834 ui_print("\n[BACKUP STARTED]\n");
835 ui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
836 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
837 LOGE("Failed to make backup folder.\n");
838 return false;
839 }
840
Dees_Troy093b7642012-09-21 15:59:38 -0400841 ui->SetProgress(0.0);
842
843 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_Troy43d8b002012-09-17 16:00:01 -0400844 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400845 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_Troy43d8b002012-09-17 16:00:01 -0400846 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400847 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_Troy43d8b002012-09-17 16:00:01 -0400848 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400849 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_Troy43d8b002012-09-17 16:00:01 -0400850 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400851 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_Troy43d8b002012-09-17 16:00:01 -0400852 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400853 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_Troy43d8b002012-09-17 16:00:01 -0400854 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400855 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_Troy43d8b002012-09-17 16:00:01 -0400856 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400857 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_Troy43d8b002012-09-17 16:00:01 -0400858 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400859 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_Troy43d8b002012-09-17 16:00:01 -0400860 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400861 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_Troy43d8b002012-09-17 16:00:01 -0400862 return false;
863
864 // Average BPS
865 if (img_time == 0)
866 img_time = 1;
867 if (file_time == 0)
868 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400869 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500870 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400871
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500872 ui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400873 ui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
874
875 time(&total_stop);
876 int total_time = (int) difftime(total_stop, total_start);
877 unsigned long long actual_backup_size = TWFunc::Get_Folder_Size(Full_Backup_Path, true);
878 actual_backup_size /= (1024LLU * 1024LLU);
879
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500880 int prev_img_bps, use_compression;
881 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400882 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
883 img_bps += (prev_img_bps * 4);
884 img_bps /= 5;
885
886 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
887 if (use_compression)
888 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
889 else
890 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
891 file_bps += (prev_file_bps * 4);
892 file_bps /= 5;
893
894 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
895 if (use_compression)
896 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
897 else
898 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
899
Dees_Troy43d8b002012-09-17 16:00:01 -0400900 ui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
901 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400902 UnMount_Main_Partitions();
Dees_Troy43d8b002012-09-17 16:00:01 -0400903 ui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000904 string backup_log = Full_Backup_Path + "recovery.log";
905 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
906 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400907}
908
Dees_Troy093b7642012-09-21 15:59:38 -0400909bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400910 time_t Start, Stop;
911 time(&Start);
Dees_Troy093b7642012-09-21 15:59:38 -0400912 ui->ShowProgress(1.0 / (float)partition_count, 150);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400913 if (!Part->Restore(Restore_Name))
914 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400915 if (Part->Has_SubPartition) {
916 std::vector<TWPartition*>::iterator subpart;
917
918 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
919 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
920 if (!(*subpart)->Restore(Restore_Name))
921 return false;
922 }
923 }
924 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400925 time(&Stop);
926 ui_print("[%s done (%d seconds)]\n\n", Part->Display_Name.c_str(), (int)difftime(Stop, Start));
927 return true;
928}
929
Dees_Troy51a0e822012-09-05 15:24:24 -0400930int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400931 int check_md5, check, partition_count = 0;
932 TWPartition* restore_sys = NULL;
933 TWPartition* restore_data = NULL;
934 TWPartition* restore_cache = NULL;
935 TWPartition* restore_boot = NULL;
936 TWPartition* restore_andsec = NULL;
937 TWPartition* restore_sdext = NULL;
938 TWPartition* restore_sp1 = NULL;
939 TWPartition* restore_sp2 = NULL;
940 TWPartition* restore_sp3 = NULL;
941 time_t rStart, rStop;
942 time(&rStart);
Dees_Troy43d8b002012-09-17 16:00:01 -0400943
Dees_Troy4a2a1262012-09-18 09:33:47 -0400944 ui_print("\n[RESTORE STARTED]\n\n");
945 ui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400946
Dees_Troy4a2a1262012-09-18 09:33:47 -0400947 if (!Mount_Current_Storage(true))
948 return false;
949
950 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
951 DataManager::GetValue(TW_RESTORE_SYSTEM_VAR, check);
Dees_Troy63c8df72012-09-10 14:02:05 -0400952 if (check > 0) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400953 restore_sys = Find_Partition_By_Path("/system");
954 if (restore_sys == NULL) {
955 LOGE("Unable to locate system partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000956 } else {
957 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400958 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400959 }
960 DataManager::GetValue(TW_RESTORE_DATA_VAR, check);
961 if (check > 0) {
962 restore_data = Find_Partition_By_Path("/data");
963 if (restore_data == NULL) {
964 LOGE("Unable to locate data partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000965 } else {
966 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400967 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400968 }
969 DataManager::GetValue(TW_RESTORE_CACHE_VAR, check);
970 if (check > 0) {
971 restore_cache = Find_Partition_By_Path("/cache");
972 if (restore_cache == NULL) {
973 LOGE("Unable to locate cache partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000974 } else {
975 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400976 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400977 }
ChampionSwimmercf33e4d2013-02-03 13:59:22 +0530978#ifndef TW_HAS_NO_BOOT_PARTITION
Dees_Troy4a2a1262012-09-18 09:33:47 -0400979 DataManager::GetValue(TW_RESTORE_BOOT_VAR, check);
980 if (check > 0) {
981 restore_boot = Find_Partition_By_Path("/boot");
982 if (restore_boot == NULL) {
983 LOGE("Unable to locate boot partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000984 } else {
985 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400986 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400987 }
ChampionSwimmercf33e4d2013-02-03 13:59:22 +0530988#endif
Dees_Troy4a2a1262012-09-18 09:33:47 -0400989 DataManager::GetValue(TW_RESTORE_ANDSEC_VAR, check);
990 if (check > 0) {
991 restore_andsec = Find_Partition_By_Path("/and-sec");
992 if (restore_andsec == NULL) {
993 LOGE("Unable to locate android secure partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000994 } else {
995 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400996 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400997 }
998 DataManager::GetValue(TW_RESTORE_SDEXT_VAR, check);
999 if (check > 0) {
1000 restore_sdext = Find_Partition_By_Path("/sd-ext");
1001 if (restore_sdext == NULL) {
1002 LOGE("Unable to locate sd-ext partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001003 } else {
1004 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001005 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001006 }
1007#ifdef SP1_NAME
1008 DataManager::GetValue(TW_RESTORE_SP1_VAR, check);
1009 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001010 restore_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001011 if (restore_sp1 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001012 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001013 } else {
1014 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001015 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001016 }
1017#endif
1018#ifdef SP2_NAME
1019 DataManager::GetValue(TW_RESTORE_SP2_VAR, check);
1020 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001021 restore_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001022 if (restore_sp2 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001023 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001024 } else {
1025 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001026 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001027 }
1028#endif
1029#ifdef SP3_NAME
1030 DataManager::GetValue(TW_RESTORE_SP3_VAR, check);
1031 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001032 restore_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001033 if (restore_sp3 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001034 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001035 } else {
1036 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001037 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001038 }
1039#endif
1040
1041 if (partition_count == 0) {
1042 LOGE("No partitions selected for restore.\n");
1043 return false;
1044 }
1045
1046 if (check_md5 > 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001047 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
Dees_Troyb46a6842012-09-25 11:06:46 -04001048 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001049 ui_print("Verifying MD5...\n");
1050 if (restore_sys != NULL && !restore_sys->Check_MD5(Restore_Name))
1051 return false;
1052 if (restore_data != NULL && !restore_data->Check_MD5(Restore_Name))
1053 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001054 if (restore_data != NULL && restore_data->Has_SubPartition) {
1055 std::vector<TWPartition*>::iterator subpart;
1056
1057 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1058 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_data->Mount_Point) {
1059 if (!(*subpart)->Check_MD5(Restore_Name))
1060 return false;
1061 }
1062 }
1063 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001064 if (restore_cache != NULL && !restore_cache->Check_MD5(Restore_Name))
1065 return false;
1066 if (restore_boot != NULL && !restore_boot->Check_MD5(Restore_Name))
1067 return false;
1068 if (restore_andsec != NULL && !restore_andsec->Check_MD5(Restore_Name))
1069 return false;
1070 if (restore_sdext != NULL && !restore_sdext->Check_MD5(Restore_Name))
1071 return false;
1072 if (restore_sp1 != NULL && !restore_sp1->Check_MD5(Restore_Name))
1073 return false;
1074 if (restore_sp2 != NULL && !restore_sp2->Check_MD5(Restore_Name))
1075 return false;
1076 if (restore_sp3 != NULL && !restore_sp3->Check_MD5(Restore_Name))
1077 return false;
1078 ui_print("Done verifying MD5.\n");
1079 } else
1080 ui_print("Skipping MD5 check based on user setting.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001081
Dees_Troy4a2a1262012-09-18 09:33:47 -04001082 ui_print("Restoring %i partitions...\n", partition_count);
Dees_Troy093b7642012-09-21 15:59:38 -04001083 ui->SetProgress(0.0);
1084 if (restore_sys != NULL && !Restore_Partition(restore_sys, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001085 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001086 if (restore_data != NULL && !Restore_Partition(restore_data, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001087 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001088 if (restore_cache != NULL && !Restore_Partition(restore_cache, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001089 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001090 if (restore_boot != NULL && !Restore_Partition(restore_boot, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001091 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001092 if (restore_andsec != NULL && !Restore_Partition(restore_andsec, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001093 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001094 if (restore_sdext != NULL && !Restore_Partition(restore_sdext, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001095 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001096 if (restore_sp1 != NULL && !Restore_Partition(restore_sp1, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001097 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001098 if (restore_sp2 != NULL && !Restore_Partition(restore_sp2, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001099 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001100 if (restore_sp3 != NULL && !Restore_Partition(restore_sp3, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001101 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001102
Dees_Troyb46a6842012-09-25 11:06:46 -04001103 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -04001104 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001105 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -04001106 time(&rStop);
1107 ui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -04001108 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001109}
1110
1111void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001112 // Start with the default values
1113 int tw_restore_system = -1;
1114 int tw_restore_data = -1;
1115 int tw_restore_cache = -1;
1116 int tw_restore_recovery = -1;
1117 int tw_restore_boot = -1;
1118 int tw_restore_andsec = -1;
1119 int tw_restore_sdext = -1;
1120 int tw_restore_sp1 = -1;
1121 int tw_restore_sp2 = -1;
1122 int tw_restore_sp3 = -1;
1123 bool get_date = true;
1124
1125 DIR* d;
1126 d = opendir(Restore_Name.c_str());
1127 if (d == NULL)
1128 {
1129 LOGE("Error opening %s\n", Restore_Name.c_str());
1130 return;
1131 }
1132
1133 struct dirent* de;
1134 while ((de = readdir(d)) != NULL)
1135 {
1136 // Strip off three components
1137 char str[256];
1138 char* label;
1139 char* fstype = NULL;
1140 char* extn = NULL;
1141 char* ptr;
1142
1143 strcpy(str, de->d_name);
1144 if (strlen(str) <= 2)
1145 continue;
1146
1147 if (get_date) {
1148 char file_path[255];
1149 struct stat st;
1150
1151 strcpy(file_path, Restore_Name.c_str());
1152 strcat(file_path, "/");
1153 strcat(file_path, str);
1154 stat(file_path, &st);
1155 string backup_date = ctime((const time_t*)(&st.st_mtime));
1156 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
1157 get_date = false;
1158 }
1159
1160 label = str;
1161 ptr = label;
1162 while (*ptr && *ptr != '.') ptr++;
1163 if (*ptr == '.')
1164 {
1165 *ptr = 0x00;
1166 ptr++;
1167 fstype = ptr;
1168 }
1169 while (*ptr && *ptr != '.') ptr++;
1170 if (*ptr == '.')
1171 {
1172 *ptr = 0x00;
1173 ptr++;
1174 extn = ptr;
1175 }
1176
1177 if (extn == NULL || (strlen(extn) >= 3 && strncmp(extn, "win", 3) != 0)) continue;
1178
1179 TWPartition* Part = Find_Partition_By_Path(label);
1180 if (Part == NULL)
1181 {
1182 LOGE(" Unable to locate partition by backup name: '%s'\n", label);
1183 continue;
1184 }
1185
1186 Part->Backup_FileName = de->d_name;
1187 if (strlen(extn) > 3) {
1188 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1189 }
1190
1191 // Now, we just need to find the correct label
Dees_Troye58d5262012-09-21 12:27:57 -04001192 if (Part->Backup_Path == "/system")
Dees_Troy63c8df72012-09-10 14:02:05 -04001193 tw_restore_system = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001194 if (Part->Backup_Path == "/data")
Dees_Troy63c8df72012-09-10 14:02:05 -04001195 tw_restore_data = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001196 if (Part->Backup_Path == "/cache")
Dees_Troy63c8df72012-09-10 14:02:05 -04001197 tw_restore_cache = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001198 if (Part->Backup_Path == "/recovery")
Dees_Troy63c8df72012-09-10 14:02:05 -04001199 tw_restore_recovery = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001200 if (Part->Backup_Path == "/boot")
Dees_Troy63c8df72012-09-10 14:02:05 -04001201 tw_restore_boot = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001202 if (Part->Backup_Path == "/and-sec")
Dees_Troy63c8df72012-09-10 14:02:05 -04001203 tw_restore_andsec = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001204 if (Part->Backup_Path == "/sd-ext")
Dees_Troy63c8df72012-09-10 14:02:05 -04001205 tw_restore_sdext = 1;
1206#ifdef SP1_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001207 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP1_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001208 tw_restore_sp1 = 1;
1209#endif
1210#ifdef SP2_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001211 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP2_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001212 tw_restore_sp2 = 1;
1213#endif
1214#ifdef SP3_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001215 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP3_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001216 tw_restore_sp3 = 1;
1217#endif
1218 }
1219 closedir(d);
1220
1221 // Set the final values
1222 DataManager::SetValue(TW_RESTORE_SYSTEM_VAR, tw_restore_system);
1223 DataManager::SetValue(TW_RESTORE_DATA_VAR, tw_restore_data);
1224 DataManager::SetValue(TW_RESTORE_CACHE_VAR, tw_restore_cache);
1225 DataManager::SetValue(TW_RESTORE_RECOVERY_VAR, tw_restore_recovery);
1226 DataManager::SetValue(TW_RESTORE_BOOT_VAR, tw_restore_boot);
1227 DataManager::SetValue(TW_RESTORE_ANDSEC_VAR, tw_restore_andsec);
1228 DataManager::SetValue(TW_RESTORE_SDEXT_VAR, tw_restore_sdext);
1229 DataManager::SetValue(TW_RESTORE_SP1_VAR, tw_restore_sp1);
1230 DataManager::SetValue(TW_RESTORE_SP2_VAR, tw_restore_sp2);
1231 DataManager::SetValue(TW_RESTORE_SP3_VAR, tw_restore_sp3);
1232
Dees_Troy51a0e822012-09-05 15:24:24 -04001233 return;
1234}
1235
1236int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001237 std::vector<TWPartition*>::iterator iter;
1238 int ret = false;
1239 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001240 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001241
1242 // Iterate through all partitions
1243 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001244 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001245 if (Path == "/and-sec")
1246 ret = (*iter)->Wipe_AndSec();
1247 else
1248 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001249 found = true;
1250 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1251 (*iter)->Wipe();
1252 }
1253 }
1254 if (found) {
1255 return ret;
1256 } else
1257 LOGE("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1258 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001259}
1260
1261int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001262 TWPartition* Part = Find_Partition_By_Block(Block);
1263
1264 if (Part) {
1265 if (Part->Has_SubPartition) {
1266 std::vector<TWPartition*>::iterator subpart;
1267
1268 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1269 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1270 (*subpart)->Wipe();
1271 }
1272 return Part->Wipe();
1273 } else
1274 return Part->Wipe();
1275 }
1276 LOGE("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
1277 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001278}
1279
1280int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001281 TWPartition* Part = Find_Partition_By_Name(Name);
1282
1283 if (Part) {
1284 if (Part->Has_SubPartition) {
1285 std::vector<TWPartition*>::iterator subpart;
1286
1287 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1288 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1289 (*subpart)->Wipe();
1290 }
1291 return Part->Wipe();
1292 } else
1293 return Part->Wipe();
1294 }
1295 LOGE("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
1296 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001297}
1298
1299int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001300 std::vector<TWPartition*>::iterator iter;
1301 int ret = true;
1302
1303 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001304 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001305 if (!(*iter)->Wipe())
1306 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001307 } else if ((*iter)->Has_Android_Secure) {
1308 if (!(*iter)->Wipe_AndSec())
1309 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001310 }
1311 }
1312 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001313}
1314
Dees_Troy38bd7602012-09-14 13:33:53 -04001315int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1316 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001317 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001318
1319 if (!Mount_By_Path("/data", true))
1320 return false;
1321
1322 if (!Mount_By_Path("/cache", true))
1323 return false;
1324
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001325 dir.push_back("/data/dalvik-cache");
1326 dir.push_back("/cache/dalvik-cache");
1327 dir.push_back("/cache/dc");
Dees_Troy38bd7602012-09-14 13:33:53 -04001328 ui_print("\nWiping Dalvik Cache Directories...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001329 for (int i = 0; i < dir.size(); ++i) {
1330 if (stat(dir.at(i).c_str(), &st) == 0) {
1331 TWFunc::removeDir(dir.at(i), false);
1332 ui_print("Cleaned: %s...\n", dir.at(i).c_str());
1333 }
1334 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001335 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
1336 if (sdext != NULL) {
1337 if (sdext->Is_Present && sdext->Mount(false)) {
1338 if (stat("/sd-ext/dalvik-cache", &st) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001339 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
1340 ui_print("Cleaned: /sd-ext/dalvik-cache...\n");
1341 }
1342 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001343 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001344 ui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
1345 return true;
1346}
1347
1348int TWPartitionManager::Wipe_Rotate_Data(void) {
1349 if (!Mount_By_Path("/data", true))
1350 return false;
1351
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001352 unlink("/data/misc/akmd*");
1353 unlink("/data/misc/rild*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001354 ui_print("Rotation data wiped.\n");
1355 return true;
1356}
1357
1358int TWPartitionManager::Wipe_Battery_Stats(void) {
1359 struct stat st;
1360
1361 if (!Mount_By_Path("/data", true))
1362 return false;
1363
1364 if (0 != stat("/data/system/batterystats.bin", &st)) {
1365 ui_print("No Battery Stats Found. No Need To Wipe.\n");
1366 } else {
1367 remove("/data/system/batterystats.bin");
1368 ui_print("Cleared battery stats.\n");
1369 }
1370 return true;
1371}
1372
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001373int TWPartitionManager::Wipe_Android_Secure(void) {
1374 std::vector<TWPartition*>::iterator iter;
1375 int ret = false;
1376 bool found = false;
1377
1378 // Iterate through all partitions
1379 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1380 if ((*iter)->Has_Android_Secure) {
1381 ret = (*iter)->Wipe_AndSec();
1382 found = true;
1383 }
1384 }
1385 if (found) {
1386 return ret;
1387 } else {
1388 LOGE("No android secure partitions found.\n");
1389 }
1390 return false;
1391}
1392
Dees_Troy38bd7602012-09-14 13:33:53 -04001393int TWPartitionManager::Format_Data(void) {
1394 TWPartition* dat = Find_Partition_By_Path("/data");
1395
1396 if (dat != NULL) {
1397 if (!dat->UnMount(true))
1398 return false;
1399
1400 return dat->Wipe_Encryption();
1401 } else {
1402 LOGE("Unable to locate /data.\n");
1403 return false;
1404 }
1405 return false;
1406}
1407
1408int TWPartitionManager::Wipe_Media_From_Data(void) {
1409 TWPartition* dat = Find_Partition_By_Path("/data");
1410
1411 if (dat != NULL) {
1412 if (!dat->Has_Data_Media) {
1413 LOGE("This device does not have /data/media\n");
1414 return false;
1415 }
1416 if (!dat->Mount(true))
1417 return false;
1418
1419 ui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001420 TWFunc::removeDir("/data/media", false);
1421 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
1422 return -1;
Dees_Troy38bd7602012-09-14 13:33:53 -04001423 if (dat->Has_Data_Media) {
1424 dat->Recreate_Media_Folder();
1425 }
1426 return true;
1427 } else {
1428 LOGE("Unable to locate /data.\n");
1429 return false;
1430 }
1431 return false;
1432}
1433
Dees_Troy51a0e822012-09-05 15:24:24 -04001434void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001435 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001436 return;
1437}
1438
1439void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001440 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001441 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001442
Dees_Troy32c8eb82012-09-11 15:28:06 -04001443 ui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001444 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001445 if ((*iter)->Can_Be_Mounted) {
1446 (*iter)->Update_Size(true);
1447 if ((*iter)->Mount_Point == "/system") {
1448 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1449 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1450 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1451 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1452 } else if ((*iter)->Mount_Point == "/cache") {
1453 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1454 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1455 } else if ((*iter)->Mount_Point == "/sd-ext") {
1456 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1457 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1458 if ((*iter)->Backup_Size == 0) {
1459 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1460 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1461 } else
1462 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001463 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001464 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001465 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001466 if ((*iter)->Backup_Size == 0) {
1467 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1468 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1469 } else
1470 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001471 } else if ((*iter)->Mount_Point == "/boot") {
1472 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1473 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1474 if ((*iter)->Backup_Size == 0) {
1475 DataManager::SetValue("tw_has_boot_partition", 0);
1476 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1477 } else
1478 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001479 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001480#ifdef SP1_NAME
1481 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1482 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1483 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1484 }
1485#endif
1486#ifdef SP2_NAME
1487 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1488 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1489 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1490 }
1491#endif
1492#ifdef SP3_NAME
1493 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1494 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1495 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1496 }
1497#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001498 } else {
1499 // Handle unmountable partitions in case we reset defaults
1500 if ((*iter)->Mount_Point == "/boot") {
1501 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1502 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1503 if ((*iter)->Backup_Size == 0) {
1504 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1505 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1506 } else
1507 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1508 } else if ((*iter)->Mount_Point == "/recovery") {
1509 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1510 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1511 if ((*iter)->Backup_Size == 0) {
1512 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1513 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1514 } else
1515 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001516 } else if ((*iter)->Mount_Point == "/data") {
1517 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001518 }
1519#ifdef SP1_NAME
1520 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1521 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1522 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1523 }
1524#endif
1525#ifdef SP2_NAME
1526 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1527 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1528 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1529 }
1530#endif
1531#ifdef SP3_NAME
1532 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1533 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1534 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1535 }
1536#endif
Dees_Troy51127312012-09-08 13:08:49 -04001537 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001538 }
Dees_Troy51127312012-09-08 13:08:49 -04001539 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1540 string current_storage_path = DataManager::GetCurrentStoragePath();
1541 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001542 if (FreeStorage != NULL) {
1543 // Attempt to mount storage
1544 if (!FreeStorage->Mount(false)) {
1545 // We couldn't mount storage... check to see if we have dual storage
1546 int has_dual_storage;
1547 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1548 if (has_dual_storage == 1) {
1549 // We have dual storage, see if we're using the internal storage that should always be present
1550 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001551 if (!FreeStorage->Is_Encrypted) {
1552 // Not able to use internal, so error!
1553 LOGE("Unable to mount internal storage.\n");
1554 }
Dees_Troy8170a922012-09-18 15:40:25 -04001555 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1556 } else {
1557 // We were using external, flip to internal
1558 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1559 current_storage_path = DataManager::GetCurrentStoragePath();
1560 FreeStorage = Find_Partition_By_Path(current_storage_path);
1561 if (FreeStorage != NULL) {
1562 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1563 } else {
1564 LOGE("Unable to locate internal storage partition.\n");
1565 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1566 }
1567 }
1568 } else {
1569 // No dual storage and unable to mount storage, error!
1570 LOGE("Unable to mount storage.\n");
1571 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1572 }
1573 } else {
1574 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1575 }
1576 } else {
Dees_Troy51127312012-09-08 13:08:49 -04001577 LOGI("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001578 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001579 if (!Write_Fstab())
1580 LOGE("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001581 return;
1582}
1583
1584int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001585#ifdef TW_INCLUDE_CRYPTO
1586 int ret_val, password_len;
1587 char crypto_blkdev[255], cPassword[255];
1588 size_t result;
1589
1590 property_set("ro.crypto.state", "encrypted");
1591#ifdef TW_INCLUDE_JB_CRYPTO
1592 // No extra flags needed
1593#else
1594 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1595 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1596 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1597 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1598 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1599 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001600
1601#ifdef CRYPTO_SD_FS_TYPE
1602 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1603 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1604 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001605#endif
a39552696ff55ce2013-01-08 16:14:56 +00001606
1607 property_set("rw.km_fips_status", "ready");
1608
1609#endif
1610
1611 // some samsung devices store "footer" on efs partition
1612 TWPartition *efs = Find_Partition_By_Path("/efs");
1613 if(efs && !efs->Is_Mounted())
1614 efs->Mount(false);
1615 else
1616 efs = 0;
1617#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001618#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001619 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001620 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001621 property_set("ro.crypto.external_encrypted", "1");
1622 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1623 } else {
1624 property_set("ro.crypto.external_encrypted", "0");
1625 }
1626#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001627#endif
a39552696ff55ce2013-01-08 16:14:56 +00001628
Dees_Troy5bf43922012-09-07 16:07:55 -04001629 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001630 int pwret = cryptfs_check_passwd(cPassword);
1631
1632 if (pwret != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001633 LOGE("Failed to decrypt data.\n");
1634 return -1;
1635 }
a39552696ff55ce2013-01-08 16:14:56 +00001636
1637 if(efs)
1638 efs->UnMount(false);
1639
Dees_Troy5bf43922012-09-07 16:07:55 -04001640 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1641 if (strcmp(crypto_blkdev, "error") == 0) {
1642 LOGE("Error retrieving decrypted data block device.\n");
1643 } else {
1644 TWPartition* dat = Find_Partition_By_Path("/data");
1645 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001646 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001647 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1648 dat->Is_Decrypted = true;
1649 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001650 dat->Setup_File_System(false);
Dees_Troy32c8eb82012-09-11 15:28:06 -04001651 ui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001652
1653#ifdef CRYPTO_SD_FS_TYPE
1654 char crypto_blkdev_sd[255];
1655 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1656 if (strcmp(crypto_blkdev_sd, "error") == 0) {
1657 LOGE("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001658 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001659 emmc->Is_Decrypted = true;
1660 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1661 emmc->Setup_File_System(false);
1662 ui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
1663 }
a39552696ff55ce2013-01-08 16:14:56 +00001664#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001665#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001666#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001667 char is_external_decrypted[255];
1668 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1669 if (strcmp(is_external_decrypted, "1") == 0) {
1670 sdcard->Is_Decrypted = true;
1671 sdcard->EcryptFS_Password = Password;
1672 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001673 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1674 MetaEcfsFile += "/.MetaEcfsFile";
1675 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1676 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1677 sdcard->UnMount(false);
1678 sdcard->Mount(false);
1679 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001680 } else {
1681 sdcard->Is_Decrypted = false;
1682 sdcard->Decrypted_Block_Device = "";
1683 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001684#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001685#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001686
Dees_Troy5bf43922012-09-07 16:07:55 -04001687 // Sleep for a bit so that the device will be ready
1688 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001689#ifdef RECOVERY_SDCARD_ON_DATA
1690 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1691 dat->Storage_Path = "/data/media/0";
1692 dat->Symlink_Path = dat->Storage_Path;
1693 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
1694 dat->UnMount(false);
1695 DataManager::SetBackupFolder();
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001696 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001697 }
1698#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001699 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001700 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001701 } else
1702 LOGE("Unable to locate data partition.\n");
1703 }
1704 return 0;
1705#else
1706 LOGE("No crypto support was compiled into this build.\n");
1707 return -1;
1708#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001709 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001710}
1711
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001712int TWPartitionManager::Fix_Permissions(void) {
1713 int result = 0;
1714 if (!Mount_By_Path("/data", true))
1715 return false;
1716
1717 if (!Mount_By_Path("/system", true))
1718 return false;
1719
1720 Mount_By_Path("/sd-ext", false);
1721
1722 fixPermissions perms;
1723 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001724 UnMount_Main_Partitions();
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001725 ui_print("Done.\n\n");
1726 return result;
1727}
1728
Dees_Troy8170a922012-09-18 15:40:25 -04001729//partial kangbang from system/vold
Dees_Troyd21618c2012-10-14 18:48:49 -04001730int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
1731 int fd;
1732 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1733
1734 if (Part == NULL) {
1735 LOGE("Unable to locate volume information for USB storage mode.");
1736 return false;
1737 }
1738 if (!Part->UnMount(true))
1739 return false;
1740
1741 if ((fd = open(Lun_File.c_str(), O_WRONLY)) < 0) {
1742 LOGE("Unable to open ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
1743 return false;
1744 }
1745
1746 if (write(fd, Part->Actual_Block_Device.c_str(), Part->Actual_Block_Device.size()) < 0) {
1747 LOGE("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
1748 close(fd);
1749 return false;
1750 }
1751 close(fd);
1752 return true;
1753}
1754
Dees_Troy8170a922012-09-18 15:40:25 -04001755int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001756 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001757 char lun_file[255];
Dees_Troy8170a922012-09-18 15:40:25 -04001758 string ext_path;
Dees_Troyd21618c2012-10-14 18:48:49 -04001759 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001760
1761 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1762 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1763 if (has_dual == 1 && has_data_media == 0) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001764 string Lun_File_str = CUSTOM_LUN_FILE;
1765 size_t found = Lun_File_str.find("%");
1766 if (found != string::npos) {
1767 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1768 if (TWFunc::Path_Exists(lun_file))
1769 has_multiple_lun = true;
1770 }
1771 if (!has_multiple_lun) {
Dees_Troyf4ca6122012-10-13 22:17:20 -04001772 // Device doesn't have multiple lun files, mount current storage
Dees_Troyf4ca6122012-10-13 22:17:20 -04001773 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001774 return Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001775 } else {
1776 // Device has multiple lun files
Dees_Troyf4ca6122012-10-13 22:17:20 -04001777 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001778 if (!Open_Lun_File(DataManager::GetSettingsStoragePath(), lun_file))
Dees_Troyf4ca6122012-10-13 22:17:20 -04001779 return false;
Dees_Troyf4ca6122012-10-13 22:17:20 -04001780 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001781 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Dees_Troyd21618c2012-10-14 18:48:49 -04001782 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001783 }
Dees_Troy8170a922012-09-18 15:40:25 -04001784 } else {
1785 if (has_data_media == 0)
1786 ext_path = DataManager::GetCurrentStoragePath();
1787 else
1788 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001789 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001790 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001791 }
1792 return true;
1793}
1794
1795int TWPartitionManager::usb_storage_disable(void) {
1796 int fd, index;
1797 char lun_file[255];
1798
1799 for (index=0; index<2; index++) {
1800 sprintf(lun_file, CUSTOM_LUN_FILE, index);
1801
1802 if ((fd = open(lun_file, O_WRONLY)) < 0) {
Dees_Troye58d5262012-09-21 12:27:57 -04001803 Mount_All_Storage();
1804 Update_System_Details();
1805 if (index == 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001806 LOGE("Unable to open ums lunfile '%s': (%s)", lun_file, strerror(errno));
Dees_Troye58d5262012-09-21 12:27:57 -04001807 return false;
1808 } else
1809 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001810 }
1811
1812 char ch = 0;
1813 if (write(fd, &ch, 1) < 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001814 close(fd);
Dees_Troye58d5262012-09-21 12:27:57 -04001815 Mount_All_Storage();
1816 Update_System_Details();
1817 if (index == 0) {
1818 LOGE("Unable to write to ums lunfile '%s': (%s)", lun_file, strerror(errno));
1819 return false;
1820 } else
1821 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001822 }
1823
1824 close(fd);
1825 }
Dees_Troye58d5262012-09-21 12:27:57 -04001826 Mount_All_Storage();
1827 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001828 UnMount_Main_Partitions();
Dees_Troy8170a922012-09-18 15:40:25 -04001829 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001830}
1831
1832void TWPartitionManager::Mount_All_Storage(void) {
1833 std::vector<TWPartition*>::iterator iter;
1834
1835 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1836 if ((*iter)->Is_Storage)
1837 (*iter)->Mount(false);
1838 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001839}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001840
Dees_Troyd0384ef2012-10-12 12:15:42 -04001841void TWPartitionManager::UnMount_Main_Partitions(void) {
1842 // Unmounts system and data if data is not data/media
1843 // Also unmounts boot if boot is mountable
1844 LOGI("Unmounting main partitions...\n");
1845
1846 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1847
1848 UnMount_By_Path("/system", true);
1849#ifndef RECOVERY_SDCARD_ON_DATA
1850 UnMount_By_Path("/data", true);
1851#endif
1852 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1853 Boot_Partition->UnMount(true);
1854}
1855
Dees_Troy9350b8d2012-09-27 12:38:38 -04001856int TWPartitionManager::Partition_SDCard(void) {
1857 char mkdir_path[255], temp[255], line[512];
1858 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1859 int ext, swap, total_size = 0, fat_size;
1860 FILE* fp;
1861
1862 ui_print("Partitioning SD Card...\n");
1863#ifdef TW_EXTERNAL_STORAGE_PATH
1864 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1865#else
1866 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1867#endif
1868 if (SDCard == NULL) {
1869 LOGE("Unable to locate device to partition.\n");
1870 return false;
1871 }
1872 if (!SDCard->UnMount(true))
1873 return false;
1874 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1875 if (SDext != NULL) {
1876 if (!SDext->UnMount(true))
1877 return false;
1878 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001879 string result;
1880 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"", result);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001881 Device = SDCard->Actual_Block_Device;
1882 // Just use the root block device
1883 Device.resize(strlen("/dev/block/mmcblkX"));
1884
1885 // Find the size of the block device:
1886 fp = fopen("/proc/partitions", "rt");
1887 if (fp == NULL) {
1888 LOGE("Unable to open /proc/partitions\n");
1889 return false;
1890 }
1891
1892 while (fgets(line, sizeof(line), fp) != NULL)
1893 {
1894 unsigned long major, minor, blocks;
1895 char device[512];
1896 char tmpString[64];
1897
1898 if (strlen(line) < 7 || line[0] == 'm') continue;
1899 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1900
1901 tmpdevice = "/dev/block/";
1902 tmpdevice += device;
1903 if (tmpdevice == Device) {
1904 // Adjust block size to byte size
1905 total_size = (int)(blocks * 1024ULL / 1000000LLU);
1906 break;
1907 }
1908 }
1909 fclose(fp);
1910
1911 DataManager::GetValue("tw_sdext_size", ext);
1912 DataManager::GetValue("tw_swap_size", swap);
1913 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1914 fat_size = total_size - ext - swap;
1915 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);
1916 memset(temp, 0, sizeof(temp));
1917 sprintf(temp, "%i", fat_size);
1918 fat_str = temp;
1919 memset(temp, 0, sizeof(temp));
1920 sprintf(temp, "%i", fat_size + ext);
1921 ext_str = temp;
1922 memset(temp, 0, sizeof(temp));
1923 sprintf(temp, "%i", fat_size + ext + swap);
1924 swap_str = temp;
1925 if (ext + swap > total_size) {
1926 LOGE("EXT + Swap size is larger than sdcard size.\n");
1927 return false;
1928 }
1929 ui_print("Removing partition table...\n");
1930 Command = "parted -s " + Device + " mklabel msdos";
1931 LOGI("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001932 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001933 LOGE("Unable to remove partition table.\n");
1934 Update_System_Details();
1935 return false;
1936 }
1937 ui_print("Creating FAT32 partition...\n");
1938 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
1939 LOGI("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001940 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001941 LOGE("Unable to create FAT32 partition.\n");
1942 return false;
1943 }
1944 if (ext > 0) {
1945 ui_print("Creating EXT partition...\n");
1946 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
1947 LOGI("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001948 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001949 LOGE("Unable to create EXT partition.\n");
1950 Update_System_Details();
1951 return false;
1952 }
1953 }
1954 if (swap > 0) {
1955 ui_print("Creating swap partition...\n");
1956 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
1957 LOGI("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001958 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001959 LOGE("Unable to create swap partition.\n");
1960 Update_System_Details();
1961 return false;
1962 }
1963 }
1964 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1965#ifdef TW_EXTERNAL_STORAGE_PATH
1966 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1967 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1968 memset(mkdir_path, 0, sizeof(mkdir_path));
1969 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1970#else
1971 Mount_By_Path("/sdcard", 1);
1972 strcpy(mkdir_path, "/sdcard/TWRP");
1973#endif
1974 mkdir(mkdir_path, 0777);
1975 DataManager::Flush();
1976#ifdef TW_EXTERNAL_STORAGE_PATH
1977 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1978 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1979 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1980#else
1981 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1982 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1983 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1984#endif
1985 if (ext > 0) {
1986 if (SDext == NULL) {
1987 LOGE("Unable to locate sd-ext partition.\n");
1988 return false;
1989 }
1990 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
1991 ui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1992 LOGI("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001993 TWFunc::Exec_Cmd(Command, result);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001994 }
1995
1996 Update_System_Details();
1997 ui_print("Partitioning complete.\n");
1998 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001999}