blob: bcabb1ca4b64462f434ba5ed3ea88512db8bd36b [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)) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000488 command = "cd '" + Backup_Folder + "' && md5sum '" + Backup_Filename + "' > '" + Backup_Filename + ".md5'";
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500489 if (TWFunc::Exec_Cmd(command, result) == 0) {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400490 ui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400491 return true;
492 } else {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400493 ui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400494 return false;
495 }
496 } else {
497 char filename[512];
498 int index = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400499 sprintf(filename, "%s%03i", Full_File.c_str(), index);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400500 while (TWFunc::Path_Exists(filename) == true) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500501 ostringstream intToStr;
502 intToStr << index;
503 ostringstream fn;
504 fn << setw(3) << setfill('0') << intToStr.str();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000505 command = "cd '" + Backup_Folder + "' && md5sum '" + Backup_Filename + fn.str() + "' > '" + Backup_Filename + fn.str() + ".md5'";
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500506 if (TWFunc::Exec_Cmd(command, result) != 0) {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400507 ui_print(" * MD5 Error.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400508 return false;
509 }
510 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400511 sprintf(filename, "%s%03i", Full_File.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400512 }
513 if (index == 0) {
514 LOGE("Backup file: '%s' not found!\n", filename);
515 return false;
516 }
Dees_Troyc5865ab2012-09-24 15:08:04 -0400517 ui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400518 }
519 return true;
520}
521
Dees_Troy093b7642012-09-21 15:59:38 -0400522bool 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 -0400523 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500524 int img_bps;
525 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400526 unsigned long total_time, remain_time, section_time;
527 int use_compression, backup_time;
528 float pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400529
530 if (Part == NULL)
531 return true;
532
Dees_Troy093b7642012-09-21 15:59:38 -0400533 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
534
535 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
536 if (use_compression)
537 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
538 else
539 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
540
541 // We know the speed for both, how far into the whole backup are we, based on time
542 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
543 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
544
545 pos = (total_time - remain_time) / (float) total_time;
546 ui->SetProgress(pos);
547
548 LOGI("Estimated Total time: %lu Estimated remaining time: %lu\n", total_time, remain_time);
549
550 // And get the time
551 if (Part->Backup_Method == 1)
552 section_time = Part->Backup_Size / file_bps;
553 else
554 section_time = Part->Backup_Size / img_bps;
555
556 // Set the position
557 pos = section_time / (float) total_time;
558 ui->ShowProgress(pos, section_time);
559
Dees_Troy43d8b002012-09-17 16:00:01 -0400560 time(&start);
561
562 if (Part->Backup(Backup_Folder)) {
Dees_Troy8170a922012-09-18 15:40:25 -0400563 if (Part->Has_SubPartition) {
564 std::vector<TWPartition*>::iterator subpart;
565
566 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
567 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
568 if (!(*subpart)->Backup(Backup_Folder))
569 return false;
570 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
571 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400572 if (Part->Backup_Method == 1) {
573 *file_bytes_remaining -= (*subpart)->Backup_Size;
574 } else {
575 *img_bytes_remaining -= (*subpart)->Backup_Size;
576 }
Dees_Troy8170a922012-09-18 15:40:25 -0400577 }
578 }
579 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400580 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400581 backup_time = (int) difftime(stop, start);
582 LOGI("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400583 if (Part->Backup_Method == 1) {
584 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400585 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400586 } else {
587 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400588 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400589 }
590 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
591 } else {
592 return false;
593 }
594}
595
596int TWPartitionManager::Run_Backup(void) {
597 int check, do_md5, partition_count = 0;
598 string Backup_Folder, Backup_Name, Full_Backup_Path;
Dees_Troy8170a922012-09-18 15:40:25 -0400599 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 -0400600 unsigned long img_time = 0, file_time = 0;
601 TWPartition* backup_sys = NULL;
602 TWPartition* backup_data = NULL;
603 TWPartition* backup_cache = NULL;
604 TWPartition* backup_recovery = NULL;
605 TWPartition* backup_boot = NULL;
606 TWPartition* backup_andsec = NULL;
607 TWPartition* backup_sdext = NULL;
608 TWPartition* backup_sp1 = NULL;
609 TWPartition* backup_sp2 = NULL;
610 TWPartition* backup_sp3 = NULL;
611 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400612 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400613 struct tm *t;
614 time_t start, stop, seconds, total_start, total_stop;
615 seconds = time(0);
616 t = localtime(&seconds);
617
618 time(&total_start);
619
620 Update_System_Details();
621
622 if (!Mount_Current_Storage(true))
623 return false;
624
625 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400626 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400627 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400628 else
629 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400630
Dees_Troy43d8b002012-09-17 16:00:01 -0400631 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
632 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400633 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name.empty()) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400634 char timestamp[255];
635 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);
636 Backup_Name = timestamp;
637 }
638 LOGI("Backup Name is: '%s'\n", Backup_Name.c_str());
639 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
640 LOGI("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
641
Dees_Troy43d8b002012-09-17 16:00:01 -0400642 LOGI("Calculating backup details...\n");
643 DataManager::GetValue(TW_BACKUP_SYSTEM_VAR, check);
644 if (check) {
645 backup_sys = Find_Partition_By_Path("/system");
646 if (backup_sys != NULL) {
647 partition_count++;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500648 if (backup_sys->Backup_Method == 1) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400649 file_bytes += backup_sys->Backup_Size;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500650 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400651 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;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500857 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400858
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500859 ui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400860 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
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500867 int prev_img_bps, use_compression;
868 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400869 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
870 img_bps += (prev_img_bps * 4);
871 img_bps /= 5;
872
873 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
874 if (use_compression)
875 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
876 else
877 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
878 file_bps += (prev_file_bps * 4);
879 file_bps /= 5;
880
881 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
882 if (use_compression)
883 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
884 else
885 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
886
Dees_Troy43d8b002012-09-17 16:00:01 -0400887 ui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
888 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400889 UnMount_Main_Partitions();
Dees_Troy43d8b002012-09-17 16:00:01 -0400890 ui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000891 string backup_log = Full_Backup_Path + "recovery.log";
892 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
893 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400894}
895
Dees_Troy093b7642012-09-21 15:59:38 -0400896bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400897 time_t Start, Stop;
898 time(&Start);
Dees_Troy093b7642012-09-21 15:59:38 -0400899 ui->ShowProgress(1.0 / (float)partition_count, 150);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400900 if (!Part->Restore(Restore_Name))
901 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400902 if (Part->Has_SubPartition) {
903 std::vector<TWPartition*>::iterator subpart;
904
905 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
906 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
907 if (!(*subpart)->Restore(Restore_Name))
908 return false;
909 }
910 }
911 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400912 time(&Stop);
913 ui_print("[%s done (%d seconds)]\n\n", Part->Display_Name.c_str(), (int)difftime(Stop, Start));
914 return true;
915}
916
Dees_Troy51a0e822012-09-05 15:24:24 -0400917int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400918 int check_md5, check, partition_count = 0;
919 TWPartition* restore_sys = NULL;
920 TWPartition* restore_data = NULL;
921 TWPartition* restore_cache = NULL;
922 TWPartition* restore_boot = NULL;
923 TWPartition* restore_andsec = NULL;
924 TWPartition* restore_sdext = NULL;
925 TWPartition* restore_sp1 = NULL;
926 TWPartition* restore_sp2 = NULL;
927 TWPartition* restore_sp3 = NULL;
928 time_t rStart, rStop;
929 time(&rStart);
Dees_Troy43d8b002012-09-17 16:00:01 -0400930
Dees_Troy4a2a1262012-09-18 09:33:47 -0400931 ui_print("\n[RESTORE STARTED]\n\n");
932 ui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400933
Dees_Troy4a2a1262012-09-18 09:33:47 -0400934 if (!Mount_Current_Storage(true))
935 return false;
936
937 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
938 DataManager::GetValue(TW_RESTORE_SYSTEM_VAR, check);
Dees_Troy63c8df72012-09-10 14:02:05 -0400939 if (check > 0) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400940 restore_sys = Find_Partition_By_Path("/system");
941 if (restore_sys == NULL) {
942 LOGE("Unable to locate system partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000943 } else {
944 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400945 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400946 }
947 DataManager::GetValue(TW_RESTORE_DATA_VAR, check);
948 if (check > 0) {
949 restore_data = Find_Partition_By_Path("/data");
950 if (restore_data == NULL) {
951 LOGE("Unable to locate data 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_CACHE_VAR, check);
957 if (check > 0) {
958 restore_cache = Find_Partition_By_Path("/cache");
959 if (restore_cache == NULL) {
960 LOGE("Unable to locate cache 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_BOOT_VAR, check);
966 if (check > 0) {
967 restore_boot = Find_Partition_By_Path("/boot");
968 if (restore_boot == NULL) {
969 LOGE("Unable to locate boot 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 }
974 DataManager::GetValue(TW_RESTORE_ANDSEC_VAR, check);
975 if (check > 0) {
976 restore_andsec = Find_Partition_By_Path("/and-sec");
977 if (restore_andsec == NULL) {
978 LOGE("Unable to locate android secure partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000979 } else {
980 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400981 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400982 }
983 DataManager::GetValue(TW_RESTORE_SDEXT_VAR, check);
984 if (check > 0) {
985 restore_sdext = Find_Partition_By_Path("/sd-ext");
986 if (restore_sdext == NULL) {
987 LOGE("Unable to locate sd-ext partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000988 } else {
989 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400990 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400991 }
992#ifdef SP1_NAME
993 DataManager::GetValue(TW_RESTORE_SP1_VAR, check);
994 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400995 restore_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400996 if (restore_sp1 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400997 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000998 } else {
999 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001000 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001001 }
1002#endif
1003#ifdef SP2_NAME
1004 DataManager::GetValue(TW_RESTORE_SP2_VAR, check);
1005 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001006 restore_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001007 if (restore_sp2 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001008 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_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 SP3_NAME
1015 DataManager::GetValue(TW_RESTORE_SP3_VAR, check);
1016 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001017 restore_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001018 if (restore_sp3 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001019 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_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
1026 if (partition_count == 0) {
1027 LOGE("No partitions selected for restore.\n");
1028 return false;
1029 }
1030
1031 if (check_md5 > 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001032 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
Dees_Troyb46a6842012-09-25 11:06:46 -04001033 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001034 ui_print("Verifying MD5...\n");
1035 if (restore_sys != NULL && !restore_sys->Check_MD5(Restore_Name))
1036 return false;
1037 if (restore_data != NULL && !restore_data->Check_MD5(Restore_Name))
1038 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001039 if (restore_data != NULL && restore_data->Has_SubPartition) {
1040 std::vector<TWPartition*>::iterator subpart;
1041
1042 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1043 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_data->Mount_Point) {
1044 if (!(*subpart)->Check_MD5(Restore_Name))
1045 return false;
1046 }
1047 }
1048 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001049 if (restore_cache != NULL && !restore_cache->Check_MD5(Restore_Name))
1050 return false;
1051 if (restore_boot != NULL && !restore_boot->Check_MD5(Restore_Name))
1052 return false;
1053 if (restore_andsec != NULL && !restore_andsec->Check_MD5(Restore_Name))
1054 return false;
1055 if (restore_sdext != NULL && !restore_sdext->Check_MD5(Restore_Name))
1056 return false;
1057 if (restore_sp1 != NULL && !restore_sp1->Check_MD5(Restore_Name))
1058 return false;
1059 if (restore_sp2 != NULL && !restore_sp2->Check_MD5(Restore_Name))
1060 return false;
1061 if (restore_sp3 != NULL && !restore_sp3->Check_MD5(Restore_Name))
1062 return false;
1063 ui_print("Done verifying MD5.\n");
1064 } else
1065 ui_print("Skipping MD5 check based on user setting.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001066
Dees_Troy4a2a1262012-09-18 09:33:47 -04001067 ui_print("Restoring %i partitions...\n", partition_count);
Dees_Troy093b7642012-09-21 15:59:38 -04001068 ui->SetProgress(0.0);
1069 if (restore_sys != NULL && !Restore_Partition(restore_sys, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001070 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001071 if (restore_data != NULL && !Restore_Partition(restore_data, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001072 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001073 if (restore_cache != NULL && !Restore_Partition(restore_cache, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001074 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001075 if (restore_boot != NULL && !Restore_Partition(restore_boot, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001076 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001077 if (restore_andsec != NULL && !Restore_Partition(restore_andsec, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001078 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001079 if (restore_sdext != NULL && !Restore_Partition(restore_sdext, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001080 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001081 if (restore_sp1 != NULL && !Restore_Partition(restore_sp1, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001082 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001083 if (restore_sp2 != NULL && !Restore_Partition(restore_sp2, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001084 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001085 if (restore_sp3 != NULL && !Restore_Partition(restore_sp3, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001086 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001087
Dees_Troyb46a6842012-09-25 11:06:46 -04001088 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -04001089 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001090 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -04001091 time(&rStop);
1092 ui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -04001093 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001094}
1095
1096void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001097 // Start with the default values
1098 int tw_restore_system = -1;
1099 int tw_restore_data = -1;
1100 int tw_restore_cache = -1;
1101 int tw_restore_recovery = -1;
1102 int tw_restore_boot = -1;
1103 int tw_restore_andsec = -1;
1104 int tw_restore_sdext = -1;
1105 int tw_restore_sp1 = -1;
1106 int tw_restore_sp2 = -1;
1107 int tw_restore_sp3 = -1;
1108 bool get_date = true;
1109
1110 DIR* d;
1111 d = opendir(Restore_Name.c_str());
1112 if (d == NULL)
1113 {
1114 LOGE("Error opening %s\n", Restore_Name.c_str());
1115 return;
1116 }
1117
1118 struct dirent* de;
1119 while ((de = readdir(d)) != NULL)
1120 {
1121 // Strip off three components
1122 char str[256];
1123 char* label;
1124 char* fstype = NULL;
1125 char* extn = NULL;
1126 char* ptr;
1127
1128 strcpy(str, de->d_name);
1129 if (strlen(str) <= 2)
1130 continue;
1131
1132 if (get_date) {
1133 char file_path[255];
1134 struct stat st;
1135
1136 strcpy(file_path, Restore_Name.c_str());
1137 strcat(file_path, "/");
1138 strcat(file_path, str);
1139 stat(file_path, &st);
1140 string backup_date = ctime((const time_t*)(&st.st_mtime));
1141 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
1142 get_date = false;
1143 }
1144
1145 label = str;
1146 ptr = label;
1147 while (*ptr && *ptr != '.') ptr++;
1148 if (*ptr == '.')
1149 {
1150 *ptr = 0x00;
1151 ptr++;
1152 fstype = ptr;
1153 }
1154 while (*ptr && *ptr != '.') ptr++;
1155 if (*ptr == '.')
1156 {
1157 *ptr = 0x00;
1158 ptr++;
1159 extn = ptr;
1160 }
1161
1162 if (extn == NULL || (strlen(extn) >= 3 && strncmp(extn, "win", 3) != 0)) continue;
1163
1164 TWPartition* Part = Find_Partition_By_Path(label);
1165 if (Part == NULL)
1166 {
1167 LOGE(" Unable to locate partition by backup name: '%s'\n", label);
1168 continue;
1169 }
1170
1171 Part->Backup_FileName = de->d_name;
1172 if (strlen(extn) > 3) {
1173 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1174 }
1175
1176 // Now, we just need to find the correct label
Dees_Troye58d5262012-09-21 12:27:57 -04001177 if (Part->Backup_Path == "/system")
Dees_Troy63c8df72012-09-10 14:02:05 -04001178 tw_restore_system = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001179 if (Part->Backup_Path == "/data")
Dees_Troy63c8df72012-09-10 14:02:05 -04001180 tw_restore_data = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001181 if (Part->Backup_Path == "/cache")
Dees_Troy63c8df72012-09-10 14:02:05 -04001182 tw_restore_cache = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001183 if (Part->Backup_Path == "/recovery")
Dees_Troy63c8df72012-09-10 14:02:05 -04001184 tw_restore_recovery = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001185 if (Part->Backup_Path == "/boot")
Dees_Troy63c8df72012-09-10 14:02:05 -04001186 tw_restore_boot = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001187 if (Part->Backup_Path == "/and-sec")
Dees_Troy63c8df72012-09-10 14:02:05 -04001188 tw_restore_andsec = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001189 if (Part->Backup_Path == "/sd-ext")
Dees_Troy63c8df72012-09-10 14:02:05 -04001190 tw_restore_sdext = 1;
1191#ifdef SP1_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001192 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP1_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001193 tw_restore_sp1 = 1;
1194#endif
1195#ifdef SP2_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001196 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP2_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001197 tw_restore_sp2 = 1;
1198#endif
1199#ifdef SP3_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001200 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP3_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001201 tw_restore_sp3 = 1;
1202#endif
1203 }
1204 closedir(d);
1205
1206 // Set the final values
1207 DataManager::SetValue(TW_RESTORE_SYSTEM_VAR, tw_restore_system);
1208 DataManager::SetValue(TW_RESTORE_DATA_VAR, tw_restore_data);
1209 DataManager::SetValue(TW_RESTORE_CACHE_VAR, tw_restore_cache);
1210 DataManager::SetValue(TW_RESTORE_RECOVERY_VAR, tw_restore_recovery);
1211 DataManager::SetValue(TW_RESTORE_BOOT_VAR, tw_restore_boot);
1212 DataManager::SetValue(TW_RESTORE_ANDSEC_VAR, tw_restore_andsec);
1213 DataManager::SetValue(TW_RESTORE_SDEXT_VAR, tw_restore_sdext);
1214 DataManager::SetValue(TW_RESTORE_SP1_VAR, tw_restore_sp1);
1215 DataManager::SetValue(TW_RESTORE_SP2_VAR, tw_restore_sp2);
1216 DataManager::SetValue(TW_RESTORE_SP3_VAR, tw_restore_sp3);
1217
Dees_Troy51a0e822012-09-05 15:24:24 -04001218 return;
1219}
1220
1221int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001222 std::vector<TWPartition*>::iterator iter;
1223 int ret = false;
1224 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001225 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001226
1227 // Iterate through all partitions
1228 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001229 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001230 if (Path == "/and-sec")
1231 ret = (*iter)->Wipe_AndSec();
1232 else
1233 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001234 found = true;
1235 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1236 (*iter)->Wipe();
1237 }
1238 }
1239 if (found) {
1240 return ret;
1241 } else
1242 LOGE("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1243 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001244}
1245
1246int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001247 TWPartition* Part = Find_Partition_By_Block(Block);
1248
1249 if (Part) {
1250 if (Part->Has_SubPartition) {
1251 std::vector<TWPartition*>::iterator subpart;
1252
1253 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1254 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1255 (*subpart)->Wipe();
1256 }
1257 return Part->Wipe();
1258 } else
1259 return Part->Wipe();
1260 }
1261 LOGE("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
1262 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001263}
1264
1265int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001266 TWPartition* Part = Find_Partition_By_Name(Name);
1267
1268 if (Part) {
1269 if (Part->Has_SubPartition) {
1270 std::vector<TWPartition*>::iterator subpart;
1271
1272 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1273 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1274 (*subpart)->Wipe();
1275 }
1276 return Part->Wipe();
1277 } else
1278 return Part->Wipe();
1279 }
1280 LOGE("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
1281 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001282}
1283
1284int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001285 std::vector<TWPartition*>::iterator iter;
1286 int ret = true;
1287
1288 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001289 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001290 if (!(*iter)->Wipe())
1291 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001292 } else if ((*iter)->Has_Android_Secure) {
1293 if (!(*iter)->Wipe_AndSec())
1294 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001295 }
1296 }
1297 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001298}
1299
Dees_Troy38bd7602012-09-14 13:33:53 -04001300int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1301 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001302 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001303
1304 if (!Mount_By_Path("/data", true))
1305 return false;
1306
1307 if (!Mount_By_Path("/cache", true))
1308 return false;
1309
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001310 dir.push_back("/data/dalvik-cache");
1311 dir.push_back("/cache/dalvik-cache");
1312 dir.push_back("/cache/dc");
Dees_Troy38bd7602012-09-14 13:33:53 -04001313 ui_print("\nWiping Dalvik Cache Directories...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001314 for (int i = 0; i < dir.size(); ++i) {
1315 if (stat(dir.at(i).c_str(), &st) == 0) {
1316 TWFunc::removeDir(dir.at(i), false);
1317 ui_print("Cleaned: %s...\n", dir.at(i).c_str());
1318 }
1319 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001320 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
1321 if (sdext != NULL) {
1322 if (sdext->Is_Present && sdext->Mount(false)) {
1323 if (stat("/sd-ext/dalvik-cache", &st) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001324 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
1325 ui_print("Cleaned: /sd-ext/dalvik-cache...\n");
1326 }
1327 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001328 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001329 ui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
1330 return true;
1331}
1332
1333int TWPartitionManager::Wipe_Rotate_Data(void) {
1334 if (!Mount_By_Path("/data", true))
1335 return false;
1336
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001337 unlink("/data/misc/akmd*");
1338 unlink("/data/misc/rild*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001339 ui_print("Rotation data wiped.\n");
1340 return true;
1341}
1342
1343int TWPartitionManager::Wipe_Battery_Stats(void) {
1344 struct stat st;
1345
1346 if (!Mount_By_Path("/data", true))
1347 return false;
1348
1349 if (0 != stat("/data/system/batterystats.bin", &st)) {
1350 ui_print("No Battery Stats Found. No Need To Wipe.\n");
1351 } else {
1352 remove("/data/system/batterystats.bin");
1353 ui_print("Cleared battery stats.\n");
1354 }
1355 return true;
1356}
1357
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001358int TWPartitionManager::Wipe_Android_Secure(void) {
1359 std::vector<TWPartition*>::iterator iter;
1360 int ret = false;
1361 bool found = false;
1362
1363 // Iterate through all partitions
1364 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1365 if ((*iter)->Has_Android_Secure) {
1366 ret = (*iter)->Wipe_AndSec();
1367 found = true;
1368 }
1369 }
1370 if (found) {
1371 return ret;
1372 } else {
1373 LOGE("No android secure partitions found.\n");
1374 }
1375 return false;
1376}
1377
Dees_Troy38bd7602012-09-14 13:33:53 -04001378int TWPartitionManager::Format_Data(void) {
1379 TWPartition* dat = Find_Partition_By_Path("/data");
1380
1381 if (dat != NULL) {
1382 if (!dat->UnMount(true))
1383 return false;
1384
1385 return dat->Wipe_Encryption();
1386 } else {
1387 LOGE("Unable to locate /data.\n");
1388 return false;
1389 }
1390 return false;
1391}
1392
1393int TWPartitionManager::Wipe_Media_From_Data(void) {
1394 TWPartition* dat = Find_Partition_By_Path("/data");
1395
1396 if (dat != NULL) {
1397 if (!dat->Has_Data_Media) {
1398 LOGE("This device does not have /data/media\n");
1399 return false;
1400 }
1401 if (!dat->Mount(true))
1402 return false;
1403
1404 ui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001405 TWFunc::removeDir("/data/media", false);
1406 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
1407 return -1;
Dees_Troy38bd7602012-09-14 13:33:53 -04001408 if (dat->Has_Data_Media) {
1409 dat->Recreate_Media_Folder();
1410 }
1411 return true;
1412 } else {
1413 LOGE("Unable to locate /data.\n");
1414 return false;
1415 }
1416 return false;
1417}
1418
Dees_Troy51a0e822012-09-05 15:24:24 -04001419void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001420 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001421 return;
1422}
1423
1424void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001425 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001426 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001427
Dees_Troy32c8eb82012-09-11 15:28:06 -04001428 ui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001429 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001430 if ((*iter)->Can_Be_Mounted) {
1431 (*iter)->Update_Size(true);
1432 if ((*iter)->Mount_Point == "/system") {
1433 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1434 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1435 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1436 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1437 } else if ((*iter)->Mount_Point == "/cache") {
1438 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1439 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1440 } else if ((*iter)->Mount_Point == "/sd-ext") {
1441 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1442 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1443 if ((*iter)->Backup_Size == 0) {
1444 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1445 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1446 } else
1447 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001448 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001449 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001450 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001451 if ((*iter)->Backup_Size == 0) {
1452 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1453 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1454 } else
1455 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001456 } else if ((*iter)->Mount_Point == "/boot") {
1457 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1458 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1459 if ((*iter)->Backup_Size == 0) {
1460 DataManager::SetValue("tw_has_boot_partition", 0);
1461 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1462 } else
1463 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001464 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001465#ifdef SP1_NAME
1466 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1467 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1468 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1469 }
1470#endif
1471#ifdef SP2_NAME
1472 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1473 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1474 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1475 }
1476#endif
1477#ifdef SP3_NAME
1478 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1479 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1480 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1481 }
1482#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001483 } else {
1484 // Handle unmountable partitions in case we reset defaults
1485 if ((*iter)->Mount_Point == "/boot") {
1486 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1487 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1488 if ((*iter)->Backup_Size == 0) {
1489 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1490 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1491 } else
1492 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1493 } else if ((*iter)->Mount_Point == "/recovery") {
1494 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1495 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1496 if ((*iter)->Backup_Size == 0) {
1497 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1498 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1499 } else
1500 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001501 } else if ((*iter)->Mount_Point == "/data") {
1502 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001503 }
1504#ifdef SP1_NAME
1505 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1506 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1507 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1508 }
1509#endif
1510#ifdef SP2_NAME
1511 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1512 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1513 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1514 }
1515#endif
1516#ifdef SP3_NAME
1517 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1518 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1519 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1520 }
1521#endif
Dees_Troy51127312012-09-08 13:08:49 -04001522 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001523 }
Dees_Troy51127312012-09-08 13:08:49 -04001524 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1525 string current_storage_path = DataManager::GetCurrentStoragePath();
1526 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001527 if (FreeStorage != NULL) {
1528 // Attempt to mount storage
1529 if (!FreeStorage->Mount(false)) {
1530 // We couldn't mount storage... check to see if we have dual storage
1531 int has_dual_storage;
1532 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1533 if (has_dual_storage == 1) {
1534 // We have dual storage, see if we're using the internal storage that should always be present
1535 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001536 if (!FreeStorage->Is_Encrypted) {
1537 // Not able to use internal, so error!
1538 LOGE("Unable to mount internal storage.\n");
1539 }
Dees_Troy8170a922012-09-18 15:40:25 -04001540 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1541 } else {
1542 // We were using external, flip to internal
1543 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1544 current_storage_path = DataManager::GetCurrentStoragePath();
1545 FreeStorage = Find_Partition_By_Path(current_storage_path);
1546 if (FreeStorage != NULL) {
1547 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1548 } else {
1549 LOGE("Unable to locate internal storage partition.\n");
1550 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1551 }
1552 }
1553 } else {
1554 // No dual storage and unable to mount storage, error!
1555 LOGE("Unable to mount storage.\n");
1556 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1557 }
1558 } else {
1559 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1560 }
1561 } else {
Dees_Troy51127312012-09-08 13:08:49 -04001562 LOGI("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001563 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001564 if (!Write_Fstab())
1565 LOGE("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001566 return;
1567}
1568
1569int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001570#ifdef TW_INCLUDE_CRYPTO
1571 int ret_val, password_len;
1572 char crypto_blkdev[255], cPassword[255];
1573 size_t result;
1574
1575 property_set("ro.crypto.state", "encrypted");
1576#ifdef TW_INCLUDE_JB_CRYPTO
1577 // No extra flags needed
1578#else
1579 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1580 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1581 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1582 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1583 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1584 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001585
1586#ifdef CRYPTO_SD_FS_TYPE
1587 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1588 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1589 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001590#endif
a39552696ff55ce2013-01-08 16:14:56 +00001591
1592 property_set("rw.km_fips_status", "ready");
1593
1594#endif
1595
1596 // some samsung devices store "footer" on efs partition
1597 TWPartition *efs = Find_Partition_By_Path("/efs");
1598 if(efs && !efs->Is_Mounted())
1599 efs->Mount(false);
1600 else
1601 efs = 0;
1602#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001603#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001604 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001605 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001606 property_set("ro.crypto.external_encrypted", "1");
1607 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1608 } else {
1609 property_set("ro.crypto.external_encrypted", "0");
1610 }
1611#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001612#endif
a39552696ff55ce2013-01-08 16:14:56 +00001613
Dees_Troy5bf43922012-09-07 16:07:55 -04001614 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001615 int pwret = cryptfs_check_passwd(cPassword);
1616
1617 if (pwret != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001618 LOGE("Failed to decrypt data.\n");
1619 return -1;
1620 }
a39552696ff55ce2013-01-08 16:14:56 +00001621
1622 if(efs)
1623 efs->UnMount(false);
1624
Dees_Troy5bf43922012-09-07 16:07:55 -04001625 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1626 if (strcmp(crypto_blkdev, "error") == 0) {
1627 LOGE("Error retrieving decrypted data block device.\n");
1628 } else {
1629 TWPartition* dat = Find_Partition_By_Path("/data");
1630 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001631 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001632 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1633 dat->Is_Decrypted = true;
1634 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001635 dat->Setup_File_System(false);
Dees_Troy32c8eb82012-09-11 15:28:06 -04001636 ui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001637
1638#ifdef CRYPTO_SD_FS_TYPE
1639 char crypto_blkdev_sd[255];
1640 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1641 if (strcmp(crypto_blkdev_sd, "error") == 0) {
1642 LOGE("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001643 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001644 emmc->Is_Decrypted = true;
1645 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1646 emmc->Setup_File_System(false);
1647 ui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
1648 }
a39552696ff55ce2013-01-08 16:14:56 +00001649#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001650#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001651#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001652 char is_external_decrypted[255];
1653 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1654 if (strcmp(is_external_decrypted, "1") == 0) {
1655 sdcard->Is_Decrypted = true;
1656 sdcard->EcryptFS_Password = Password;
1657 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001658 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1659 MetaEcfsFile += "/.MetaEcfsFile";
1660 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1661 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1662 sdcard->UnMount(false);
1663 sdcard->Mount(false);
1664 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001665 } else {
1666 sdcard->Is_Decrypted = false;
1667 sdcard->Decrypted_Block_Device = "";
1668 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001669#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001670#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001671
Dees_Troy5bf43922012-09-07 16:07:55 -04001672 // Sleep for a bit so that the device will be ready
1673 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001674#ifdef RECOVERY_SDCARD_ON_DATA
1675 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1676 dat->Storage_Path = "/data/media/0";
1677 dat->Symlink_Path = dat->Storage_Path;
1678 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
1679 dat->UnMount(false);
1680 DataManager::SetBackupFolder();
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001681 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001682 }
1683#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001684 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001685 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001686 } else
1687 LOGE("Unable to locate data partition.\n");
1688 }
1689 return 0;
1690#else
1691 LOGE("No crypto support was compiled into this build.\n");
1692 return -1;
1693#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001694 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001695}
1696
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001697int TWPartitionManager::Fix_Permissions(void) {
1698 int result = 0;
1699 if (!Mount_By_Path("/data", true))
1700 return false;
1701
1702 if (!Mount_By_Path("/system", true))
1703 return false;
1704
1705 Mount_By_Path("/sd-ext", false);
1706
1707 fixPermissions perms;
1708 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001709 UnMount_Main_Partitions();
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001710 ui_print("Done.\n\n");
1711 return result;
1712}
1713
Dees_Troy8170a922012-09-18 15:40:25 -04001714//partial kangbang from system/vold
Dees_Troyd21618c2012-10-14 18:48:49 -04001715int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
1716 int fd;
1717 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1718
1719 if (Part == NULL) {
1720 LOGE("Unable to locate volume information for USB storage mode.");
1721 return false;
1722 }
1723 if (!Part->UnMount(true))
1724 return false;
1725
1726 if ((fd = open(Lun_File.c_str(), O_WRONLY)) < 0) {
1727 LOGE("Unable to open ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
1728 return false;
1729 }
1730
1731 if (write(fd, Part->Actual_Block_Device.c_str(), Part->Actual_Block_Device.size()) < 0) {
1732 LOGE("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
1733 close(fd);
1734 return false;
1735 }
1736 close(fd);
1737 return true;
1738}
1739
Dees_Troy8170a922012-09-18 15:40:25 -04001740int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001741 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001742 char lun_file[255];
Dees_Troy8170a922012-09-18 15:40:25 -04001743 string ext_path;
Dees_Troyd21618c2012-10-14 18:48:49 -04001744 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001745
1746 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1747 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1748 if (has_dual == 1 && has_data_media == 0) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001749 string Lun_File_str = CUSTOM_LUN_FILE;
1750 size_t found = Lun_File_str.find("%");
1751 if (found != string::npos) {
1752 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1753 if (TWFunc::Path_Exists(lun_file))
1754 has_multiple_lun = true;
1755 }
1756 if (!has_multiple_lun) {
Dees_Troyf4ca6122012-10-13 22:17:20 -04001757 // Device doesn't have multiple lun files, mount current storage
Dees_Troyf4ca6122012-10-13 22:17:20 -04001758 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001759 return Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001760 } else {
1761 // Device has multiple lun files
Dees_Troyf4ca6122012-10-13 22:17:20 -04001762 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001763 if (!Open_Lun_File(DataManager::GetSettingsStoragePath(), lun_file))
Dees_Troyf4ca6122012-10-13 22:17:20 -04001764 return false;
Dees_Troyf4ca6122012-10-13 22:17:20 -04001765 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001766 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Dees_Troyd21618c2012-10-14 18:48:49 -04001767 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001768 }
Dees_Troy8170a922012-09-18 15:40:25 -04001769 } else {
1770 if (has_data_media == 0)
1771 ext_path = DataManager::GetCurrentStoragePath();
1772 else
1773 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001774 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001775 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001776 }
1777 return true;
1778}
1779
1780int TWPartitionManager::usb_storage_disable(void) {
1781 int fd, index;
1782 char lun_file[255];
1783
1784 for (index=0; index<2; index++) {
1785 sprintf(lun_file, CUSTOM_LUN_FILE, index);
1786
1787 if ((fd = open(lun_file, O_WRONLY)) < 0) {
Dees_Troye58d5262012-09-21 12:27:57 -04001788 Mount_All_Storage();
1789 Update_System_Details();
1790 if (index == 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001791 LOGE("Unable to open ums lunfile '%s': (%s)", lun_file, strerror(errno));
Dees_Troye58d5262012-09-21 12:27:57 -04001792 return false;
1793 } else
1794 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001795 }
1796
1797 char ch = 0;
1798 if (write(fd, &ch, 1) < 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001799 close(fd);
Dees_Troye58d5262012-09-21 12:27:57 -04001800 Mount_All_Storage();
1801 Update_System_Details();
1802 if (index == 0) {
1803 LOGE("Unable to write to ums lunfile '%s': (%s)", lun_file, strerror(errno));
1804 return false;
1805 } else
1806 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001807 }
1808
1809 close(fd);
1810 }
Dees_Troye58d5262012-09-21 12:27:57 -04001811 Mount_All_Storage();
1812 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001813 UnMount_Main_Partitions();
Dees_Troy8170a922012-09-18 15:40:25 -04001814 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001815}
1816
1817void TWPartitionManager::Mount_All_Storage(void) {
1818 std::vector<TWPartition*>::iterator iter;
1819
1820 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1821 if ((*iter)->Is_Storage)
1822 (*iter)->Mount(false);
1823 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001824}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001825
Dees_Troyd0384ef2012-10-12 12:15:42 -04001826void TWPartitionManager::UnMount_Main_Partitions(void) {
1827 // Unmounts system and data if data is not data/media
1828 // Also unmounts boot if boot is mountable
1829 LOGI("Unmounting main partitions...\n");
1830
1831 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1832
1833 UnMount_By_Path("/system", true);
1834#ifndef RECOVERY_SDCARD_ON_DATA
1835 UnMount_By_Path("/data", true);
1836#endif
1837 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1838 Boot_Partition->UnMount(true);
1839}
1840
Dees_Troy9350b8d2012-09-27 12:38:38 -04001841int TWPartitionManager::Partition_SDCard(void) {
1842 char mkdir_path[255], temp[255], line[512];
1843 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1844 int ext, swap, total_size = 0, fat_size;
1845 FILE* fp;
1846
1847 ui_print("Partitioning SD Card...\n");
1848#ifdef TW_EXTERNAL_STORAGE_PATH
1849 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1850#else
1851 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1852#endif
1853 if (SDCard == NULL) {
1854 LOGE("Unable to locate device to partition.\n");
1855 return false;
1856 }
1857 if (!SDCard->UnMount(true))
1858 return false;
1859 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1860 if (SDext != NULL) {
1861 if (!SDext->UnMount(true))
1862 return false;
1863 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001864 string result;
1865 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"", result);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001866 Device = SDCard->Actual_Block_Device;
1867 // Just use the root block device
1868 Device.resize(strlen("/dev/block/mmcblkX"));
1869
1870 // Find the size of the block device:
1871 fp = fopen("/proc/partitions", "rt");
1872 if (fp == NULL) {
1873 LOGE("Unable to open /proc/partitions\n");
1874 return false;
1875 }
1876
1877 while (fgets(line, sizeof(line), fp) != NULL)
1878 {
1879 unsigned long major, minor, blocks;
1880 char device[512];
1881 char tmpString[64];
1882
1883 if (strlen(line) < 7 || line[0] == 'm') continue;
1884 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1885
1886 tmpdevice = "/dev/block/";
1887 tmpdevice += device;
1888 if (tmpdevice == Device) {
1889 // Adjust block size to byte size
1890 total_size = (int)(blocks * 1024ULL / 1000000LLU);
1891 break;
1892 }
1893 }
1894 fclose(fp);
1895
1896 DataManager::GetValue("tw_sdext_size", ext);
1897 DataManager::GetValue("tw_swap_size", swap);
1898 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1899 fat_size = total_size - ext - swap;
1900 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);
1901 memset(temp, 0, sizeof(temp));
1902 sprintf(temp, "%i", fat_size);
1903 fat_str = temp;
1904 memset(temp, 0, sizeof(temp));
1905 sprintf(temp, "%i", fat_size + ext);
1906 ext_str = temp;
1907 memset(temp, 0, sizeof(temp));
1908 sprintf(temp, "%i", fat_size + ext + swap);
1909 swap_str = temp;
1910 if (ext + swap > total_size) {
1911 LOGE("EXT + Swap size is larger than sdcard size.\n");
1912 return false;
1913 }
1914 ui_print("Removing partition table...\n");
1915 Command = "parted -s " + Device + " mklabel msdos";
1916 LOGI("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001917 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001918 LOGE("Unable to remove partition table.\n");
1919 Update_System_Details();
1920 return false;
1921 }
1922 ui_print("Creating FAT32 partition...\n");
1923 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
1924 LOGI("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001925 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001926 LOGE("Unable to create FAT32 partition.\n");
1927 return false;
1928 }
1929 if (ext > 0) {
1930 ui_print("Creating EXT partition...\n");
1931 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
1932 LOGI("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001933 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001934 LOGE("Unable to create EXT partition.\n");
1935 Update_System_Details();
1936 return false;
1937 }
1938 }
1939 if (swap > 0) {
1940 ui_print("Creating swap partition...\n");
1941 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
1942 LOGI("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001943 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001944 LOGE("Unable to create swap partition.\n");
1945 Update_System_Details();
1946 return false;
1947 }
1948 }
1949 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1950#ifdef TW_EXTERNAL_STORAGE_PATH
1951 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1952 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1953 memset(mkdir_path, 0, sizeof(mkdir_path));
1954 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1955#else
1956 Mount_By_Path("/sdcard", 1);
1957 strcpy(mkdir_path, "/sdcard/TWRP");
1958#endif
1959 mkdir(mkdir_path, 0777);
1960 DataManager::Flush();
1961#ifdef TW_EXTERNAL_STORAGE_PATH
1962 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1963 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1964 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1965#else
1966 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1967 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1968 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1969#endif
1970 if (ext > 0) {
1971 if (SDext == NULL) {
1972 LOGE("Unable to locate sd-ext partition.\n");
1973 return false;
1974 }
1975 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
1976 ui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1977 LOGI("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001978 TWFunc::Exec_Cmd(Command, result);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001979 }
1980
1981 Update_System_Details();
1982 ui_print("Partitioning complete.\n");
1983 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001984}