blob: 4364a225280c816f32c3f6ef19ab42a0b4de03da [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;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400504 while (TWFunc::Path_Exists(filename) == true) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400505 md5sum.setfn(filename);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500506 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_Troy43d8b002012-09-17 16:00:01 -0400515 }
516 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400517 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500518 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400519 }
520 if (index == 0) {
521 LOGE("Backup file: '%s' not found!\n", filename);
522 return false;
523 }
Dees_Troyc5865ab2012-09-24 15:08:04 -0400524 ui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400525 }
526 return true;
527}
528
Dees_Troy093b7642012-09-21 15:59:38 -0400529bool 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 -0400530 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500531 int img_bps;
532 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400533 unsigned long total_time, remain_time, section_time;
534 int use_compression, backup_time;
535 float pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400536
537 if (Part == NULL)
538 return true;
539
Dees_Troy093b7642012-09-21 15:59:38 -0400540 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_Troy43d8b002012-09-17 16:00:01 -0400567 time(&start);
568
569 if (Part->Backup(Backup_Folder)) {
Dees_Troy8170a922012-09-18 15:40:25 -0400570 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_Troy093b7642012-09-21 15:59:38 -0400579 if (Part->Backup_Method == 1) {
580 *file_bytes_remaining -= (*subpart)->Backup_Size;
581 } else {
582 *img_bytes_remaining -= (*subpart)->Backup_Size;
583 }
Dees_Troy8170a922012-09-18 15:40:25 -0400584 }
585 }
586 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400587 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400588 backup_time = (int) difftime(stop, start);
589 LOGI("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400590 if (Part->Backup_Method == 1) {
591 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400592 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400593 } else {
594 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400595 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400596 }
597 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
598 } else {
599 return false;
600 }
601}
602
603int TWPartitionManager::Run_Backup(void) {
604 int check, do_md5, partition_count = 0;
605 string Backup_Folder, Backup_Name, Full_Backup_Path;
Dees_Troy8170a922012-09-18 15:40:25 -0400606 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 -0400607 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_Troy8170a922012-09-18 15:40:25 -0400619 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400620 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_Troy4a2a1262012-09-18 09:33:47 -0400633 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400634 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400635 else
636 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400637
Dees_Troy43d8b002012-09-17 16:00:01 -0400638 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
639 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400640 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name.empty()) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400641 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_Troy43d8b002012-09-17 16:00:01 -0400649 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 bigbiff2c57d782013-02-19 10:09:21 -0500655 if (backup_sys->Backup_Method == 1) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400656 file_bytes += backup_sys->Backup_Size;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500657 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400658 else
659 img_bytes += backup_sys->Backup_Size;
660 } else {
661 LOGE("Unable to locate system partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000662 DataManager::SetValue(TW_BACKUP_SYSTEM_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400663 }
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_Troy8170a922012-09-18 15:40:25 -0400670 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_Troy43d8b002012-09-17 16:00:01 -0400677 if (backup_data->Backup_Method == 1)
Dees_Troy8170a922012-09-18 15:40:25 -0400678 file_bytes += backup_data->Backup_Size + subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400679 else
Dees_Troy8170a922012-09-18 15:40:25 -0400680 img_bytes += backup_data->Backup_Size + subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400681 } else {
682 LOGE("Unable to locate data partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000683 DataManager::SetValue(TW_BACKUP_DATA_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400684 }
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_Troydc8bc1b2013-01-17 01:39:28 +0000697 DataManager::SetValue(TW_BACKUP_CACHE_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400698 }
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_Troydc8bc1b2013-01-17 01:39:28 +0000711 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400712 }
713 }
ChampionSwimmercf33e4d2013-02-03 13:59:22 +0530714#ifndef TW_HAS_NO_BOOT_PARTITION
Dees_Troy43d8b002012-09-17 16:00:01 -0400715 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_Troydc8bc1b2013-01-17 01:39:28 +0000726 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400727 }
728 }
ChampionSwimmercf33e4d2013-02-03 13:59:22 +0530729#endif
Dees_Troy43d8b002012-09-17 16:00:01 -0400730 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_Troydc8bc1b2013-01-17 01:39:28 +0000741 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400742 }
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_Troydc8bc1b2013-01-17 01:39:28 +0000755 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400756 }
757 }
758#ifdef SP1_NAME
759 DataManager::GetValue(TW_BACKUP_SP1_VAR, check);
760 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400761 backup_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400762 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_Troyab10ee22012-09-21 14:27:30 -0400769 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000770 DataManager::SetValue(TW_BACKUP_SP1_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400771 }
772 }
773#endif
774#ifdef SP2_NAME
775 DataManager::GetValue(TW_BACKUP_SP2_VAR, check);
776 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400777 backup_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400778 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_Troyab10ee22012-09-21 14:27:30 -0400785 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000786 DataManager::SetValue(TW_BACKUP_SP2_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400787 }
788 }
789#endif
790#ifdef SP3_NAME
791 DataManager::GetValue(TW_BACKUP_SP3_VAR, check);
792 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400793 backup_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400794 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_Troyab10ee22012-09-21 14:27:30 -0400801 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000802 DataManager::SetValue(TW_BACKUP_SP3_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400803 }
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_Troyd4b22b02013-01-18 17:17:58 +0000822 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400823 // 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_Troyd4b22b02013-01-18 17:17:58 +0000830 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_Troy093b7642012-09-21 15:59:38 -0400837 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_Troy43d8b002012-09-17 16:00:01 -0400840 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400841 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 -0400842 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400843 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 -0400844 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400845 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 -0400846 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400847 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 -0400848 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400849 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 -0400850 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400851 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 -0400852 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400853 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 -0400854 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400855 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 -0400856 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400857 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 -0400858 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_Troy093b7642012-09-21 15:59:38 -0400865 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500866 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400867
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500868 ui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400869 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 bigbiff2c57d782013-02-19 10:09:21 -0500876 int prev_img_bps, use_compression;
877 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400878 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_Troy43d8b002012-09-17 16:00:01 -0400896 ui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
897 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400898 UnMount_Main_Partitions();
Dees_Troy43d8b002012-09-17 16:00:01 -0400899 ui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000900 string backup_log = Full_Backup_Path + "recovery.log";
901 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
902 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400903}
904
Dees_Troy093b7642012-09-21 15:59:38 -0400905bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400906 time_t Start, Stop;
907 time(&Start);
Dees_Troy093b7642012-09-21 15:59:38 -0400908 ui->ShowProgress(1.0 / (float)partition_count, 150);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400909 if (!Part->Restore(Restore_Name))
910 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400911 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_Troy4a2a1262012-09-18 09:33:47 -0400921 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_Troy51a0e822012-09-05 15:24:24 -0400926int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400927 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_Troy43d8b002012-09-17 16:00:01 -0400939
Dees_Troy4a2a1262012-09-18 09:33:47 -0400940 ui_print("\n[RESTORE STARTED]\n\n");
941 ui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400942
Dees_Troy4a2a1262012-09-18 09:33:47 -0400943 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_Troy63c8df72012-09-10 14:02:05 -0400948 if (check > 0) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400949 restore_sys = Find_Partition_By_Path("/system");
950 if (restore_sys == NULL) {
951 LOGE("Unable to locate system partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000952 } else {
953 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400954 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400955 }
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_Troydc8bc1b2013-01-17 01:39:28 +0000961 } else {
962 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400963 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400964 }
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_Troydc8bc1b2013-01-17 01:39:28 +0000970 } else {
971 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400972 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400973 }
ChampionSwimmercf33e4d2013-02-03 13:59:22 +0530974#ifndef TW_HAS_NO_BOOT_PARTITION
Dees_Troy4a2a1262012-09-18 09:33:47 -0400975 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_Troydc8bc1b2013-01-17 01:39:28 +0000980 } else {
981 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400982 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400983 }
ChampionSwimmercf33e4d2013-02-03 13:59:22 +0530984#endif
Dees_Troy4a2a1262012-09-18 09:33:47 -0400985 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_Troydc8bc1b2013-01-17 01:39:28 +0000990 } else {
991 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400992 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400993 }
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_Troydc8bc1b2013-01-17 01:39:28 +0000999 } else {
1000 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001001 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001002 }
1003#ifdef SP1_NAME
1004 DataManager::GetValue(TW_RESTORE_SP1_VAR, check);
1005 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001006 restore_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001007 if (restore_sp1 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001008 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001009 } else {
1010 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001011 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001012 }
1013#endif
1014#ifdef SP2_NAME
1015 DataManager::GetValue(TW_RESTORE_SP2_VAR, check);
1016 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001017 restore_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001018 if (restore_sp2 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001019 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001020 } else {
1021 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001022 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001023 }
1024#endif
1025#ifdef SP3_NAME
1026 DataManager::GetValue(TW_RESTORE_SP3_VAR, check);
1027 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001028 restore_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001029 if (restore_sp3 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001030 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001031 } else {
1032 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001033 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001034 }
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_Troy43d8b002012-09-17 16:00:01 -04001043 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
Dees_Troyb46a6842012-09-25 11:06:46 -04001044 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001045 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_Troy8170a922012-09-18 15:40:25 -04001050 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_Troy4a2a1262012-09-18 09:33:47 -04001060 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_Troy43d8b002012-09-17 16:00:01 -04001077
Dees_Troy4a2a1262012-09-18 09:33:47 -04001078 ui_print("Restoring %i partitions...\n", partition_count);
Dees_Troy093b7642012-09-21 15:59:38 -04001079 ui->SetProgress(0.0);
1080 if (restore_sys != NULL && !Restore_Partition(restore_sys, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001081 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001082 if (restore_data != NULL && !Restore_Partition(restore_data, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001083 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001084 if (restore_cache != NULL && !Restore_Partition(restore_cache, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001085 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001086 if (restore_boot != NULL && !Restore_Partition(restore_boot, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001087 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001088 if (restore_andsec != NULL && !Restore_Partition(restore_andsec, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001089 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001090 if (restore_sdext != NULL && !Restore_Partition(restore_sdext, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001091 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001092 if (restore_sp1 != NULL && !Restore_Partition(restore_sp1, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001093 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001094 if (restore_sp2 != NULL && !Restore_Partition(restore_sp2, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001095 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001096 if (restore_sp3 != NULL && !Restore_Partition(restore_sp3, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001097 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001098
Dees_Troyb46a6842012-09-25 11:06:46 -04001099 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -04001100 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001101 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -04001102 time(&rStop);
1103 ui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -04001104 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001105}
1106
1107void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001108 // 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_Troye58d5262012-09-21 12:27:57 -04001188 if (Part->Backup_Path == "/system")
Dees_Troy63c8df72012-09-10 14:02:05 -04001189 tw_restore_system = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001190 if (Part->Backup_Path == "/data")
Dees_Troy63c8df72012-09-10 14:02:05 -04001191 tw_restore_data = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001192 if (Part->Backup_Path == "/cache")
Dees_Troy63c8df72012-09-10 14:02:05 -04001193 tw_restore_cache = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001194 if (Part->Backup_Path == "/recovery")
Dees_Troy63c8df72012-09-10 14:02:05 -04001195 tw_restore_recovery = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001196 if (Part->Backup_Path == "/boot")
Dees_Troy63c8df72012-09-10 14:02:05 -04001197 tw_restore_boot = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001198 if (Part->Backup_Path == "/and-sec")
Dees_Troy63c8df72012-09-10 14:02:05 -04001199 tw_restore_andsec = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001200 if (Part->Backup_Path == "/sd-ext")
Dees_Troy63c8df72012-09-10 14:02:05 -04001201 tw_restore_sdext = 1;
1202#ifdef SP1_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001203 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP1_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001204 tw_restore_sp1 = 1;
1205#endif
1206#ifdef SP2_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001207 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP2_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001208 tw_restore_sp2 = 1;
1209#endif
1210#ifdef SP3_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001211 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP3_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001212 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_Troy51a0e822012-09-05 15:24:24 -04001229 return;
1230}
1231
1232int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001233 std::vector<TWPartition*>::iterator iter;
1234 int ret = false;
1235 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001236 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001237
1238 // Iterate through all partitions
1239 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001240 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001241 if (Path == "/and-sec")
1242 ret = (*iter)->Wipe_AndSec();
1243 else
1244 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001245 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_Troy51a0e822012-09-05 15:24:24 -04001255}
1256
1257int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001258 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_Troy51a0e822012-09-05 15:24:24 -04001274}
1275
1276int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001277 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_Troy51a0e822012-09-05 15:24:24 -04001293}
1294
1295int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001296 std::vector<TWPartition*>::iterator iter;
1297 int ret = true;
1298
1299 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001300 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001301 if (!(*iter)->Wipe())
1302 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001303 } else if ((*iter)->Has_Android_Secure) {
1304 if (!(*iter)->Wipe_AndSec())
1305 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001306 }
1307 }
1308 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001309}
1310
Dees_Troy38bd7602012-09-14 13:33:53 -04001311int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1312 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001313 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001314
1315 if (!Mount_By_Path("/data", true))
1316 return false;
1317
1318 if (!Mount_By_Path("/cache", true))
1319 return false;
1320
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001321 dir.push_back("/data/dalvik-cache");
1322 dir.push_back("/cache/dalvik-cache");
1323 dir.push_back("/cache/dc");
Dees_Troy38bd7602012-09-14 13:33:53 -04001324 ui_print("\nWiping Dalvik Cache Directories...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001325 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_Troy38bd7602012-09-14 13:33:53 -04001331 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 bigbiff9c754052013-01-09 09:09:08 -05001335 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
1336 ui_print("Cleaned: /sd-ext/dalvik-cache...\n");
1337 }
1338 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001339 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001340 ui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
1341 return true;
1342}
1343
1344int TWPartitionManager::Wipe_Rotate_Data(void) {
1345 if (!Mount_By_Path("/data", true))
1346 return false;
1347
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001348 unlink("/data/misc/akmd*");
1349 unlink("/data/misc/rild*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001350 ui_print("Rotation data wiped.\n");
1351 return true;
1352}
1353
1354int 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_Troy2ff5a8d2012-09-26 14:53:02 -04001369int 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_Troy38bd7602012-09-14 13:33:53 -04001389int 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
1404int 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 bigbiff9c754052013-01-09 09:09:08 -05001416 TWFunc::removeDir("/data/media", false);
1417 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
1418 return -1;
Dees_Troy38bd7602012-09-14 13:33:53 -04001419 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_Troy51a0e822012-09-05 15:24:24 -04001430void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001431 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001432 return;
1433}
1434
1435void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001436 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001437 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001438
Dees_Troy32c8eb82012-09-11 15:28:06 -04001439 ui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001440 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001441 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_Troye58d5262012-09-21 12:27:57 -04001459 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001460 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001461 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001462 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_Troy2c50e182012-09-26 20:05:28 -04001467 } 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_Troy51127312012-09-08 13:08:49 -04001475 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001476#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_Troyaa9cc402012-10-13 12:14:05 -04001494 } 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 Peck82599a82012-11-21 16:23:12 -08001512 } else if ((*iter)->Mount_Point == "/data") {
1513 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001514 }
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_Troy51127312012-09-08 13:08:49 -04001533 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001534 }
Dees_Troy51127312012-09-08 13:08:49 -04001535 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_Troy8170a922012-09-18 15:40:25 -04001538 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_Troyab10ee22012-09-21 14:27:30 -04001547 if (!FreeStorage->Is_Encrypted) {
1548 // Not able to use internal, so error!
1549 LOGE("Unable to mount internal storage.\n");
1550 }
Dees_Troy8170a922012-09-18 15:40:25 -04001551 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_Troy51127312012-09-08 13:08:49 -04001573 LOGI("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001574 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001575 if (!Write_Fstab())
1576 LOGE("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001577 return;
1578}
1579
1580int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001581#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);
a39552696ff55ce2013-01-08 16:14:56 +00001596
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_Troy5bf43922012-09-07 16:07:55 -04001601#endif
a39552696ff55ce2013-01-08 16:14:56 +00001602
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_Troy20c02c02013-01-10 14:14:10 +00001614#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001615 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001616 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001617 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_Troy20c02c02013-01-10 14:14:10 +00001623#endif
a39552696ff55ce2013-01-08 16:14:56 +00001624
Dees_Troy5bf43922012-09-07 16:07:55 -04001625 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001626 int pwret = cryptfs_check_passwd(cPassword);
1627
1628 if (pwret != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001629 LOGE("Failed to decrypt data.\n");
1630 return -1;
1631 }
a39552696ff55ce2013-01-08 16:14:56 +00001632
1633 if(efs)
1634 efs->UnMount(false);
1635
Dees_Troy5bf43922012-09-07 16:07:55 -04001636 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_Troy38bd7602012-09-14 13:33:53 -04001642 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001643 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1644 dat->Is_Decrypted = true;
1645 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001646 dat->Setup_File_System(false);
Dees_Troy32c8eb82012-09-11 15:28:06 -04001647 ui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001648
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_Troyc8bafa12013-01-10 15:43:00 +00001654 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001655 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 }
a39552696ff55ce2013-01-08 16:14:56 +00001660#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001661#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001662#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001663 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_Troy999b39d2013-01-14 15:36:13 +00001669 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_Troy85f44ed2013-01-09 18:42:36 +00001676 } else {
1677 sdcard->Is_Decrypted = false;
1678 sdcard->Decrypted_Block_Device = "";
1679 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001680#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001681#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001682
Dees_Troy5bf43922012-09-07 16:07:55 -04001683 // Sleep for a bit so that the device will be ready
1684 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001685#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_Troydc8bc1b2013-01-17 01:39:28 +00001692 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001693 }
1694#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001695 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001696 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001697 } 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_Troy51a0e822012-09-05 15:24:24 -04001705 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001706}
1707
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001708int 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_Troy1a650e62012-10-19 20:54:32 -04001720 UnMount_Main_Partitions();
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001721 ui_print("Done.\n\n");
1722 return result;
1723}
1724
Dees_Troy8170a922012-09-18 15:40:25 -04001725//partial kangbang from system/vold
Dees_Troyd21618c2012-10-14 18:48:49 -04001726int 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_Troy8170a922012-09-18 15:40:25 -04001751int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001752 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001753 char lun_file[255];
Dees_Troy8170a922012-09-18 15:40:25 -04001754 string ext_path;
Dees_Troyd21618c2012-10-14 18:48:49 -04001755 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001756
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_Troyd21618c2012-10-14 18:48:49 -04001760 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_Troyf4ca6122012-10-13 22:17:20 -04001768 // Device doesn't have multiple lun files, mount current storage
Dees_Troyf4ca6122012-10-13 22:17:20 -04001769 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001770 return Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001771 } else {
1772 // Device has multiple lun files
Dees_Troyf4ca6122012-10-13 22:17:20 -04001773 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001774 if (!Open_Lun_File(DataManager::GetSettingsStoragePath(), lun_file))
Dees_Troyf4ca6122012-10-13 22:17:20 -04001775 return false;
Dees_Troyf4ca6122012-10-13 22:17:20 -04001776 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001777 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Dees_Troyd21618c2012-10-14 18:48:49 -04001778 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001779 }
Dees_Troy8170a922012-09-18 15:40:25 -04001780 } else {
1781 if (has_data_media == 0)
1782 ext_path = DataManager::GetCurrentStoragePath();
1783 else
1784 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001785 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001786 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001787 }
1788 return true;
1789}
1790
1791int 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_Troye58d5262012-09-21 12:27:57 -04001799 Mount_All_Storage();
1800 Update_System_Details();
1801 if (index == 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001802 LOGE("Unable to open ums lunfile '%s': (%s)", lun_file, strerror(errno));
Dees_Troye58d5262012-09-21 12:27:57 -04001803 return false;
1804 } else
1805 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001806 }
1807
1808 char ch = 0;
1809 if (write(fd, &ch, 1) < 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001810 close(fd);
Dees_Troye58d5262012-09-21 12:27:57 -04001811 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_Troy8170a922012-09-18 15:40:25 -04001818 }
1819
1820 close(fd);
1821 }
Dees_Troye58d5262012-09-21 12:27:57 -04001822 Mount_All_Storage();
1823 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001824 UnMount_Main_Partitions();
Dees_Troy8170a922012-09-18 15:40:25 -04001825 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001826}
1827
1828void 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_Troy2c50e182012-09-26 20:05:28 -04001835}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001836
Dees_Troyd0384ef2012-10-12 12:15:42 -04001837void 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_Troy9350b8d2012-09-27 12:38:38 -04001852int 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 bigbiff9c754052013-01-09 09:09:08 -05001875 string result;
1876 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"", result);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001877 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 bigbiff9c754052013-01-09 09:09:08 -05001928 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001929 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 bigbiff9c754052013-01-09 09:09:08 -05001936 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001937 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 bigbiff9c754052013-01-09 09:09:08 -05001944 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001945 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 bigbiff9c754052013-01-09 09:09:08 -05001954 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001955 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 bigbiff9c754052013-01-09 09:09:08 -05001989 TWFunc::Exec_Cmd(Command, result);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001990 }
1991
1992 Update_System_Details();
1993 ui_print("Partitioning complete.\n");
1994 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001995}