blob: 57e1108672c3ba4435d0d5e45012737fc79282e7 [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>
Dees_Troy51a0e822012-09-05 15:24:24 -040034
35#include "variables.h"
36#include "common.h"
Dees_Troy093b7642012-09-21 15:59:38 -040037#include "ui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040039#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "twrp-functions.hpp"
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040041#include "fixPermissions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040042
Dees_Troy5bf43922012-09-07 16:07:55 -040043#ifdef TW_INCLUDE_CRYPTO
44 #ifdef TW_INCLUDE_JB_CRYPTO
45 #include "crypto/jb/cryptfs.h"
46 #else
47 #include "crypto/ics/cryptfs.h"
48 #endif
49 #include "cutils/properties.h"
50#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040051
Dees_Troy093b7642012-09-21 15:59:38 -040052extern RecoveryUI* ui;
53
Dees_Troy51a0e822012-09-05 15:24:24 -040054int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040055 FILE *fstabFile;
56 char fstab_line[MAX_FSTAB_LINE_LENGTH];
57
58 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
59 if (fstabFile == NULL) {
60 LOGE("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
61 return false;
62 }
63
64 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
65 if (fstab_line[0] != '/')
66 continue;
67
Dees_Troy2a923582012-09-20 12:13:34 -040068 if (fstab_line[strlen(fstab_line) - 1] != '\n')
69 fstab_line[strlen(fstab_line)] = '\n';
70
Dees_Troy5bf43922012-09-07 16:07:55 -040071 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040072 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040073 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040074
Dees_Troy5bf43922012-09-07 16:07:55 -040075 if (partition->Process_Fstab_Line(line, Display_Error)) {
76 Partitions.push_back(partition);
77 } else {
78 delete partition;
79 }
80 }
81 fclose(fstabFile);
82 if (!Write_Fstab()) {
83 if (Display_Error)
84 LOGE("Error creating fstab\n");
85 else
86 LOGI("Error creating fstab\n");
87 }
Dees_Troy51127312012-09-08 13:08:49 -040088 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -040089 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -040090 return true;
91}
92
93int TWPartitionManager::Write_Fstab(void) {
94 FILE *fp;
95 std::vector<TWPartition*>::iterator iter;
96 string Line;
97
98 fp = fopen("/etc/fstab", "w");
99 if (fp == NULL) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400100 LOGI("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400101 return false;
102 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400103 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400104 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400105 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400106 fputs(Line.c_str(), fp);
Dees_Troy51127312012-09-08 13:08:49 -0400107 // Handle subpartition tracking
108 if ((*iter)->Is_SubPartition) {
109 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
110 if (ParentPartition)
111 ParentPartition->Has_SubPartition = true;
112 else
113 LOGE("Unable to locate parent partition '%s' of '%s'\n", (*iter)->SubPartition_Of.c_str(), (*iter)->Mount_Point.c_str());
114 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400115 }
116 }
117 fclose(fp);
118 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400119}
120
Dees_Troy8170a922012-09-18 15:40:25 -0400121void TWPartitionManager::Output_Partition_Logging(void) {
122 std::vector<TWPartition*>::iterator iter;
123
124 printf("\n\nPartition Logs:\n");
125 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
126 Output_Partition((*iter));
127}
128
129void TWPartitionManager::Output_Partition(TWPartition* Part) {
130 unsigned long long mb = 1048576;
131
132 if (Part->Can_Be_Mounted) {
133 printf("%s | %s | Size: %iMB Used: %iMB Free: %iMB Backup Size: %iMB\n Flags: ", Part->Mount_Point.c_str(), Part->Actual_Block_Device.c_str(), (int)(Part->Size / mb), (int)(Part->Used / mb), (int)(Part->Free / mb), (int)(Part->Backup_Size / mb));
134 if (Part->Can_Be_Wiped)
135 printf("Can_Be_Wiped ");
136 if (Part->Wipe_During_Factory_Reset)
137 printf("Wipe_During_Factory_Reset ");
138 if (Part->Wipe_Available_in_GUI)
139 printf("Wipe_Available_in_GUI ");
140 if (Part->Is_SubPartition)
141 printf("Is_SubPartition ");
142 if (Part->Has_SubPartition)
143 printf("Has_SubPartition ");
144 if (Part->Removable)
145 printf("Removable ");
146 if (Part->Is_Present)
147 printf("IsPresent ");
148 if (Part->Can_Be_Encrypted)
149 printf("Can_Be_Encrypted ");
150 if (Part->Is_Encrypted)
151 printf("Is_Encrypted ");
152 if (Part->Is_Decrypted)
153 printf("Is_Decrypted ");
154 if (Part->Has_Data_Media)
155 printf("Has_Data_Media ");
Dees_Troye58d5262012-09-21 12:27:57 -0400156 if (Part->Has_Android_Secure)
157 printf("Has_Android_Secure ");
Dees_Troy8170a922012-09-18 15:40:25 -0400158 if (Part->Is_Storage)
159 printf("Is_Storage ");
160 printf("\n");
161 if (!Part->SubPartition_Of.empty())
162 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
163 if (!Part->Symlink_Path.empty())
164 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
165 if (!Part->Symlink_Mount_Point.empty())
166 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
167 if (!Part->Primary_Block_Device.empty())
168 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
169 if (!Part->Alternate_Block_Device.empty())
170 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
171 if (!Part->Decrypted_Block_Device.empty())
172 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
173 if (Part->Length != 0)
174 printf(" Length: %i\n", Part->Length);
175 if (!Part->Display_Name.empty())
176 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400177 if (!Part->Backup_Path.empty())
178 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400179 if (!Part->Backup_Name.empty())
180 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
181 if (!Part->Backup_FileName.empty())
182 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400183 if (!Part->Storage_Path.empty())
184 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
185 if (!Part->Current_File_System.empty())
186 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
187 if (!Part->Fstab_File_System.empty())
188 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
189 if (Part->Format_Block_Size != 0)
190 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
191 } else {
192 printf("%s | %s | Size: %iMB\n", Part->Mount_Point.c_str(), Part->Actual_Block_Device.c_str(), (int)(Part->Size / mb));
193 }
Dees_Troy094207a2012-09-26 12:00:39 -0400194 if (!Part->MTD_Name.empty())
195 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400196 string back_meth = Part->Backup_Method_By_Name();
197 printf(" Backup_Method: %s\n\n", back_meth.c_str());
198}
199
Dees_Troy51a0e822012-09-05 15:24:24 -0400200int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400201 std::vector<TWPartition*>::iterator iter;
202 int ret = false;
203 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400204 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400205
Dees_Troy43d8b002012-09-17 16:00:01 -0400206 if (Local_Path == "/tmp")
207 return true;
208
Dees_Troy5bf43922012-09-07 16:07:55 -0400209 // Iterate through all partitions
210 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400211 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400212 ret = (*iter)->Mount(Display_Error);
213 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400214 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400215 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400216 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400217 }
218 if (found) {
219 return ret;
220 } else if (Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400221 LOGE("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400222 } else {
Dees_Troy51127312012-09-08 13:08:49 -0400223 LOGI("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400224 }
225 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400226}
227
228int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400229 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400230
Dees_Troy51127312012-09-08 13:08:49 -0400231 if (Part) {
232 if (Part->Has_SubPartition) {
233 std::vector<TWPartition*>::iterator subpart;
234
235 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
236 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
237 (*subpart)->Mount(Display_Error);
238 }
239 return Part->Mount(Display_Error);
240 } else
241 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400242 }
243 if (Display_Error)
Dees_Troy51127312012-09-08 13:08:49 -0400244 LOGE("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400245 else
Dees_Troy51127312012-09-08 13:08:49 -0400246 LOGI("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400247 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400248}
249
250int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400251 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400252
Dees_Troy51127312012-09-08 13:08:49 -0400253 if (Part) {
254 if (Part->Has_SubPartition) {
255 std::vector<TWPartition*>::iterator subpart;
256
257 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
258 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
259 (*subpart)->Mount(Display_Error);
260 }
261 return Part->Mount(Display_Error);
262 } else
263 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400264 }
265 if (Display_Error)
Dees_Troy51127312012-09-08 13:08:49 -0400266 LOGE("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400267 else
Dees_Troy51127312012-09-08 13:08:49 -0400268 LOGI("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400269 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400270}
271
272int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400273 std::vector<TWPartition*>::iterator iter;
274 int ret = false;
275 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400276 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400277
278 // Iterate through all partitions
279 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400280 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400281 ret = (*iter)->UnMount(Display_Error);
282 found = true;
283 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
284 (*iter)->UnMount(Display_Error);
285 }
286 }
287 if (found) {
288 return ret;
289 } else if (Display_Error) {
290 LOGE("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
291 } else {
292 LOGI("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
293 }
294 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400295}
296
297int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400298 TWPartition* Part = Find_Partition_By_Block(Block);
299
300 if (Part) {
301 if (Part->Has_SubPartition) {
302 std::vector<TWPartition*>::iterator subpart;
303
304 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
305 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
306 (*subpart)->UnMount(Display_Error);
307 }
308 return Part->UnMount(Display_Error);
309 } else
310 return Part->UnMount(Display_Error);
311 }
312 if (Display_Error)
313 LOGE("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
314 else
315 LOGI("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
316 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400317}
318
319int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400320 TWPartition* Part = Find_Partition_By_Name(Name);
321
322 if (Part) {
323 if (Part->Has_SubPartition) {
324 std::vector<TWPartition*>::iterator subpart;
325
326 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
327 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
328 (*subpart)->UnMount(Display_Error);
329 }
330 return Part->UnMount(Display_Error);
331 } else
332 return Part->UnMount(Display_Error);
333 }
334 if (Display_Error)
335 LOGE("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
336 else
337 LOGI("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
338 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400339}
340
341int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400342 TWPartition* Part = Find_Partition_By_Path(Path);
343
344 if (Part)
345 return Part->Is_Mounted();
346 else
347 LOGI("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
348 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400349}
350
351int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400352 TWPartition* Part = Find_Partition_By_Block(Block);
353
354 if (Part)
355 return Part->Is_Mounted();
356 else
357 LOGI("Is_Mounted: Unable to find partition for block '%s'\n", Block.c_str());
358 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400359}
360
361int TWPartitionManager::Is_Mounted_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400362 TWPartition* Part = Find_Partition_By_Name(Name);
363
364 if (Part)
365 return Part->Is_Mounted();
366 else
367 LOGI("Is_Mounted: Unable to find partition for name '%s'\n", Name.c_str());
368 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400369}
370
Dees_Troy5bf43922012-09-07 16:07:55 -0400371int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400372 string current_storage_path = DataManager::GetCurrentStoragePath();
373
374 if (Mount_By_Path(current_storage_path, Display_Error)) {
375 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
376 if (FreeStorage)
377 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
378 return true;
379 }
380 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400381}
382
Dees_Troy5bf43922012-09-07 16:07:55 -0400383int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
384 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
385}
386
387TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
388 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400389 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400390
391 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400392 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400393 return (*iter);
394 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400395 return NULL;
396}
397
Dees_Troy5bf43922012-09-07 16:07:55 -0400398TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400399 std::vector<TWPartition*>::iterator iter;
400
401 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400402 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 -0400403 return (*iter);
404 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400405 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400406}
407
408TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400409 std::vector<TWPartition*>::iterator iter;
410
411 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
412 if ((*iter)->Display_Name == Name)
413 return (*iter);
414 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400415 return NULL;
416}
Dees_Troy51a0e822012-09-05 15:24:24 -0400417
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400418int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
419 // Check the backup name to ensure that it is the correct size and contains only valid characters
420 // and that a backup with that name doesn't already exist
421 char backup_name[MAX_BACKUP_NAME_LEN];
422 char backup_loc[255], tw_image_dir[255];
423 int copy_size;
424 int index, cur_char;
425 string Backup_Name, Backup_Loc;
426
427 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
428 copy_size = Backup_Name.size();
429 // Check size
430 if (copy_size > MAX_BACKUP_NAME_LEN) {
431 if (Display_Error)
432 LOGE("Backup name is too long.\n");
433 return -2;
434 }
435
436 // Check each character
437 strncpy(backup_name, Backup_Name.c_str(), copy_size);
438 if (strcmp(backup_name, "0") == 0)
439 return 0; // A "0" (zero) means to use the current timestamp for the backup name
440 for (index=0; index<copy_size; index++) {
441 cur_char = (int)backup_name[index];
442 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) {
443 // These are valid characters
444 // Numbers
445 // Upper case letters
446 // Lower case letters
447 // Space
448 // and -_.{}[]
449 } else {
450 if (Display_Error)
451 LOGE("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
452 return -3;
453 }
454 }
455
456 // Check to make sure that a backup with this name doesn't already exist
457 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
458 strcpy(backup_loc, Backup_Loc.c_str());
459 sprintf(tw_image_dir,"%s/%s/.", backup_loc, backup_name);
460 if (TWFunc::Path_Exists(tw_image_dir)) {
461 if (Display_Error)
462 LOGE("A backup with this name already exists.\n");
463 return -4;
464 }
465
466 // No problems found, return 0
467 return 0;
468}
469
Dees_Troy43d8b002012-09-17 16:00:01 -0400470bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
471{
472 char command[512];
473 string Full_File = Backup_Folder + Backup_Filename;
474
Dees_Troy4a2a1262012-09-18 09:33:47 -0400475 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400476 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400477
Dees_Troyb46a6842012-09-25 11:06:46 -0400478 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troyc51f1f92012-09-20 15:32:13 -0400479 ui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400480
481 if (TWFunc::Path_Exists(Full_File)) {
482 sprintf(command, "cd '%s' && md5sum %s > %s.md5",Backup_Folder.c_str(), Backup_Filename.c_str(), Backup_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400483 if (system(command) == 0) {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400484 ui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400485 return true;
486 } else {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400487 ui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400488 return false;
489 }
490 } else {
491 char filename[512];
492 int index = 0;
493
494 sprintf(filename, "%s%03i", Full_File.c_str(), index);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400495 while (TWFunc::Path_Exists(filename) == true) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400496 sprintf(command, "cd '%s' && md5sum %s%03i > %s%03i.md5",Backup_Folder.c_str(), Backup_Filename.c_str(), index, Backup_Filename.c_str(), index);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400497 if (system(command) != 0) {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400498 ui_print(" * MD5 Error.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400499 return false;
500 }
501 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400502 sprintf(filename, "%s%03i", Full_File.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400503 }
504 if (index == 0) {
505 LOGE("Backup file: '%s' not found!\n", filename);
506 return false;
507 }
Dees_Troyc5865ab2012-09-24 15:08:04 -0400508 ui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400509 }
510 return true;
511}
512
Dees_Troy093b7642012-09-21 15:59:38 -0400513bool 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 -0400514 time_t start, stop;
Dees_Troy093b7642012-09-21 15:59:38 -0400515 int img_bps, file_bps;
516 unsigned long total_time, remain_time, section_time;
517 int use_compression, backup_time;
518 float pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400519
520 if (Part == NULL)
521 return true;
522
Dees_Troy093b7642012-09-21 15:59:38 -0400523 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
524
525 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
526 if (use_compression)
527 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
528 else
529 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
530
531 // We know the speed for both, how far into the whole backup are we, based on time
532 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
533 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
534
535 pos = (total_time - remain_time) / (float) total_time;
536 ui->SetProgress(pos);
537
538 LOGI("Estimated Total time: %lu Estimated remaining time: %lu\n", total_time, remain_time);
539
540 // And get the time
541 if (Part->Backup_Method == 1)
542 section_time = Part->Backup_Size / file_bps;
543 else
544 section_time = Part->Backup_Size / img_bps;
545
546 // Set the position
547 pos = section_time / (float) total_time;
548 ui->ShowProgress(pos, section_time);
549
Dees_Troy43d8b002012-09-17 16:00:01 -0400550 time(&start);
551
552 if (Part->Backup(Backup_Folder)) {
Dees_Troy8170a922012-09-18 15:40:25 -0400553 if (Part->Has_SubPartition) {
554 std::vector<TWPartition*>::iterator subpart;
555
556 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
557 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
558 if (!(*subpart)->Backup(Backup_Folder))
559 return false;
560 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
561 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400562 if (Part->Backup_Method == 1) {
563 *file_bytes_remaining -= (*subpart)->Backup_Size;
564 } else {
565 *img_bytes_remaining -= (*subpart)->Backup_Size;
566 }
Dees_Troy8170a922012-09-18 15:40:25 -0400567 }
568 }
569 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400570 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400571 backup_time = (int) difftime(stop, start);
572 LOGI("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400573 if (Part->Backup_Method == 1) {
574 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400575 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400576 } else {
577 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400578 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400579 }
580 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
581 } else {
582 return false;
583 }
584}
585
586int TWPartitionManager::Run_Backup(void) {
587 int check, do_md5, partition_count = 0;
588 string Backup_Folder, Backup_Name, Full_Backup_Path;
Dees_Troy8170a922012-09-18 15:40:25 -0400589 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 -0400590 unsigned long img_time = 0, file_time = 0;
591 TWPartition* backup_sys = NULL;
592 TWPartition* backup_data = NULL;
593 TWPartition* backup_cache = NULL;
594 TWPartition* backup_recovery = NULL;
595 TWPartition* backup_boot = NULL;
596 TWPartition* backup_andsec = NULL;
597 TWPartition* backup_sdext = NULL;
598 TWPartition* backup_sp1 = NULL;
599 TWPartition* backup_sp2 = NULL;
600 TWPartition* backup_sp3 = NULL;
601 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400602 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400603 struct tm *t;
604 time_t start, stop, seconds, total_start, total_stop;
605 seconds = time(0);
606 t = localtime(&seconds);
607
608 time(&total_start);
609
610 Update_System_Details();
611
612 if (!Mount_Current_Storage(true))
613 return false;
614
615 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400616 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400617 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400618 else
619 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400620
Dees_Troy43d8b002012-09-17 16:00:01 -0400621 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
622 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400623 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name.empty()) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400624 char timestamp[255];
625 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);
626 Backup_Name = timestamp;
627 }
628 LOGI("Backup Name is: '%s'\n", Backup_Name.c_str());
629 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
630 LOGI("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
631
632 ui_print("\n[BACKUP STARTED]\n");
633 ui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
634 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
635 LOGE("Failed to make backup folder.\n");
636 return false;
637 }
638
639 LOGI("Calculating backup details...\n");
640 DataManager::GetValue(TW_BACKUP_SYSTEM_VAR, check);
641 if (check) {
642 backup_sys = Find_Partition_By_Path("/system");
643 if (backup_sys != NULL) {
644 partition_count++;
645 if (backup_sys->Backup_Method == 1)
646 file_bytes += backup_sys->Backup_Size;
647 else
648 img_bytes += backup_sys->Backup_Size;
649 } else {
650 LOGE("Unable to locate system partition.\n");
651 return false;
652 }
653 }
654 DataManager::GetValue(TW_BACKUP_DATA_VAR, check);
655 if (check) {
656 backup_data = Find_Partition_By_Path("/data");
657 if (backup_data != NULL) {
658 partition_count++;
Dees_Troy8170a922012-09-18 15:40:25 -0400659 subpart_size = 0;
660 if (backup_data->Has_SubPartition) {
661 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
662 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == backup_data->Mount_Point)
663 subpart_size += (*subpart)->Backup_Size;
664 }
665 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400666 if (backup_data->Backup_Method == 1)
Dees_Troy8170a922012-09-18 15:40:25 -0400667 file_bytes += backup_data->Backup_Size + subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400668 else
Dees_Troy8170a922012-09-18 15:40:25 -0400669 img_bytes += backup_data->Backup_Size + subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400670 } else {
671 LOGE("Unable to locate data partition.\n");
672 return false;
673 }
674 }
675 DataManager::GetValue(TW_BACKUP_CACHE_VAR, check);
676 if (check) {
677 backup_cache = Find_Partition_By_Path("/cache");
678 if (backup_cache != NULL) {
679 partition_count++;
680 if (backup_cache->Backup_Method == 1)
681 file_bytes += backup_cache->Backup_Size;
682 else
683 img_bytes += backup_cache->Backup_Size;
684 } else {
685 LOGE("Unable to locate cache partition.\n");
686 return false;
687 }
688 }
689 DataManager::GetValue(TW_BACKUP_RECOVERY_VAR, check);
690 if (check) {
691 backup_recovery = Find_Partition_By_Path("/recovery");
692 if (backup_recovery != NULL) {
693 partition_count++;
694 if (backup_recovery->Backup_Method == 1)
695 file_bytes += backup_recovery->Backup_Size;
696 else
697 img_bytes += backup_recovery->Backup_Size;
698 } else {
699 LOGE("Unable to locate recovery partition.\n");
700 return false;
701 }
702 }
703 DataManager::GetValue(TW_BACKUP_BOOT_VAR, check);
704 if (check) {
705 backup_boot = Find_Partition_By_Path("/boot");
706 if (backup_boot != NULL) {
707 partition_count++;
708 if (backup_boot->Backup_Method == 1)
709 file_bytes += backup_boot->Backup_Size;
710 else
711 img_bytes += backup_boot->Backup_Size;
712 } else {
713 LOGE("Unable to locate boot partition.\n");
714 return false;
715 }
716 }
717 DataManager::GetValue(TW_BACKUP_ANDSEC_VAR, check);
718 if (check) {
719 backup_andsec = Find_Partition_By_Path("/and-sec");
720 if (backup_andsec != NULL) {
721 partition_count++;
722 if (backup_andsec->Backup_Method == 1)
723 file_bytes += backup_andsec->Backup_Size;
724 else
725 img_bytes += backup_andsec->Backup_Size;
726 } else {
727 LOGE("Unable to locate android secure partition.\n");
728 return false;
729 }
730 }
731 DataManager::GetValue(TW_BACKUP_SDEXT_VAR, check);
732 if (check) {
733 backup_sdext = Find_Partition_By_Path("/sd-ext");
734 if (backup_sdext != NULL) {
735 partition_count++;
736 if (backup_sdext->Backup_Method == 1)
737 file_bytes += backup_sdext->Backup_Size;
738 else
739 img_bytes += backup_sdext->Backup_Size;
740 } else {
741 LOGE("Unable to locate sd-ext partition.\n");
742 return false;
743 }
744 }
745#ifdef SP1_NAME
746 DataManager::GetValue(TW_BACKUP_SP1_VAR, check);
747 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400748 backup_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400749 if (backup_sp1 != NULL) {
750 partition_count++;
751 if (backup_sp1->Backup_Method == 1)
752 file_bytes += backup_sp1->Backup_Size;
753 else
754 img_bytes += backup_sp1->Backup_Size;
755 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400756 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400757 return false;
758 }
759 }
760#endif
761#ifdef SP2_NAME
762 DataManager::GetValue(TW_BACKUP_SP2_VAR, check);
763 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400764 backup_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400765 if (backup_sp2 != NULL) {
766 partition_count++;
767 if (backup_sp2->Backup_Method == 1)
768 file_bytes += backup_sp2->Backup_Size;
769 else
770 img_bytes += backup_sp2->Backup_Size;
771 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400772 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400773 return false;
774 }
775 }
776#endif
777#ifdef SP3_NAME
778 DataManager::GetValue(TW_BACKUP_SP3_VAR, check);
779 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400780 backup_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400781 if (backup_sp3 != NULL) {
782 partition_count++;
783 if (backup_sp3->Backup_Method == 1)
784 file_bytes += backup_sp3->Backup_Size;
785 else
786 img_bytes += backup_sp3->Backup_Size;
787 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400788 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400789 return false;
790 }
791 }
792#endif
793
794 if (partition_count == 0) {
795 ui_print("No partitions selected for backup.\n");
796 return false;
797 }
798 total_bytes = file_bytes + img_bytes;
799 ui_print(" * Total number of partitions to back up: %d\n", partition_count);
800 ui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
801 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
802 if (storage != NULL) {
803 free_space = storage->Free;
804 ui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
805 } else {
806 LOGE("Unable to locate storage device.\n");
807 return false;
808 }
809 if (free_space + (32 * 1024 * 1024) < total_bytes) {
810 // We require an extra 32MB just in case
811 LOGE("Not enough free space on storage.\n");
812 return false;
813 }
814 img_bytes_remaining = img_bytes;
815 file_bytes_remaining = file_bytes;
816
Dees_Troy093b7642012-09-21 15:59:38 -0400817 ui->SetProgress(0.0);
818
819 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 -0400820 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400821 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 -0400822 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400823 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 -0400824 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400825 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 -0400826 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400827 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 -0400828 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400829 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 -0400830 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400831 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 -0400832 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400833 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 -0400834 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400835 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 -0400836 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400837 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 -0400838 return false;
839
840 // Average BPS
841 if (img_time == 0)
842 img_time = 1;
843 if (file_time == 0)
844 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400845 int img_bps = (int)img_bytes / (int)img_time;
846 int file_bps = (int)file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400847
848 ui_print("Average backup rate for file systems: %lu MB/sec\n", (file_bps / (1024 * 1024)));
849 ui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
850
851 time(&total_stop);
852 int total_time = (int) difftime(total_stop, total_start);
853 unsigned long long actual_backup_size = TWFunc::Get_Folder_Size(Full_Backup_Path, true);
854 actual_backup_size /= (1024LLU * 1024LLU);
855
Dees_Troy093b7642012-09-21 15:59:38 -0400856 int prev_img_bps, prev_file_bps, use_compression;
857 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
858 img_bps += (prev_img_bps * 4);
859 img_bps /= 5;
860
861 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
862 if (use_compression)
863 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
864 else
865 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
866 file_bps += (prev_file_bps * 4);
867 file_bps /= 5;
868
869 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
870 if (use_compression)
871 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
872 else
873 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
874
Dees_Troy43d8b002012-09-17 16:00:01 -0400875 ui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
876 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400877 UnMount_Main_Partitions();
Dees_Troy43d8b002012-09-17 16:00:01 -0400878 ui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
879 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400880}
881
Dees_Troy093b7642012-09-21 15:59:38 -0400882bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400883 time_t Start, Stop;
884 time(&Start);
Dees_Troy093b7642012-09-21 15:59:38 -0400885 ui->ShowProgress(1.0 / (float)partition_count, 150);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400886 if (!Part->Restore(Restore_Name))
887 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400888 if (Part->Has_SubPartition) {
889 std::vector<TWPartition*>::iterator subpart;
890
891 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
892 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
893 if (!(*subpart)->Restore(Restore_Name))
894 return false;
895 }
896 }
897 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400898 time(&Stop);
899 ui_print("[%s done (%d seconds)]\n\n", Part->Display_Name.c_str(), (int)difftime(Stop, Start));
900 return true;
901}
902
Dees_Troy51a0e822012-09-05 15:24:24 -0400903int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400904 int check_md5, check, partition_count = 0;
905 TWPartition* restore_sys = NULL;
906 TWPartition* restore_data = NULL;
907 TWPartition* restore_cache = NULL;
908 TWPartition* restore_boot = NULL;
909 TWPartition* restore_andsec = NULL;
910 TWPartition* restore_sdext = NULL;
911 TWPartition* restore_sp1 = NULL;
912 TWPartition* restore_sp2 = NULL;
913 TWPartition* restore_sp3 = NULL;
914 time_t rStart, rStop;
915 time(&rStart);
Dees_Troy43d8b002012-09-17 16:00:01 -0400916
Dees_Troy4a2a1262012-09-18 09:33:47 -0400917 ui_print("\n[RESTORE STARTED]\n\n");
918 ui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400919
Dees_Troy4a2a1262012-09-18 09:33:47 -0400920 if (!Mount_Current_Storage(true))
921 return false;
922
923 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
924 DataManager::GetValue(TW_RESTORE_SYSTEM_VAR, check);
Dees_Troy63c8df72012-09-10 14:02:05 -0400925 if (check > 0) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400926 restore_sys = Find_Partition_By_Path("/system");
927 if (restore_sys == NULL) {
928 LOGE("Unable to locate system partition.\n");
929 return false;
930 }
931 partition_count++;
932 }
933 DataManager::GetValue(TW_RESTORE_DATA_VAR, check);
934 if (check > 0) {
935 restore_data = Find_Partition_By_Path("/data");
936 if (restore_data == NULL) {
937 LOGE("Unable to locate data partition.\n");
938 return false;
939 }
940 partition_count++;
941 }
942 DataManager::GetValue(TW_RESTORE_CACHE_VAR, check);
943 if (check > 0) {
944 restore_cache = Find_Partition_By_Path("/cache");
945 if (restore_cache == NULL) {
946 LOGE("Unable to locate cache partition.\n");
947 return false;
948 }
949 partition_count++;
950 }
951 DataManager::GetValue(TW_RESTORE_BOOT_VAR, check);
952 if (check > 0) {
953 restore_boot = Find_Partition_By_Path("/boot");
954 if (restore_boot == NULL) {
955 LOGE("Unable to locate boot partition.\n");
956 return false;
957 }
958 partition_count++;
959 }
960 DataManager::GetValue(TW_RESTORE_ANDSEC_VAR, check);
961 if (check > 0) {
962 restore_andsec = Find_Partition_By_Path("/and-sec");
963 if (restore_andsec == NULL) {
964 LOGE("Unable to locate android secure partition.\n");
965 return false;
966 }
967 partition_count++;
968 }
969 DataManager::GetValue(TW_RESTORE_SDEXT_VAR, check);
970 if (check > 0) {
971 restore_sdext = Find_Partition_By_Path("/sd-ext");
972 if (restore_sdext == NULL) {
973 LOGE("Unable to locate sd-ext partition.\n");
974 return false;
975 }
976 partition_count++;
977 }
978#ifdef SP1_NAME
979 DataManager::GetValue(TW_RESTORE_SP1_VAR, check);
980 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400981 restore_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400982 if (restore_sp1 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400983 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400984 return false;
985 }
986 partition_count++;
987 }
988#endif
989#ifdef SP2_NAME
990 DataManager::GetValue(TW_RESTORE_SP2_VAR, check);
991 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400992 restore_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400993 if (restore_sp2 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400994 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400995 return false;
996 }
997 partition_count++;
998 }
999#endif
1000#ifdef SP3_NAME
1001 DataManager::GetValue(TW_RESTORE_SP3_VAR, check);
1002 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001003 restore_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001004 if (restore_sp3 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001005 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001006 return false;
1007 }
1008 partition_count++;
1009 }
1010#endif
1011
1012 if (partition_count == 0) {
1013 LOGE("No partitions selected for restore.\n");
1014 return false;
1015 }
1016
1017 if (check_md5 > 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001018 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
Dees_Troyb46a6842012-09-25 11:06:46 -04001019 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001020 ui_print("Verifying MD5...\n");
1021 if (restore_sys != NULL && !restore_sys->Check_MD5(Restore_Name))
1022 return false;
1023 if (restore_data != NULL && !restore_data->Check_MD5(Restore_Name))
1024 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001025 if (restore_data != NULL && restore_data->Has_SubPartition) {
1026 std::vector<TWPartition*>::iterator subpart;
1027
1028 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1029 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_data->Mount_Point) {
1030 if (!(*subpart)->Check_MD5(Restore_Name))
1031 return false;
1032 }
1033 }
1034 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001035 if (restore_cache != NULL && !restore_cache->Check_MD5(Restore_Name))
1036 return false;
1037 if (restore_boot != NULL && !restore_boot->Check_MD5(Restore_Name))
1038 return false;
1039 if (restore_andsec != NULL && !restore_andsec->Check_MD5(Restore_Name))
1040 return false;
1041 if (restore_sdext != NULL && !restore_sdext->Check_MD5(Restore_Name))
1042 return false;
1043 if (restore_sp1 != NULL && !restore_sp1->Check_MD5(Restore_Name))
1044 return false;
1045 if (restore_sp2 != NULL && !restore_sp2->Check_MD5(Restore_Name))
1046 return false;
1047 if (restore_sp3 != NULL && !restore_sp3->Check_MD5(Restore_Name))
1048 return false;
1049 ui_print("Done verifying MD5.\n");
1050 } else
1051 ui_print("Skipping MD5 check based on user setting.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001052
Dees_Troy4a2a1262012-09-18 09:33:47 -04001053 ui_print("Restoring %i partitions...\n", partition_count);
Dees_Troy093b7642012-09-21 15:59:38 -04001054 ui->SetProgress(0.0);
1055 if (restore_sys != NULL && !Restore_Partition(restore_sys, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001056 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001057 if (restore_data != NULL && !Restore_Partition(restore_data, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001058 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001059 if (restore_cache != NULL && !Restore_Partition(restore_cache, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001060 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001061 if (restore_boot != NULL && !Restore_Partition(restore_boot, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001062 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001063 if (restore_andsec != NULL && !Restore_Partition(restore_andsec, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001064 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001065 if (restore_sdext != NULL && !Restore_Partition(restore_sdext, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001066 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001067 if (restore_sp1 != NULL && !Restore_Partition(restore_sp1, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001068 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001069 if (restore_sp2 != NULL && !Restore_Partition(restore_sp2, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001070 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001071 if (restore_sp3 != NULL && !Restore_Partition(restore_sp3, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001072 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001073
Dees_Troyb46a6842012-09-25 11:06:46 -04001074 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -04001075 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001076 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -04001077 time(&rStop);
1078 ui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -04001079 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001080}
1081
1082void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001083 // Start with the default values
1084 int tw_restore_system = -1;
1085 int tw_restore_data = -1;
1086 int tw_restore_cache = -1;
1087 int tw_restore_recovery = -1;
1088 int tw_restore_boot = -1;
1089 int tw_restore_andsec = -1;
1090 int tw_restore_sdext = -1;
1091 int tw_restore_sp1 = -1;
1092 int tw_restore_sp2 = -1;
1093 int tw_restore_sp3 = -1;
1094 bool get_date = true;
1095
1096 DIR* d;
1097 d = opendir(Restore_Name.c_str());
1098 if (d == NULL)
1099 {
1100 LOGE("Error opening %s\n", Restore_Name.c_str());
1101 return;
1102 }
1103
1104 struct dirent* de;
1105 while ((de = readdir(d)) != NULL)
1106 {
1107 // Strip off three components
1108 char str[256];
1109 char* label;
1110 char* fstype = NULL;
1111 char* extn = NULL;
1112 char* ptr;
1113
1114 strcpy(str, de->d_name);
1115 if (strlen(str) <= 2)
1116 continue;
1117
1118 if (get_date) {
1119 char file_path[255];
1120 struct stat st;
1121
1122 strcpy(file_path, Restore_Name.c_str());
1123 strcat(file_path, "/");
1124 strcat(file_path, str);
1125 stat(file_path, &st);
1126 string backup_date = ctime((const time_t*)(&st.st_mtime));
1127 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
1128 get_date = false;
1129 }
1130
1131 label = str;
1132 ptr = label;
1133 while (*ptr && *ptr != '.') ptr++;
1134 if (*ptr == '.')
1135 {
1136 *ptr = 0x00;
1137 ptr++;
1138 fstype = ptr;
1139 }
1140 while (*ptr && *ptr != '.') ptr++;
1141 if (*ptr == '.')
1142 {
1143 *ptr = 0x00;
1144 ptr++;
1145 extn = ptr;
1146 }
1147
1148 if (extn == NULL || (strlen(extn) >= 3 && strncmp(extn, "win", 3) != 0)) continue;
1149
1150 TWPartition* Part = Find_Partition_By_Path(label);
1151 if (Part == NULL)
1152 {
1153 LOGE(" Unable to locate partition by backup name: '%s'\n", label);
1154 continue;
1155 }
1156
1157 Part->Backup_FileName = de->d_name;
1158 if (strlen(extn) > 3) {
1159 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1160 }
1161
1162 // Now, we just need to find the correct label
Dees_Troye58d5262012-09-21 12:27:57 -04001163 if (Part->Backup_Path == "/system")
Dees_Troy63c8df72012-09-10 14:02:05 -04001164 tw_restore_system = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001165 if (Part->Backup_Path == "/data")
Dees_Troy63c8df72012-09-10 14:02:05 -04001166 tw_restore_data = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001167 if (Part->Backup_Path == "/cache")
Dees_Troy63c8df72012-09-10 14:02:05 -04001168 tw_restore_cache = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001169 if (Part->Backup_Path == "/recovery")
Dees_Troy63c8df72012-09-10 14:02:05 -04001170 tw_restore_recovery = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001171 if (Part->Backup_Path == "/boot")
Dees_Troy63c8df72012-09-10 14:02:05 -04001172 tw_restore_boot = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001173 if (Part->Backup_Path == "/and-sec")
Dees_Troy63c8df72012-09-10 14:02:05 -04001174 tw_restore_andsec = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001175 if (Part->Backup_Path == "/sd-ext")
Dees_Troy63c8df72012-09-10 14:02:05 -04001176 tw_restore_sdext = 1;
1177#ifdef SP1_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001178 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP1_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001179 tw_restore_sp1 = 1;
1180#endif
1181#ifdef SP2_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001182 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP2_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001183 tw_restore_sp2 = 1;
1184#endif
1185#ifdef SP3_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001186 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP3_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001187 tw_restore_sp3 = 1;
1188#endif
1189 }
1190 closedir(d);
1191
1192 // Set the final values
1193 DataManager::SetValue(TW_RESTORE_SYSTEM_VAR, tw_restore_system);
1194 DataManager::SetValue(TW_RESTORE_DATA_VAR, tw_restore_data);
1195 DataManager::SetValue(TW_RESTORE_CACHE_VAR, tw_restore_cache);
1196 DataManager::SetValue(TW_RESTORE_RECOVERY_VAR, tw_restore_recovery);
1197 DataManager::SetValue(TW_RESTORE_BOOT_VAR, tw_restore_boot);
1198 DataManager::SetValue(TW_RESTORE_ANDSEC_VAR, tw_restore_andsec);
1199 DataManager::SetValue(TW_RESTORE_SDEXT_VAR, tw_restore_sdext);
1200 DataManager::SetValue(TW_RESTORE_SP1_VAR, tw_restore_sp1);
1201 DataManager::SetValue(TW_RESTORE_SP2_VAR, tw_restore_sp2);
1202 DataManager::SetValue(TW_RESTORE_SP3_VAR, tw_restore_sp3);
1203
Dees_Troy51a0e822012-09-05 15:24:24 -04001204 return;
1205}
1206
1207int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001208 std::vector<TWPartition*>::iterator iter;
1209 int ret = false;
1210 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001211 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001212
1213 // Iterate through all partitions
1214 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001215 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001216 if (Path == "/and-sec")
1217 ret = (*iter)->Wipe_AndSec();
1218 else
1219 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001220 found = true;
1221 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1222 (*iter)->Wipe();
1223 }
1224 }
1225 if (found) {
1226 return ret;
1227 } else
1228 LOGE("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1229 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001230}
1231
1232int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001233 TWPartition* Part = Find_Partition_By_Block(Block);
1234
1235 if (Part) {
1236 if (Part->Has_SubPartition) {
1237 std::vector<TWPartition*>::iterator subpart;
1238
1239 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1240 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1241 (*subpart)->Wipe();
1242 }
1243 return Part->Wipe();
1244 } else
1245 return Part->Wipe();
1246 }
1247 LOGE("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
1248 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001249}
1250
1251int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001252 TWPartition* Part = Find_Partition_By_Name(Name);
1253
1254 if (Part) {
1255 if (Part->Has_SubPartition) {
1256 std::vector<TWPartition*>::iterator subpart;
1257
1258 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1259 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1260 (*subpart)->Wipe();
1261 }
1262 return Part->Wipe();
1263 } else
1264 return Part->Wipe();
1265 }
1266 LOGE("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
1267 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001268}
1269
1270int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001271 std::vector<TWPartition*>::iterator iter;
1272 int ret = true;
1273
1274 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001275 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001276 if (!(*iter)->Wipe())
1277 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001278 } else if ((*iter)->Has_Android_Secure) {
1279 if (!(*iter)->Wipe_AndSec())
1280 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001281 }
1282 }
1283 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001284}
1285
Dees_Troy38bd7602012-09-14 13:33:53 -04001286int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1287 struct stat st;
1288
1289 if (!Mount_By_Path("/data", true))
1290 return false;
1291
1292 if (!Mount_By_Path("/cache", true))
1293 return false;
1294
1295 ui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001296 system("rm -rf /data/dalvik-cache");
Dees_Troy38bd7602012-09-14 13:33:53 -04001297 ui_print("Cleaned: /data/dalvik-cache...\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001298 system("rm -rf /cache/dalvik-cache");
Dees_Troy38bd7602012-09-14 13:33:53 -04001299 ui_print("Cleaned: /cache/dalvik-cache...\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001300 system("rm -rf /cache/dc");
Dees_Troy38bd7602012-09-14 13:33:53 -04001301 ui_print("Cleaned: /cache/dc\n");
1302
1303 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
1304 if (sdext != NULL) {
1305 if (sdext->Is_Present && sdext->Mount(false)) {
1306 if (stat("/sd-ext/dalvik-cache", &st) == 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001307 system("rm -rf /sd-ext/dalvik-cache");
Dees_Troy38bd7602012-09-14 13:33:53 -04001308 ui_print("Cleaned: /sd-ext/dalvik-cache...\n");
1309 }
1310 }
1311 }
1312 ui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
1313 return true;
1314}
1315
1316int TWPartitionManager::Wipe_Rotate_Data(void) {
1317 if (!Mount_By_Path("/data", true))
1318 return false;
1319
Dees_Troy8170a922012-09-18 15:40:25 -04001320 system("rm -r /data/misc/akmd*");
1321 system("rm -r /data/misc/rild*");
1322 system("rm -r /data/misc/rild*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001323 ui_print("Rotation data wiped.\n");
1324 return true;
1325}
1326
1327int TWPartitionManager::Wipe_Battery_Stats(void) {
1328 struct stat st;
1329
1330 if (!Mount_By_Path("/data", true))
1331 return false;
1332
1333 if (0 != stat("/data/system/batterystats.bin", &st)) {
1334 ui_print("No Battery Stats Found. No Need To Wipe.\n");
1335 } else {
1336 remove("/data/system/batterystats.bin");
1337 ui_print("Cleared battery stats.\n");
1338 }
1339 return true;
1340}
1341
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001342int TWPartitionManager::Wipe_Android_Secure(void) {
1343 std::vector<TWPartition*>::iterator iter;
1344 int ret = false;
1345 bool found = false;
1346
1347 // Iterate through all partitions
1348 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1349 if ((*iter)->Has_Android_Secure) {
1350 ret = (*iter)->Wipe_AndSec();
1351 found = true;
1352 }
1353 }
1354 if (found) {
1355 return ret;
1356 } else {
1357 LOGE("No android secure partitions found.\n");
1358 }
1359 return false;
1360}
1361
Dees_Troy38bd7602012-09-14 13:33:53 -04001362int TWPartitionManager::Format_Data(void) {
1363 TWPartition* dat = Find_Partition_By_Path("/data");
1364
1365 if (dat != NULL) {
1366 if (!dat->UnMount(true))
1367 return false;
1368
1369 return dat->Wipe_Encryption();
1370 } else {
1371 LOGE("Unable to locate /data.\n");
1372 return false;
1373 }
1374 return false;
1375}
1376
1377int TWPartitionManager::Wipe_Media_From_Data(void) {
1378 TWPartition* dat = Find_Partition_By_Path("/data");
1379
1380 if (dat != NULL) {
1381 if (!dat->Has_Data_Media) {
1382 LOGE("This device does not have /data/media\n");
1383 return false;
1384 }
1385 if (!dat->Mount(true))
1386 return false;
1387
1388 ui_print("Wiping internal storage -- /data/media...\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001389 system("rm -rf /data/media");
1390 system("cd /data && mkdir media && chmod 775 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001391 if (dat->Has_Data_Media) {
1392 dat->Recreate_Media_Folder();
1393 }
1394 return true;
1395 } else {
1396 LOGE("Unable to locate /data.\n");
1397 return false;
1398 }
1399 return false;
1400}
1401
Dees_Troy51a0e822012-09-05 15:24:24 -04001402void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001403 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001404 return;
1405}
1406
1407void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001408 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001409 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001410
Dees_Troy32c8eb82012-09-11 15:28:06 -04001411 ui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001412 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001413 if ((*iter)->Can_Be_Mounted) {
1414 (*iter)->Update_Size(true);
1415 if ((*iter)->Mount_Point == "/system") {
1416 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1417 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1418 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1419 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1420 } else if ((*iter)->Mount_Point == "/cache") {
1421 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1422 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1423 } else if ((*iter)->Mount_Point == "/sd-ext") {
1424 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1425 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1426 if ((*iter)->Backup_Size == 0) {
1427 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1428 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1429 } else
1430 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001431 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001432 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001433 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001434 if ((*iter)->Backup_Size == 0) {
1435 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1436 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1437 } else
1438 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001439 } else if ((*iter)->Mount_Point == "/boot") {
1440 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1441 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1442 if ((*iter)->Backup_Size == 0) {
1443 DataManager::SetValue("tw_has_boot_partition", 0);
1444 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1445 } else
1446 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001447 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001448#ifdef SP1_NAME
1449 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1450 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1451 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1452 }
1453#endif
1454#ifdef SP2_NAME
1455 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1456 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1457 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1458 }
1459#endif
1460#ifdef SP3_NAME
1461 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1462 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1463 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1464 }
1465#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001466 } else {
1467 // Handle unmountable partitions in case we reset defaults
1468 if ((*iter)->Mount_Point == "/boot") {
1469 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1470 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1471 if ((*iter)->Backup_Size == 0) {
1472 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1473 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1474 } else
1475 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1476 } else if ((*iter)->Mount_Point == "/recovery") {
1477 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1478 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1479 if ((*iter)->Backup_Size == 0) {
1480 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1481 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1482 } else
1483 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
1484 }
1485#ifdef SP1_NAME
1486 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1487 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1488 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1489 }
1490#endif
1491#ifdef SP2_NAME
1492 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1493 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1494 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1495 }
1496#endif
1497#ifdef SP3_NAME
1498 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1499 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1500 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1501 }
1502#endif
Dees_Troy51127312012-09-08 13:08:49 -04001503 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001504 }
Dees_Troy51127312012-09-08 13:08:49 -04001505 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1506 string current_storage_path = DataManager::GetCurrentStoragePath();
1507 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001508 if (FreeStorage != NULL) {
1509 // Attempt to mount storage
1510 if (!FreeStorage->Mount(false)) {
1511 // We couldn't mount storage... check to see if we have dual storage
1512 int has_dual_storage;
1513 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1514 if (has_dual_storage == 1) {
1515 // We have dual storage, see if we're using the internal storage that should always be present
1516 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001517 if (!FreeStorage->Is_Encrypted) {
1518 // Not able to use internal, so error!
1519 LOGE("Unable to mount internal storage.\n");
1520 }
Dees_Troy8170a922012-09-18 15:40:25 -04001521 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1522 } else {
1523 // We were using external, flip to internal
1524 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1525 current_storage_path = DataManager::GetCurrentStoragePath();
1526 FreeStorage = Find_Partition_By_Path(current_storage_path);
1527 if (FreeStorage != NULL) {
1528 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1529 } else {
1530 LOGE("Unable to locate internal storage partition.\n");
1531 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1532 }
1533 }
1534 } else {
1535 // No dual storage and unable to mount storage, error!
1536 LOGE("Unable to mount storage.\n");
1537 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1538 }
1539 } else {
1540 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1541 }
1542 } else {
Dees_Troy51127312012-09-08 13:08:49 -04001543 LOGI("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001544 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001545 if (!Write_Fstab())
1546 LOGE("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001547 return;
1548}
1549
1550int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001551#ifdef TW_INCLUDE_CRYPTO
1552 int ret_val, password_len;
1553 char crypto_blkdev[255], cPassword[255];
1554 size_t result;
1555
1556 property_set("ro.crypto.state", "encrypted");
1557#ifdef TW_INCLUDE_JB_CRYPTO
1558 // No extra flags needed
1559#else
1560 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1561 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1562 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1563 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1564 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1565 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
1566#endif
1567 strcpy(cPassword, Password.c_str());
1568 if (cryptfs_check_passwd(cPassword) != 0) {
1569 LOGE("Failed to decrypt data.\n");
1570 return -1;
1571 }
1572 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1573 if (strcmp(crypto_blkdev, "error") == 0) {
1574 LOGE("Error retrieving decrypted data block device.\n");
1575 } else {
1576 TWPartition* dat = Find_Partition_By_Path("/data");
1577 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001578 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001579 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1580 dat->Is_Decrypted = true;
1581 dat->Decrypted_Block_Device = crypto_blkdev;
Dees_Troy32c8eb82012-09-11 15:28:06 -04001582 ui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy5bf43922012-09-07 16:07:55 -04001583 // Sleep for a bit so that the device will be ready
1584 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001585#ifdef RECOVERY_SDCARD_ON_DATA
1586 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1587 dat->Storage_Path = "/data/media/0";
1588 dat->Symlink_Path = dat->Storage_Path;
1589 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
1590 dat->UnMount(false);
1591 DataManager::SetBackupFolder();
1592 }
1593#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001594 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001595 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001596 } else
1597 LOGE("Unable to locate data partition.\n");
1598 }
1599 return 0;
1600#else
1601 LOGE("No crypto support was compiled into this build.\n");
1602 return -1;
1603#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001604 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001605}
1606
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001607int TWPartitionManager::Fix_Permissions(void) {
1608 int result = 0;
1609 if (!Mount_By_Path("/data", true))
1610 return false;
1611
1612 if (!Mount_By_Path("/system", true))
1613 return false;
1614
1615 Mount_By_Path("/sd-ext", false);
1616
1617 fixPermissions perms;
1618 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001619 UnMount_Main_Partitions();
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001620 ui_print("Done.\n\n");
1621 return result;
1622}
1623
Dees_Troy8170a922012-09-18 15:40:25 -04001624//partial kangbang from system/vold
1625#ifndef CUSTOM_LUN_FILE
1626#define CUSTOM_LUN_FILE "/sys/devices/platform/usb_mass_storage/lun%d/file"
1627#endif
1628
Dees_Troyd21618c2012-10-14 18:48:49 -04001629int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
1630 int fd;
1631 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1632
1633 if (Part == NULL) {
1634 LOGE("Unable to locate volume information for USB storage mode.");
1635 return false;
1636 }
1637 if (!Part->UnMount(true))
1638 return false;
1639
1640 if ((fd = open(Lun_File.c_str(), O_WRONLY)) < 0) {
1641 LOGE("Unable to open ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
1642 return false;
1643 }
1644
1645 if (write(fd, Part->Actual_Block_Device.c_str(), Part->Actual_Block_Device.size()) < 0) {
1646 LOGE("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
1647 close(fd);
1648 return false;
1649 }
1650 close(fd);
1651 return true;
1652}
1653
Dees_Troy8170a922012-09-18 15:40:25 -04001654int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001655 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001656 char lun_file[255];
Dees_Troy8170a922012-09-18 15:40:25 -04001657 string ext_path;
Dees_Troyd21618c2012-10-14 18:48:49 -04001658 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001659
1660 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1661 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1662 if (has_dual == 1 && has_data_media == 0) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001663 string Lun_File_str = CUSTOM_LUN_FILE;
1664 size_t found = Lun_File_str.find("%");
1665 if (found != string::npos) {
1666 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1667 if (TWFunc::Path_Exists(lun_file))
1668 has_multiple_lun = true;
1669 }
1670 if (!has_multiple_lun) {
Dees_Troyf4ca6122012-10-13 22:17:20 -04001671 // Device doesn't have multiple lun files, mount current storage
Dees_Troyf4ca6122012-10-13 22:17:20 -04001672 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001673 return Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001674 } else {
1675 // Device has multiple lun files
Dees_Troyf4ca6122012-10-13 22:17:20 -04001676 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001677 if (!Open_Lun_File(DataManager::GetSettingsStoragePath(), lun_file))
Dees_Troyf4ca6122012-10-13 22:17:20 -04001678 return false;
Dees_Troyf4ca6122012-10-13 22:17:20 -04001679 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001680 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Dees_Troyd21618c2012-10-14 18:48:49 -04001681 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001682 }
Dees_Troy8170a922012-09-18 15:40:25 -04001683 } else {
1684 if (has_data_media == 0)
1685 ext_path = DataManager::GetCurrentStoragePath();
1686 else
1687 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001688 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001689 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001690 }
1691 return true;
1692}
1693
1694int TWPartitionManager::usb_storage_disable(void) {
1695 int fd, index;
1696 char lun_file[255];
1697
1698 for (index=0; index<2; index++) {
1699 sprintf(lun_file, CUSTOM_LUN_FILE, index);
1700
1701 if ((fd = open(lun_file, O_WRONLY)) < 0) {
Dees_Troye58d5262012-09-21 12:27:57 -04001702 Mount_All_Storage();
1703 Update_System_Details();
1704 if (index == 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001705 LOGE("Unable to open ums lunfile '%s': (%s)", lun_file, strerror(errno));
Dees_Troye58d5262012-09-21 12:27:57 -04001706 return false;
1707 } else
1708 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001709 }
1710
1711 char ch = 0;
1712 if (write(fd, &ch, 1) < 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001713 close(fd);
Dees_Troye58d5262012-09-21 12:27:57 -04001714 Mount_All_Storage();
1715 Update_System_Details();
1716 if (index == 0) {
1717 LOGE("Unable to write to ums lunfile '%s': (%s)", lun_file, strerror(errno));
1718 return false;
1719 } else
1720 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001721 }
1722
1723 close(fd);
1724 }
Dees_Troye58d5262012-09-21 12:27:57 -04001725 Mount_All_Storage();
1726 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001727 UnMount_Main_Partitions();
Dees_Troy8170a922012-09-18 15:40:25 -04001728 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001729}
1730
1731void TWPartitionManager::Mount_All_Storage(void) {
1732 std::vector<TWPartition*>::iterator iter;
1733
1734 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1735 if ((*iter)->Is_Storage)
1736 (*iter)->Mount(false);
1737 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001738}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001739
Dees_Troyd0384ef2012-10-12 12:15:42 -04001740void TWPartitionManager::UnMount_Main_Partitions(void) {
1741 // Unmounts system and data if data is not data/media
1742 // Also unmounts boot if boot is mountable
1743 LOGI("Unmounting main partitions...\n");
1744
1745 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1746
1747 UnMount_By_Path("/system", true);
1748#ifndef RECOVERY_SDCARD_ON_DATA
1749 UnMount_By_Path("/data", true);
1750#endif
1751 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1752 Boot_Partition->UnMount(true);
1753}
1754
Dees_Troy9350b8d2012-09-27 12:38:38 -04001755int TWPartitionManager::Partition_SDCard(void) {
1756 char mkdir_path[255], temp[255], line[512];
1757 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1758 int ext, swap, total_size = 0, fat_size;
1759 FILE* fp;
1760
1761 ui_print("Partitioning SD Card...\n");
1762#ifdef TW_EXTERNAL_STORAGE_PATH
1763 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1764#else
1765 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1766#endif
1767 if (SDCard == NULL) {
1768 LOGE("Unable to locate device to partition.\n");
1769 return false;
1770 }
1771 if (!SDCard->UnMount(true))
1772 return false;
1773 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1774 if (SDext != NULL) {
1775 if (!SDext->UnMount(true))
1776 return false;
1777 }
1778 system("umount \"$SWAPPATH\"");
1779 Device = SDCard->Actual_Block_Device;
1780 // Just use the root block device
1781 Device.resize(strlen("/dev/block/mmcblkX"));
1782
1783 // Find the size of the block device:
1784 fp = fopen("/proc/partitions", "rt");
1785 if (fp == NULL) {
1786 LOGE("Unable to open /proc/partitions\n");
1787 return false;
1788 }
1789
1790 while (fgets(line, sizeof(line), fp) != NULL)
1791 {
1792 unsigned long major, minor, blocks;
1793 char device[512];
1794 char tmpString[64];
1795
1796 if (strlen(line) < 7 || line[0] == 'm') continue;
1797 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1798
1799 tmpdevice = "/dev/block/";
1800 tmpdevice += device;
1801 if (tmpdevice == Device) {
1802 // Adjust block size to byte size
1803 total_size = (int)(blocks * 1024ULL / 1000000LLU);
1804 break;
1805 }
1806 }
1807 fclose(fp);
1808
1809 DataManager::GetValue("tw_sdext_size", ext);
1810 DataManager::GetValue("tw_swap_size", swap);
1811 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1812 fat_size = total_size - ext - swap;
1813 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);
1814 memset(temp, 0, sizeof(temp));
1815 sprintf(temp, "%i", fat_size);
1816 fat_str = temp;
1817 memset(temp, 0, sizeof(temp));
1818 sprintf(temp, "%i", fat_size + ext);
1819 ext_str = temp;
1820 memset(temp, 0, sizeof(temp));
1821 sprintf(temp, "%i", fat_size + ext + swap);
1822 swap_str = temp;
1823 if (ext + swap > total_size) {
1824 LOGE("EXT + Swap size is larger than sdcard size.\n");
1825 return false;
1826 }
1827 ui_print("Removing partition table...\n");
1828 Command = "parted -s " + Device + " mklabel msdos";
1829 LOGI("Command is: '%s'\n", Command.c_str());
1830 if (system(Command.c_str()) != 0) {
1831 LOGE("Unable to remove partition table.\n");
1832 Update_System_Details();
1833 return false;
1834 }
1835 ui_print("Creating FAT32 partition...\n");
1836 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
1837 LOGI("Command is: '%s'\n", Command.c_str());
1838 if (system(Command.c_str()) != 0) {
1839 LOGE("Unable to create FAT32 partition.\n");
1840 return false;
1841 }
1842 if (ext > 0) {
1843 ui_print("Creating EXT partition...\n");
1844 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
1845 LOGI("Command is: '%s'\n", Command.c_str());
1846 if (system(Command.c_str()) != 0) {
1847 LOGE("Unable to create EXT partition.\n");
1848 Update_System_Details();
1849 return false;
1850 }
1851 }
1852 if (swap > 0) {
1853 ui_print("Creating swap partition...\n");
1854 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
1855 LOGI("Command is: '%s'\n", Command.c_str());
1856 if (system(Command.c_str()) != 0) {
1857 LOGE("Unable to create swap partition.\n");
1858 Update_System_Details();
1859 return false;
1860 }
1861 }
1862 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1863#ifdef TW_EXTERNAL_STORAGE_PATH
1864 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1865 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1866 memset(mkdir_path, 0, sizeof(mkdir_path));
1867 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1868#else
1869 Mount_By_Path("/sdcard", 1);
1870 strcpy(mkdir_path, "/sdcard/TWRP");
1871#endif
1872 mkdir(mkdir_path, 0777);
1873 DataManager::Flush();
1874#ifdef TW_EXTERNAL_STORAGE_PATH
1875 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1876 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1877 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1878#else
1879 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1880 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1881 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1882#endif
1883 if (ext > 0) {
1884 if (SDext == NULL) {
1885 LOGE("Unable to locate sd-ext partition.\n");
1886 return false;
1887 }
1888 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
1889 ui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1890 LOGI("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
1891 system(Command.c_str());
1892 }
1893
1894 Update_System_Details();
1895 ui_print("Partitioning complete.\n");
1896 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001897}