blob: 536e867803999716e36dd373c63d5d496bf259b5 [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
Gary Peck004d48b2012-11-21 16:28:18 -0800132 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 -0400133 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800134 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 -0400135 }
Gary Peck004d48b2012-11-21 16:28:18 -0800136 printf("\n Flags: ");
137 if (Part->Can_Be_Wiped)
138 printf("Can_Be_Wiped ");
139 if (Part->Wipe_During_Factory_Reset)
140 printf("Wipe_During_Factory_Reset ");
141 if (Part->Wipe_Available_in_GUI)
142 printf("Wipe_Available_in_GUI ");
143 if (Part->Is_SubPartition)
144 printf("Is_SubPartition ");
145 if (Part->Has_SubPartition)
146 printf("Has_SubPartition ");
147 if (Part->Removable)
148 printf("Removable ");
149 if (Part->Is_Present)
150 printf("IsPresent ");
151 if (Part->Can_Be_Encrypted)
152 printf("Can_Be_Encrypted ");
153 if (Part->Is_Encrypted)
154 printf("Is_Encrypted ");
155 if (Part->Is_Decrypted)
156 printf("Is_Decrypted ");
157 if (Part->Has_Data_Media)
158 printf("Has_Data_Media ");
159 if (Part->Has_Android_Secure)
160 printf("Has_Android_Secure ");
161 if (Part->Is_Storage)
162 printf("Is_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000163 if (Part->Ignore_Blkid)
164 printf("Ignore_Blkid ");
Gary Peck004d48b2012-11-21 16:28:18 -0800165 printf("\n");
166 if (!Part->SubPartition_Of.empty())
167 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
168 if (!Part->Symlink_Path.empty())
169 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
170 if (!Part->Symlink_Mount_Point.empty())
171 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
172 if (!Part->Primary_Block_Device.empty())
173 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
174 if (!Part->Alternate_Block_Device.empty())
175 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
176 if (!Part->Decrypted_Block_Device.empty())
177 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
178 if (Part->Length != 0)
179 printf(" Length: %i\n", Part->Length);
180 if (!Part->Display_Name.empty())
181 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
182 if (!Part->Backup_Path.empty())
183 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
184 if (!Part->Backup_Name.empty())
185 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
186 if (!Part->Backup_FileName.empty())
187 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
188 if (!Part->Storage_Path.empty())
189 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
190 if (!Part->Current_File_System.empty())
191 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
192 if (!Part->Fstab_File_System.empty())
193 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
194 if (Part->Format_Block_Size != 0)
195 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400196 if (!Part->MTD_Name.empty())
197 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400198 string back_meth = Part->Backup_Method_By_Name();
199 printf(" Backup_Method: %s\n\n", back_meth.c_str());
200}
201
Dees_Troy51a0e822012-09-05 15:24:24 -0400202int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400203 std::vector<TWPartition*>::iterator iter;
204 int ret = false;
205 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400206 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400207
Dees_Troy43d8b002012-09-17 16:00:01 -0400208 if (Local_Path == "/tmp")
209 return true;
210
Dees_Troy5bf43922012-09-07 16:07:55 -0400211 // Iterate through all partitions
212 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400213 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400214 ret = (*iter)->Mount(Display_Error);
215 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400216 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400217 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400218 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400219 }
220 if (found) {
221 return ret;
222 } else if (Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400223 LOGE("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400224 } else {
Dees_Troy51127312012-09-08 13:08:49 -0400225 LOGI("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400226 }
227 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400228}
229
230int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400231 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400232
Dees_Troy51127312012-09-08 13:08:49 -0400233 if (Part) {
234 if (Part->Has_SubPartition) {
235 std::vector<TWPartition*>::iterator subpart;
236
237 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
238 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
239 (*subpart)->Mount(Display_Error);
240 }
241 return Part->Mount(Display_Error);
242 } else
243 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400244 }
245 if (Display_Error)
Dees_Troy51127312012-09-08 13:08:49 -0400246 LOGE("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400247 else
Dees_Troy51127312012-09-08 13:08:49 -0400248 LOGI("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400249 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400250}
251
252int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400253 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400254
Dees_Troy51127312012-09-08 13:08:49 -0400255 if (Part) {
256 if (Part->Has_SubPartition) {
257 std::vector<TWPartition*>::iterator subpart;
258
259 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
260 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
261 (*subpart)->Mount(Display_Error);
262 }
263 return Part->Mount(Display_Error);
264 } else
265 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400266 }
267 if (Display_Error)
Dees_Troy51127312012-09-08 13:08:49 -0400268 LOGE("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400269 else
Dees_Troy51127312012-09-08 13:08:49 -0400270 LOGI("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400271 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400272}
273
274int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400275 std::vector<TWPartition*>::iterator iter;
276 int ret = false;
277 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400278 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400279
280 // Iterate through all partitions
281 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400282 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400283 ret = (*iter)->UnMount(Display_Error);
284 found = true;
285 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
286 (*iter)->UnMount(Display_Error);
287 }
288 }
289 if (found) {
290 return ret;
291 } else if (Display_Error) {
292 LOGE("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
293 } else {
294 LOGI("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
295 }
296 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400297}
298
299int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400300 TWPartition* Part = Find_Partition_By_Block(Block);
301
302 if (Part) {
303 if (Part->Has_SubPartition) {
304 std::vector<TWPartition*>::iterator subpart;
305
306 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
307 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
308 (*subpart)->UnMount(Display_Error);
309 }
310 return Part->UnMount(Display_Error);
311 } else
312 return Part->UnMount(Display_Error);
313 }
314 if (Display_Error)
315 LOGE("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
316 else
317 LOGI("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
318 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400319}
320
321int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400322 TWPartition* Part = Find_Partition_By_Name(Name);
323
324 if (Part) {
325 if (Part->Has_SubPartition) {
326 std::vector<TWPartition*>::iterator subpart;
327
328 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
329 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
330 (*subpart)->UnMount(Display_Error);
331 }
332 return Part->UnMount(Display_Error);
333 } else
334 return Part->UnMount(Display_Error);
335 }
336 if (Display_Error)
337 LOGE("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
338 else
339 LOGI("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
340 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400341}
342
343int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400344 TWPartition* Part = Find_Partition_By_Path(Path);
345
346 if (Part)
347 return Part->Is_Mounted();
348 else
349 LOGI("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
350 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400351}
352
353int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400354 TWPartition* Part = Find_Partition_By_Block(Block);
355
356 if (Part)
357 return Part->Is_Mounted();
358 else
359 LOGI("Is_Mounted: Unable to find partition for block '%s'\n", Block.c_str());
360 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400361}
362
363int TWPartitionManager::Is_Mounted_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400364 TWPartition* Part = Find_Partition_By_Name(Name);
365
366 if (Part)
367 return Part->Is_Mounted();
368 else
369 LOGI("Is_Mounted: Unable to find partition for name '%s'\n", Name.c_str());
370 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400371}
372
Dees_Troy5bf43922012-09-07 16:07:55 -0400373int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400374 string current_storage_path = DataManager::GetCurrentStoragePath();
375
376 if (Mount_By_Path(current_storage_path, Display_Error)) {
377 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
378 if (FreeStorage)
379 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
380 return true;
381 }
382 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400383}
384
Dees_Troy5bf43922012-09-07 16:07:55 -0400385int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
386 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
387}
388
389TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
390 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400391 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400392
393 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400394 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400395 return (*iter);
396 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400397 return NULL;
398}
399
Dees_Troy5bf43922012-09-07 16:07:55 -0400400TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400401 std::vector<TWPartition*>::iterator iter;
402
403 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400404 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 -0400405 return (*iter);
406 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400407 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400408}
409
410TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400411 std::vector<TWPartition*>::iterator iter;
412
413 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
414 if ((*iter)->Display_Name == Name)
415 return (*iter);
416 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400417 return NULL;
418}
Dees_Troy51a0e822012-09-05 15:24:24 -0400419
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400420int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
421 // Check the backup name to ensure that it is the correct size and contains only valid characters
422 // and that a backup with that name doesn't already exist
423 char backup_name[MAX_BACKUP_NAME_LEN];
424 char backup_loc[255], tw_image_dir[255];
425 int copy_size;
426 int index, cur_char;
427 string Backup_Name, Backup_Loc;
428
429 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
430 copy_size = Backup_Name.size();
431 // Check size
432 if (copy_size > MAX_BACKUP_NAME_LEN) {
433 if (Display_Error)
434 LOGE("Backup name is too long.\n");
435 return -2;
436 }
437
438 // Check each character
439 strncpy(backup_name, Backup_Name.c_str(), copy_size);
440 if (strcmp(backup_name, "0") == 0)
441 return 0; // A "0" (zero) means to use the current timestamp for the backup name
442 for (index=0; index<copy_size; index++) {
443 cur_char = (int)backup_name[index];
444 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) {
445 // These are valid characters
446 // Numbers
447 // Upper case letters
448 // Lower case letters
449 // Space
450 // and -_.{}[]
451 } else {
452 if (Display_Error)
453 LOGE("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
454 return -3;
455 }
456 }
457
458 // Check to make sure that a backup with this name doesn't already exist
459 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
460 strcpy(backup_loc, Backup_Loc.c_str());
461 sprintf(tw_image_dir,"%s/%s/.", backup_loc, backup_name);
462 if (TWFunc::Path_Exists(tw_image_dir)) {
463 if (Display_Error)
464 LOGE("A backup with this name already exists.\n");
465 return -4;
466 }
467
468 // No problems found, return 0
469 return 0;
470}
471
Dees_Troy43d8b002012-09-17 16:00:01 -0400472bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
473{
474 char command[512];
475 string Full_File = Backup_Folder + Backup_Filename;
476
Dees_Troy4a2a1262012-09-18 09:33:47 -0400477 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400478 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400479
Dees_Troyb46a6842012-09-25 11:06:46 -0400480 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troyc51f1f92012-09-20 15:32:13 -0400481 ui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400482
483 if (TWFunc::Path_Exists(Full_File)) {
484 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 -0400485 if (system(command) == 0) {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400486 ui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400487 return true;
488 } else {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400489 ui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400490 return false;
491 }
492 } else {
493 char filename[512];
494 int index = 0;
495
496 sprintf(filename, "%s%03i", Full_File.c_str(), index);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400497 while (TWFunc::Path_Exists(filename) == true) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400498 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 -0400499 if (system(command) != 0) {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400500 ui_print(" * MD5 Error.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400501 return false;
502 }
503 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400504 sprintf(filename, "%s%03i", Full_File.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400505 }
506 if (index == 0) {
507 LOGE("Backup file: '%s' not found!\n", filename);
508 return false;
509 }
Dees_Troyc5865ab2012-09-24 15:08:04 -0400510 ui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400511 }
512 return true;
513}
514
Dees_Troy093b7642012-09-21 15:59:38 -0400515bool 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 -0400516 time_t start, stop;
Dees_Troy093b7642012-09-21 15:59:38 -0400517 int img_bps, file_bps;
518 unsigned long total_time, remain_time, section_time;
519 int use_compression, backup_time;
520 float pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400521
522 if (Part == NULL)
523 return true;
524
Dees_Troy093b7642012-09-21 15:59:38 -0400525 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
526
527 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
528 if (use_compression)
529 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
530 else
531 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
532
533 // We know the speed for both, how far into the whole backup are we, based on time
534 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
535 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
536
537 pos = (total_time - remain_time) / (float) total_time;
538 ui->SetProgress(pos);
539
540 LOGI("Estimated Total time: %lu Estimated remaining time: %lu\n", total_time, remain_time);
541
542 // And get the time
543 if (Part->Backup_Method == 1)
544 section_time = Part->Backup_Size / file_bps;
545 else
546 section_time = Part->Backup_Size / img_bps;
547
548 // Set the position
549 pos = section_time / (float) total_time;
550 ui->ShowProgress(pos, section_time);
551
Dees_Troy43d8b002012-09-17 16:00:01 -0400552 time(&start);
553
554 if (Part->Backup(Backup_Folder)) {
Dees_Troy8170a922012-09-18 15:40:25 -0400555 if (Part->Has_SubPartition) {
556 std::vector<TWPartition*>::iterator subpart;
557
558 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
559 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
560 if (!(*subpart)->Backup(Backup_Folder))
561 return false;
562 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
563 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400564 if (Part->Backup_Method == 1) {
565 *file_bytes_remaining -= (*subpart)->Backup_Size;
566 } else {
567 *img_bytes_remaining -= (*subpart)->Backup_Size;
568 }
Dees_Troy8170a922012-09-18 15:40:25 -0400569 }
570 }
571 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400572 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400573 backup_time = (int) difftime(stop, start);
574 LOGI("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400575 if (Part->Backup_Method == 1) {
576 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400577 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400578 } else {
579 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400580 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400581 }
582 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
583 } else {
584 return false;
585 }
586}
587
588int TWPartitionManager::Run_Backup(void) {
589 int check, do_md5, partition_count = 0;
590 string Backup_Folder, Backup_Name, Full_Backup_Path;
Dees_Troy8170a922012-09-18 15:40:25 -0400591 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 -0400592 unsigned long img_time = 0, file_time = 0;
593 TWPartition* backup_sys = NULL;
594 TWPartition* backup_data = NULL;
595 TWPartition* backup_cache = NULL;
596 TWPartition* backup_recovery = NULL;
597 TWPartition* backup_boot = NULL;
598 TWPartition* backup_andsec = NULL;
599 TWPartition* backup_sdext = NULL;
600 TWPartition* backup_sp1 = NULL;
601 TWPartition* backup_sp2 = NULL;
602 TWPartition* backup_sp3 = NULL;
603 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400604 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400605 struct tm *t;
606 time_t start, stop, seconds, total_start, total_stop;
607 seconds = time(0);
608 t = localtime(&seconds);
609
610 time(&total_start);
611
612 Update_System_Details();
613
614 if (!Mount_Current_Storage(true))
615 return false;
616
617 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400618 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400619 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400620 else
621 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400622
Dees_Troy43d8b002012-09-17 16:00:01 -0400623 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
624 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400625 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name.empty()) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400626 char timestamp[255];
627 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);
628 Backup_Name = timestamp;
629 }
630 LOGI("Backup Name is: '%s'\n", Backup_Name.c_str());
631 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
632 LOGI("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
633
634 ui_print("\n[BACKUP STARTED]\n");
635 ui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
636 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
637 LOGE("Failed to make backup folder.\n");
638 return false;
639 }
640
641 LOGI("Calculating backup details...\n");
642 DataManager::GetValue(TW_BACKUP_SYSTEM_VAR, check);
643 if (check) {
644 backup_sys = Find_Partition_By_Path("/system");
645 if (backup_sys != NULL) {
646 partition_count++;
647 if (backup_sys->Backup_Method == 1)
648 file_bytes += backup_sys->Backup_Size;
649 else
650 img_bytes += backup_sys->Backup_Size;
651 } else {
652 LOGE("Unable to locate system partition.\n");
653 return false;
654 }
655 }
656 DataManager::GetValue(TW_BACKUP_DATA_VAR, check);
657 if (check) {
658 backup_data = Find_Partition_By_Path("/data");
659 if (backup_data != NULL) {
660 partition_count++;
Dees_Troy8170a922012-09-18 15:40:25 -0400661 subpart_size = 0;
662 if (backup_data->Has_SubPartition) {
663 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
664 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == backup_data->Mount_Point)
665 subpart_size += (*subpart)->Backup_Size;
666 }
667 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400668 if (backup_data->Backup_Method == 1)
Dees_Troy8170a922012-09-18 15:40:25 -0400669 file_bytes += backup_data->Backup_Size + subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400670 else
Dees_Troy8170a922012-09-18 15:40:25 -0400671 img_bytes += backup_data->Backup_Size + subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400672 } else {
673 LOGE("Unable to locate data partition.\n");
674 return false;
675 }
676 }
677 DataManager::GetValue(TW_BACKUP_CACHE_VAR, check);
678 if (check) {
679 backup_cache = Find_Partition_By_Path("/cache");
680 if (backup_cache != NULL) {
681 partition_count++;
682 if (backup_cache->Backup_Method == 1)
683 file_bytes += backup_cache->Backup_Size;
684 else
685 img_bytes += backup_cache->Backup_Size;
686 } else {
687 LOGE("Unable to locate cache partition.\n");
688 return false;
689 }
690 }
691 DataManager::GetValue(TW_BACKUP_RECOVERY_VAR, check);
692 if (check) {
693 backup_recovery = Find_Partition_By_Path("/recovery");
694 if (backup_recovery != NULL) {
695 partition_count++;
696 if (backup_recovery->Backup_Method == 1)
697 file_bytes += backup_recovery->Backup_Size;
698 else
699 img_bytes += backup_recovery->Backup_Size;
700 } else {
701 LOGE("Unable to locate recovery partition.\n");
702 return false;
703 }
704 }
705 DataManager::GetValue(TW_BACKUP_BOOT_VAR, check);
706 if (check) {
707 backup_boot = Find_Partition_By_Path("/boot");
708 if (backup_boot != NULL) {
709 partition_count++;
710 if (backup_boot->Backup_Method == 1)
711 file_bytes += backup_boot->Backup_Size;
712 else
713 img_bytes += backup_boot->Backup_Size;
714 } else {
715 LOGE("Unable to locate boot partition.\n");
716 return false;
717 }
718 }
719 DataManager::GetValue(TW_BACKUP_ANDSEC_VAR, check);
720 if (check) {
721 backup_andsec = Find_Partition_By_Path("/and-sec");
722 if (backup_andsec != NULL) {
723 partition_count++;
724 if (backup_andsec->Backup_Method == 1)
725 file_bytes += backup_andsec->Backup_Size;
726 else
727 img_bytes += backup_andsec->Backup_Size;
728 } else {
729 LOGE("Unable to locate android secure partition.\n");
730 return false;
731 }
732 }
733 DataManager::GetValue(TW_BACKUP_SDEXT_VAR, check);
734 if (check) {
735 backup_sdext = Find_Partition_By_Path("/sd-ext");
736 if (backup_sdext != NULL) {
737 partition_count++;
738 if (backup_sdext->Backup_Method == 1)
739 file_bytes += backup_sdext->Backup_Size;
740 else
741 img_bytes += backup_sdext->Backup_Size;
742 } else {
743 LOGE("Unable to locate sd-ext partition.\n");
744 return false;
745 }
746 }
747#ifdef SP1_NAME
748 DataManager::GetValue(TW_BACKUP_SP1_VAR, check);
749 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400750 backup_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400751 if (backup_sp1 != NULL) {
752 partition_count++;
753 if (backup_sp1->Backup_Method == 1)
754 file_bytes += backup_sp1->Backup_Size;
755 else
756 img_bytes += backup_sp1->Backup_Size;
757 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400758 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400759 return false;
760 }
761 }
762#endif
763#ifdef SP2_NAME
764 DataManager::GetValue(TW_BACKUP_SP2_VAR, check);
765 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400766 backup_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400767 if (backup_sp2 != NULL) {
768 partition_count++;
769 if (backup_sp2->Backup_Method == 1)
770 file_bytes += backup_sp2->Backup_Size;
771 else
772 img_bytes += backup_sp2->Backup_Size;
773 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400774 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400775 return false;
776 }
777 }
778#endif
779#ifdef SP3_NAME
780 DataManager::GetValue(TW_BACKUP_SP3_VAR, check);
781 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400782 backup_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400783 if (backup_sp3 != NULL) {
784 partition_count++;
785 if (backup_sp3->Backup_Method == 1)
786 file_bytes += backup_sp3->Backup_Size;
787 else
788 img_bytes += backup_sp3->Backup_Size;
789 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400790 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400791 return false;
792 }
793 }
794#endif
795
796 if (partition_count == 0) {
797 ui_print("No partitions selected for backup.\n");
798 return false;
799 }
800 total_bytes = file_bytes + img_bytes;
801 ui_print(" * Total number of partitions to back up: %d\n", partition_count);
802 ui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
803 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
804 if (storage != NULL) {
805 free_space = storage->Free;
806 ui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
807 } else {
808 LOGE("Unable to locate storage device.\n");
809 return false;
810 }
811 if (free_space + (32 * 1024 * 1024) < total_bytes) {
812 // We require an extra 32MB just in case
813 LOGE("Not enough free space on storage.\n");
814 return false;
815 }
816 img_bytes_remaining = img_bytes;
817 file_bytes_remaining = file_bytes;
818
Dees_Troy093b7642012-09-21 15:59:38 -0400819 ui->SetProgress(0.0);
820
821 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 -0400822 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400823 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 -0400824 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400825 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 -0400826 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400827 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 -0400828 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400829 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 -0400830 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400831 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 -0400832 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400833 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 -0400834 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400835 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 -0400836 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400837 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 -0400838 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400839 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 -0400840 return false;
841
842 // Average BPS
843 if (img_time == 0)
844 img_time = 1;
845 if (file_time == 0)
846 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400847 int img_bps = (int)img_bytes / (int)img_time;
848 int file_bps = (int)file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400849
850 ui_print("Average backup rate for file systems: %lu MB/sec\n", (file_bps / (1024 * 1024)));
851 ui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
852
853 time(&total_stop);
854 int total_time = (int) difftime(total_stop, total_start);
855 unsigned long long actual_backup_size = TWFunc::Get_Folder_Size(Full_Backup_Path, true);
856 actual_backup_size /= (1024LLU * 1024LLU);
857
Dees_Troy093b7642012-09-21 15:59:38 -0400858 int prev_img_bps, prev_file_bps, use_compression;
859 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
860 img_bps += (prev_img_bps * 4);
861 img_bps /= 5;
862
863 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
864 if (use_compression)
865 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
866 else
867 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
868 file_bps += (prev_file_bps * 4);
869 file_bps /= 5;
870
871 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
872 if (use_compression)
873 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
874 else
875 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
876
Dees_Troy43d8b002012-09-17 16:00:01 -0400877 ui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
878 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400879 UnMount_Main_Partitions();
Dees_Troy43d8b002012-09-17 16:00:01 -0400880 ui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
881 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400882}
883
Dees_Troy093b7642012-09-21 15:59:38 -0400884bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400885 time_t Start, Stop;
886 time(&Start);
Dees_Troy093b7642012-09-21 15:59:38 -0400887 ui->ShowProgress(1.0 / (float)partition_count, 150);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400888 if (!Part->Restore(Restore_Name))
889 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400890 if (Part->Has_SubPartition) {
891 std::vector<TWPartition*>::iterator subpart;
892
893 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
894 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
895 if (!(*subpart)->Restore(Restore_Name))
896 return false;
897 }
898 }
899 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400900 time(&Stop);
901 ui_print("[%s done (%d seconds)]\n\n", Part->Display_Name.c_str(), (int)difftime(Stop, Start));
902 return true;
903}
904
Dees_Troy51a0e822012-09-05 15:24:24 -0400905int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400906 int check_md5, check, partition_count = 0;
907 TWPartition* restore_sys = NULL;
908 TWPartition* restore_data = NULL;
909 TWPartition* restore_cache = NULL;
910 TWPartition* restore_boot = NULL;
911 TWPartition* restore_andsec = NULL;
912 TWPartition* restore_sdext = NULL;
913 TWPartition* restore_sp1 = NULL;
914 TWPartition* restore_sp2 = NULL;
915 TWPartition* restore_sp3 = NULL;
916 time_t rStart, rStop;
917 time(&rStart);
Dees_Troy43d8b002012-09-17 16:00:01 -0400918
Dees_Troy4a2a1262012-09-18 09:33:47 -0400919 ui_print("\n[RESTORE STARTED]\n\n");
920 ui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400921
Dees_Troy4a2a1262012-09-18 09:33:47 -0400922 if (!Mount_Current_Storage(true))
923 return false;
924
925 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
926 DataManager::GetValue(TW_RESTORE_SYSTEM_VAR, check);
Dees_Troy63c8df72012-09-10 14:02:05 -0400927 if (check > 0) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400928 restore_sys = Find_Partition_By_Path("/system");
929 if (restore_sys == NULL) {
930 LOGE("Unable to locate system partition.\n");
931 return false;
932 }
933 partition_count++;
934 }
935 DataManager::GetValue(TW_RESTORE_DATA_VAR, check);
936 if (check > 0) {
937 restore_data = Find_Partition_By_Path("/data");
938 if (restore_data == NULL) {
939 LOGE("Unable to locate data partition.\n");
940 return false;
941 }
942 partition_count++;
943 }
944 DataManager::GetValue(TW_RESTORE_CACHE_VAR, check);
945 if (check > 0) {
946 restore_cache = Find_Partition_By_Path("/cache");
947 if (restore_cache == NULL) {
948 LOGE("Unable to locate cache partition.\n");
949 return false;
950 }
951 partition_count++;
952 }
953 DataManager::GetValue(TW_RESTORE_BOOT_VAR, check);
954 if (check > 0) {
955 restore_boot = Find_Partition_By_Path("/boot");
956 if (restore_boot == NULL) {
957 LOGE("Unable to locate boot partition.\n");
958 return false;
959 }
960 partition_count++;
961 }
962 DataManager::GetValue(TW_RESTORE_ANDSEC_VAR, check);
963 if (check > 0) {
964 restore_andsec = Find_Partition_By_Path("/and-sec");
965 if (restore_andsec == NULL) {
966 LOGE("Unable to locate android secure partition.\n");
967 return false;
968 }
969 partition_count++;
970 }
971 DataManager::GetValue(TW_RESTORE_SDEXT_VAR, check);
972 if (check > 0) {
973 restore_sdext = Find_Partition_By_Path("/sd-ext");
974 if (restore_sdext == NULL) {
975 LOGE("Unable to locate sd-ext partition.\n");
976 return false;
977 }
978 partition_count++;
979 }
980#ifdef SP1_NAME
981 DataManager::GetValue(TW_RESTORE_SP1_VAR, check);
982 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400983 restore_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400984 if (restore_sp1 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400985 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400986 return false;
987 }
988 partition_count++;
989 }
990#endif
991#ifdef SP2_NAME
992 DataManager::GetValue(TW_RESTORE_SP2_VAR, check);
993 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400994 restore_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400995 if (restore_sp2 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400996 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400997 return false;
998 }
999 partition_count++;
1000 }
1001#endif
1002#ifdef SP3_NAME
1003 DataManager::GetValue(TW_RESTORE_SP3_VAR, check);
1004 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001005 restore_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001006 if (restore_sp3 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001007 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001008 return false;
1009 }
1010 partition_count++;
1011 }
1012#endif
1013
1014 if (partition_count == 0) {
1015 LOGE("No partitions selected for restore.\n");
1016 return false;
1017 }
1018
1019 if (check_md5 > 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001020 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
Dees_Troyb46a6842012-09-25 11:06:46 -04001021 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001022 ui_print("Verifying MD5...\n");
1023 if (restore_sys != NULL && !restore_sys->Check_MD5(Restore_Name))
1024 return false;
1025 if (restore_data != NULL && !restore_data->Check_MD5(Restore_Name))
1026 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001027 if (restore_data != NULL && restore_data->Has_SubPartition) {
1028 std::vector<TWPartition*>::iterator subpart;
1029
1030 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1031 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_data->Mount_Point) {
1032 if (!(*subpart)->Check_MD5(Restore_Name))
1033 return false;
1034 }
1035 }
1036 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001037 if (restore_cache != NULL && !restore_cache->Check_MD5(Restore_Name))
1038 return false;
1039 if (restore_boot != NULL && !restore_boot->Check_MD5(Restore_Name))
1040 return false;
1041 if (restore_andsec != NULL && !restore_andsec->Check_MD5(Restore_Name))
1042 return false;
1043 if (restore_sdext != NULL && !restore_sdext->Check_MD5(Restore_Name))
1044 return false;
1045 if (restore_sp1 != NULL && !restore_sp1->Check_MD5(Restore_Name))
1046 return false;
1047 if (restore_sp2 != NULL && !restore_sp2->Check_MD5(Restore_Name))
1048 return false;
1049 if (restore_sp3 != NULL && !restore_sp3->Check_MD5(Restore_Name))
1050 return false;
1051 ui_print("Done verifying MD5.\n");
1052 } else
1053 ui_print("Skipping MD5 check based on user setting.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001054
Dees_Troy4a2a1262012-09-18 09:33:47 -04001055 ui_print("Restoring %i partitions...\n", partition_count);
Dees_Troy093b7642012-09-21 15:59:38 -04001056 ui->SetProgress(0.0);
1057 if (restore_sys != NULL && !Restore_Partition(restore_sys, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001058 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001059 if (restore_data != NULL && !Restore_Partition(restore_data, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001060 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001061 if (restore_cache != NULL && !Restore_Partition(restore_cache, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001062 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001063 if (restore_boot != NULL && !Restore_Partition(restore_boot, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001064 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001065 if (restore_andsec != NULL && !Restore_Partition(restore_andsec, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001066 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001067 if (restore_sdext != NULL && !Restore_Partition(restore_sdext, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001068 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001069 if (restore_sp1 != NULL && !Restore_Partition(restore_sp1, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001070 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001071 if (restore_sp2 != NULL && !Restore_Partition(restore_sp2, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001072 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001073 if (restore_sp3 != NULL && !Restore_Partition(restore_sp3, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001074 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001075
Dees_Troyb46a6842012-09-25 11:06:46 -04001076 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -04001077 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001078 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -04001079 time(&rStop);
1080 ui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -04001081 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001082}
1083
1084void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001085 // Start with the default values
1086 int tw_restore_system = -1;
1087 int tw_restore_data = -1;
1088 int tw_restore_cache = -1;
1089 int tw_restore_recovery = -1;
1090 int tw_restore_boot = -1;
1091 int tw_restore_andsec = -1;
1092 int tw_restore_sdext = -1;
1093 int tw_restore_sp1 = -1;
1094 int tw_restore_sp2 = -1;
1095 int tw_restore_sp3 = -1;
1096 bool get_date = true;
1097
1098 DIR* d;
1099 d = opendir(Restore_Name.c_str());
1100 if (d == NULL)
1101 {
1102 LOGE("Error opening %s\n", Restore_Name.c_str());
1103 return;
1104 }
1105
1106 struct dirent* de;
1107 while ((de = readdir(d)) != NULL)
1108 {
1109 // Strip off three components
1110 char str[256];
1111 char* label;
1112 char* fstype = NULL;
1113 char* extn = NULL;
1114 char* ptr;
1115
1116 strcpy(str, de->d_name);
1117 if (strlen(str) <= 2)
1118 continue;
1119
1120 if (get_date) {
1121 char file_path[255];
1122 struct stat st;
1123
1124 strcpy(file_path, Restore_Name.c_str());
1125 strcat(file_path, "/");
1126 strcat(file_path, str);
1127 stat(file_path, &st);
1128 string backup_date = ctime((const time_t*)(&st.st_mtime));
1129 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
1130 get_date = false;
1131 }
1132
1133 label = str;
1134 ptr = label;
1135 while (*ptr && *ptr != '.') ptr++;
1136 if (*ptr == '.')
1137 {
1138 *ptr = 0x00;
1139 ptr++;
1140 fstype = ptr;
1141 }
1142 while (*ptr && *ptr != '.') ptr++;
1143 if (*ptr == '.')
1144 {
1145 *ptr = 0x00;
1146 ptr++;
1147 extn = ptr;
1148 }
1149
1150 if (extn == NULL || (strlen(extn) >= 3 && strncmp(extn, "win", 3) != 0)) continue;
1151
1152 TWPartition* Part = Find_Partition_By_Path(label);
1153 if (Part == NULL)
1154 {
1155 LOGE(" Unable to locate partition by backup name: '%s'\n", label);
1156 continue;
1157 }
1158
1159 Part->Backup_FileName = de->d_name;
1160 if (strlen(extn) > 3) {
1161 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1162 }
1163
1164 // Now, we just need to find the correct label
Dees_Troye58d5262012-09-21 12:27:57 -04001165 if (Part->Backup_Path == "/system")
Dees_Troy63c8df72012-09-10 14:02:05 -04001166 tw_restore_system = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001167 if (Part->Backup_Path == "/data")
Dees_Troy63c8df72012-09-10 14:02:05 -04001168 tw_restore_data = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001169 if (Part->Backup_Path == "/cache")
Dees_Troy63c8df72012-09-10 14:02:05 -04001170 tw_restore_cache = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001171 if (Part->Backup_Path == "/recovery")
Dees_Troy63c8df72012-09-10 14:02:05 -04001172 tw_restore_recovery = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001173 if (Part->Backup_Path == "/boot")
Dees_Troy63c8df72012-09-10 14:02:05 -04001174 tw_restore_boot = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001175 if (Part->Backup_Path == "/and-sec")
Dees_Troy63c8df72012-09-10 14:02:05 -04001176 tw_restore_andsec = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001177 if (Part->Backup_Path == "/sd-ext")
Dees_Troy63c8df72012-09-10 14:02:05 -04001178 tw_restore_sdext = 1;
1179#ifdef SP1_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001180 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP1_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001181 tw_restore_sp1 = 1;
1182#endif
1183#ifdef SP2_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001184 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP2_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001185 tw_restore_sp2 = 1;
1186#endif
1187#ifdef SP3_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001188 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP3_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001189 tw_restore_sp3 = 1;
1190#endif
1191 }
1192 closedir(d);
1193
1194 // Set the final values
1195 DataManager::SetValue(TW_RESTORE_SYSTEM_VAR, tw_restore_system);
1196 DataManager::SetValue(TW_RESTORE_DATA_VAR, tw_restore_data);
1197 DataManager::SetValue(TW_RESTORE_CACHE_VAR, tw_restore_cache);
1198 DataManager::SetValue(TW_RESTORE_RECOVERY_VAR, tw_restore_recovery);
1199 DataManager::SetValue(TW_RESTORE_BOOT_VAR, tw_restore_boot);
1200 DataManager::SetValue(TW_RESTORE_ANDSEC_VAR, tw_restore_andsec);
1201 DataManager::SetValue(TW_RESTORE_SDEXT_VAR, tw_restore_sdext);
1202 DataManager::SetValue(TW_RESTORE_SP1_VAR, tw_restore_sp1);
1203 DataManager::SetValue(TW_RESTORE_SP2_VAR, tw_restore_sp2);
1204 DataManager::SetValue(TW_RESTORE_SP3_VAR, tw_restore_sp3);
1205
Dees_Troy51a0e822012-09-05 15:24:24 -04001206 return;
1207}
1208
1209int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001210 std::vector<TWPartition*>::iterator iter;
1211 int ret = false;
1212 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001213 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001214
1215 // Iterate through all partitions
1216 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001217 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001218 if (Path == "/and-sec")
1219 ret = (*iter)->Wipe_AndSec();
1220 else
1221 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001222 found = true;
1223 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1224 (*iter)->Wipe();
1225 }
1226 }
1227 if (found) {
1228 return ret;
1229 } else
1230 LOGE("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1231 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001232}
1233
1234int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001235 TWPartition* Part = Find_Partition_By_Block(Block);
1236
1237 if (Part) {
1238 if (Part->Has_SubPartition) {
1239 std::vector<TWPartition*>::iterator subpart;
1240
1241 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1242 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1243 (*subpart)->Wipe();
1244 }
1245 return Part->Wipe();
1246 } else
1247 return Part->Wipe();
1248 }
1249 LOGE("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
1250 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001251}
1252
1253int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001254 TWPartition* Part = Find_Partition_By_Name(Name);
1255
1256 if (Part) {
1257 if (Part->Has_SubPartition) {
1258 std::vector<TWPartition*>::iterator subpart;
1259
1260 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1261 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1262 (*subpart)->Wipe();
1263 }
1264 return Part->Wipe();
1265 } else
1266 return Part->Wipe();
1267 }
1268 LOGE("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
1269 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001270}
1271
1272int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001273 std::vector<TWPartition*>::iterator iter;
1274 int ret = true;
1275
1276 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001277 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001278 if (!(*iter)->Wipe())
1279 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001280 } else if ((*iter)->Has_Android_Secure) {
1281 if (!(*iter)->Wipe_AndSec())
1282 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001283 }
1284 }
1285 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001286}
1287
Dees_Troy38bd7602012-09-14 13:33:53 -04001288int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1289 struct stat st;
1290
1291 if (!Mount_By_Path("/data", true))
1292 return false;
1293
1294 if (!Mount_By_Path("/cache", true))
1295 return false;
1296
1297 ui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001298 system("rm -rf /data/dalvik-cache");
Dees_Troy38bd7602012-09-14 13:33:53 -04001299 ui_print("Cleaned: /data/dalvik-cache...\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001300 system("rm -rf /cache/dalvik-cache");
Dees_Troy38bd7602012-09-14 13:33:53 -04001301 ui_print("Cleaned: /cache/dalvik-cache...\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001302 system("rm -rf /cache/dc");
Dees_Troy38bd7602012-09-14 13:33:53 -04001303 ui_print("Cleaned: /cache/dc\n");
1304
1305 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
1306 if (sdext != NULL) {
1307 if (sdext->Is_Present && sdext->Mount(false)) {
1308 if (stat("/sd-ext/dalvik-cache", &st) == 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001309 system("rm -rf /sd-ext/dalvik-cache");
Dees_Troy38bd7602012-09-14 13:33:53 -04001310 ui_print("Cleaned: /sd-ext/dalvik-cache...\n");
1311 }
1312 }
1313 }
1314 ui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
1315 return true;
1316}
1317
1318int TWPartitionManager::Wipe_Rotate_Data(void) {
1319 if (!Mount_By_Path("/data", true))
1320 return false;
1321
Dees_Troy8170a922012-09-18 15:40:25 -04001322 system("rm -r /data/misc/akmd*");
1323 system("rm -r /data/misc/rild*");
1324 system("rm -r /data/misc/rild*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001325 ui_print("Rotation data wiped.\n");
1326 return true;
1327}
1328
1329int TWPartitionManager::Wipe_Battery_Stats(void) {
1330 struct stat st;
1331
1332 if (!Mount_By_Path("/data", true))
1333 return false;
1334
1335 if (0 != stat("/data/system/batterystats.bin", &st)) {
1336 ui_print("No Battery Stats Found. No Need To Wipe.\n");
1337 } else {
1338 remove("/data/system/batterystats.bin");
1339 ui_print("Cleared battery stats.\n");
1340 }
1341 return true;
1342}
1343
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001344int TWPartitionManager::Wipe_Android_Secure(void) {
1345 std::vector<TWPartition*>::iterator iter;
1346 int ret = false;
1347 bool found = false;
1348
1349 // Iterate through all partitions
1350 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1351 if ((*iter)->Has_Android_Secure) {
1352 ret = (*iter)->Wipe_AndSec();
1353 found = true;
1354 }
1355 }
1356 if (found) {
1357 return ret;
1358 } else {
1359 LOGE("No android secure partitions found.\n");
1360 }
1361 return false;
1362}
1363
Dees_Troy38bd7602012-09-14 13:33:53 -04001364int TWPartitionManager::Format_Data(void) {
1365 TWPartition* dat = Find_Partition_By_Path("/data");
1366
1367 if (dat != NULL) {
1368 if (!dat->UnMount(true))
1369 return false;
1370
1371 return dat->Wipe_Encryption();
1372 } else {
1373 LOGE("Unable to locate /data.\n");
1374 return false;
1375 }
1376 return false;
1377}
1378
1379int TWPartitionManager::Wipe_Media_From_Data(void) {
1380 TWPartition* dat = Find_Partition_By_Path("/data");
1381
1382 if (dat != NULL) {
1383 if (!dat->Has_Data_Media) {
1384 LOGE("This device does not have /data/media\n");
1385 return false;
1386 }
1387 if (!dat->Mount(true))
1388 return false;
1389
1390 ui_print("Wiping internal storage -- /data/media...\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001391 system("rm -rf /data/media");
1392 system("cd /data && mkdir media && chmod 775 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001393 if (dat->Has_Data_Media) {
1394 dat->Recreate_Media_Folder();
1395 }
1396 return true;
1397 } else {
1398 LOGE("Unable to locate /data.\n");
1399 return false;
1400 }
1401 return false;
1402}
1403
Dees_Troy51a0e822012-09-05 15:24:24 -04001404void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001405 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001406 return;
1407}
1408
1409void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001410 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001411 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001412
Dees_Troy32c8eb82012-09-11 15:28:06 -04001413 ui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001414 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001415 if ((*iter)->Can_Be_Mounted) {
1416 (*iter)->Update_Size(true);
1417 if ((*iter)->Mount_Point == "/system") {
1418 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1419 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1420 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1421 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1422 } else if ((*iter)->Mount_Point == "/cache") {
1423 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1424 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1425 } else if ((*iter)->Mount_Point == "/sd-ext") {
1426 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1427 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1428 if ((*iter)->Backup_Size == 0) {
1429 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1430 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1431 } else
1432 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001433 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001434 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001435 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001436 if ((*iter)->Backup_Size == 0) {
1437 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1438 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1439 } else
1440 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001441 } else if ((*iter)->Mount_Point == "/boot") {
1442 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1443 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1444 if ((*iter)->Backup_Size == 0) {
1445 DataManager::SetValue("tw_has_boot_partition", 0);
1446 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1447 } else
1448 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001449 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001450#ifdef SP1_NAME
1451 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1452 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1453 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1454 }
1455#endif
1456#ifdef SP2_NAME
1457 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1458 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1459 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1460 }
1461#endif
1462#ifdef SP3_NAME
1463 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1464 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1465 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1466 }
1467#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001468 } else {
1469 // Handle unmountable partitions in case we reset defaults
1470 if ((*iter)->Mount_Point == "/boot") {
1471 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1472 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1473 if ((*iter)->Backup_Size == 0) {
1474 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1475 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1476 } else
1477 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1478 } else if ((*iter)->Mount_Point == "/recovery") {
1479 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1480 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1481 if ((*iter)->Backup_Size == 0) {
1482 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1483 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1484 } else
1485 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
1486 }
1487#ifdef SP1_NAME
1488 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1489 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1490 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1491 }
1492#endif
1493#ifdef SP2_NAME
1494 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1495 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1496 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1497 }
1498#endif
1499#ifdef SP3_NAME
1500 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1501 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1502 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1503 }
1504#endif
Dees_Troy51127312012-09-08 13:08:49 -04001505 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001506 }
Dees_Troy51127312012-09-08 13:08:49 -04001507 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1508 string current_storage_path = DataManager::GetCurrentStoragePath();
1509 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001510 if (FreeStorage != NULL) {
1511 // Attempt to mount storage
1512 if (!FreeStorage->Mount(false)) {
1513 // We couldn't mount storage... check to see if we have dual storage
1514 int has_dual_storage;
1515 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1516 if (has_dual_storage == 1) {
1517 // We have dual storage, see if we're using the internal storage that should always be present
1518 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001519 if (!FreeStorage->Is_Encrypted) {
1520 // Not able to use internal, so error!
1521 LOGE("Unable to mount internal storage.\n");
1522 }
Dees_Troy8170a922012-09-18 15:40:25 -04001523 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1524 } else {
1525 // We were using external, flip to internal
1526 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1527 current_storage_path = DataManager::GetCurrentStoragePath();
1528 FreeStorage = Find_Partition_By_Path(current_storage_path);
1529 if (FreeStorage != NULL) {
1530 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1531 } else {
1532 LOGE("Unable to locate internal storage partition.\n");
1533 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1534 }
1535 }
1536 } else {
1537 // No dual storage and unable to mount storage, error!
1538 LOGE("Unable to mount storage.\n");
1539 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1540 }
1541 } else {
1542 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1543 }
1544 } else {
Dees_Troy51127312012-09-08 13:08:49 -04001545 LOGI("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001546 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001547 if (!Write_Fstab())
1548 LOGE("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001549 return;
1550}
1551
1552int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001553#ifdef TW_INCLUDE_CRYPTO
1554 int ret_val, password_len;
1555 char crypto_blkdev[255], cPassword[255];
1556 size_t result;
1557
1558 property_set("ro.crypto.state", "encrypted");
1559#ifdef TW_INCLUDE_JB_CRYPTO
1560 // No extra flags needed
1561#else
1562 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1563 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1564 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1565 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1566 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1567 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
1568#endif
1569 strcpy(cPassword, Password.c_str());
1570 if (cryptfs_check_passwd(cPassword) != 0) {
1571 LOGE("Failed to decrypt data.\n");
1572 return -1;
1573 }
1574 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1575 if (strcmp(crypto_blkdev, "error") == 0) {
1576 LOGE("Error retrieving decrypted data block device.\n");
1577 } else {
1578 TWPartition* dat = Find_Partition_By_Path("/data");
1579 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001580 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001581 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1582 dat->Is_Decrypted = true;
1583 dat->Decrypted_Block_Device = crypto_blkdev;
Dees_Troy32c8eb82012-09-11 15:28:06 -04001584 ui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy5bf43922012-09-07 16:07:55 -04001585 // Sleep for a bit so that the device will be ready
1586 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001587#ifdef RECOVERY_SDCARD_ON_DATA
1588 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1589 dat->Storage_Path = "/data/media/0";
1590 dat->Symlink_Path = dat->Storage_Path;
1591 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
1592 dat->UnMount(false);
1593 DataManager::SetBackupFolder();
1594 }
1595#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001596 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001597 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001598 } else
1599 LOGE("Unable to locate data partition.\n");
1600 }
1601 return 0;
1602#else
1603 LOGE("No crypto support was compiled into this build.\n");
1604 return -1;
1605#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001606 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001607}
1608
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001609int TWPartitionManager::Fix_Permissions(void) {
1610 int result = 0;
1611 if (!Mount_By_Path("/data", true))
1612 return false;
1613
1614 if (!Mount_By_Path("/system", true))
1615 return false;
1616
1617 Mount_By_Path("/sd-ext", false);
1618
1619 fixPermissions perms;
1620 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001621 UnMount_Main_Partitions();
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001622 ui_print("Done.\n\n");
1623 return result;
1624}
1625
Dees_Troy8170a922012-09-18 15:40:25 -04001626//partial kangbang from system/vold
1627#ifndef CUSTOM_LUN_FILE
1628#define CUSTOM_LUN_FILE "/sys/devices/platform/usb_mass_storage/lun%d/file"
1629#endif
1630
Dees_Troyd21618c2012-10-14 18:48:49 -04001631int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
1632 int fd;
1633 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1634
1635 if (Part == NULL) {
1636 LOGE("Unable to locate volume information for USB storage mode.");
1637 return false;
1638 }
1639 if (!Part->UnMount(true))
1640 return false;
1641
1642 if ((fd = open(Lun_File.c_str(), O_WRONLY)) < 0) {
1643 LOGE("Unable to open ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
1644 return false;
1645 }
1646
1647 if (write(fd, Part->Actual_Block_Device.c_str(), Part->Actual_Block_Device.size()) < 0) {
1648 LOGE("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
1649 close(fd);
1650 return false;
1651 }
1652 close(fd);
1653 return true;
1654}
1655
Dees_Troy8170a922012-09-18 15:40:25 -04001656int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001657 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001658 char lun_file[255];
Dees_Troy8170a922012-09-18 15:40:25 -04001659 string ext_path;
Dees_Troyd21618c2012-10-14 18:48:49 -04001660 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001661
1662 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1663 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1664 if (has_dual == 1 && has_data_media == 0) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001665 string Lun_File_str = CUSTOM_LUN_FILE;
1666 size_t found = Lun_File_str.find("%");
1667 if (found != string::npos) {
1668 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1669 if (TWFunc::Path_Exists(lun_file))
1670 has_multiple_lun = true;
1671 }
1672 if (!has_multiple_lun) {
Dees_Troyf4ca6122012-10-13 22:17:20 -04001673 // Device doesn't have multiple lun files, mount current storage
Dees_Troyf4ca6122012-10-13 22:17:20 -04001674 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001675 return Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001676 } else {
1677 // Device has multiple lun files
Dees_Troyf4ca6122012-10-13 22:17:20 -04001678 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001679 if (!Open_Lun_File(DataManager::GetSettingsStoragePath(), lun_file))
Dees_Troyf4ca6122012-10-13 22:17:20 -04001680 return false;
Dees_Troyf4ca6122012-10-13 22:17:20 -04001681 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001682 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Dees_Troyd21618c2012-10-14 18:48:49 -04001683 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001684 }
Dees_Troy8170a922012-09-18 15:40:25 -04001685 } else {
1686 if (has_data_media == 0)
1687 ext_path = DataManager::GetCurrentStoragePath();
1688 else
1689 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001690 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001691 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001692 }
1693 return true;
1694}
1695
1696int TWPartitionManager::usb_storage_disable(void) {
1697 int fd, index;
1698 char lun_file[255];
1699
1700 for (index=0; index<2; index++) {
1701 sprintf(lun_file, CUSTOM_LUN_FILE, index);
1702
1703 if ((fd = open(lun_file, O_WRONLY)) < 0) {
Dees_Troye58d5262012-09-21 12:27:57 -04001704 Mount_All_Storage();
1705 Update_System_Details();
1706 if (index == 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001707 LOGE("Unable to open ums lunfile '%s': (%s)", lun_file, strerror(errno));
Dees_Troye58d5262012-09-21 12:27:57 -04001708 return false;
1709 } else
1710 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001711 }
1712
1713 char ch = 0;
1714 if (write(fd, &ch, 1) < 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001715 close(fd);
Dees_Troye58d5262012-09-21 12:27:57 -04001716 Mount_All_Storage();
1717 Update_System_Details();
1718 if (index == 0) {
1719 LOGE("Unable to write to ums lunfile '%s': (%s)", lun_file, strerror(errno));
1720 return false;
1721 } else
1722 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001723 }
1724
1725 close(fd);
1726 }
Dees_Troye58d5262012-09-21 12:27:57 -04001727 Mount_All_Storage();
1728 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001729 UnMount_Main_Partitions();
Dees_Troy8170a922012-09-18 15:40:25 -04001730 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001731}
1732
1733void TWPartitionManager::Mount_All_Storage(void) {
1734 std::vector<TWPartition*>::iterator iter;
1735
1736 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1737 if ((*iter)->Is_Storage)
1738 (*iter)->Mount(false);
1739 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001740}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001741
Dees_Troyd0384ef2012-10-12 12:15:42 -04001742void TWPartitionManager::UnMount_Main_Partitions(void) {
1743 // Unmounts system and data if data is not data/media
1744 // Also unmounts boot if boot is mountable
1745 LOGI("Unmounting main partitions...\n");
1746
1747 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1748
1749 UnMount_By_Path("/system", true);
1750#ifndef RECOVERY_SDCARD_ON_DATA
1751 UnMount_By_Path("/data", true);
1752#endif
1753 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1754 Boot_Partition->UnMount(true);
1755}
1756
Dees_Troy9350b8d2012-09-27 12:38:38 -04001757int TWPartitionManager::Partition_SDCard(void) {
1758 char mkdir_path[255], temp[255], line[512];
1759 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1760 int ext, swap, total_size = 0, fat_size;
1761 FILE* fp;
1762
1763 ui_print("Partitioning SD Card...\n");
1764#ifdef TW_EXTERNAL_STORAGE_PATH
1765 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1766#else
1767 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1768#endif
1769 if (SDCard == NULL) {
1770 LOGE("Unable to locate device to partition.\n");
1771 return false;
1772 }
1773 if (!SDCard->UnMount(true))
1774 return false;
1775 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1776 if (SDext != NULL) {
1777 if (!SDext->UnMount(true))
1778 return false;
1779 }
1780 system("umount \"$SWAPPATH\"");
1781 Device = SDCard->Actual_Block_Device;
1782 // Just use the root block device
1783 Device.resize(strlen("/dev/block/mmcblkX"));
1784
1785 // Find the size of the block device:
1786 fp = fopen("/proc/partitions", "rt");
1787 if (fp == NULL) {
1788 LOGE("Unable to open /proc/partitions\n");
1789 return false;
1790 }
1791
1792 while (fgets(line, sizeof(line), fp) != NULL)
1793 {
1794 unsigned long major, minor, blocks;
1795 char device[512];
1796 char tmpString[64];
1797
1798 if (strlen(line) < 7 || line[0] == 'm') continue;
1799 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1800
1801 tmpdevice = "/dev/block/";
1802 tmpdevice += device;
1803 if (tmpdevice == Device) {
1804 // Adjust block size to byte size
1805 total_size = (int)(blocks * 1024ULL / 1000000LLU);
1806 break;
1807 }
1808 }
1809 fclose(fp);
1810
1811 DataManager::GetValue("tw_sdext_size", ext);
1812 DataManager::GetValue("tw_swap_size", swap);
1813 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1814 fat_size = total_size - ext - swap;
1815 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);
1816 memset(temp, 0, sizeof(temp));
1817 sprintf(temp, "%i", fat_size);
1818 fat_str = temp;
1819 memset(temp, 0, sizeof(temp));
1820 sprintf(temp, "%i", fat_size + ext);
1821 ext_str = temp;
1822 memset(temp, 0, sizeof(temp));
1823 sprintf(temp, "%i", fat_size + ext + swap);
1824 swap_str = temp;
1825 if (ext + swap > total_size) {
1826 LOGE("EXT + Swap size is larger than sdcard size.\n");
1827 return false;
1828 }
1829 ui_print("Removing partition table...\n");
1830 Command = "parted -s " + Device + " mklabel msdos";
1831 LOGI("Command is: '%s'\n", Command.c_str());
1832 if (system(Command.c_str()) != 0) {
1833 LOGE("Unable to remove partition table.\n");
1834 Update_System_Details();
1835 return false;
1836 }
1837 ui_print("Creating FAT32 partition...\n");
1838 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
1839 LOGI("Command is: '%s'\n", Command.c_str());
1840 if (system(Command.c_str()) != 0) {
1841 LOGE("Unable to create FAT32 partition.\n");
1842 return false;
1843 }
1844 if (ext > 0) {
1845 ui_print("Creating EXT partition...\n");
1846 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
1847 LOGI("Command is: '%s'\n", Command.c_str());
1848 if (system(Command.c_str()) != 0) {
1849 LOGE("Unable to create EXT partition.\n");
1850 Update_System_Details();
1851 return false;
1852 }
1853 }
1854 if (swap > 0) {
1855 ui_print("Creating swap partition...\n");
1856 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
1857 LOGI("Command is: '%s'\n", Command.c_str());
1858 if (system(Command.c_str()) != 0) {
1859 LOGE("Unable to create swap partition.\n");
1860 Update_System_Details();
1861 return false;
1862 }
1863 }
1864 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1865#ifdef TW_EXTERNAL_STORAGE_PATH
1866 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1867 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1868 memset(mkdir_path, 0, sizeof(mkdir_path));
1869 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1870#else
1871 Mount_By_Path("/sdcard", 1);
1872 strcpy(mkdir_path, "/sdcard/TWRP");
1873#endif
1874 mkdir(mkdir_path, 0777);
1875 DataManager::Flush();
1876#ifdef TW_EXTERNAL_STORAGE_PATH
1877 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1878 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1879 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1880#else
1881 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1882 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1883 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1884#endif
1885 if (ext > 0) {
1886 if (SDext == NULL) {
1887 LOGE("Unable to locate sd-ext partition.\n");
1888 return false;
1889 }
1890 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
1891 ui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1892 LOGI("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
1893 system(Command.c_str());
1894 }
1895
1896 Update_System_Details();
1897 ui_print("Partitioning complete.\n");
1898 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001899}