blob: 895d3617f78073d2b5071e69aa8f06b5c6b687bd [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 }
ChampionSwimmercf33e4d2013-02-03 13:59:22 +0530707#ifndef TW_HAS_NO_BOOT_PARTITION
Dees_Troy43d8b002012-09-17 16:00:01 -0400708 DataManager::GetValue(TW_BACKUP_BOOT_VAR, check);
709 if (check) {
710 backup_boot = Find_Partition_By_Path("/boot");
711 if (backup_boot != NULL) {
712 partition_count++;
713 if (backup_boot->Backup_Method == 1)
714 file_bytes += backup_boot->Backup_Size;
715 else
716 img_bytes += backup_boot->Backup_Size;
717 } else {
718 LOGE("Unable to locate boot partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000719 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400720 }
721 }
ChampionSwimmercf33e4d2013-02-03 13:59:22 +0530722#endif
Dees_Troy43d8b002012-09-17 16:00:01 -0400723 DataManager::GetValue(TW_BACKUP_ANDSEC_VAR, check);
724 if (check) {
725 backup_andsec = Find_Partition_By_Path("/and-sec");
726 if (backup_andsec != NULL) {
727 partition_count++;
728 if (backup_andsec->Backup_Method == 1)
729 file_bytes += backup_andsec->Backup_Size;
730 else
731 img_bytes += backup_andsec->Backup_Size;
732 } else {
733 LOGE("Unable to locate android secure partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000734 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400735 }
736 }
737 DataManager::GetValue(TW_BACKUP_SDEXT_VAR, check);
738 if (check) {
739 backup_sdext = Find_Partition_By_Path("/sd-ext");
740 if (backup_sdext != NULL) {
741 partition_count++;
742 if (backup_sdext->Backup_Method == 1)
743 file_bytes += backup_sdext->Backup_Size;
744 else
745 img_bytes += backup_sdext->Backup_Size;
746 } else {
747 LOGE("Unable to locate sd-ext partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000748 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400749 }
750 }
751#ifdef SP1_NAME
752 DataManager::GetValue(TW_BACKUP_SP1_VAR, check);
753 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400754 backup_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400755 if (backup_sp1 != NULL) {
756 partition_count++;
757 if (backup_sp1->Backup_Method == 1)
758 file_bytes += backup_sp1->Backup_Size;
759 else
760 img_bytes += backup_sp1->Backup_Size;
761 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400762 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000763 DataManager::SetValue(TW_BACKUP_SP1_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400764 }
765 }
766#endif
767#ifdef SP2_NAME
768 DataManager::GetValue(TW_BACKUP_SP2_VAR, check);
769 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400770 backup_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400771 if (backup_sp2 != NULL) {
772 partition_count++;
773 if (backup_sp2->Backup_Method == 1)
774 file_bytes += backup_sp2->Backup_Size;
775 else
776 img_bytes += backup_sp2->Backup_Size;
777 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400778 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000779 DataManager::SetValue(TW_BACKUP_SP2_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400780 }
781 }
782#endif
783#ifdef SP3_NAME
784 DataManager::GetValue(TW_BACKUP_SP3_VAR, check);
785 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400786 backup_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400787 if (backup_sp3 != NULL) {
788 partition_count++;
789 if (backup_sp3->Backup_Method == 1)
790 file_bytes += backup_sp3->Backup_Size;
791 else
792 img_bytes += backup_sp3->Backup_Size;
793 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400794 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000795 DataManager::SetValue(TW_BACKUP_SP3_VAR, 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400796 }
797 }
798#endif
799
800 if (partition_count == 0) {
801 ui_print("No partitions selected for backup.\n");
802 return false;
803 }
804 total_bytes = file_bytes + img_bytes;
805 ui_print(" * Total number of partitions to back up: %d\n", partition_count);
806 ui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
807 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
808 if (storage != NULL) {
809 free_space = storage->Free;
810 ui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
811 } else {
812 LOGE("Unable to locate storage device.\n");
813 return false;
814 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000815 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400816 // We require an extra 32MB just in case
817 LOGE("Not enough free space on storage.\n");
818 return false;
819 }
820 img_bytes_remaining = img_bytes;
821 file_bytes_remaining = file_bytes;
822
Dees_Troyd4b22b02013-01-18 17:17:58 +0000823 ui_print("\n[BACKUP STARTED]\n");
824 ui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
825 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
826 LOGE("Failed to make backup folder.\n");
827 return false;
828 }
829
Dees_Troy093b7642012-09-21 15:59:38 -0400830 ui->SetProgress(0.0);
831
832 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 -0400833 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400834 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 -0400835 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400836 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 -0400837 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400838 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 -0400839 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400840 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 -0400841 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400842 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 -0400843 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400844 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 -0400845 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400846 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 -0400847 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400848 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 -0400849 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400850 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 -0400851 return false;
852
853 // Average BPS
854 if (img_time == 0)
855 img_time = 1;
856 if (file_time == 0)
857 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400858 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500859 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400860
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500861 ui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400862 ui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
863
864 time(&total_stop);
865 int total_time = (int) difftime(total_stop, total_start);
866 unsigned long long actual_backup_size = TWFunc::Get_Folder_Size(Full_Backup_Path, true);
867 actual_backup_size /= (1024LLU * 1024LLU);
868
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500869 int prev_img_bps, use_compression;
870 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400871 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
872 img_bps += (prev_img_bps * 4);
873 img_bps /= 5;
874
875 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
876 if (use_compression)
877 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
878 else
879 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
880 file_bps += (prev_file_bps * 4);
881 file_bps /= 5;
882
883 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
884 if (use_compression)
885 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
886 else
887 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
888
Dees_Troy43d8b002012-09-17 16:00:01 -0400889 ui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
890 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400891 UnMount_Main_Partitions();
Dees_Troy43d8b002012-09-17 16:00:01 -0400892 ui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000893 string backup_log = Full_Backup_Path + "recovery.log";
894 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
895 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400896}
897
Dees_Troy093b7642012-09-21 15:59:38 -0400898bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400899 time_t Start, Stop;
900 time(&Start);
Dees_Troy093b7642012-09-21 15:59:38 -0400901 ui->ShowProgress(1.0 / (float)partition_count, 150);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400902 if (!Part->Restore(Restore_Name))
903 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400904 if (Part->Has_SubPartition) {
905 std::vector<TWPartition*>::iterator subpart;
906
907 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
908 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
909 if (!(*subpart)->Restore(Restore_Name))
910 return false;
911 }
912 }
913 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400914 time(&Stop);
915 ui_print("[%s done (%d seconds)]\n\n", Part->Display_Name.c_str(), (int)difftime(Stop, Start));
916 return true;
917}
918
Dees_Troy51a0e822012-09-05 15:24:24 -0400919int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400920 int check_md5, check, partition_count = 0;
921 TWPartition* restore_sys = NULL;
922 TWPartition* restore_data = NULL;
923 TWPartition* restore_cache = NULL;
924 TWPartition* restore_boot = NULL;
925 TWPartition* restore_andsec = NULL;
926 TWPartition* restore_sdext = NULL;
927 TWPartition* restore_sp1 = NULL;
928 TWPartition* restore_sp2 = NULL;
929 TWPartition* restore_sp3 = NULL;
930 time_t rStart, rStop;
931 time(&rStart);
Dees_Troy43d8b002012-09-17 16:00:01 -0400932
Dees_Troy4a2a1262012-09-18 09:33:47 -0400933 ui_print("\n[RESTORE STARTED]\n\n");
934 ui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400935
Dees_Troy4a2a1262012-09-18 09:33:47 -0400936 if (!Mount_Current_Storage(true))
937 return false;
938
939 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
940 DataManager::GetValue(TW_RESTORE_SYSTEM_VAR, check);
Dees_Troy63c8df72012-09-10 14:02:05 -0400941 if (check > 0) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400942 restore_sys = Find_Partition_By_Path("/system");
943 if (restore_sys == NULL) {
944 LOGE("Unable to locate system partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000945 } else {
946 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400947 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400948 }
949 DataManager::GetValue(TW_RESTORE_DATA_VAR, check);
950 if (check > 0) {
951 restore_data = Find_Partition_By_Path("/data");
952 if (restore_data == NULL) {
953 LOGE("Unable to locate data partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000954 } else {
955 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400956 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400957 }
958 DataManager::GetValue(TW_RESTORE_CACHE_VAR, check);
959 if (check > 0) {
960 restore_cache = Find_Partition_By_Path("/cache");
961 if (restore_cache == NULL) {
962 LOGE("Unable to locate cache partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000963 } else {
964 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400965 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400966 }
ChampionSwimmercf33e4d2013-02-03 13:59:22 +0530967#ifndef TW_HAS_NO_BOOT_PARTITION
Dees_Troy4a2a1262012-09-18 09:33:47 -0400968 DataManager::GetValue(TW_RESTORE_BOOT_VAR, check);
969 if (check > 0) {
970 restore_boot = Find_Partition_By_Path("/boot");
971 if (restore_boot == NULL) {
972 LOGE("Unable to locate boot partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000973 } else {
974 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400975 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400976 }
ChampionSwimmercf33e4d2013-02-03 13:59:22 +0530977#endif
Dees_Troy4a2a1262012-09-18 09:33:47 -0400978 DataManager::GetValue(TW_RESTORE_ANDSEC_VAR, check);
979 if (check > 0) {
980 restore_andsec = Find_Partition_By_Path("/and-sec");
981 if (restore_andsec == NULL) {
982 LOGE("Unable to locate android secure partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000983 } else {
984 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400985 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400986 }
987 DataManager::GetValue(TW_RESTORE_SDEXT_VAR, check);
988 if (check > 0) {
989 restore_sdext = Find_Partition_By_Path("/sd-ext");
990 if (restore_sdext == NULL) {
991 LOGE("Unable to locate sd-ext partition.\n");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000992 } else {
993 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400994 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400995 }
996#ifdef SP1_NAME
997 DataManager::GetValue(TW_RESTORE_SP1_VAR, check);
998 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400999 restore_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001000 if (restore_sp1 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001001 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001002 } else {
1003 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001004 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001005 }
1006#endif
1007#ifdef SP2_NAME
1008 DataManager::GetValue(TW_RESTORE_SP2_VAR, check);
1009 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001010 restore_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001011 if (restore_sp2 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001012 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001013 } else {
1014 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001015 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001016 }
1017#endif
1018#ifdef SP3_NAME
1019 DataManager::GetValue(TW_RESTORE_SP3_VAR, check);
1020 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001021 restore_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001022 if (restore_sp3 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001023 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_NAME));
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001024 } else {
1025 partition_count++;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001026 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001027 }
1028#endif
1029
1030 if (partition_count == 0) {
1031 LOGE("No partitions selected for restore.\n");
1032 return false;
1033 }
1034
1035 if (check_md5 > 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001036 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
Dees_Troyb46a6842012-09-25 11:06:46 -04001037 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001038 ui_print("Verifying MD5...\n");
1039 if (restore_sys != NULL && !restore_sys->Check_MD5(Restore_Name))
1040 return false;
1041 if (restore_data != NULL && !restore_data->Check_MD5(Restore_Name))
1042 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001043 if (restore_data != NULL && restore_data->Has_SubPartition) {
1044 std::vector<TWPartition*>::iterator subpart;
1045
1046 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1047 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_data->Mount_Point) {
1048 if (!(*subpart)->Check_MD5(Restore_Name))
1049 return false;
1050 }
1051 }
1052 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001053 if (restore_cache != NULL && !restore_cache->Check_MD5(Restore_Name))
1054 return false;
1055 if (restore_boot != NULL && !restore_boot->Check_MD5(Restore_Name))
1056 return false;
1057 if (restore_andsec != NULL && !restore_andsec->Check_MD5(Restore_Name))
1058 return false;
1059 if (restore_sdext != NULL && !restore_sdext->Check_MD5(Restore_Name))
1060 return false;
1061 if (restore_sp1 != NULL && !restore_sp1->Check_MD5(Restore_Name))
1062 return false;
1063 if (restore_sp2 != NULL && !restore_sp2->Check_MD5(Restore_Name))
1064 return false;
1065 if (restore_sp3 != NULL && !restore_sp3->Check_MD5(Restore_Name))
1066 return false;
1067 ui_print("Done verifying MD5.\n");
1068 } else
1069 ui_print("Skipping MD5 check based on user setting.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001070
Dees_Troy4a2a1262012-09-18 09:33:47 -04001071 ui_print("Restoring %i partitions...\n", partition_count);
Dees_Troy093b7642012-09-21 15:59:38 -04001072 ui->SetProgress(0.0);
1073 if (restore_sys != NULL && !Restore_Partition(restore_sys, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001074 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001075 if (restore_data != NULL && !Restore_Partition(restore_data, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001076 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001077 if (restore_cache != NULL && !Restore_Partition(restore_cache, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001078 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001079 if (restore_boot != NULL && !Restore_Partition(restore_boot, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001080 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001081 if (restore_andsec != NULL && !Restore_Partition(restore_andsec, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001082 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001083 if (restore_sdext != NULL && !Restore_Partition(restore_sdext, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001084 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001085 if (restore_sp1 != NULL && !Restore_Partition(restore_sp1, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001086 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001087 if (restore_sp2 != NULL && !Restore_Partition(restore_sp2, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001088 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001089 if (restore_sp3 != NULL && !Restore_Partition(restore_sp3, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001090 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001091
Dees_Troyb46a6842012-09-25 11:06:46 -04001092 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -04001093 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001094 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -04001095 time(&rStop);
1096 ui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -04001097 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001098}
1099
1100void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001101 // Start with the default values
1102 int tw_restore_system = -1;
1103 int tw_restore_data = -1;
1104 int tw_restore_cache = -1;
1105 int tw_restore_recovery = -1;
1106 int tw_restore_boot = -1;
1107 int tw_restore_andsec = -1;
1108 int tw_restore_sdext = -1;
1109 int tw_restore_sp1 = -1;
1110 int tw_restore_sp2 = -1;
1111 int tw_restore_sp3 = -1;
1112 bool get_date = true;
1113
1114 DIR* d;
1115 d = opendir(Restore_Name.c_str());
1116 if (d == NULL)
1117 {
1118 LOGE("Error opening %s\n", Restore_Name.c_str());
1119 return;
1120 }
1121
1122 struct dirent* de;
1123 while ((de = readdir(d)) != NULL)
1124 {
1125 // Strip off three components
1126 char str[256];
1127 char* label;
1128 char* fstype = NULL;
1129 char* extn = NULL;
1130 char* ptr;
1131
1132 strcpy(str, de->d_name);
1133 if (strlen(str) <= 2)
1134 continue;
1135
1136 if (get_date) {
1137 char file_path[255];
1138 struct stat st;
1139
1140 strcpy(file_path, Restore_Name.c_str());
1141 strcat(file_path, "/");
1142 strcat(file_path, str);
1143 stat(file_path, &st);
1144 string backup_date = ctime((const time_t*)(&st.st_mtime));
1145 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
1146 get_date = false;
1147 }
1148
1149 label = str;
1150 ptr = label;
1151 while (*ptr && *ptr != '.') ptr++;
1152 if (*ptr == '.')
1153 {
1154 *ptr = 0x00;
1155 ptr++;
1156 fstype = ptr;
1157 }
1158 while (*ptr && *ptr != '.') ptr++;
1159 if (*ptr == '.')
1160 {
1161 *ptr = 0x00;
1162 ptr++;
1163 extn = ptr;
1164 }
1165
1166 if (extn == NULL || (strlen(extn) >= 3 && strncmp(extn, "win", 3) != 0)) continue;
1167
1168 TWPartition* Part = Find_Partition_By_Path(label);
1169 if (Part == NULL)
1170 {
1171 LOGE(" Unable to locate partition by backup name: '%s'\n", label);
1172 continue;
1173 }
1174
1175 Part->Backup_FileName = de->d_name;
1176 if (strlen(extn) > 3) {
1177 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1178 }
1179
1180 // Now, we just need to find the correct label
Dees_Troye58d5262012-09-21 12:27:57 -04001181 if (Part->Backup_Path == "/system")
Dees_Troy63c8df72012-09-10 14:02:05 -04001182 tw_restore_system = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001183 if (Part->Backup_Path == "/data")
Dees_Troy63c8df72012-09-10 14:02:05 -04001184 tw_restore_data = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001185 if (Part->Backup_Path == "/cache")
Dees_Troy63c8df72012-09-10 14:02:05 -04001186 tw_restore_cache = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001187 if (Part->Backup_Path == "/recovery")
Dees_Troy63c8df72012-09-10 14:02:05 -04001188 tw_restore_recovery = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001189 if (Part->Backup_Path == "/boot")
Dees_Troy63c8df72012-09-10 14:02:05 -04001190 tw_restore_boot = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001191 if (Part->Backup_Path == "/and-sec")
Dees_Troy63c8df72012-09-10 14:02:05 -04001192 tw_restore_andsec = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001193 if (Part->Backup_Path == "/sd-ext")
Dees_Troy63c8df72012-09-10 14:02:05 -04001194 tw_restore_sdext = 1;
1195#ifdef SP1_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001196 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP1_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001197 tw_restore_sp1 = 1;
1198#endif
1199#ifdef SP2_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001200 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP2_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001201 tw_restore_sp2 = 1;
1202#endif
1203#ifdef SP3_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001204 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP3_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001205 tw_restore_sp3 = 1;
1206#endif
1207 }
1208 closedir(d);
1209
1210 // Set the final values
1211 DataManager::SetValue(TW_RESTORE_SYSTEM_VAR, tw_restore_system);
1212 DataManager::SetValue(TW_RESTORE_DATA_VAR, tw_restore_data);
1213 DataManager::SetValue(TW_RESTORE_CACHE_VAR, tw_restore_cache);
1214 DataManager::SetValue(TW_RESTORE_RECOVERY_VAR, tw_restore_recovery);
1215 DataManager::SetValue(TW_RESTORE_BOOT_VAR, tw_restore_boot);
1216 DataManager::SetValue(TW_RESTORE_ANDSEC_VAR, tw_restore_andsec);
1217 DataManager::SetValue(TW_RESTORE_SDEXT_VAR, tw_restore_sdext);
1218 DataManager::SetValue(TW_RESTORE_SP1_VAR, tw_restore_sp1);
1219 DataManager::SetValue(TW_RESTORE_SP2_VAR, tw_restore_sp2);
1220 DataManager::SetValue(TW_RESTORE_SP3_VAR, tw_restore_sp3);
1221
Dees_Troy51a0e822012-09-05 15:24:24 -04001222 return;
1223}
1224
1225int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001226 std::vector<TWPartition*>::iterator iter;
1227 int ret = false;
1228 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001229 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001230
1231 // Iterate through all partitions
1232 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001233 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001234 if (Path == "/and-sec")
1235 ret = (*iter)->Wipe_AndSec();
1236 else
1237 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001238 found = true;
1239 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1240 (*iter)->Wipe();
1241 }
1242 }
1243 if (found) {
1244 return ret;
1245 } else
1246 LOGE("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1247 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001248}
1249
1250int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001251 TWPartition* Part = Find_Partition_By_Block(Block);
1252
1253 if (Part) {
1254 if (Part->Has_SubPartition) {
1255 std::vector<TWPartition*>::iterator subpart;
1256
1257 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1258 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1259 (*subpart)->Wipe();
1260 }
1261 return Part->Wipe();
1262 } else
1263 return Part->Wipe();
1264 }
1265 LOGE("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
1266 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001267}
1268
1269int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001270 TWPartition* Part = Find_Partition_By_Name(Name);
1271
1272 if (Part) {
1273 if (Part->Has_SubPartition) {
1274 std::vector<TWPartition*>::iterator subpart;
1275
1276 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1277 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1278 (*subpart)->Wipe();
1279 }
1280 return Part->Wipe();
1281 } else
1282 return Part->Wipe();
1283 }
1284 LOGE("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
1285 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001286}
1287
1288int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001289 std::vector<TWPartition*>::iterator iter;
1290 int ret = true;
1291
1292 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001293 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001294 if (!(*iter)->Wipe())
1295 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001296 } else if ((*iter)->Has_Android_Secure) {
1297 if (!(*iter)->Wipe_AndSec())
1298 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001299 }
1300 }
1301 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001302}
1303
Dees_Troy38bd7602012-09-14 13:33:53 -04001304int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1305 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001306 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001307
1308 if (!Mount_By_Path("/data", true))
1309 return false;
1310
1311 if (!Mount_By_Path("/cache", true))
1312 return false;
1313
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001314 dir.push_back("/data/dalvik-cache");
1315 dir.push_back("/cache/dalvik-cache");
1316 dir.push_back("/cache/dc");
Dees_Troy38bd7602012-09-14 13:33:53 -04001317 ui_print("\nWiping Dalvik Cache Directories...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001318 for (int i = 0; i < dir.size(); ++i) {
1319 if (stat(dir.at(i).c_str(), &st) == 0) {
1320 TWFunc::removeDir(dir.at(i), false);
1321 ui_print("Cleaned: %s...\n", dir.at(i).c_str());
1322 }
1323 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001324 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
1325 if (sdext != NULL) {
1326 if (sdext->Is_Present && sdext->Mount(false)) {
1327 if (stat("/sd-ext/dalvik-cache", &st) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001328 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
1329 ui_print("Cleaned: /sd-ext/dalvik-cache...\n");
1330 }
1331 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001332 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001333 ui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
1334 return true;
1335}
1336
1337int TWPartitionManager::Wipe_Rotate_Data(void) {
1338 if (!Mount_By_Path("/data", true))
1339 return false;
1340
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001341 unlink("/data/misc/akmd*");
1342 unlink("/data/misc/rild*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001343 ui_print("Rotation data wiped.\n");
1344 return true;
1345}
1346
1347int TWPartitionManager::Wipe_Battery_Stats(void) {
1348 struct stat st;
1349
1350 if (!Mount_By_Path("/data", true))
1351 return false;
1352
1353 if (0 != stat("/data/system/batterystats.bin", &st)) {
1354 ui_print("No Battery Stats Found. No Need To Wipe.\n");
1355 } else {
1356 remove("/data/system/batterystats.bin");
1357 ui_print("Cleared battery stats.\n");
1358 }
1359 return true;
1360}
1361
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001362int TWPartitionManager::Wipe_Android_Secure(void) {
1363 std::vector<TWPartition*>::iterator iter;
1364 int ret = false;
1365 bool found = false;
1366
1367 // Iterate through all partitions
1368 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1369 if ((*iter)->Has_Android_Secure) {
1370 ret = (*iter)->Wipe_AndSec();
1371 found = true;
1372 }
1373 }
1374 if (found) {
1375 return ret;
1376 } else {
1377 LOGE("No android secure partitions found.\n");
1378 }
1379 return false;
1380}
1381
Dees_Troy38bd7602012-09-14 13:33:53 -04001382int TWPartitionManager::Format_Data(void) {
1383 TWPartition* dat = Find_Partition_By_Path("/data");
1384
1385 if (dat != NULL) {
1386 if (!dat->UnMount(true))
1387 return false;
1388
1389 return dat->Wipe_Encryption();
1390 } else {
1391 LOGE("Unable to locate /data.\n");
1392 return false;
1393 }
1394 return false;
1395}
1396
1397int TWPartitionManager::Wipe_Media_From_Data(void) {
1398 TWPartition* dat = Find_Partition_By_Path("/data");
1399
1400 if (dat != NULL) {
1401 if (!dat->Has_Data_Media) {
1402 LOGE("This device does not have /data/media\n");
1403 return false;
1404 }
1405 if (!dat->Mount(true))
1406 return false;
1407
1408 ui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001409 TWFunc::removeDir("/data/media", false);
1410 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
1411 return -1;
Dees_Troy38bd7602012-09-14 13:33:53 -04001412 if (dat->Has_Data_Media) {
1413 dat->Recreate_Media_Folder();
1414 }
1415 return true;
1416 } else {
1417 LOGE("Unable to locate /data.\n");
1418 return false;
1419 }
1420 return false;
1421}
1422
Dees_Troy51a0e822012-09-05 15:24:24 -04001423void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001424 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001425 return;
1426}
1427
1428void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001429 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001430 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001431
Dees_Troy32c8eb82012-09-11 15:28:06 -04001432 ui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001433 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001434 if ((*iter)->Can_Be_Mounted) {
1435 (*iter)->Update_Size(true);
1436 if ((*iter)->Mount_Point == "/system") {
1437 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1438 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1439 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1440 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1441 } else if ((*iter)->Mount_Point == "/cache") {
1442 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1443 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1444 } else if ((*iter)->Mount_Point == "/sd-ext") {
1445 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1446 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1447 if ((*iter)->Backup_Size == 0) {
1448 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1449 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1450 } else
1451 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001452 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001453 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001454 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001455 if ((*iter)->Backup_Size == 0) {
1456 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1457 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1458 } else
1459 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001460 } else if ((*iter)->Mount_Point == "/boot") {
1461 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1462 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1463 if ((*iter)->Backup_Size == 0) {
1464 DataManager::SetValue("tw_has_boot_partition", 0);
1465 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1466 } else
1467 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001468 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001469#ifdef SP1_NAME
1470 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1471 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1472 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1473 }
1474#endif
1475#ifdef SP2_NAME
1476 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1477 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1478 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1479 }
1480#endif
1481#ifdef SP3_NAME
1482 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1483 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1484 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1485 }
1486#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001487 } else {
1488 // Handle unmountable partitions in case we reset defaults
1489 if ((*iter)->Mount_Point == "/boot") {
1490 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1491 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1492 if ((*iter)->Backup_Size == 0) {
1493 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1494 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1495 } else
1496 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1497 } else if ((*iter)->Mount_Point == "/recovery") {
1498 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1499 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1500 if ((*iter)->Backup_Size == 0) {
1501 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1502 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1503 } else
1504 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001505 } else if ((*iter)->Mount_Point == "/data") {
1506 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001507 }
1508#ifdef SP1_NAME
1509 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1510 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1511 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1512 }
1513#endif
1514#ifdef SP2_NAME
1515 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1516 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1517 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1518 }
1519#endif
1520#ifdef SP3_NAME
1521 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1522 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1523 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1524 }
1525#endif
Dees_Troy51127312012-09-08 13:08:49 -04001526 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001527 }
Dees_Troy51127312012-09-08 13:08:49 -04001528 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1529 string current_storage_path = DataManager::GetCurrentStoragePath();
1530 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001531 if (FreeStorage != NULL) {
1532 // Attempt to mount storage
1533 if (!FreeStorage->Mount(false)) {
1534 // We couldn't mount storage... check to see if we have dual storage
1535 int has_dual_storage;
1536 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1537 if (has_dual_storage == 1) {
1538 // We have dual storage, see if we're using the internal storage that should always be present
1539 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001540 if (!FreeStorage->Is_Encrypted) {
1541 // Not able to use internal, so error!
1542 LOGE("Unable to mount internal storage.\n");
1543 }
Dees_Troy8170a922012-09-18 15:40:25 -04001544 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1545 } else {
1546 // We were using external, flip to internal
1547 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1548 current_storage_path = DataManager::GetCurrentStoragePath();
1549 FreeStorage = Find_Partition_By_Path(current_storage_path);
1550 if (FreeStorage != NULL) {
1551 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1552 } else {
1553 LOGE("Unable to locate internal storage partition.\n");
1554 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1555 }
1556 }
1557 } else {
1558 // No dual storage and unable to mount storage, error!
1559 LOGE("Unable to mount storage.\n");
1560 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1561 }
1562 } else {
1563 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1564 }
1565 } else {
Dees_Troy51127312012-09-08 13:08:49 -04001566 LOGI("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001567 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001568 if (!Write_Fstab())
1569 LOGE("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001570 return;
1571}
1572
1573int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001574#ifdef TW_INCLUDE_CRYPTO
1575 int ret_val, password_len;
1576 char crypto_blkdev[255], cPassword[255];
1577 size_t result;
1578
1579 property_set("ro.crypto.state", "encrypted");
1580#ifdef TW_INCLUDE_JB_CRYPTO
1581 // No extra flags needed
1582#else
1583 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1584 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1585 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1586 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1587 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1588 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001589
1590#ifdef CRYPTO_SD_FS_TYPE
1591 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1592 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1593 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001594#endif
a39552696ff55ce2013-01-08 16:14:56 +00001595
1596 property_set("rw.km_fips_status", "ready");
1597
1598#endif
1599
1600 // some samsung devices store "footer" on efs partition
1601 TWPartition *efs = Find_Partition_By_Path("/efs");
1602 if(efs && !efs->Is_Mounted())
1603 efs->Mount(false);
1604 else
1605 efs = 0;
1606#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001607#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001608 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001609 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001610 property_set("ro.crypto.external_encrypted", "1");
1611 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1612 } else {
1613 property_set("ro.crypto.external_encrypted", "0");
1614 }
1615#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001616#endif
a39552696ff55ce2013-01-08 16:14:56 +00001617
Dees_Troy5bf43922012-09-07 16:07:55 -04001618 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001619 int pwret = cryptfs_check_passwd(cPassword);
1620
1621 if (pwret != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001622 LOGE("Failed to decrypt data.\n");
1623 return -1;
1624 }
a39552696ff55ce2013-01-08 16:14:56 +00001625
1626 if(efs)
1627 efs->UnMount(false);
1628
Dees_Troy5bf43922012-09-07 16:07:55 -04001629 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1630 if (strcmp(crypto_blkdev, "error") == 0) {
1631 LOGE("Error retrieving decrypted data block device.\n");
1632 } else {
1633 TWPartition* dat = Find_Partition_By_Path("/data");
1634 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001635 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001636 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1637 dat->Is_Decrypted = true;
1638 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001639 dat->Setup_File_System(false);
Dees_Troy32c8eb82012-09-11 15:28:06 -04001640 ui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001641
1642#ifdef CRYPTO_SD_FS_TYPE
1643 char crypto_blkdev_sd[255];
1644 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1645 if (strcmp(crypto_blkdev_sd, "error") == 0) {
1646 LOGE("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001647 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001648 emmc->Is_Decrypted = true;
1649 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1650 emmc->Setup_File_System(false);
1651 ui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
1652 }
a39552696ff55ce2013-01-08 16:14:56 +00001653#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001654#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001655#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001656 char is_external_decrypted[255];
1657 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1658 if (strcmp(is_external_decrypted, "1") == 0) {
1659 sdcard->Is_Decrypted = true;
1660 sdcard->EcryptFS_Password = Password;
1661 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001662 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1663 MetaEcfsFile += "/.MetaEcfsFile";
1664 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1665 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1666 sdcard->UnMount(false);
1667 sdcard->Mount(false);
1668 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001669 } else {
1670 sdcard->Is_Decrypted = false;
1671 sdcard->Decrypted_Block_Device = "";
1672 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001673#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001674#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001675
Dees_Troy5bf43922012-09-07 16:07:55 -04001676 // Sleep for a bit so that the device will be ready
1677 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001678#ifdef RECOVERY_SDCARD_ON_DATA
1679 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1680 dat->Storage_Path = "/data/media/0";
1681 dat->Symlink_Path = dat->Storage_Path;
1682 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
1683 dat->UnMount(false);
1684 DataManager::SetBackupFolder();
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001685 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001686 }
1687#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001688 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001689 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001690 } else
1691 LOGE("Unable to locate data partition.\n");
1692 }
1693 return 0;
1694#else
1695 LOGE("No crypto support was compiled into this build.\n");
1696 return -1;
1697#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001698 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001699}
1700
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001701int TWPartitionManager::Fix_Permissions(void) {
1702 int result = 0;
1703 if (!Mount_By_Path("/data", true))
1704 return false;
1705
1706 if (!Mount_By_Path("/system", true))
1707 return false;
1708
1709 Mount_By_Path("/sd-ext", false);
1710
1711 fixPermissions perms;
1712 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001713 UnMount_Main_Partitions();
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001714 ui_print("Done.\n\n");
1715 return result;
1716}
1717
Dees_Troy8170a922012-09-18 15:40:25 -04001718//partial kangbang from system/vold
Dees_Troyd21618c2012-10-14 18:48:49 -04001719int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
1720 int fd;
1721 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1722
1723 if (Part == NULL) {
1724 LOGE("Unable to locate volume information for USB storage mode.");
1725 return false;
1726 }
1727 if (!Part->UnMount(true))
1728 return false;
1729
1730 if ((fd = open(Lun_File.c_str(), O_WRONLY)) < 0) {
1731 LOGE("Unable to open ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
1732 return false;
1733 }
1734
1735 if (write(fd, Part->Actual_Block_Device.c_str(), Part->Actual_Block_Device.size()) < 0) {
1736 LOGE("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
1737 close(fd);
1738 return false;
1739 }
1740 close(fd);
1741 return true;
1742}
1743
Dees_Troy8170a922012-09-18 15:40:25 -04001744int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001745 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001746 char lun_file[255];
Dees_Troy8170a922012-09-18 15:40:25 -04001747 string ext_path;
Dees_Troyd21618c2012-10-14 18:48:49 -04001748 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001749
1750 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1751 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1752 if (has_dual == 1 && has_data_media == 0) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001753 string Lun_File_str = CUSTOM_LUN_FILE;
1754 size_t found = Lun_File_str.find("%");
1755 if (found != string::npos) {
1756 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1757 if (TWFunc::Path_Exists(lun_file))
1758 has_multiple_lun = true;
1759 }
1760 if (!has_multiple_lun) {
Dees_Troyf4ca6122012-10-13 22:17:20 -04001761 // Device doesn't have multiple lun files, mount current storage
Dees_Troyf4ca6122012-10-13 22:17:20 -04001762 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001763 return Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001764 } else {
1765 // Device has multiple lun files
Dees_Troyf4ca6122012-10-13 22:17:20 -04001766 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001767 if (!Open_Lun_File(DataManager::GetSettingsStoragePath(), lun_file))
Dees_Troyf4ca6122012-10-13 22:17:20 -04001768 return false;
Dees_Troyf4ca6122012-10-13 22:17:20 -04001769 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001770 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Dees_Troyd21618c2012-10-14 18:48:49 -04001771 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001772 }
Dees_Troy8170a922012-09-18 15:40:25 -04001773 } else {
1774 if (has_data_media == 0)
1775 ext_path = DataManager::GetCurrentStoragePath();
1776 else
1777 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001778 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001779 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001780 }
1781 return true;
1782}
1783
1784int TWPartitionManager::usb_storage_disable(void) {
1785 int fd, index;
1786 char lun_file[255];
1787
1788 for (index=0; index<2; index++) {
1789 sprintf(lun_file, CUSTOM_LUN_FILE, index);
1790
1791 if ((fd = open(lun_file, O_WRONLY)) < 0) {
Dees_Troye58d5262012-09-21 12:27:57 -04001792 Mount_All_Storage();
1793 Update_System_Details();
1794 if (index == 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001795 LOGE("Unable to open ums lunfile '%s': (%s)", lun_file, strerror(errno));
Dees_Troye58d5262012-09-21 12:27:57 -04001796 return false;
1797 } else
1798 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001799 }
1800
1801 char ch = 0;
1802 if (write(fd, &ch, 1) < 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001803 close(fd);
Dees_Troye58d5262012-09-21 12:27:57 -04001804 Mount_All_Storage();
1805 Update_System_Details();
1806 if (index == 0) {
1807 LOGE("Unable to write to ums lunfile '%s': (%s)", lun_file, strerror(errno));
1808 return false;
1809 } else
1810 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001811 }
1812
1813 close(fd);
1814 }
Dees_Troye58d5262012-09-21 12:27:57 -04001815 Mount_All_Storage();
1816 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001817 UnMount_Main_Partitions();
Dees_Troy8170a922012-09-18 15:40:25 -04001818 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001819}
1820
1821void TWPartitionManager::Mount_All_Storage(void) {
1822 std::vector<TWPartition*>::iterator iter;
1823
1824 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1825 if ((*iter)->Is_Storage)
1826 (*iter)->Mount(false);
1827 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001828}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001829
Dees_Troyd0384ef2012-10-12 12:15:42 -04001830void TWPartitionManager::UnMount_Main_Partitions(void) {
1831 // Unmounts system and data if data is not data/media
1832 // Also unmounts boot if boot is mountable
1833 LOGI("Unmounting main partitions...\n");
1834
1835 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1836
1837 UnMount_By_Path("/system", true);
1838#ifndef RECOVERY_SDCARD_ON_DATA
1839 UnMount_By_Path("/data", true);
1840#endif
1841 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1842 Boot_Partition->UnMount(true);
1843}
1844
Dees_Troy9350b8d2012-09-27 12:38:38 -04001845int TWPartitionManager::Partition_SDCard(void) {
1846 char mkdir_path[255], temp[255], line[512];
1847 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1848 int ext, swap, total_size = 0, fat_size;
1849 FILE* fp;
1850
1851 ui_print("Partitioning SD Card...\n");
1852#ifdef TW_EXTERNAL_STORAGE_PATH
1853 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1854#else
1855 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1856#endif
1857 if (SDCard == NULL) {
1858 LOGE("Unable to locate device to partition.\n");
1859 return false;
1860 }
1861 if (!SDCard->UnMount(true))
1862 return false;
1863 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1864 if (SDext != NULL) {
1865 if (!SDext->UnMount(true))
1866 return false;
1867 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001868 string result;
1869 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"", result);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001870 Device = SDCard->Actual_Block_Device;
1871 // Just use the root block device
1872 Device.resize(strlen("/dev/block/mmcblkX"));
1873
1874 // Find the size of the block device:
1875 fp = fopen("/proc/partitions", "rt");
1876 if (fp == NULL) {
1877 LOGE("Unable to open /proc/partitions\n");
1878 return false;
1879 }
1880
1881 while (fgets(line, sizeof(line), fp) != NULL)
1882 {
1883 unsigned long major, minor, blocks;
1884 char device[512];
1885 char tmpString[64];
1886
1887 if (strlen(line) < 7 || line[0] == 'm') continue;
1888 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1889
1890 tmpdevice = "/dev/block/";
1891 tmpdevice += device;
1892 if (tmpdevice == Device) {
1893 // Adjust block size to byte size
1894 total_size = (int)(blocks * 1024ULL / 1000000LLU);
1895 break;
1896 }
1897 }
1898 fclose(fp);
1899
1900 DataManager::GetValue("tw_sdext_size", ext);
1901 DataManager::GetValue("tw_swap_size", swap);
1902 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1903 fat_size = total_size - ext - swap;
1904 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);
1905 memset(temp, 0, sizeof(temp));
1906 sprintf(temp, "%i", fat_size);
1907 fat_str = temp;
1908 memset(temp, 0, sizeof(temp));
1909 sprintf(temp, "%i", fat_size + ext);
1910 ext_str = temp;
1911 memset(temp, 0, sizeof(temp));
1912 sprintf(temp, "%i", fat_size + ext + swap);
1913 swap_str = temp;
1914 if (ext + swap > total_size) {
1915 LOGE("EXT + Swap size is larger than sdcard size.\n");
1916 return false;
1917 }
1918 ui_print("Removing partition table...\n");
1919 Command = "parted -s " + Device + " mklabel msdos";
1920 LOGI("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001921 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001922 LOGE("Unable to remove partition table.\n");
1923 Update_System_Details();
1924 return false;
1925 }
1926 ui_print("Creating FAT32 partition...\n");
1927 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
1928 LOGI("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001929 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001930 LOGE("Unable to create FAT32 partition.\n");
1931 return false;
1932 }
1933 if (ext > 0) {
1934 ui_print("Creating EXT partition...\n");
1935 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
1936 LOGI("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001937 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001938 LOGE("Unable to create EXT partition.\n");
1939 Update_System_Details();
1940 return false;
1941 }
1942 }
1943 if (swap > 0) {
1944 ui_print("Creating swap partition...\n");
1945 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
1946 LOGI("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001947 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001948 LOGE("Unable to create swap partition.\n");
1949 Update_System_Details();
1950 return false;
1951 }
1952 }
1953 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1954#ifdef TW_EXTERNAL_STORAGE_PATH
1955 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1956 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1957 memset(mkdir_path, 0, sizeof(mkdir_path));
1958 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1959#else
1960 Mount_By_Path("/sdcard", 1);
1961 strcpy(mkdir_path, "/sdcard/TWRP");
1962#endif
1963 mkdir(mkdir_path, 0777);
1964 DataManager::Flush();
1965#ifdef TW_EXTERNAL_STORAGE_PATH
1966 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1967 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1968 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1969#else
1970 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1971 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1972 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1973#endif
1974 if (ext > 0) {
1975 if (SDext == NULL) {
1976 LOGE("Unable to locate sd-ext partition.\n");
1977 return false;
1978 }
1979 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
1980 ui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1981 LOGI("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001982 TWFunc::Exec_Cmd(Command, result);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001983 }
1984
1985 Update_System_Details();
1986 ui_print("Partitioning complete.\n");
1987 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001988}