blob: 5230810f1c4179e425ed3e59e7986e7828b1230b [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"
Dees_Troy38bd7602012-09-14 13:33:53 -040043
Dees_Troy5bf43922012-09-07 16:07:55 -040044#ifdef TW_INCLUDE_CRYPTO
45 #ifdef TW_INCLUDE_JB_CRYPTO
46 #include "crypto/jb/cryptfs.h"
47 #else
48 #include "crypto/ics/cryptfs.h"
49 #endif
50 #include "cutils/properties.h"
51#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040052
Dees_Troy093b7642012-09-21 15:59:38 -040053extern RecoveryUI* ui;
54
Dees_Troy51a0e822012-09-05 15:24:24 -040055int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040056 FILE *fstabFile;
57 char fstab_line[MAX_FSTAB_LINE_LENGTH];
58
59 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
60 if (fstabFile == NULL) {
61 LOGE("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
62 return false;
63 }
64
65 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
66 if (fstab_line[0] != '/')
67 continue;
68
Dees_Troy2a923582012-09-20 12:13:34 -040069 if (fstab_line[strlen(fstab_line) - 1] != '\n')
70 fstab_line[strlen(fstab_line)] = '\n';
71
Dees_Troy5bf43922012-09-07 16:07:55 -040072 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040073 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040074 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040075
Dees_Troy5bf43922012-09-07 16:07:55 -040076 if (partition->Process_Fstab_Line(line, Display_Error)) {
77 Partitions.push_back(partition);
78 } else {
79 delete partition;
80 }
81 }
82 fclose(fstabFile);
83 if (!Write_Fstab()) {
84 if (Display_Error)
85 LOGE("Error creating fstab\n");
86 else
87 LOGI("Error creating fstab\n");
88 }
Dees_Troy51127312012-09-08 13:08:49 -040089 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -040090 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -040091 return true;
92}
93
94int TWPartitionManager::Write_Fstab(void) {
95 FILE *fp;
96 std::vector<TWPartition*>::iterator iter;
97 string Line;
98
99 fp = fopen("/etc/fstab", "w");
100 if (fp == NULL) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400101 LOGI("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400102 return false;
103 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400104 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400105 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400106 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400107 fputs(Line.c_str(), fp);
Dees_Troy51127312012-09-08 13:08:49 -0400108 // Handle subpartition tracking
109 if ((*iter)->Is_SubPartition) {
110 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
111 if (ParentPartition)
112 ParentPartition->Has_SubPartition = true;
113 else
114 LOGE("Unable to locate parent partition '%s' of '%s'\n", (*iter)->SubPartition_Of.c_str(), (*iter)->Mount_Point.c_str());
115 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400116 }
117 }
118 fclose(fp);
119 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400120}
121
Dees_Troy8170a922012-09-18 15:40:25 -0400122void TWPartitionManager::Output_Partition_Logging(void) {
123 std::vector<TWPartition*>::iterator iter;
124
125 printf("\n\nPartition Logs:\n");
126 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
127 Output_Partition((*iter));
128}
129
130void TWPartitionManager::Output_Partition(TWPartition* Part) {
131 unsigned long long mb = 1048576;
132
Gary Peck004d48b2012-11-21 16:28:18 -0800133 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 -0400134 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800135 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 -0400136 }
Gary Peck004d48b2012-11-21 16:28:18 -0800137 printf("\n Flags: ");
138 if (Part->Can_Be_Wiped)
139 printf("Can_Be_Wiped ");
140 if (Part->Wipe_During_Factory_Reset)
141 printf("Wipe_During_Factory_Reset ");
142 if (Part->Wipe_Available_in_GUI)
143 printf("Wipe_Available_in_GUI ");
144 if (Part->Is_SubPartition)
145 printf("Is_SubPartition ");
146 if (Part->Has_SubPartition)
147 printf("Has_SubPartition ");
148 if (Part->Removable)
149 printf("Removable ");
150 if (Part->Is_Present)
151 printf("IsPresent ");
152 if (Part->Can_Be_Encrypted)
153 printf("Can_Be_Encrypted ");
154 if (Part->Is_Encrypted)
155 printf("Is_Encrypted ");
156 if (Part->Is_Decrypted)
157 printf("Is_Decrypted ");
158 if (Part->Has_Data_Media)
159 printf("Has_Data_Media ");
160 if (Part->Has_Android_Secure)
161 printf("Has_Android_Secure ");
162 if (Part->Is_Storage)
163 printf("Is_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000164 if (Part->Ignore_Blkid)
165 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000166 if (Part->Retain_Layout_Version)
167 printf("Retain_Layout_Version ");
Gary Peck004d48b2012-11-21 16:28:18 -0800168 printf("\n");
169 if (!Part->SubPartition_Of.empty())
170 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
171 if (!Part->Symlink_Path.empty())
172 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
173 if (!Part->Symlink_Mount_Point.empty())
174 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
175 if (!Part->Primary_Block_Device.empty())
176 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
177 if (!Part->Alternate_Block_Device.empty())
178 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
179 if (!Part->Decrypted_Block_Device.empty())
180 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
181 if (Part->Length != 0)
182 printf(" Length: %i\n", Part->Length);
183 if (!Part->Display_Name.empty())
184 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
185 if (!Part->Backup_Path.empty())
186 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
187 if (!Part->Backup_Name.empty())
188 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
189 if (!Part->Backup_FileName.empty())
190 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
191 if (!Part->Storage_Path.empty())
192 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
193 if (!Part->Current_File_System.empty())
194 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
195 if (!Part->Fstab_File_System.empty())
196 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
197 if (Part->Format_Block_Size != 0)
198 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400199 if (!Part->MTD_Name.empty())
200 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400201 string back_meth = Part->Backup_Method_By_Name();
202 printf(" Backup_Method: %s\n\n", back_meth.c_str());
203}
204
Dees_Troy51a0e822012-09-05 15:24:24 -0400205int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400206 std::vector<TWPartition*>::iterator iter;
207 int ret = false;
208 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400209 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400210
Dees_Troy43d8b002012-09-17 16:00:01 -0400211 if (Local_Path == "/tmp")
212 return true;
213
Dees_Troy5bf43922012-09-07 16:07:55 -0400214 // Iterate through all partitions
215 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400216 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400217 ret = (*iter)->Mount(Display_Error);
218 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400219 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400220 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400221 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400222 }
223 if (found) {
224 return ret;
225 } else if (Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400226 LOGE("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400227 } else {
Dees_Troy51127312012-09-08 13:08:49 -0400228 LOGI("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400229 }
230 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400231}
232
233int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400234 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400235
Dees_Troy51127312012-09-08 13:08:49 -0400236 if (Part) {
237 if (Part->Has_SubPartition) {
238 std::vector<TWPartition*>::iterator subpart;
239
240 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
241 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
242 (*subpart)->Mount(Display_Error);
243 }
244 return Part->Mount(Display_Error);
245 } else
246 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400247 }
248 if (Display_Error)
Dees_Troy51127312012-09-08 13:08:49 -0400249 LOGE("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400250 else
Dees_Troy51127312012-09-08 13:08:49 -0400251 LOGI("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400252 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400253}
254
255int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400256 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400257
Dees_Troy51127312012-09-08 13:08:49 -0400258 if (Part) {
259 if (Part->Has_SubPartition) {
260 std::vector<TWPartition*>::iterator subpart;
261
262 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
263 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
264 (*subpart)->Mount(Display_Error);
265 }
266 return Part->Mount(Display_Error);
267 } else
268 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400269 }
270 if (Display_Error)
Dees_Troy51127312012-09-08 13:08:49 -0400271 LOGE("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400272 else
Dees_Troy51127312012-09-08 13:08:49 -0400273 LOGI("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400274 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400275}
276
277int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400278 std::vector<TWPartition*>::iterator iter;
279 int ret = false;
280 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400281 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400282
283 // Iterate through all partitions
284 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400285 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400286 ret = (*iter)->UnMount(Display_Error);
287 found = true;
288 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
289 (*iter)->UnMount(Display_Error);
290 }
291 }
292 if (found) {
293 return ret;
294 } else if (Display_Error) {
295 LOGE("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
296 } else {
297 LOGI("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
298 }
299 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400300}
301
302int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400303 TWPartition* Part = Find_Partition_By_Block(Block);
304
305 if (Part) {
306 if (Part->Has_SubPartition) {
307 std::vector<TWPartition*>::iterator subpart;
308
309 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
310 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
311 (*subpart)->UnMount(Display_Error);
312 }
313 return Part->UnMount(Display_Error);
314 } else
315 return Part->UnMount(Display_Error);
316 }
317 if (Display_Error)
318 LOGE("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
319 else
320 LOGI("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
321 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400322}
323
324int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400325 TWPartition* Part = Find_Partition_By_Name(Name);
326
327 if (Part) {
328 if (Part->Has_SubPartition) {
329 std::vector<TWPartition*>::iterator subpart;
330
331 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
332 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
333 (*subpart)->UnMount(Display_Error);
334 }
335 return Part->UnMount(Display_Error);
336 } else
337 return Part->UnMount(Display_Error);
338 }
339 if (Display_Error)
340 LOGE("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
341 else
342 LOGI("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
343 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400344}
345
346int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400347 TWPartition* Part = Find_Partition_By_Path(Path);
348
349 if (Part)
350 return Part->Is_Mounted();
351 else
352 LOGI("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
353 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400354}
355
356int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400357 TWPartition* Part = Find_Partition_By_Block(Block);
358
359 if (Part)
360 return Part->Is_Mounted();
361 else
362 LOGI("Is_Mounted: Unable to find partition for block '%s'\n", Block.c_str());
363 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400364}
365
366int TWPartitionManager::Is_Mounted_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400367 TWPartition* Part = Find_Partition_By_Name(Name);
368
369 if (Part)
370 return Part->Is_Mounted();
371 else
372 LOGI("Is_Mounted: Unable to find partition for name '%s'\n", Name.c_str());
373 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400374}
375
Dees_Troy5bf43922012-09-07 16:07:55 -0400376int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400377 string current_storage_path = DataManager::GetCurrentStoragePath();
378
379 if (Mount_By_Path(current_storage_path, Display_Error)) {
380 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
381 if (FreeStorage)
382 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
383 return true;
384 }
385 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400386}
387
Dees_Troy5bf43922012-09-07 16:07:55 -0400388int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
389 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
390}
391
392TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
393 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400394 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400395
396 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400397 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400398 return (*iter);
399 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400400 return NULL;
401}
402
Dees_Troy5bf43922012-09-07 16:07:55 -0400403TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400404 std::vector<TWPartition*>::iterator iter;
405
406 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400407 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 -0400408 return (*iter);
409 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400410 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400411}
412
413TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400414 std::vector<TWPartition*>::iterator iter;
415
416 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
417 if ((*iter)->Display_Name == Name)
418 return (*iter);
419 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400420 return NULL;
421}
Dees_Troy51a0e822012-09-05 15:24:24 -0400422
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400423int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
424 // Check the backup name to ensure that it is the correct size and contains only valid characters
425 // and that a backup with that name doesn't already exist
426 char backup_name[MAX_BACKUP_NAME_LEN];
427 char backup_loc[255], tw_image_dir[255];
428 int copy_size;
429 int index, cur_char;
430 string Backup_Name, Backup_Loc;
431
432 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
433 copy_size = Backup_Name.size();
434 // Check size
435 if (copy_size > MAX_BACKUP_NAME_LEN) {
436 if (Display_Error)
437 LOGE("Backup name is too long.\n");
438 return -2;
439 }
440
441 // Check each character
442 strncpy(backup_name, Backup_Name.c_str(), copy_size);
443 if (strcmp(backup_name, "0") == 0)
444 return 0; // A "0" (zero) means to use the current timestamp for the backup name
445 for (index=0; index<copy_size; index++) {
446 cur_char = (int)backup_name[index];
447 if (cur_char == 32 || (cur_char >= 48 && cur_char <= 57) || (cur_char >= 65 && cur_char <= 91) || cur_char == 93 || cur_char == 95 || (cur_char >= 97 && cur_char <= 123) || cur_char == 125 || cur_char == 45 || cur_char == 46) {
448 // These are valid characters
449 // Numbers
450 // Upper case letters
451 // Lower case letters
452 // Space
453 // and -_.{}[]
454 } else {
455 if (Display_Error)
456 LOGE("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
457 return -3;
458 }
459 }
460
461 // Check to make sure that a backup with this name doesn't already exist
462 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
463 strcpy(backup_loc, Backup_Loc.c_str());
464 sprintf(tw_image_dir,"%s/%s/.", backup_loc, backup_name);
465 if (TWFunc::Path_Exists(tw_image_dir)) {
466 if (Display_Error)
467 LOGE("A backup with this name already exists.\n");
468 return -4;
469 }
470
471 // No problems found, return 0
472 return 0;
473}
474
Dees_Troy43d8b002012-09-17 16:00:01 -0400475bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
476{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500477 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400478 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500479 string result;
Dees_Troy43d8b002012-09-17 16:00:01 -0400480
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500481 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400482 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400483
Dees_Troyb46a6842012-09-25 11:06:46 -0400484 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troyc51f1f92012-09-20 15:32:13 -0400485 ui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400486
487 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500488 command = "md5sum " + Backup_Filename + " > " + Backup_Filename + ".md5";
489 chdir(Backup_Folder.c_str());
490 if (TWFunc::Exec_Cmd(command, result) == 0) {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400491 ui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400492 return true;
493 } else {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400494 ui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400495 return false;
496 }
497 } else {
498 char filename[512];
499 int index = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400500 sprintf(filename, "%s%03i", Full_File.c_str(), index);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400501 while (TWFunc::Path_Exists(filename) == true) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500502 ostringstream intToStr;
503 intToStr << index;
504 ostringstream fn;
505 fn << setw(3) << setfill('0') << intToStr.str();
506 command = "md5sum " + Backup_Filename + fn.str() + " >" + Backup_Filename + fn.str() + ".md5";
507 chdir(Backup_Folder.c_str());
508 if (TWFunc::Exec_Cmd(command, result) != 0) {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400509 ui_print(" * MD5 Error.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400510 return false;
511 }
512 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400513 sprintf(filename, "%s%03i", Full_File.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400514 }
515 if (index == 0) {
516 LOGE("Backup file: '%s' not found!\n", filename);
517 return false;
518 }
Dees_Troyc5865ab2012-09-24 15:08:04 -0400519 ui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400520 }
521 return true;
522}
523
Dees_Troy093b7642012-09-21 15:59:38 -0400524bool 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 -0400525 time_t start, stop;
Dees_Troy093b7642012-09-21 15:59:38 -0400526 int img_bps, file_bps;
527 unsigned long total_time, remain_time, section_time;
528 int use_compression, backup_time;
529 float pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400530
531 if (Part == NULL)
532 return true;
533
Dees_Troy093b7642012-09-21 15:59:38 -0400534 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
535
536 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
537 if (use_compression)
538 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
539 else
540 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
541
542 // We know the speed for both, how far into the whole backup are we, based on time
543 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
544 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
545
546 pos = (total_time - remain_time) / (float) total_time;
547 ui->SetProgress(pos);
548
549 LOGI("Estimated Total time: %lu Estimated remaining time: %lu\n", total_time, remain_time);
550
551 // And get the time
552 if (Part->Backup_Method == 1)
553 section_time = Part->Backup_Size / file_bps;
554 else
555 section_time = Part->Backup_Size / img_bps;
556
557 // Set the position
558 pos = section_time / (float) total_time;
559 ui->ShowProgress(pos, section_time);
560
Dees_Troy43d8b002012-09-17 16:00:01 -0400561 time(&start);
562
563 if (Part->Backup(Backup_Folder)) {
Dees_Troy8170a922012-09-18 15:40:25 -0400564 if (Part->Has_SubPartition) {
565 std::vector<TWPartition*>::iterator subpart;
566
567 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
568 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
569 if (!(*subpart)->Backup(Backup_Folder))
570 return false;
571 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
572 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400573 if (Part->Backup_Method == 1) {
574 *file_bytes_remaining -= (*subpart)->Backup_Size;
575 } else {
576 *img_bytes_remaining -= (*subpart)->Backup_Size;
577 }
Dees_Troy8170a922012-09-18 15:40:25 -0400578 }
579 }
580 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400581 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400582 backup_time = (int) difftime(stop, start);
583 LOGI("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400584 if (Part->Backup_Method == 1) {
585 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400586 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400587 } else {
588 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400589 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400590 }
591 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
592 } else {
593 return false;
594 }
595}
596
597int TWPartitionManager::Run_Backup(void) {
598 int check, do_md5, partition_count = 0;
599 string Backup_Folder, Backup_Name, Full_Backup_Path;
Dees_Troy8170a922012-09-18 15:40:25 -0400600 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 -0400601 unsigned long img_time = 0, file_time = 0;
602 TWPartition* backup_sys = NULL;
603 TWPartition* backup_data = NULL;
604 TWPartition* backup_cache = NULL;
605 TWPartition* backup_recovery = NULL;
606 TWPartition* backup_boot = NULL;
607 TWPartition* backup_andsec = NULL;
608 TWPartition* backup_sdext = NULL;
609 TWPartition* backup_sp1 = NULL;
610 TWPartition* backup_sp2 = NULL;
611 TWPartition* backup_sp3 = NULL;
612 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400613 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400614 struct tm *t;
615 time_t start, stop, seconds, total_start, total_stop;
616 seconds = time(0);
617 t = localtime(&seconds);
618
619 time(&total_start);
620
621 Update_System_Details();
622
623 if (!Mount_Current_Storage(true))
624 return false;
625
626 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400627 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400628 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400629 else
630 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400631
Dees_Troy43d8b002012-09-17 16:00:01 -0400632 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
633 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400634 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name.empty()) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400635 char timestamp[255];
636 sprintf(timestamp,"%04d-%02d-%02d--%02d-%02d-%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
637 Backup_Name = timestamp;
638 }
639 LOGI("Backup Name is: '%s'\n", Backup_Name.c_str());
640 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
641 LOGI("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
642
Dees_Troy43d8b002012-09-17 16:00:01 -0400643 LOGI("Calculating backup details...\n");
644 DataManager::GetValue(TW_BACKUP_SYSTEM_VAR, check);
645 if (check) {
646 backup_sys = Find_Partition_By_Path("/system");
647 if (backup_sys != NULL) {
648 partition_count++;
649 if (backup_sys->Backup_Method == 1)
650 file_bytes += backup_sys->Backup_Size;
651 else
652 img_bytes += backup_sys->Backup_Size;
653 } else {
654 LOGE("Unable to locate system partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000655 DataManager::SetValue(TW_BACKUP_SYSTEM_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400656 }
657 }
658 DataManager::GetValue(TW_BACKUP_DATA_VAR, check);
659 if (check) {
660 backup_data = Find_Partition_By_Path("/data");
661 if (backup_data != NULL) {
662 partition_count++;
Dees_Troy8170a922012-09-18 15:40:25 -0400663 subpart_size = 0;
664 if (backup_data->Has_SubPartition) {
665 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
666 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == backup_data->Mount_Point)
667 subpart_size += (*subpart)->Backup_Size;
668 }
669 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400670 if (backup_data->Backup_Method == 1)
Dees_Troy8170a922012-09-18 15:40:25 -0400671 file_bytes += backup_data->Backup_Size + subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400672 else
Dees_Troy8170a922012-09-18 15:40:25 -0400673 img_bytes += backup_data->Backup_Size + subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400674 } else {
675 LOGE("Unable to locate data partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000676 DataManager::SetValue(TW_BACKUP_DATA_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400677 }
678 }
679 DataManager::GetValue(TW_BACKUP_CACHE_VAR, check);
680 if (check) {
681 backup_cache = Find_Partition_By_Path("/cache");
682 if (backup_cache != NULL) {
683 partition_count++;
684 if (backup_cache->Backup_Method == 1)
685 file_bytes += backup_cache->Backup_Size;
686 else
687 img_bytes += backup_cache->Backup_Size;
688 } else {
689 LOGE("Unable to locate cache partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000690 DataManager::SetValue(TW_BACKUP_CACHE_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400691 }
692 }
693 DataManager::GetValue(TW_BACKUP_RECOVERY_VAR, check);
694 if (check) {
695 backup_recovery = Find_Partition_By_Path("/recovery");
696 if (backup_recovery != NULL) {
697 partition_count++;
698 if (backup_recovery->Backup_Method == 1)
699 file_bytes += backup_recovery->Backup_Size;
700 else
701 img_bytes += backup_recovery->Backup_Size;
702 } else {
703 LOGE("Unable to locate recovery partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000704 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400705 }
706 }
707 DataManager::GetValue(TW_BACKUP_BOOT_VAR, check);
708 if (check) {
709 backup_boot = Find_Partition_By_Path("/boot");
710 if (backup_boot != NULL) {
711 partition_count++;
712 if (backup_boot->Backup_Method == 1)
713 file_bytes += backup_boot->Backup_Size;
714 else
715 img_bytes += backup_boot->Backup_Size;
716 } else {
717 LOGE("Unable to locate boot partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000718 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400719 }
720 }
721 DataManager::GetValue(TW_BACKUP_ANDSEC_VAR, check);
722 if (check) {
723 backup_andsec = Find_Partition_By_Path("/and-sec");
724 if (backup_andsec != NULL) {
725 partition_count++;
726 if (backup_andsec->Backup_Method == 1)
727 file_bytes += backup_andsec->Backup_Size;
728 else
729 img_bytes += backup_andsec->Backup_Size;
730 } else {
731 LOGE("Unable to locate android secure partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000732 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400733 }
734 }
735 DataManager::GetValue(TW_BACKUP_SDEXT_VAR, check);
736 if (check) {
737 backup_sdext = Find_Partition_By_Path("/sd-ext");
738 if (backup_sdext != NULL) {
739 partition_count++;
740 if (backup_sdext->Backup_Method == 1)
741 file_bytes += backup_sdext->Backup_Size;
742 else
743 img_bytes += backup_sdext->Backup_Size;
744 } else {
745 LOGE("Unable to locate sd-ext partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000746 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400747 }
748 }
749#ifdef SP1_NAME
750 DataManager::GetValue(TW_BACKUP_SP1_VAR, check);
751 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400752 backup_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400753 if (backup_sp1 != NULL) {
754 partition_count++;
755 if (backup_sp1->Backup_Method == 1)
756 file_bytes += backup_sp1->Backup_Size;
757 else
758 img_bytes += backup_sp1->Backup_Size;
759 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400760 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000761 DataManager::SetValue(TW_BACKUP_SP1_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400762 }
763 }
764#endif
765#ifdef SP2_NAME
766 DataManager::GetValue(TW_BACKUP_SP2_VAR, check);
767 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400768 backup_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400769 if (backup_sp2 != NULL) {
770 partition_count++;
771 if (backup_sp2->Backup_Method == 1)
772 file_bytes += backup_sp2->Backup_Size;
773 else
774 img_bytes += backup_sp2->Backup_Size;
775 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400776 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000777 DataManager::SetValue(TW_BACKUP_SP2_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400778 }
779 }
780#endif
781#ifdef SP3_NAME
782 DataManager::GetValue(TW_BACKUP_SP3_VAR, check);
783 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400784 backup_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400785 if (backup_sp3 != NULL) {
786 partition_count++;
787 if (backup_sp3->Backup_Method == 1)
788 file_bytes += backup_sp3->Backup_Size;
789 else
790 img_bytes += backup_sp3->Backup_Size;
791 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400792 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000793 DataManager::SetValue(TW_BACKUP_SP3_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400794 }
795 }
796#endif
797
798 if (partition_count == 0) {
799 ui_print("No partitions selected for backup.\n");
800 return false;
801 }
802 total_bytes = file_bytes + img_bytes;
803 ui_print(" * Total number of partitions to back up: %d\n", partition_count);
804 ui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
805 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
806 if (storage != NULL) {
807 free_space = storage->Free;
808 ui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
809 } else {
810 LOGE("Unable to locate storage device.\n");
811 return false;
812 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000813 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400814 // We require an extra 32MB just in case
815 LOGE("Not enough free space on storage.\n");
816 return false;
817 }
818 img_bytes_remaining = img_bytes;
819 file_bytes_remaining = file_bytes;
820
Dees_Troyd4b22b02013-01-18 17:17:58 +0000821 ui_print("\n[BACKUP STARTED]\n");
822 ui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
823 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
824 LOGE("Failed to make backup folder.\n");
825 return false;
826 }
827
Dees_Troy093b7642012-09-21 15:59:38 -0400828 ui->SetProgress(0.0);
829
830 if (!Backup_Partition(backup_sys, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
Dees_Troy43d8b002012-09-17 16:00:01 -0400831 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400832 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 -0400833 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400834 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 -0400835 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400836 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 -0400837 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400838 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 -0400839 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400840 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 -0400841 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400842 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 -0400843 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400844 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 -0400845 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400846 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 -0400847 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400848 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 -0400849 return false;
850
851 // Average BPS
852 if (img_time == 0)
853 img_time = 1;
854 if (file_time == 0)
855 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400856 int img_bps = (int)img_bytes / (int)img_time;
857 int file_bps = (int)file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400858
859 ui_print("Average backup rate for file systems: %lu MB/sec\n", (file_bps / (1024 * 1024)));
860 ui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
861
862 time(&total_stop);
863 int total_time = (int) difftime(total_stop, total_start);
864 unsigned long long actual_backup_size = TWFunc::Get_Folder_Size(Full_Backup_Path, true);
865 actual_backup_size /= (1024LLU * 1024LLU);
866
Dees_Troy093b7642012-09-21 15:59:38 -0400867 int prev_img_bps, prev_file_bps, use_compression;
868 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
869 img_bps += (prev_img_bps * 4);
870 img_bps /= 5;
871
872 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
873 if (use_compression)
874 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
875 else
876 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
877 file_bps += (prev_file_bps * 4);
878 file_bps /= 5;
879
880 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
881 if (use_compression)
882 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
883 else
884 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
885
Dees_Troy43d8b002012-09-17 16:00:01 -0400886 ui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
887 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400888 UnMount_Main_Partitions();
Dees_Troy43d8b002012-09-17 16:00:01 -0400889 ui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500890 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400891}
892
Dees_Troy093b7642012-09-21 15:59:38 -0400893bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400894 time_t Start, Stop;
895 time(&Start);
Dees_Troy093b7642012-09-21 15:59:38 -0400896 ui->ShowProgress(1.0 / (float)partition_count, 150);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400897 if (!Part->Restore(Restore_Name))
898 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400899 if (Part->Has_SubPartition) {
900 std::vector<TWPartition*>::iterator subpart;
901
902 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
903 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
904 if (!(*subpart)->Restore(Restore_Name))
905 return false;
906 }
907 }
908 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400909 time(&Stop);
910 ui_print("[%s done (%d seconds)]\n\n", Part->Display_Name.c_str(), (int)difftime(Stop, Start));
911 return true;
912}
913
Dees_Troy51a0e822012-09-05 15:24:24 -0400914int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400915 int check_md5, check, partition_count = 0;
916 TWPartition* restore_sys = NULL;
917 TWPartition* restore_data = NULL;
918 TWPartition* restore_cache = NULL;
919 TWPartition* restore_boot = NULL;
920 TWPartition* restore_andsec = NULL;
921 TWPartition* restore_sdext = NULL;
922 TWPartition* restore_sp1 = NULL;
923 TWPartition* restore_sp2 = NULL;
924 TWPartition* restore_sp3 = NULL;
925 time_t rStart, rStop;
926 time(&rStart);
Dees_Troy43d8b002012-09-17 16:00:01 -0400927
Dees_Troy4a2a1262012-09-18 09:33:47 -0400928 ui_print("\n[RESTORE STARTED]\n\n");
929 ui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400930
Dees_Troy4a2a1262012-09-18 09:33:47 -0400931 if (!Mount_Current_Storage(true))
932 return false;
933
934 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
935 DataManager::GetValue(TW_RESTORE_SYSTEM_VAR, check);
Dees_Troy63c8df72012-09-10 14:02:05 -0400936 if (check > 0) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400937 restore_sys = Find_Partition_By_Path("/system");
938 if (restore_sys == NULL) {
939 LOGE("Unable to locate system partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000940 } else {
941 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400942 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400943 }
944 DataManager::GetValue(TW_RESTORE_DATA_VAR, check);
945 if (check > 0) {
946 restore_data = Find_Partition_By_Path("/data");
947 if (restore_data == NULL) {
948 LOGE("Unable to locate data partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000949 } else {
950 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400951 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400952 }
953 DataManager::GetValue(TW_RESTORE_CACHE_VAR, check);
954 if (check > 0) {
955 restore_cache = Find_Partition_By_Path("/cache");
956 if (restore_cache == NULL) {
957 LOGE("Unable to locate cache partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000958 } else {
959 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400960 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400961 }
962 DataManager::GetValue(TW_RESTORE_BOOT_VAR, check);
963 if (check > 0) {
964 restore_boot = Find_Partition_By_Path("/boot");
965 if (restore_boot == NULL) {
966 LOGE("Unable to locate boot partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000967 } else {
968 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400969 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400970 }
971 DataManager::GetValue(TW_RESTORE_ANDSEC_VAR, check);
972 if (check > 0) {
973 restore_andsec = Find_Partition_By_Path("/and-sec");
974 if (restore_andsec == NULL) {
975 LOGE("Unable to locate android secure partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000976 } else {
977 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400978 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400979 }
980 DataManager::GetValue(TW_RESTORE_SDEXT_VAR, check);
981 if (check > 0) {
982 restore_sdext = Find_Partition_By_Path("/sd-ext");
983 if (restore_sdext == NULL) {
984 LOGE("Unable to locate sd-ext partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000985 } else {
986 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400987 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400988 }
989#ifdef SP1_NAME
990 DataManager::GetValue(TW_RESTORE_SP1_VAR, check);
991 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400992 restore_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400993 if (restore_sp1 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400994 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000995 } else {
996 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400997 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400998 }
999#endif
1000#ifdef SP2_NAME
1001 DataManager::GetValue(TW_RESTORE_SP2_VAR, check);
1002 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001003 restore_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001004 if (restore_sp2 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001005 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001006 } else {
1007 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001008 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001009 }
1010#endif
1011#ifdef SP3_NAME
1012 DataManager::GetValue(TW_RESTORE_SP3_VAR, check);
1013 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001014 restore_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001015 if (restore_sp3 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001016 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001017 } else {
1018 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001019 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001020 }
1021#endif
1022
1023 if (partition_count == 0) {
1024 LOGE("No partitions selected for restore.\n");
1025 return false;
1026 }
1027
1028 if (check_md5 > 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001029 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
Dees_Troyb46a6842012-09-25 11:06:46 -04001030 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001031 ui_print("Verifying MD5...\n");
1032 if (restore_sys != NULL && !restore_sys->Check_MD5(Restore_Name))
1033 return false;
1034 if (restore_data != NULL && !restore_data->Check_MD5(Restore_Name))
1035 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001036 if (restore_data != NULL && restore_data->Has_SubPartition) {
1037 std::vector<TWPartition*>::iterator subpart;
1038
1039 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1040 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_data->Mount_Point) {
1041 if (!(*subpart)->Check_MD5(Restore_Name))
1042 return false;
1043 }
1044 }
1045 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001046 if (restore_cache != NULL && !restore_cache->Check_MD5(Restore_Name))
1047 return false;
1048 if (restore_boot != NULL && !restore_boot->Check_MD5(Restore_Name))
1049 return false;
1050 if (restore_andsec != NULL && !restore_andsec->Check_MD5(Restore_Name))
1051 return false;
1052 if (restore_sdext != NULL && !restore_sdext->Check_MD5(Restore_Name))
1053 return false;
1054 if (restore_sp1 != NULL && !restore_sp1->Check_MD5(Restore_Name))
1055 return false;
1056 if (restore_sp2 != NULL && !restore_sp2->Check_MD5(Restore_Name))
1057 return false;
1058 if (restore_sp3 != NULL && !restore_sp3->Check_MD5(Restore_Name))
1059 return false;
1060 ui_print("Done verifying MD5.\n");
1061 } else
1062 ui_print("Skipping MD5 check based on user setting.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001063
Dees_Troy4a2a1262012-09-18 09:33:47 -04001064 ui_print("Restoring %i partitions...\n", partition_count);
Dees_Troy093b7642012-09-21 15:59:38 -04001065 ui->SetProgress(0.0);
1066 if (restore_sys != NULL && !Restore_Partition(restore_sys, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001067 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001068 if (restore_data != NULL && !Restore_Partition(restore_data, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001069 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001070 if (restore_cache != NULL && !Restore_Partition(restore_cache, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001071 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001072 if (restore_boot != NULL && !Restore_Partition(restore_boot, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001073 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001074 if (restore_andsec != NULL && !Restore_Partition(restore_andsec, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001075 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001076 if (restore_sdext != NULL && !Restore_Partition(restore_sdext, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001077 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001078 if (restore_sp1 != NULL && !Restore_Partition(restore_sp1, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001079 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001080 if (restore_sp2 != NULL && !Restore_Partition(restore_sp2, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001081 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001082 if (restore_sp3 != NULL && !Restore_Partition(restore_sp3, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001083 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001084
Dees_Troyb46a6842012-09-25 11:06:46 -04001085 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -04001086 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001087 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -04001088 time(&rStop);
1089 ui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -04001090 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001091}
1092
1093void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001094 // Start with the default values
1095 int tw_restore_system = -1;
1096 int tw_restore_data = -1;
1097 int tw_restore_cache = -1;
1098 int tw_restore_recovery = -1;
1099 int tw_restore_boot = -1;
1100 int tw_restore_andsec = -1;
1101 int tw_restore_sdext = -1;
1102 int tw_restore_sp1 = -1;
1103 int tw_restore_sp2 = -1;
1104 int tw_restore_sp3 = -1;
1105 bool get_date = true;
1106
1107 DIR* d;
1108 d = opendir(Restore_Name.c_str());
1109 if (d == NULL)
1110 {
1111 LOGE("Error opening %s\n", Restore_Name.c_str());
1112 return;
1113 }
1114
1115 struct dirent* de;
1116 while ((de = readdir(d)) != NULL)
1117 {
1118 // Strip off three components
1119 char str[256];
1120 char* label;
1121 char* fstype = NULL;
1122 char* extn = NULL;
1123 char* ptr;
1124
1125 strcpy(str, de->d_name);
1126 if (strlen(str) <= 2)
1127 continue;
1128
1129 if (get_date) {
1130 char file_path[255];
1131 struct stat st;
1132
1133 strcpy(file_path, Restore_Name.c_str());
1134 strcat(file_path, "/");
1135 strcat(file_path, str);
1136 stat(file_path, &st);
1137 string backup_date = ctime((const time_t*)(&st.st_mtime));
1138 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
1139 get_date = false;
1140 }
1141
1142 label = str;
1143 ptr = label;
1144 while (*ptr && *ptr != '.') ptr++;
1145 if (*ptr == '.')
1146 {
1147 *ptr = 0x00;
1148 ptr++;
1149 fstype = ptr;
1150 }
1151 while (*ptr && *ptr != '.') ptr++;
1152 if (*ptr == '.')
1153 {
1154 *ptr = 0x00;
1155 ptr++;
1156 extn = ptr;
1157 }
1158
1159 if (extn == NULL || (strlen(extn) >= 3 && strncmp(extn, "win", 3) != 0)) continue;
1160
1161 TWPartition* Part = Find_Partition_By_Path(label);
1162 if (Part == NULL)
1163 {
1164 LOGE(" Unable to locate partition by backup name: '%s'\n", label);
1165 continue;
1166 }
1167
1168 Part->Backup_FileName = de->d_name;
1169 if (strlen(extn) > 3) {
1170 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1171 }
1172
1173 // Now, we just need to find the correct label
Dees_Troye58d5262012-09-21 12:27:57 -04001174 if (Part->Backup_Path == "/system")
Dees_Troy63c8df72012-09-10 14:02:05 -04001175 tw_restore_system = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001176 if (Part->Backup_Path == "/data")
Dees_Troy63c8df72012-09-10 14:02:05 -04001177 tw_restore_data = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001178 if (Part->Backup_Path == "/cache")
Dees_Troy63c8df72012-09-10 14:02:05 -04001179 tw_restore_cache = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001180 if (Part->Backup_Path == "/recovery")
Dees_Troy63c8df72012-09-10 14:02:05 -04001181 tw_restore_recovery = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001182 if (Part->Backup_Path == "/boot")
Dees_Troy63c8df72012-09-10 14:02:05 -04001183 tw_restore_boot = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001184 if (Part->Backup_Path == "/and-sec")
Dees_Troy63c8df72012-09-10 14:02:05 -04001185 tw_restore_andsec = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001186 if (Part->Backup_Path == "/sd-ext")
Dees_Troy63c8df72012-09-10 14:02:05 -04001187 tw_restore_sdext = 1;
1188#ifdef SP1_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001189 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP1_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001190 tw_restore_sp1 = 1;
1191#endif
1192#ifdef SP2_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001193 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP2_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001194 tw_restore_sp2 = 1;
1195#endif
1196#ifdef SP3_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001197 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP3_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001198 tw_restore_sp3 = 1;
1199#endif
1200 }
1201 closedir(d);
1202
1203 // Set the final values
1204 DataManager::SetValue(TW_RESTORE_SYSTEM_VAR, tw_restore_system);
1205 DataManager::SetValue(TW_RESTORE_DATA_VAR, tw_restore_data);
1206 DataManager::SetValue(TW_RESTORE_CACHE_VAR, tw_restore_cache);
1207 DataManager::SetValue(TW_RESTORE_RECOVERY_VAR, tw_restore_recovery);
1208 DataManager::SetValue(TW_RESTORE_BOOT_VAR, tw_restore_boot);
1209 DataManager::SetValue(TW_RESTORE_ANDSEC_VAR, tw_restore_andsec);
1210 DataManager::SetValue(TW_RESTORE_SDEXT_VAR, tw_restore_sdext);
1211 DataManager::SetValue(TW_RESTORE_SP1_VAR, tw_restore_sp1);
1212 DataManager::SetValue(TW_RESTORE_SP2_VAR, tw_restore_sp2);
1213 DataManager::SetValue(TW_RESTORE_SP3_VAR, tw_restore_sp3);
1214
Dees_Troy51a0e822012-09-05 15:24:24 -04001215 return;
1216}
1217
1218int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001219 std::vector<TWPartition*>::iterator iter;
1220 int ret = false;
1221 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001222 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001223
1224 // Iterate through all partitions
1225 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001226 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001227 if (Path == "/and-sec")
1228 ret = (*iter)->Wipe_AndSec();
1229 else
1230 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001231 found = true;
1232 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1233 (*iter)->Wipe();
1234 }
1235 }
1236 if (found) {
1237 return ret;
1238 } else
1239 LOGE("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1240 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001241}
1242
1243int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001244 TWPartition* Part = Find_Partition_By_Block(Block);
1245
1246 if (Part) {
1247 if (Part->Has_SubPartition) {
1248 std::vector<TWPartition*>::iterator subpart;
1249
1250 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1251 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1252 (*subpart)->Wipe();
1253 }
1254 return Part->Wipe();
1255 } else
1256 return Part->Wipe();
1257 }
1258 LOGE("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
1259 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001260}
1261
1262int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001263 TWPartition* Part = Find_Partition_By_Name(Name);
1264
1265 if (Part) {
1266 if (Part->Has_SubPartition) {
1267 std::vector<TWPartition*>::iterator subpart;
1268
1269 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1270 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1271 (*subpart)->Wipe();
1272 }
1273 return Part->Wipe();
1274 } else
1275 return Part->Wipe();
1276 }
1277 LOGE("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
1278 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001279}
1280
1281int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001282 std::vector<TWPartition*>::iterator iter;
1283 int ret = true;
1284
1285 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001286 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001287 if (!(*iter)->Wipe())
1288 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001289 } else if ((*iter)->Has_Android_Secure) {
1290 if (!(*iter)->Wipe_AndSec())
1291 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001292 }
1293 }
1294 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001295}
1296
Dees_Troy38bd7602012-09-14 13:33:53 -04001297int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1298 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001299 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001300
1301 if (!Mount_By_Path("/data", true))
1302 return false;
1303
1304 if (!Mount_By_Path("/cache", true))
1305 return false;
1306
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001307 dir.push_back("/data/dalvik-cache");
1308 dir.push_back("/cache/dalvik-cache");
1309 dir.push_back("/cache/dc");
Dees_Troy38bd7602012-09-14 13:33:53 -04001310 ui_print("\nWiping Dalvik Cache Directories...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001311 for (int i = 0; i < dir.size(); ++i) {
1312 if (stat(dir.at(i).c_str(), &st) == 0) {
1313 TWFunc::removeDir(dir.at(i), false);
1314 ui_print("Cleaned: %s...\n", dir.at(i).c_str());
1315 }
1316 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001317 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
1318 if (sdext != NULL) {
1319 if (sdext->Is_Present && sdext->Mount(false)) {
1320 if (stat("/sd-ext/dalvik-cache", &st) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001321 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
1322 ui_print("Cleaned: /sd-ext/dalvik-cache...\n");
1323 }
1324 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001325 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001326 ui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
1327 return true;
1328}
1329
1330int TWPartitionManager::Wipe_Rotate_Data(void) {
1331 if (!Mount_By_Path("/data", true))
1332 return false;
1333
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001334 unlink("/data/misc/akmd*");
1335 unlink("/data/misc/rild*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001336 ui_print("Rotation data wiped.\n");
1337 return true;
1338}
1339
1340int TWPartitionManager::Wipe_Battery_Stats(void) {
1341 struct stat st;
1342
1343 if (!Mount_By_Path("/data", true))
1344 return false;
1345
1346 if (0 != stat("/data/system/batterystats.bin", &st)) {
1347 ui_print("No Battery Stats Found. No Need To Wipe.\n");
1348 } else {
1349 remove("/data/system/batterystats.bin");
1350 ui_print("Cleared battery stats.\n");
1351 }
1352 return true;
1353}
1354
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001355int TWPartitionManager::Wipe_Android_Secure(void) {
1356 std::vector<TWPartition*>::iterator iter;
1357 int ret = false;
1358 bool found = false;
1359
1360 // Iterate through all partitions
1361 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1362 if ((*iter)->Has_Android_Secure) {
1363 ret = (*iter)->Wipe_AndSec();
1364 found = true;
1365 }
1366 }
1367 if (found) {
1368 return ret;
1369 } else {
1370 LOGE("No android secure partitions found.\n");
1371 }
1372 return false;
1373}
1374
Dees_Troy38bd7602012-09-14 13:33:53 -04001375int TWPartitionManager::Format_Data(void) {
1376 TWPartition* dat = Find_Partition_By_Path("/data");
1377
1378 if (dat != NULL) {
1379 if (!dat->UnMount(true))
1380 return false;
1381
1382 return dat->Wipe_Encryption();
1383 } else {
1384 LOGE("Unable to locate /data.\n");
1385 return false;
1386 }
1387 return false;
1388}
1389
1390int TWPartitionManager::Wipe_Media_From_Data(void) {
1391 TWPartition* dat = Find_Partition_By_Path("/data");
1392
1393 if (dat != NULL) {
1394 if (!dat->Has_Data_Media) {
1395 LOGE("This device does not have /data/media\n");
1396 return false;
1397 }
1398 if (!dat->Mount(true))
1399 return false;
1400
1401 ui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001402 TWFunc::removeDir("/data/media", false);
1403 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
1404 return -1;
Dees_Troy38bd7602012-09-14 13:33:53 -04001405 if (dat->Has_Data_Media) {
1406 dat->Recreate_Media_Folder();
1407 }
1408 return true;
1409 } else {
1410 LOGE("Unable to locate /data.\n");
1411 return false;
1412 }
1413 return false;
1414}
1415
Dees_Troy51a0e822012-09-05 15:24:24 -04001416void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001417 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001418 return;
1419}
1420
1421void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001422 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001423 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001424
Dees_Troy32c8eb82012-09-11 15:28:06 -04001425 ui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001426 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001427 if ((*iter)->Can_Be_Mounted) {
1428 (*iter)->Update_Size(true);
1429 if ((*iter)->Mount_Point == "/system") {
1430 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1431 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1432 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1433 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1434 } else if ((*iter)->Mount_Point == "/cache") {
1435 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1436 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1437 } else if ((*iter)->Mount_Point == "/sd-ext") {
1438 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1439 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1440 if ((*iter)->Backup_Size == 0) {
1441 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1442 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1443 } else
1444 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001445 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001446 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001447 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001448 if ((*iter)->Backup_Size == 0) {
1449 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1450 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1451 } else
1452 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001453 } else if ((*iter)->Mount_Point == "/boot") {
1454 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1455 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1456 if ((*iter)->Backup_Size == 0) {
1457 DataManager::SetValue("tw_has_boot_partition", 0);
1458 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1459 } else
1460 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001461 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001462#ifdef SP1_NAME
1463 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1464 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1465 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1466 }
1467#endif
1468#ifdef SP2_NAME
1469 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1470 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1471 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1472 }
1473#endif
1474#ifdef SP3_NAME
1475 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1476 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1477 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1478 }
1479#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001480 } else {
1481 // Handle unmountable partitions in case we reset defaults
1482 if ((*iter)->Mount_Point == "/boot") {
1483 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1484 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1485 if ((*iter)->Backup_Size == 0) {
1486 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1487 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1488 } else
1489 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1490 } else if ((*iter)->Mount_Point == "/recovery") {
1491 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1492 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1493 if ((*iter)->Backup_Size == 0) {
1494 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1495 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1496 } else
1497 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001498 } else if ((*iter)->Mount_Point == "/data") {
1499 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001500 }
1501#ifdef SP1_NAME
1502 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1503 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1504 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1505 }
1506#endif
1507#ifdef SP2_NAME
1508 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1509 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1510 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1511 }
1512#endif
1513#ifdef SP3_NAME
1514 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1515 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1516 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1517 }
1518#endif
Dees_Troy51127312012-09-08 13:08:49 -04001519 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001520 }
Dees_Troy51127312012-09-08 13:08:49 -04001521 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1522 string current_storage_path = DataManager::GetCurrentStoragePath();
1523 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001524 if (FreeStorage != NULL) {
1525 // Attempt to mount storage
1526 if (!FreeStorage->Mount(false)) {
1527 // We couldn't mount storage... check to see if we have dual storage
1528 int has_dual_storage;
1529 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1530 if (has_dual_storage == 1) {
1531 // We have dual storage, see if we're using the internal storage that should always be present
1532 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001533 if (!FreeStorage->Is_Encrypted) {
1534 // Not able to use internal, so error!
1535 LOGE("Unable to mount internal storage.\n");
1536 }
Dees_Troy8170a922012-09-18 15:40:25 -04001537 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1538 } else {
1539 // We were using external, flip to internal
1540 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1541 current_storage_path = DataManager::GetCurrentStoragePath();
1542 FreeStorage = Find_Partition_By_Path(current_storage_path);
1543 if (FreeStorage != NULL) {
1544 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1545 } else {
1546 LOGE("Unable to locate internal storage partition.\n");
1547 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1548 }
1549 }
1550 } else {
1551 // No dual storage and unable to mount storage, error!
1552 LOGE("Unable to mount storage.\n");
1553 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1554 }
1555 } else {
1556 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1557 }
1558 } else {
Dees_Troy51127312012-09-08 13:08:49 -04001559 LOGI("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001560 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001561 if (!Write_Fstab())
1562 LOGE("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001563 return;
1564}
1565
1566int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001567#ifdef TW_INCLUDE_CRYPTO
1568 int ret_val, password_len;
1569 char crypto_blkdev[255], cPassword[255];
1570 size_t result;
1571
1572 property_set("ro.crypto.state", "encrypted");
1573#ifdef TW_INCLUDE_JB_CRYPTO
1574 // No extra flags needed
1575#else
1576 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1577 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1578 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1579 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1580 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1581 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001582
1583#ifdef CRYPTO_SD_FS_TYPE
1584 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1585 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1586 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001587#endif
a39552696ff55ce2013-01-08 16:14:56 +00001588
1589 property_set("rw.km_fips_status", "ready");
1590
1591#endif
1592
1593 // some samsung devices store "footer" on efs partition
1594 TWPartition *efs = Find_Partition_By_Path("/efs");
1595 if(efs && !efs->Is_Mounted())
1596 efs->Mount(false);
1597 else
1598 efs = 0;
1599#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001600#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001601 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001602 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001603 property_set("ro.crypto.external_encrypted", "1");
1604 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1605 } else {
1606 property_set("ro.crypto.external_encrypted", "0");
1607 }
1608#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001609#endif
a39552696ff55ce2013-01-08 16:14:56 +00001610
Dees_Troy5bf43922012-09-07 16:07:55 -04001611 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001612 int pwret = cryptfs_check_passwd(cPassword);
1613
1614 if (pwret != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001615 LOGE("Failed to decrypt data.\n");
1616 return -1;
1617 }
a39552696ff55ce2013-01-08 16:14:56 +00001618
1619 if(efs)
1620 efs->UnMount(false);
1621
Dees_Troy5bf43922012-09-07 16:07:55 -04001622 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1623 if (strcmp(crypto_blkdev, "error") == 0) {
1624 LOGE("Error retrieving decrypted data block device.\n");
1625 } else {
1626 TWPartition* dat = Find_Partition_By_Path("/data");
1627 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001628 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001629 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1630 dat->Is_Decrypted = true;
1631 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001632 dat->Setup_File_System(false);
Dees_Troy32c8eb82012-09-11 15:28:06 -04001633 ui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001634
1635#ifdef CRYPTO_SD_FS_TYPE
1636 char crypto_blkdev_sd[255];
1637 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1638 if (strcmp(crypto_blkdev_sd, "error") == 0) {
1639 LOGE("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001640 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001641 emmc->Is_Decrypted = true;
1642 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1643 emmc->Setup_File_System(false);
1644 ui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
1645 }
a39552696ff55ce2013-01-08 16:14:56 +00001646#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001647#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001648#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001649 char is_external_decrypted[255];
1650 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1651 if (strcmp(is_external_decrypted, "1") == 0) {
1652 sdcard->Is_Decrypted = true;
1653 sdcard->EcryptFS_Password = Password;
1654 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001655 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1656 MetaEcfsFile += "/.MetaEcfsFile";
1657 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1658 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1659 sdcard->UnMount(false);
1660 sdcard->Mount(false);
1661 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001662 } else {
1663 sdcard->Is_Decrypted = false;
1664 sdcard->Decrypted_Block_Device = "";
1665 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001666#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001667#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001668
Dees_Troy5bf43922012-09-07 16:07:55 -04001669 // Sleep for a bit so that the device will be ready
1670 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001671#ifdef RECOVERY_SDCARD_ON_DATA
1672 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1673 dat->Storage_Path = "/data/media/0";
1674 dat->Symlink_Path = dat->Storage_Path;
1675 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
1676 dat->UnMount(false);
1677 DataManager::SetBackupFolder();
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001678 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001679 }
1680#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001681 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001682 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001683 } else
1684 LOGE("Unable to locate data partition.\n");
1685 }
1686 return 0;
1687#else
1688 LOGE("No crypto support was compiled into this build.\n");
1689 return -1;
1690#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001691 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001692}
1693
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001694int TWPartitionManager::Fix_Permissions(void) {
1695 int result = 0;
1696 if (!Mount_By_Path("/data", true))
1697 return false;
1698
1699 if (!Mount_By_Path("/system", true))
1700 return false;
1701
1702 Mount_By_Path("/sd-ext", false);
1703
1704 fixPermissions perms;
1705 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001706 UnMount_Main_Partitions();
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001707 ui_print("Done.\n\n");
1708 return result;
1709}
1710
Dees_Troy8170a922012-09-18 15:40:25 -04001711//partial kangbang from system/vold
Dees_Troyd21618c2012-10-14 18:48:49 -04001712int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
1713 int fd;
1714 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1715
1716 if (Part == NULL) {
1717 LOGE("Unable to locate volume information for USB storage mode.");
1718 return false;
1719 }
1720 if (!Part->UnMount(true))
1721 return false;
1722
1723 if ((fd = open(Lun_File.c_str(), O_WRONLY)) < 0) {
1724 LOGE("Unable to open ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
1725 return false;
1726 }
1727
1728 if (write(fd, Part->Actual_Block_Device.c_str(), Part->Actual_Block_Device.size()) < 0) {
1729 LOGE("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
1730 close(fd);
1731 return false;
1732 }
1733 close(fd);
1734 return true;
1735}
1736
Dees_Troy8170a922012-09-18 15:40:25 -04001737int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001738 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001739 char lun_file[255];
Dees_Troy8170a922012-09-18 15:40:25 -04001740 string ext_path;
Dees_Troyd21618c2012-10-14 18:48:49 -04001741 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001742
1743 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1744 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1745 if (has_dual == 1 && has_data_media == 0) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001746 string Lun_File_str = CUSTOM_LUN_FILE;
1747 size_t found = Lun_File_str.find("%");
1748 if (found != string::npos) {
1749 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1750 if (TWFunc::Path_Exists(lun_file))
1751 has_multiple_lun = true;
1752 }
1753 if (!has_multiple_lun) {
Dees_Troyf4ca6122012-10-13 22:17:20 -04001754 // Device doesn't have multiple lun files, mount current storage
Dees_Troyf4ca6122012-10-13 22:17:20 -04001755 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001756 return Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001757 } else {
1758 // Device has multiple lun files
Dees_Troyf4ca6122012-10-13 22:17:20 -04001759 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001760 if (!Open_Lun_File(DataManager::GetSettingsStoragePath(), lun_file))
Dees_Troyf4ca6122012-10-13 22:17:20 -04001761 return false;
Dees_Troyf4ca6122012-10-13 22:17:20 -04001762 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001763 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Dees_Troyd21618c2012-10-14 18:48:49 -04001764 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001765 }
Dees_Troy8170a922012-09-18 15:40:25 -04001766 } else {
1767 if (has_data_media == 0)
1768 ext_path = DataManager::GetCurrentStoragePath();
1769 else
1770 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001771 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001772 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001773 }
1774 return true;
1775}
1776
1777int TWPartitionManager::usb_storage_disable(void) {
1778 int fd, index;
1779 char lun_file[255];
1780
1781 for (index=0; index<2; index++) {
1782 sprintf(lun_file, CUSTOM_LUN_FILE, index);
1783
1784 if ((fd = open(lun_file, O_WRONLY)) < 0) {
Dees_Troye58d5262012-09-21 12:27:57 -04001785 Mount_All_Storage();
1786 Update_System_Details();
1787 if (index == 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001788 LOGE("Unable to open ums lunfile '%s': (%s)", lun_file, strerror(errno));
Dees_Troye58d5262012-09-21 12:27:57 -04001789 return false;
1790 } else
1791 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001792 }
1793
1794 char ch = 0;
1795 if (write(fd, &ch, 1) < 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001796 close(fd);
Dees_Troye58d5262012-09-21 12:27:57 -04001797 Mount_All_Storage();
1798 Update_System_Details();
1799 if (index == 0) {
1800 LOGE("Unable to write to ums lunfile '%s': (%s)", lun_file, strerror(errno));
1801 return false;
1802 } else
1803 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001804 }
1805
1806 close(fd);
1807 }
Dees_Troye58d5262012-09-21 12:27:57 -04001808 Mount_All_Storage();
1809 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001810 UnMount_Main_Partitions();
Dees_Troy8170a922012-09-18 15:40:25 -04001811 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001812}
1813
1814void TWPartitionManager::Mount_All_Storage(void) {
1815 std::vector<TWPartition*>::iterator iter;
1816
1817 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1818 if ((*iter)->Is_Storage)
1819 (*iter)->Mount(false);
1820 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001821}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001822
Dees_Troyd0384ef2012-10-12 12:15:42 -04001823void TWPartitionManager::UnMount_Main_Partitions(void) {
1824 // Unmounts system and data if data is not data/media
1825 // Also unmounts boot if boot is mountable
1826 LOGI("Unmounting main partitions...\n");
1827
1828 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1829
1830 UnMount_By_Path("/system", true);
1831#ifndef RECOVERY_SDCARD_ON_DATA
1832 UnMount_By_Path("/data", true);
1833#endif
1834 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1835 Boot_Partition->UnMount(true);
1836}
1837
Dees_Troy9350b8d2012-09-27 12:38:38 -04001838int TWPartitionManager::Partition_SDCard(void) {
1839 char mkdir_path[255], temp[255], line[512];
1840 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1841 int ext, swap, total_size = 0, fat_size;
1842 FILE* fp;
1843
<