blob: ab73a11100e989c18a721f0793f5a38463bebf42 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/* Partition Management classes for TWRP
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 *
17 * The code was written from scratch by Dees_Troy dees_troy at
18 * yahoo
19 *
20 * Copyright (c) 2012
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/vfs.h>
28#include <unistd.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040029#include <vector>
Dees_Troy63c8df72012-09-10 14:02:05 -040030#include <dirent.h>
31#include <time.h>
Dees_Troy8170a922012-09-18 15:40:25 -040032#include <errno.h>
33#include <fcntl.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050034#include <iostream>
35#include <iomanip>
Dees_Troy51a0e822012-09-05 15:24:24 -040036#include "variables.h"
37#include "common.h"
Dees_Troy093b7642012-09-21 15:59:38 -040038#include "ui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040040#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040041#include "twrp-functions.hpp"
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040042#include "fixPermissions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040043
Dees_Troy5bf43922012-09-07 16:07:55 -040044#ifdef TW_INCLUDE_CRYPTO
45 #ifdef TW_INCLUDE_JB_CRYPTO
46 #include "crypto/jb/cryptfs.h"
47 #else
48 #include "crypto/ics/cryptfs.h"
49 #endif
50 #include "cutils/properties.h"
51#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040052
Dees_Troy093b7642012-09-21 15:59:38 -040053extern RecoveryUI* ui;
54
Dees_Troy51a0e822012-09-05 15:24:24 -040055int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040056 FILE *fstabFile;
57 char fstab_line[MAX_FSTAB_LINE_LENGTH];
58
59 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
60 if (fstabFile == NULL) {
61 LOGE("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
62 return false;
63 }
64
65 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
66 if (fstab_line[0] != '/')
67 continue;
68
Dees_Troy2a923582012-09-20 12:13:34 -040069 if (fstab_line[strlen(fstab_line) - 1] != '\n')
70 fstab_line[strlen(fstab_line)] = '\n';
71
Dees_Troy5bf43922012-09-07 16:07:55 -040072 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040073 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040074 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040075
Dees_Troy5bf43922012-09-07 16:07:55 -040076 if (partition->Process_Fstab_Line(line, Display_Error)) {
77 Partitions.push_back(partition);
78 } else {
79 delete partition;
80 }
81 }
82 fclose(fstabFile);
83 if (!Write_Fstab()) {
84 if (Display_Error)
85 LOGE("Error creating fstab\n");
86 else
87 LOGI("Error creating fstab\n");
88 }
Dees_Troy51127312012-09-08 13:08:49 -040089 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -040090 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -040091 return true;
92}
93
94int TWPartitionManager::Write_Fstab(void) {
95 FILE *fp;
96 std::vector<TWPartition*>::iterator iter;
97 string Line;
98
99 fp = fopen("/etc/fstab", "w");
100 if (fp == NULL) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400101 LOGI("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400102 return false;
103 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400104 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400105 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400106 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400107 fputs(Line.c_str(), fp);
Dees_Troy51127312012-09-08 13:08:49 -0400108 // Handle subpartition tracking
109 if ((*iter)->Is_SubPartition) {
110 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
111 if (ParentPartition)
112 ParentPartition->Has_SubPartition = true;
113 else
114 LOGE("Unable to locate parent partition '%s' of '%s'\n", (*iter)->SubPartition_Of.c_str(), (*iter)->Mount_Point.c_str());
115 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400116 }
117 }
118 fclose(fp);
119 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400120}
121
Dees_Troy8170a922012-09-18 15:40:25 -0400122void TWPartitionManager::Output_Partition_Logging(void) {
123 std::vector<TWPartition*>::iterator iter;
124
125 printf("\n\nPartition Logs:\n");
126 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
127 Output_Partition((*iter));
128}
129
130void TWPartitionManager::Output_Partition(TWPartition* Part) {
131 unsigned long long mb = 1048576;
132
Gary Peck004d48b2012-11-21 16:28:18 -0800133 printf("%s | %s | Size: %iMB", Part->Mount_Point.c_str(), Part->Actual_Block_Device.c_str(), (int)(Part->Size / mb));
Dees_Troy8170a922012-09-18 15:40:25 -0400134 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800135 printf(" Used: %iMB Free: %iMB Backup Size: %iMB", (int)(Part->Used / mb), (int)(Part->Free / mb), (int)(Part->Backup_Size / mb));
Dees_Troy8170a922012-09-18 15:40:25 -0400136 }
Gary Peck004d48b2012-11-21 16:28:18 -0800137 printf("\n Flags: ");
138 if (Part->Can_Be_Wiped)
139 printf("Can_Be_Wiped ");
140 if (Part->Wipe_During_Factory_Reset)
141 printf("Wipe_During_Factory_Reset ");
142 if (Part->Wipe_Available_in_GUI)
143 printf("Wipe_Available_in_GUI ");
144 if (Part->Is_SubPartition)
145 printf("Is_SubPartition ");
146 if (Part->Has_SubPartition)
147 printf("Has_SubPartition ");
148 if (Part->Removable)
149 printf("Removable ");
150 if (Part->Is_Present)
151 printf("IsPresent ");
152 if (Part->Can_Be_Encrypted)
153 printf("Can_Be_Encrypted ");
154 if (Part->Is_Encrypted)
155 printf("Is_Encrypted ");
156 if (Part->Is_Decrypted)
157 printf("Is_Decrypted ");
158 if (Part->Has_Data_Media)
159 printf("Has_Data_Media ");
160 if (Part->Has_Android_Secure)
161 printf("Has_Android_Secure ");
162 if (Part->Is_Storage)
163 printf("Is_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000164 if (Part->Ignore_Blkid)
165 printf("Ignore_Blkid ");
Gary Peck004d48b2012-11-21 16:28:18 -0800166 printf("\n");
167 if (!Part->SubPartition_Of.empty())
168 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
169 if (!Part->Symlink_Path.empty())
170 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
171 if (!Part->Symlink_Mount_Point.empty())
172 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
173 if (!Part->Primary_Block_Device.empty())
174 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
175 if (!Part->Alternate_Block_Device.empty())
176 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
177 if (!Part->Decrypted_Block_Device.empty())
178 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
179 if (Part->Length != 0)
180 printf(" Length: %i\n", Part->Length);
181 if (!Part->Display_Name.empty())
182 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
183 if (!Part->Backup_Path.empty())
184 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
185 if (!Part->Backup_Name.empty())
186 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
187 if (!Part->Backup_FileName.empty())
188 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
189 if (!Part->Storage_Path.empty())
190 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
191 if (!Part->Current_File_System.empty())
192 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
193 if (!Part->Fstab_File_System.empty())
194 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
195 if (Part->Format_Block_Size != 0)
196 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400197 if (!Part->MTD_Name.empty())
198 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400199 string back_meth = Part->Backup_Method_By_Name();
200 printf(" Backup_Method: %s\n\n", back_meth.c_str());
201}
202
Dees_Troy51a0e822012-09-05 15:24:24 -0400203int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400204 std::vector<TWPartition*>::iterator iter;
205 int ret = false;
206 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400207 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400208
Dees_Troy43d8b002012-09-17 16:00:01 -0400209 if (Local_Path == "/tmp")
210 return true;
211
Dees_Troy5bf43922012-09-07 16:07:55 -0400212 // Iterate through all partitions
213 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400214 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400215 ret = (*iter)->Mount(Display_Error);
216 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400217 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400218 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400219 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400220 }
221 if (found) {
222 return ret;
223 } else if (Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400224 LOGE("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400225 } else {
Dees_Troy51127312012-09-08 13:08:49 -0400226 LOGI("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400227 }
228 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400229}
230
231int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400232 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400233
Dees_Troy51127312012-09-08 13:08:49 -0400234 if (Part) {
235 if (Part->Has_SubPartition) {
236 std::vector<TWPartition*>::iterator subpart;
237
238 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
239 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
240 (*subpart)->Mount(Display_Error);
241 }
242 return Part->Mount(Display_Error);
243 } else
244 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400245 }
246 if (Display_Error)
Dees_Troy51127312012-09-08 13:08:49 -0400247 LOGE("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400248 else
Dees_Troy51127312012-09-08 13:08:49 -0400249 LOGI("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400250 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251}
252
253int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400254 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400255
Dees_Troy51127312012-09-08 13:08:49 -0400256 if (Part) {
257 if (Part->Has_SubPartition) {
258 std::vector<TWPartition*>::iterator subpart;
259
260 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
261 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
262 (*subpart)->Mount(Display_Error);
263 }
264 return Part->Mount(Display_Error);
265 } else
266 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400267 }
268 if (Display_Error)
Dees_Troy51127312012-09-08 13:08:49 -0400269 LOGE("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400270 else
Dees_Troy51127312012-09-08 13:08:49 -0400271 LOGI("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400272 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400273}
274
275int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400276 std::vector<TWPartition*>::iterator iter;
277 int ret = false;
278 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400279 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400280
281 // Iterate through all partitions
282 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400283 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400284 ret = (*iter)->UnMount(Display_Error);
285 found = true;
286 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
287 (*iter)->UnMount(Display_Error);
288 }
289 }
290 if (found) {
291 return ret;
292 } else if (Display_Error) {
293 LOGE("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
294 } else {
295 LOGI("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
296 }
297 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400298}
299
300int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400301 TWPartition* Part = Find_Partition_By_Block(Block);
302
303 if (Part) {
304 if (Part->Has_SubPartition) {
305 std::vector<TWPartition*>::iterator subpart;
306
307 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
308 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
309 (*subpart)->UnMount(Display_Error);
310 }
311 return Part->UnMount(Display_Error);
312 } else
313 return Part->UnMount(Display_Error);
314 }
315 if (Display_Error)
316 LOGE("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
317 else
318 LOGI("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
319 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400320}
321
322int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400323 TWPartition* Part = Find_Partition_By_Name(Name);
324
325 if (Part) {
326 if (Part->Has_SubPartition) {
327 std::vector<TWPartition*>::iterator subpart;
328
329 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
330 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
331 (*subpart)->UnMount(Display_Error);
332 }
333 return Part->UnMount(Display_Error);
334 } else
335 return Part->UnMount(Display_Error);
336 }
337 if (Display_Error)
338 LOGE("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
339 else
340 LOGI("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
341 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400342}
343
344int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400345 TWPartition* Part = Find_Partition_By_Path(Path);
346
347 if (Part)
348 return Part->Is_Mounted();
349 else
350 LOGI("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
351 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400352}
353
354int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400355 TWPartition* Part = Find_Partition_By_Block(Block);
356
357 if (Part)
358 return Part->Is_Mounted();
359 else
360 LOGI("Is_Mounted: Unable to find partition for block '%s'\n", Block.c_str());
361 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400362}
363
364int TWPartitionManager::Is_Mounted_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400365 TWPartition* Part = Find_Partition_By_Name(Name);
366
367 if (Part)
368 return Part->Is_Mounted();
369 else
370 LOGI("Is_Mounted: Unable to find partition for name '%s'\n", Name.c_str());
371 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400372}
373
Dees_Troy5bf43922012-09-07 16:07:55 -0400374int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400375 string current_storage_path = DataManager::GetCurrentStoragePath();
376
377 if (Mount_By_Path(current_storage_path, Display_Error)) {
378 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
379 if (FreeStorage)
380 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
381 return true;
382 }
383 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400384}
385
Dees_Troy5bf43922012-09-07 16:07:55 -0400386int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
387 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
388}
389
390TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
391 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400392 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400393
394 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400395 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400396 return (*iter);
397 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400398 return NULL;
399}
400
Dees_Troy5bf43922012-09-07 16:07:55 -0400401TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400402 std::vector<TWPartition*>::iterator iter;
403
404 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400405 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 -0400406 return (*iter);
407 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400408 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400409}
410
411TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400412 std::vector<TWPartition*>::iterator iter;
413
414 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
415 if ((*iter)->Display_Name == Name)
416 return (*iter);
417 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400418 return NULL;
419}
Dees_Troy51a0e822012-09-05 15:24:24 -0400420
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400421int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
422 // Check the backup name to ensure that it is the correct size and contains only valid characters
423 // and that a backup with that name doesn't already exist
424 char backup_name[MAX_BACKUP_NAME_LEN];
425 char backup_loc[255], tw_image_dir[255];
426 int copy_size;
427 int index, cur_char;
428 string Backup_Name, Backup_Loc;
429
430 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
431 copy_size = Backup_Name.size();
432 // Check size
433 if (copy_size > MAX_BACKUP_NAME_LEN) {
434 if (Display_Error)
435 LOGE("Backup name is too long.\n");
436 return -2;
437 }
438
439 // Check each character
440 strncpy(backup_name, Backup_Name.c_str(), copy_size);
441 if (strcmp(backup_name, "0") == 0)
442 return 0; // A "0" (zero) means to use the current timestamp for the backup name
443 for (index=0; index<copy_size; index++) {
444 cur_char = (int)backup_name[index];
445 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) {
446 // These are valid characters
447 // Numbers
448 // Upper case letters
449 // Lower case letters
450 // Space
451 // and -_.{}[]
452 } else {
453 if (Display_Error)
454 LOGE("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
455 return -3;
456 }
457 }
458
459 // Check to make sure that a backup with this name doesn't already exist
460 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
461 strcpy(backup_loc, Backup_Loc.c_str());
462 sprintf(tw_image_dir,"%s/%s/.", backup_loc, backup_name);
463 if (TWFunc::Path_Exists(tw_image_dir)) {
464 if (Display_Error)
465 LOGE("A backup with this name already exists.\n");
466 return -4;
467 }
468
469 // No problems found, return 0
470 return 0;
471}
472
Dees_Troy43d8b002012-09-17 16:00:01 -0400473bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
474{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500475 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400476 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500477 string result;
Dees_Troy43d8b002012-09-17 16:00:01 -0400478
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500479 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400480 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400481
Dees_Troyb46a6842012-09-25 11:06:46 -0400482 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troyc51f1f92012-09-20 15:32:13 -0400483 ui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400484
485 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500486 command = "md5sum " + Backup_Filename + " > " + Backup_Filename + ".md5";
487 chdir(Backup_Folder.c_str());
488 if (TWFunc::Exec_Cmd(command, result) == 0) {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400489 ui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400490 return true;
491 } else {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400492 ui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400493 return false;
494 }
495 } else {
496 char filename[512];
497 int index = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400498 sprintf(filename, "%s%03i", Full_File.c_str(), index);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400499 while (TWFunc::Path_Exists(filename) == true) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500500 ostringstream intToStr;
501 intToStr << index;
502 ostringstream fn;
503 fn << setw(3) << setfill('0') << intToStr.str();
504 command = "md5sum " + Backup_Filename + fn.str() + " >" + Backup_Filename + fn.str() + ".md5";
505 chdir(Backup_Folder.c_str());
506 if (TWFunc::Exec_Cmd(command, result) != 0) {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400507 ui_print(" * MD5 Error.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400508 return false;
509 }
510 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400511 sprintf(filename, "%s%03i", Full_File.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400512 }
513 if (index == 0) {
514 LOGE("Backup file: '%s' not found!\n", filename);
515 return false;
516 }
Dees_Troyc5865ab2012-09-24 15:08:04 -0400517 ui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400518 }
519 return true;
520}
521
Dees_Troy093b7642012-09-21 15:59:38 -0400522bool TWPartitionManager::Backup_Partition(TWPartition* Part, string Backup_Folder, bool generate_md5, unsigned long long* img_bytes_remaining, unsigned long long* file_bytes_remaining, unsigned long *img_time, unsigned long *file_time, unsigned long long *img_bytes, unsigned long long *file_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400523 time_t start, stop;
Dees_Troy093b7642012-09-21 15:59:38 -0400524 int img_bps, file_bps;
525 unsigned long total_time, remain_time, section_time;
526 int use_compression, backup_time;
527 float pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400528
529 if (Part == NULL)
530 return true;
531
Dees_Troy093b7642012-09-21 15:59:38 -0400532 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
533
534 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
535 if (use_compression)
536 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
537 else
538 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
539
540 // We know the speed for both, how far into the whole backup are we, based on time
541 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
542 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
543
544 pos = (total_time - remain_time) / (float) total_time;
545 ui->SetProgress(pos);
546
547 LOGI("Estimated Total time: %lu Estimated remaining time: %lu\n", total_time, remain_time);
548
549 // And get the time
550 if (Part->Backup_Method == 1)
551 section_time = Part->Backup_Size / file_bps;
552 else
553 section_time = Part->Backup_Size / img_bps;
554
555 // Set the position
556 pos = section_time / (float) total_time;
557 ui->ShowProgress(pos, section_time);
558
Dees_Troy43d8b002012-09-17 16:00:01 -0400559 time(&start);
560
561 if (Part->Backup(Backup_Folder)) {
Dees_Troy8170a922012-09-18 15:40:25 -0400562 if (Part->Has_SubPartition) {
563 std::vector<TWPartition*>::iterator subpart;
564
565 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
566 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
567 if (!(*subpart)->Backup(Backup_Folder))
568 return false;
569 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
570 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400571 if (Part->Backup_Method == 1) {
572 *file_bytes_remaining -= (*subpart)->Backup_Size;
573 } else {
574 *img_bytes_remaining -= (*subpart)->Backup_Size;
575 }
Dees_Troy8170a922012-09-18 15:40:25 -0400576 }
577 }
578 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400579 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400580 backup_time = (int) difftime(stop, start);
581 LOGI("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400582 if (Part->Backup_Method == 1) {
583 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400584 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400585 } else {
586 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400587 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400588 }
589 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
590 } else {
591 return false;
592 }
593}
594
595int TWPartitionManager::Run_Backup(void) {
596 int check, do_md5, partition_count = 0;
597 string Backup_Folder, Backup_Name, Full_Backup_Path;
Dees_Troy8170a922012-09-18 15:40:25 -0400598 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 -0400599 unsigned long img_time = 0, file_time = 0;
600 TWPartition* backup_sys = NULL;
601 TWPartition* backup_data = NULL;
602 TWPartition* backup_cache = NULL;
603 TWPartition* backup_recovery = NULL;
604 TWPartition* backup_boot = NULL;
605 TWPartition* backup_andsec = NULL;
606 TWPartition* backup_sdext = NULL;
607 TWPartition* backup_sp1 = NULL;
608 TWPartition* backup_sp2 = NULL;
609 TWPartition* backup_sp3 = NULL;
610 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400611 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400612 struct tm *t;
613 time_t start, stop, seconds, total_start, total_stop;
614 seconds = time(0);
615 t = localtime(&seconds);
616
617 time(&total_start);
618
619 Update_System_Details();
620
621 if (!Mount_Current_Storage(true))
622 return false;
623
624 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400625 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400626 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400627 else
628 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400629
Dees_Troy43d8b002012-09-17 16:00:01 -0400630 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
631 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400632 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name.empty()) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400633 char timestamp[255];
634 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);
635 Backup_Name = timestamp;
636 }
637 LOGI("Backup Name is: '%s'\n", Backup_Name.c_str());
638 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
639 LOGI("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
640
641 ui_print("\n[BACKUP STARTED]\n");
642 ui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
643 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
644 LOGE("Failed to make backup folder.\n");
645 return false;
646 }
647
648 LOGI("Calculating backup details...\n");
649 DataManager::GetValue(TW_BACKUP_SYSTEM_VAR, check);
650 if (check) {
651 backup_sys = Find_Partition_By_Path("/system");
652 if (backup_sys != NULL) {
653 partition_count++;
654 if (backup_sys->Backup_Method == 1)
655 file_bytes += backup_sys->Backup_Size;
656 else
657 img_bytes += backup_sys->Backup_Size;
658 } else {
659 LOGE("Unable to locate system partition.\n");
660 return false;
661 }
662 }
663 DataManager::GetValue(TW_BACKUP_DATA_VAR, check);
664 if (check) {
665 backup_data = Find_Partition_By_Path("/data");
666 if (backup_data != NULL) {
667 partition_count++;
Dees_Troy8170a922012-09-18 15:40:25 -0400668 subpart_size = 0;
669 if (backup_data->Has_SubPartition) {
670 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
671 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == backup_data->Mount_Point)
672 subpart_size += (*subpart)->Backup_Size;
673 }
674 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400675 if (backup_data->Backup_Method == 1)
Dees_Troy8170a922012-09-18 15:40:25 -0400676 file_bytes += backup_data->Backup_Size + subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400677 else
Dees_Troy8170a922012-09-18 15:40:25 -0400678 img_bytes += backup_data->Backup_Size + subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400679 } else {
680 LOGE("Unable to locate data partition.\n");
681 return false;
682 }
683 }
684 DataManager::GetValue(TW_BACKUP_CACHE_VAR, check);
685 if (check) {
686 backup_cache = Find_Partition_By_Path("/cache");
687 if (backup_cache != NULL) {
688 partition_count++;
689 if (backup_cache->Backup_Method == 1)
690 file_bytes += backup_cache->Backup_Size;
691 else
692 img_bytes += backup_cache->Backup_Size;
693 } else {
694 LOGE("Unable to locate cache partition.\n");
695 return false;
696 }
697 }
698 DataManager::GetValue(TW_BACKUP_RECOVERY_VAR, check);
699 if (check) {
700 backup_recovery = Find_Partition_By_Path("/recovery");
701 if (backup_recovery != NULL) {
702 partition_count++;
703 if (backup_recovery->Backup_Method == 1)
704 file_bytes += backup_recovery->Backup_Size;
705 else
706 img_bytes += backup_recovery->Backup_Size;
707 } else {
708 LOGE("Unable to locate recovery partition.\n");
709 return false;
710 }
711 }
712 DataManager::GetValue(TW_BACKUP_BOOT_VAR, check);
713 if (check) {
714 backup_boot = Find_Partition_By_Path("/boot");
715 if (backup_boot != NULL) {
716 partition_count++;
717 if (backup_boot->Backup_Method == 1)
718 file_bytes += backup_boot->Backup_Size;
719 else
720 img_bytes += backup_boot->Backup_Size;
721 } else {
722 LOGE("Unable to locate boot partition.\n");
723 return false;
724 }
725 }
726 DataManager::GetValue(TW_BACKUP_ANDSEC_VAR, check);
727 if (check) {
728 backup_andsec = Find_Partition_By_Path("/and-sec");
729 if (backup_andsec != NULL) {
730 partition_count++;
731 if (backup_andsec->Backup_Method == 1)
732 file_bytes += backup_andsec->Backup_Size;
733 else
734 img_bytes += backup_andsec->Backup_Size;
735 } else {
736 LOGE("Unable to locate android secure partition.\n");
737 return false;
738 }
739 }
740 DataManager::GetValue(TW_BACKUP_SDEXT_VAR, check);
741 if (check) {
742 backup_sdext = Find_Partition_By_Path("/sd-ext");
743 if (backup_sdext != NULL) {
744 partition_count++;
745 if (backup_sdext->Backup_Method == 1)
746 file_bytes += backup_sdext->Backup_Size;
747 else
748 img_bytes += backup_sdext->Backup_Size;
749 } else {
750 LOGE("Unable to locate sd-ext partition.\n");
751 return false;
752 }
753 }
754#ifdef SP1_NAME
755 DataManager::GetValue(TW_BACKUP_SP1_VAR, check);
756 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400757 backup_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400758 if (backup_sp1 != NULL) {
759 partition_count++;
760 if (backup_sp1->Backup_Method == 1)
761 file_bytes += backup_sp1->Backup_Size;
762 else
763 img_bytes += backup_sp1->Backup_Size;
764 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400765 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400766 return false;
767 }
768 }
769#endif
770#ifdef SP2_NAME
771 DataManager::GetValue(TW_BACKUP_SP2_VAR, check);
772 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400773 backup_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400774 if (backup_sp2 != NULL) {
775 partition_count++;
776 if (backup_sp2->Backup_Method == 1)
777 file_bytes += backup_sp2->Backup_Size;
778 else
779 img_bytes += backup_sp2->Backup_Size;
780 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400781 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400782 return false;
783 }
784 }
785#endif
786#ifdef SP3_NAME
787 DataManager::GetValue(TW_BACKUP_SP3_VAR, check);
788 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400789 backup_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400790 if (backup_sp3 != NULL) {
791 partition_count++;
792 if (backup_sp3->Backup_Method == 1)
793 file_bytes += backup_sp3->Backup_Size;
794 else
795 img_bytes += backup_sp3->Backup_Size;
796 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400797 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400798 return false;
799 }
800 }
801#endif
802
803 if (partition_count == 0) {
804 ui_print("No partitions selected for backup.\n");
805 return false;
806 }
807 total_bytes = file_bytes + img_bytes;
808 ui_print(" * Total number of partitions to back up: %d\n", partition_count);
809 ui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
810 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
811 if (storage != NULL) {
812 free_space = storage->Free;
813 ui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
814 } else {
815 LOGE("Unable to locate storage device.\n");
816 return false;
817 }
818 if (free_space + (32 * 1024 * 1024) < total_bytes) {
819 // We require an extra 32MB just in case
820 LOGE("Not enough free space on storage.\n");
821 return false;
822 }
823 img_bytes_remaining = img_bytes;
824 file_bytes_remaining = file_bytes;
825
Dees_Troy093b7642012-09-21 15:59:38 -0400826 ui->SetProgress(0.0);
827
828 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 -0400829 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400830 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 -0400831 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400832 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 -0400833 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400834 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 -0400835 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400836 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 -0400837 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400838 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 -0400839 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400840 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 -0400841 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400842 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 -0400843 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400844 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 -0400845 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400846 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 -0400847 return false;
848
849 // Average BPS
850 if (img_time == 0)
851 img_time = 1;
852 if (file_time == 0)
853 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400854 int img_bps = (int)img_bytes / (int)img_time;
855 int file_bps = (int)file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400856
857 ui_print("Average backup rate for file systems: %lu MB/sec\n", (file_bps / (1024 * 1024)));
858 ui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
859
860 time(&total_stop);
861 int total_time = (int) difftime(total_stop, total_start);
862 unsigned long long actual_backup_size = TWFunc::Get_Folder_Size(Full_Backup_Path, true);
863 actual_backup_size /= (1024LLU * 1024LLU);
864
Dees_Troy093b7642012-09-21 15:59:38 -0400865 int prev_img_bps, prev_file_bps, use_compression;
866 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
867 img_bps += (prev_img_bps * 4);
868 img_bps /= 5;
869
870 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
871 if (use_compression)
872 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
873 else
874 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
875 file_bps += (prev_file_bps * 4);
876 file_bps /= 5;
877
878 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
879 if (use_compression)
880 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
881 else
882 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
883
Dees_Troy43d8b002012-09-17 16:00:01 -0400884 ui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
885 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400886 UnMount_Main_Partitions();
Dees_Troy43d8b002012-09-17 16:00:01 -0400887 ui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500888 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400889}
890
Dees_Troy093b7642012-09-21 15:59:38 -0400891bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400892 time_t Start, Stop;
893 time(&Start);
Dees_Troy093b7642012-09-21 15:59:38 -0400894 ui->ShowProgress(1.0 / (float)partition_count, 150);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400895 if (!Part->Restore(Restore_Name))
896 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400897 if (Part->Has_SubPartition) {
898 std::vector<TWPartition*>::iterator subpart;
899
900 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
901 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
902 if (!(*subpart)->Restore(Restore_Name))
903 return false;
904 }
905 }
906 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400907 time(&Stop);
908 ui_print("[%s done (%d seconds)]\n\n", Part->Display_Name.c_str(), (int)difftime(Stop, Start));
909 return true;
910}
911
Dees_Troy51a0e822012-09-05 15:24:24 -0400912int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400913 int check_md5, check, partition_count = 0;
914 TWPartition* restore_sys = NULL;
915 TWPartition* restore_data = NULL;
916 TWPartition* restore_cache = NULL;
917 TWPartition* restore_boot = NULL;
918 TWPartition* restore_andsec = NULL;
919 TWPartition* restore_sdext = NULL;
920 TWPartition* restore_sp1 = NULL;
921 TWPartition* restore_sp2 = NULL;
922 TWPartition* restore_sp3 = NULL;
923 time_t rStart, rStop;
924 time(&rStart);
Dees_Troy43d8b002012-09-17 16:00:01 -0400925
Dees_Troy4a2a1262012-09-18 09:33:47 -0400926 ui_print("\n[RESTORE STARTED]\n\n");
927 ui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400928
Dees_Troy4a2a1262012-09-18 09:33:47 -0400929 if (!Mount_Current_Storage(true))
930 return false;
931
932 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
933 DataManager::GetValue(TW_RESTORE_SYSTEM_VAR, check);
Dees_Troy63c8df72012-09-10 14:02:05 -0400934 if (check > 0) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400935 restore_sys = Find_Partition_By_Path("/system");
936 if (restore_sys == NULL) {
937 LOGE("Unable to locate system partition.\n");
938 return false;
939 }
940 partition_count++;
941 }
942 DataManager::GetValue(TW_RESTORE_DATA_VAR, check);
943 if (check > 0) {
944 restore_data = Find_Partition_By_Path("/data");
945 if (restore_data == NULL) {
946 LOGE("Unable to locate data partition.\n");
947 return false;
948 }
949 partition_count++;
950 }
951 DataManager::GetValue(TW_RESTORE_CACHE_VAR, check);
952 if (check > 0) {
953 restore_cache = Find_Partition_By_Path("/cache");
954 if (restore_cache == NULL) {
955 LOGE("Unable to locate cache partition.\n");
956 return false;
957 }
958 partition_count++;
959 }
960 DataManager::GetValue(TW_RESTORE_BOOT_VAR, check);
961 if (check > 0) {
962 restore_boot = Find_Partition_By_Path("/boot");
963 if (restore_boot == NULL) {
964 LOGE("Unable to locate boot partition.\n");
965 return false;
966 }
967 partition_count++;
968 }
969 DataManager::GetValue(TW_RESTORE_ANDSEC_VAR, check);
970 if (check > 0) {
971 restore_andsec = Find_Partition_By_Path("/and-sec");
972 if (restore_andsec == NULL) {
973 LOGE("Unable to locate android secure partition.\n");
974 return false;
975 }
976 partition_count++;
977 }
978 DataManager::GetValue(TW_RESTORE_SDEXT_VAR, check);
979 if (check > 0) {
980 restore_sdext = Find_Partition_By_Path("/sd-ext");
981 if (restore_sdext == NULL) {
982 LOGE("Unable to locate sd-ext partition.\n");
983 return false;
984 }
985 partition_count++;
986 }
987#ifdef SP1_NAME
988 DataManager::GetValue(TW_RESTORE_SP1_VAR, check);
989 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400990 restore_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400991 if (restore_sp1 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400992 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400993 return false;
994 }
995 partition_count++;
996 }
997#endif
998#ifdef SP2_NAME
999 DataManager::GetValue(TW_RESTORE_SP2_VAR, check);
1000 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001001 restore_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001002 if (restore_sp2 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001003 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001004 return false;
1005 }
1006 partition_count++;
1007 }
1008#endif
1009#ifdef SP3_NAME
1010 DataManager::GetValue(TW_RESTORE_SP3_VAR, check);
1011 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001012 restore_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001013 if (restore_sp3 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001014 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001015 return false;
1016 }
1017 partition_count++;
1018 }
1019#endif
1020
1021 if (partition_count == 0) {
1022 LOGE("No partitions selected for restore.\n");
1023 return false;
1024 }
1025
1026 if (check_md5 > 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001027 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
Dees_Troyb46a6842012-09-25 11:06:46 -04001028 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001029 ui_print("Verifying MD5...\n");
1030 if (restore_sys != NULL && !restore_sys->Check_MD5(Restore_Name))
1031 return false;
1032 if (restore_data != NULL && !restore_data->Check_MD5(Restore_Name))
1033 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001034 if (restore_data != NULL && restore_data->Has_SubPartition) {
1035 std::vector<TWPartition*>::iterator subpart;
1036
1037 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1038 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_data->Mount_Point) {
1039 if (!(*subpart)->Check_MD5(Restore_Name))
1040 return false;
1041 }
1042 }
1043 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001044 if (restore_cache != NULL && !restore_cache->Check_MD5(Restore_Name))
1045 return false;
1046 if (restore_boot != NULL && !restore_boot->Check_MD5(Restore_Name))
1047 return false;
1048 if (restore_andsec != NULL && !restore_andsec->Check_MD5(Restore_Name))
1049 return false;
1050 if (restore_sdext != NULL && !restore_sdext->Check_MD5(Restore_Name))
1051 return false;
1052 if (restore_sp1 != NULL && !restore_sp1->Check_MD5(Restore_Name))
1053 return false;
1054 if (restore_sp2 != NULL && !restore_sp2->Check_MD5(Restore_Name))
1055 return false;
1056 if (restore_sp3 != NULL && !restore_sp3->Check_MD5(Restore_Name))
1057 return false;
1058 ui_print("Done verifying MD5.\n");
1059 } else
1060 ui_print("Skipping MD5 check based on user setting.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001061
Dees_Troy4a2a1262012-09-18 09:33:47 -04001062 ui_print("Restoring %i partitions...\n", partition_count);
Dees_Troy093b7642012-09-21 15:59:38 -04001063 ui->SetProgress(0.0);
1064 if (restore_sys != NULL && !Restore_Partition(restore_sys, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001065 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001066 if (restore_data != NULL && !Restore_Partition(restore_data, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001067 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001068 if (restore_cache != NULL && !Restore_Partition(restore_cache, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001069 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001070 if (restore_boot != NULL && !Restore_Partition(restore_boot, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001071 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001072 if (restore_andsec != NULL && !Restore_Partition(restore_andsec, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001073 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001074 if (restore_sdext != NULL && !Restore_Partition(restore_sdext, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001075 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001076 if (restore_sp1 != NULL && !Restore_Partition(restore_sp1, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001077 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001078 if (restore_sp2 != NULL && !Restore_Partition(restore_sp2, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001079 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001080 if (restore_sp3 != NULL && !Restore_Partition(restore_sp3, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001081 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001082
Dees_Troyb46a6842012-09-25 11:06:46 -04001083 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -04001084 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001085 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -04001086 time(&rStop);
1087 ui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -04001088 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001089}
1090
1091void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001092 // Start with the default values
1093 int tw_restore_system = -1;
1094 int tw_restore_data = -1;
1095 int tw_restore_cache = -1;
1096 int tw_restore_recovery = -1;
1097 int tw_restore_boot = -1;
1098 int tw_restore_andsec = -1;
1099 int tw_restore_sdext = -1;
1100 int tw_restore_sp1 = -1;
1101 int tw_restore_sp2 = -1;
1102 int tw_restore_sp3 = -1;
1103 bool get_date = true;
1104
1105 DIR* d;
1106 d = opendir(Restore_Name.c_str());
1107 if (d == NULL)
1108 {
1109 LOGE("Error opening %s\n", Restore_Name.c_str());
1110 return;
1111 }
1112
1113 struct dirent* de;
1114 while ((de = readdir(d)) != NULL)
1115 {
1116 // Strip off three components
1117 char str[256];
1118 char* label;
1119 char* fstype = NULL;
1120 char* extn = NULL;
1121 char* ptr;
1122
1123 strcpy(str, de->d_name);
1124 if (strlen(str) <= 2)
1125 continue;
1126
1127 if (get_date) {
1128 char file_path[255];
1129 struct stat st;
1130
1131 strcpy(file_path, Restore_Name.c_str());
1132 strcat(file_path, "/");
1133 strcat(file_path, str);
1134 stat(file_path, &st);
1135 string backup_date = ctime((const time_t*)(&st.st_mtime));
1136 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
1137 get_date = false;
1138 }
1139
1140 label = str;
1141 ptr = label;
1142 while (*ptr && *ptr != '.') ptr++;
1143 if (*ptr == '.')
1144 {
1145 *ptr = 0x00;
1146 ptr++;
1147 fstype = ptr;
1148 }
1149 while (*ptr && *ptr != '.') ptr++;
1150 if (*ptr == '.')
1151 {
1152 *ptr = 0x00;
1153 ptr++;
1154 extn = ptr;
1155 }
1156
1157 if (extn == NULL || (strlen(extn) >= 3 && strncmp(extn, "win", 3) != 0)) continue;
1158
1159 TWPartition* Part = Find_Partition_By_Path(label);
1160 if (Part == NULL)
1161 {
1162 LOGE(" Unable to locate partition by backup name: '%s'\n", label);
1163 continue;
1164 }
1165
1166 Part->Backup_FileName = de->d_name;
1167 if (strlen(extn) > 3) {
1168 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1169 }
1170
1171 // Now, we just need to find the correct label
Dees_Troye58d5262012-09-21 12:27:57 -04001172 if (Part->Backup_Path == "/system")
Dees_Troy63c8df72012-09-10 14:02:05 -04001173 tw_restore_system = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001174 if (Part->Backup_Path == "/data")
Dees_Troy63c8df72012-09-10 14:02:05 -04001175 tw_restore_data = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001176 if (Part->Backup_Path == "/cache")
Dees_Troy63c8df72012-09-10 14:02:05 -04001177 tw_restore_cache = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001178 if (Part->Backup_Path == "/recovery")
Dees_Troy63c8df72012-09-10 14:02:05 -04001179 tw_restore_recovery = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001180 if (Part->Backup_Path == "/boot")
Dees_Troy63c8df72012-09-10 14:02:05 -04001181 tw_restore_boot = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001182 if (Part->Backup_Path == "/and-sec")
Dees_Troy63c8df72012-09-10 14:02:05 -04001183 tw_restore_andsec = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001184 if (Part->Backup_Path == "/sd-ext")
Dees_Troy63c8df72012-09-10 14:02:05 -04001185 tw_restore_sdext = 1;
1186#ifdef SP1_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001187 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP1_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001188 tw_restore_sp1 = 1;
1189#endif
1190#ifdef SP2_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001191 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP2_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001192 tw_restore_sp2 = 1;
1193#endif
1194#ifdef SP3_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001195 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP3_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001196 tw_restore_sp3 = 1;
1197#endif
1198 }
1199 closedir(d);
1200
1201 // Set the final values
1202 DataManager::SetValue(TW_RESTORE_SYSTEM_VAR, tw_restore_system);
1203 DataManager::SetValue(TW_RESTORE_DATA_VAR, tw_restore_data);
1204 DataManager::SetValue(TW_RESTORE_CACHE_VAR, tw_restore_cache);
1205 DataManager::SetValue(TW_RESTORE_RECOVERY_VAR, tw_restore_recovery);
1206 DataManager::SetValue(TW_RESTORE_BOOT_VAR, tw_restore_boot);
1207 DataManager::SetValue(TW_RESTORE_ANDSEC_VAR, tw_restore_andsec);
1208 DataManager::SetValue(TW_RESTORE_SDEXT_VAR, tw_restore_sdext);
1209 DataManager::SetValue(TW_RESTORE_SP1_VAR, tw_restore_sp1);
1210 DataManager::SetValue(TW_RESTORE_SP2_VAR, tw_restore_sp2);
1211 DataManager::SetValue(TW_RESTORE_SP3_VAR, tw_restore_sp3);
1212
Dees_Troy51a0e822012-09-05 15:24:24 -04001213 return;
1214}
1215
1216int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001217 std::vector<TWPartition*>::iterator iter;
1218 int ret = false;
1219 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001220 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001221
1222 // Iterate through all partitions
1223 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001224 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001225 if (Path == "/and-sec")
1226 ret = (*iter)->Wipe_AndSec();
1227 else
1228 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001229 found = true;
1230 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1231 (*iter)->Wipe();
1232 }
1233 }
1234 if (found) {
1235 return ret;
1236 } else
1237 LOGE("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1238 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001239}
1240
1241int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001242 TWPartition* Part = Find_Partition_By_Block(Block);
1243
1244 if (Part) {
1245 if (Part->Has_SubPartition) {
1246 std::vector<TWPartition*>::iterator subpart;
1247
1248 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1249 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1250 (*subpart)->Wipe();
1251 }
1252 return Part->Wipe();
1253 } else
1254 return Part->Wipe();
1255 }
1256 LOGE("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
1257 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001258}
1259
1260int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001261 TWPartition* Part = Find_Partition_By_Name(Name);
1262
1263 if (Part) {
1264 if (Part->Has_SubPartition) {
1265 std::vector<TWPartition*>::iterator subpart;
1266
1267 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1268 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1269 (*subpart)->Wipe();
1270 }
1271 return Part->Wipe();
1272 } else
1273 return Part->Wipe();
1274 }
1275 LOGE("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
1276 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001277}
1278
1279int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001280 std::vector<TWPartition*>::iterator iter;
1281 int ret = true;
1282
1283 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001284 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001285 if (!(*iter)->Wipe())
1286 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001287 } else if ((*iter)->Has_Android_Secure) {
1288 if (!(*iter)->Wipe_AndSec())
1289 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001290 }
1291 }
1292 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001293}
1294
Dees_Troy38bd7602012-09-14 13:33:53 -04001295int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1296 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001297 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001298
1299 if (!Mount_By_Path("/data", true))
1300 return false;
1301
1302 if (!Mount_By_Path("/cache", true))
1303 return false;
1304
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001305 dir.push_back("/data/dalvik-cache");
1306 dir.push_back("/cache/dalvik-cache");
1307 dir.push_back("/cache/dc");
Dees_Troy38bd7602012-09-14 13:33:53 -04001308 ui_print("\nWiping Dalvik Cache Directories...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001309 for (int i = 0; i < dir.size(); ++i) {
1310 if (stat(dir.at(i).c_str(), &st) == 0) {
1311 TWFunc::removeDir(dir.at(i), false);
1312 ui_print("Cleaned: %s...\n", dir.at(i).c_str());
1313 }
1314 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001315 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
1316 if (sdext != NULL) {
1317 if (sdext->Is_Present && sdext->Mount(false)) {
1318 if (stat("/sd-ext/dalvik-cache", &st) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001319 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
1320 ui_print("Cleaned: /sd-ext/dalvik-cache...\n");
1321 }
1322 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001323 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001324 ui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
1325 return true;
1326}
1327
1328int TWPartitionManager::Wipe_Rotate_Data(void) {
1329 if (!Mount_By_Path("/data", true))
1330 return false;
1331
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001332 unlink("/data/misc/akmd*");
1333 unlink("/data/misc/rild*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001334 ui_print("Rotation data wiped.\n");
1335 return true;
1336}
1337
1338int TWPartitionManager::Wipe_Battery_Stats(void) {
1339 struct stat st;
1340
1341 if (!Mount_By_Path("/data", true))
1342 return false;
1343
1344 if (0 != stat("/data/system/batterystats.bin", &st)) {
1345 ui_print("No Battery Stats Found. No Need To Wipe.\n");
1346 } else {
1347 remove("/data/system/batterystats.bin");
1348 ui_print("Cleared battery stats.\n");
1349 }
1350 return true;
1351}
1352
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001353int TWPartitionManager::Wipe_Android_Secure(void) {
1354 std::vector<TWPartition*>::iterator iter;
1355 int ret = false;
1356 bool found = false;
1357
1358 // Iterate through all partitions
1359 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1360 if ((*iter)->Has_Android_Secure) {
1361 ret = (*iter)->Wipe_AndSec();
1362 found = true;
1363 }
1364 }
1365 if (found) {
1366 return ret;
1367 } else {
1368 LOGE("No android secure partitions found.\n");
1369 }
1370 return false;
1371}
1372
Dees_Troy38bd7602012-09-14 13:33:53 -04001373int TWPartitionManager::Format_Data(void) {
1374 TWPartition* dat = Find_Partition_By_Path("/data");
1375
1376 if (dat != NULL) {
1377 if (!dat->UnMount(true))
1378 return false;
1379
1380 return dat->Wipe_Encryption();
1381 } else {
1382 LOGE("Unable to locate /data.\n");
1383 return false;
1384 }
1385 return false;
1386}
1387
1388int TWPartitionManager::Wipe_Media_From_Data(void) {
1389 TWPartition* dat = Find_Partition_By_Path("/data");
1390
1391 if (dat != NULL) {
1392 if (!dat->Has_Data_Media) {
1393 LOGE("This device does not have /data/media\n");
1394 return false;
1395 }
1396 if (!dat->Mount(true))
1397 return false;
1398
1399 ui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001400 TWFunc::removeDir("/data/media", false);
1401 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
1402 return -1;
Dees_Troy38bd7602012-09-14 13:33:53 -04001403 if (dat->Has_Data_Media) {
1404 dat->Recreate_Media_Folder();
1405 }
1406 return true;
1407 } else {
1408 LOGE("Unable to locate /data.\n");
1409 return false;
1410 }
1411 return false;
1412}
1413
Dees_Troy51a0e822012-09-05 15:24:24 -04001414void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001415 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001416 return;
1417}
1418
1419void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001420 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001421 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001422
Dees_Troy32c8eb82012-09-11 15:28:06 -04001423 ui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001424 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001425 if ((*iter)->Can_Be_Mounted) {
1426 (*iter)->Update_Size(true);
1427 if ((*iter)->Mount_Point == "/system") {
1428 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1429 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1430 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1431 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1432 } else if ((*iter)->Mount_Point == "/cache") {
1433 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1434 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1435 } else if ((*iter)->Mount_Point == "/sd-ext") {
1436 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1437 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1438 if ((*iter)->Backup_Size == 0) {
1439 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1440 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1441 } else
1442 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001443 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001444 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001445 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001446 if ((*iter)->Backup_Size == 0) {
1447 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1448 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1449 } else
1450 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001451 } else if ((*iter)->Mount_Point == "/boot") {
1452 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1453 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1454 if ((*iter)->Backup_Size == 0) {
1455 DataManager::SetValue("tw_has_boot_partition", 0);
1456 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1457 } else
1458 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001459 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001460#ifdef SP1_NAME
1461 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1462 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1463 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1464 }
1465#endif
1466#ifdef SP2_NAME
1467 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1468 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1469 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1470 }
1471#endif
1472#ifdef SP3_NAME
1473 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1474 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1475 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1476 }
1477#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001478 } else {
1479 // Handle unmountable partitions in case we reset defaults
1480 if ((*iter)->Mount_Point == "/boot") {
1481 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1482 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1483 if ((*iter)->Backup_Size == 0) {
1484 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1485 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1486 } else
1487 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1488 } else if ((*iter)->Mount_Point == "/recovery") {
1489 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1490 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1491 if ((*iter)->Backup_Size == 0) {
1492 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1493 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1494 } else
1495 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001496 } else if ((*iter)->Mount_Point == "/data") {
1497 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001498 }
1499#ifdef SP1_NAME
1500 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1501 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1502 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1503 }
1504#endif
1505#ifdef SP2_NAME
1506 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1507 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1508 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1509 }
1510#endif
1511#ifdef SP3_NAME
1512 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1513 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1514 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1515 }
1516#endif
Dees_Troy51127312012-09-08 13:08:49 -04001517 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001518 }
Dees_Troy51127312012-09-08 13:08:49 -04001519 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1520 string current_storage_path = DataManager::GetCurrentStoragePath();
1521 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001522 if (FreeStorage != NULL) {
1523 // Attempt to mount storage
1524 if (!FreeStorage->Mount(false)) {
1525 // We couldn't mount storage... check to see if we have dual storage
1526 int has_dual_storage;
1527 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1528 if (has_dual_storage == 1) {
1529 // We have dual storage, see if we're using the internal storage that should always be present
1530 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001531 if (!FreeStorage->Is_Encrypted) {
1532 // Not able to use internal, so error!
1533 LOGE("Unable to mount internal storage.\n");
1534 }
Dees_Troy8170a922012-09-18 15:40:25 -04001535 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1536 } else {
1537 // We were using external, flip to internal
1538 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1539 current_storage_path = DataManager::GetCurrentStoragePath();
1540 FreeStorage = Find_Partition_By_Path(current_storage_path);
1541 if (FreeStorage != NULL) {
1542 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1543 } else {
1544 LOGE("Unable to locate internal storage partition.\n");
1545 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1546 }
1547 }
1548 } else {
1549 // No dual storage and unable to mount storage, error!
1550 LOGE("Unable to mount storage.\n");
1551 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1552 }
1553 } else {
1554 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1555 }
1556 } else {
Dees_Troy51127312012-09-08 13:08:49 -04001557 LOGI("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001558 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001559 if (!Write_Fstab())
1560 LOGE("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001561 return;
1562}
1563
1564int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001565#ifdef TW_INCLUDE_CRYPTO
1566 int ret_val, password_len;
1567 char crypto_blkdev[255], cPassword[255];
1568 size_t result;
1569
1570 property_set("ro.crypto.state", "encrypted");
1571#ifdef TW_INCLUDE_JB_CRYPTO
1572 // No extra flags needed
1573#else
1574 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1575 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1576 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1577 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1578 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1579 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001580
1581#ifdef CRYPTO_SD_FS_TYPE
1582 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1583 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1584 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001585#endif
a39552696ff55ce2013-01-08 16:14:56 +00001586
1587 property_set("rw.km_fips_status", "ready");
1588
1589#endif
1590
1591 // some samsung devices store "footer" on efs partition
1592 TWPartition *efs = Find_Partition_By_Path("/efs");
1593 if(efs && !efs->Is_Mounted())
1594 efs->Mount(false);
1595 else
1596 efs = 0;
1597#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001598#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001599 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001600 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001601 property_set("ro.crypto.external_encrypted", "1");
1602 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1603 } else {
1604 property_set("ro.crypto.external_encrypted", "0");
1605 }
1606#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001607#endif
a39552696ff55ce2013-01-08 16:14:56 +00001608
Dees_Troy5bf43922012-09-07 16:07:55 -04001609 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001610 int pwret = cryptfs_check_passwd(cPassword);
1611
1612 if (pwret != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001613 LOGE("Failed to decrypt data.\n");
1614 return -1;
1615 }
a39552696ff55ce2013-01-08 16:14:56 +00001616
1617 if(efs)
1618 efs->UnMount(false);
1619
Dees_Troy5bf43922012-09-07 16:07:55 -04001620 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1621 if (strcmp(crypto_blkdev, "error") == 0) {
1622 LOGE("Error retrieving decrypted data block device.\n");
1623 } else {
1624 TWPartition* dat = Find_Partition_By_Path("/data");
1625 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001626 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001627 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1628 dat->Is_Decrypted = true;
1629 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001630 dat->Setup_File_System(false);
Dees_Troy32c8eb82012-09-11 15:28:06 -04001631 ui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001632
1633#ifdef CRYPTO_SD_FS_TYPE
1634 char crypto_blkdev_sd[255];
1635 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1636 if (strcmp(crypto_blkdev_sd, "error") == 0) {
1637 LOGE("Error retrieving decrypted data block device.\n");
1638 } else if(TWPartition* emmc = Find_Partition_By_Path(TW_INTERNAL_STORAGE_PATH)){
1639 emmc->Is_Decrypted = true;
1640 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1641 emmc->Setup_File_System(false);
1642 ui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
1643 }
a39552696ff55ce2013-01-08 16:14:56 +00001644#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001645#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001646#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001647 char is_external_decrypted[255];
1648 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1649 if (strcmp(is_external_decrypted, "1") == 0) {
1650 sdcard->Is_Decrypted = true;
1651 sdcard->EcryptFS_Password = Password;
1652 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
1653 } else {
1654 sdcard->Is_Decrypted = false;
1655 sdcard->Decrypted_Block_Device = "";
1656 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001657#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001658#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001659
Dees_Troy5bf43922012-09-07 16:07:55 -04001660 // Sleep for a bit so that the device will be ready
1661 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001662#ifdef RECOVERY_SDCARD_ON_DATA
1663 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1664 dat->Storage_Path = "/data/media/0";
1665 dat->Symlink_Path = dat->Storage_Path;
1666 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
1667 dat->UnMount(false);
1668 DataManager::SetBackupFolder();
1669 }
1670#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001671 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001672 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001673 } else
1674 LOGE("Unable to locate data partition.\n");
1675 }
1676 return 0;
1677#else
1678 LOGE("No crypto support was compiled into this build.\n");
1679 return -1;
1680#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001681 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001682}
1683
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001684int TWPartitionManager::Fix_Permissions(void) {
1685 int result = 0;
1686 if (!Mount_By_Path("/data", true))
1687 return false;
1688
1689 if (!Mount_By_Path("/system", true))
1690 return false;
1691
1692 Mount_By_Path("/sd-ext", false);
1693
1694 fixPermissions perms;
1695 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001696 UnMount_Main_Partitions();
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001697 ui_print("Done.\n\n");
1698 return result;
1699}
1700
Dees_Troy8170a922012-09-18 15:40:25 -04001701//partial kangbang from system/vold
1702#ifndef CUSTOM_LUN_FILE
1703#define CUSTOM_LUN_FILE "/sys/devices/platform/usb_mass_storage/lun%d/file"
1704#endif
1705
Dees_Troyd21618c2012-10-14 18:48:49 -04001706int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
1707 int fd;
1708 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1709
1710 if (Part == NULL) {
1711 LOGE("Unable to locate volume information for USB storage mode.");
1712 return false;
1713 }
1714 if (!Part->UnMount(true))
1715 return false;
1716
1717 if ((fd = open(Lun_File.c_str(), O_WRONLY)) < 0) {
1718 LOGE("Unable to open ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
1719 return false;
1720 }
1721
1722 if (write(fd, Part->Actual_Block_Device.c_str(), Part->Actual_Block_Device.size()) < 0) {
1723 LOGE("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
1724 close(fd);
1725 return false;
1726 }
1727 close(fd);
1728 return true;
1729}
1730
Dees_Troy8170a922012-09-18 15:40:25 -04001731int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001732 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001733 char lun_file[255];
Dees_Troy8170a922012-09-18 15:40:25 -04001734 string ext_path;
Dees_Troyd21618c2012-10-14 18:48:49 -04001735 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001736
1737 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1738 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1739 if (has_dual == 1 && has_data_media == 0) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001740 string Lun_File_str = CUSTOM_LUN_FILE;
1741 size_t found = Lun_File_str.find("%");
1742 if (found != string::npos) {
1743 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1744 if (TWFunc::Path_Exists(lun_file))
1745 has_multiple_lun = true;
1746 }
1747 if (!has_multiple_lun) {
Dees_Troyf4ca6122012-10-13 22:17:20 -04001748 // Device doesn't have multiple lun files, mount current storage
Dees_Troyf4ca6122012-10-13 22:17:20 -04001749 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001750 return Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001751 } else {
1752 // Device has multiple lun files
Dees_Troyf4ca6122012-10-13 22:17:20 -04001753 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001754 if (!Open_Lun_File(DataManager::GetSettingsStoragePath(), lun_file))
Dees_Troyf4ca6122012-10-13 22:17:20 -04001755 return false;
Dees_Troyf4ca6122012-10-13 22:17:20 -04001756 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001757 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Dees_Troyd21618c2012-10-14 18:48:49 -04001758 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001759 }
Dees_Troy8170a922012-09-18 15:40:25 -04001760 } else {
1761 if (has_data_media == 0)
1762 ext_path = DataManager::GetCurrentStoragePath();
1763 else
1764 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001765 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001766 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001767 }
1768 return true;
1769}
1770
1771int TWPartitionManager::usb_storage_disable(void) {
1772 int fd, index;
1773 char lun_file[255];
1774
1775 for (index=0; index<2; index++) {
1776 sprintf(lun_file, CUSTOM_LUN_FILE, index);
1777
1778 if ((fd = open(lun_file, O_WRONLY)) < 0) {
Dees_Troye58d5262012-09-21 12:27:57 -04001779 Mount_All_Storage();
1780 Update_System_Details();
1781 if (index == 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001782 LOGE("Unable to open ums lunfile '%s': (%s)", lun_file, strerror(errno));
Dees_Troye58d5262012-09-21 12:27:57 -04001783 return false;
1784 } else
1785 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001786 }
1787
1788 char ch = 0;
1789 if (write(fd, &ch, 1) < 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001790 close(fd);
Dees_Troye58d5262012-09-21 12:27:57 -04001791 Mount_All_Storage();
1792 Update_System_Details();
1793 if (index == 0) {
1794 LOGE("Unable to write to ums lunfile '%s': (%s)", lun_file, strerror(errno));
1795 return false;
1796 } else
1797 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001798 }
1799
1800 close(fd);
1801 }
Dees_Troye58d5262012-09-21 12:27:57 -04001802 Mount_All_Storage();
1803 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001804 UnMount_Main_Partitions();
Dees_Troy8170a922012-09-18 15:40:25 -04001805 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001806}
1807
1808void TWPartitionManager::Mount_All_Storage(void) {
1809 std::vector<TWPartition*>::iterator iter;
1810
1811 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1812 if ((*iter)->Is_Storage)
1813 (*iter)->Mount(false);
1814 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001815}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001816
Dees_Troyd0384ef2012-10-12 12:15:42 -04001817void TWPartitionManager::UnMount_Main_Partitions(void) {
1818 // Unmounts system and data if data is not data/media
1819 // Also unmounts boot if boot is mountable
1820 LOGI("Unmounting main partitions...\n");
1821
1822 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1823
1824 UnMount_By_Path("/system", true);
1825#ifndef RECOVERY_SDCARD_ON_DATA
1826 UnMount_By_Path("/data", true);
1827#endif
1828 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1829 Boot_Partition->UnMount(true);
1830}
1831
Dees_Troy9350b8d2012-09-27 12:38:38 -04001832int TWPartitionManager::Partition_SDCard(void) {
1833 char mkdir_path[255], temp[255], line[512];
1834 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1835 int ext, swap, total_size = 0, fat_size;
1836 FILE* fp;
1837
1838 ui_print("Partitioning SD Card...\n");
1839#ifdef TW_EXTERNAL_STORAGE_PATH
1840 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1841#else
1842 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1843#endif
1844 if (SDCard == NULL) {
1845 LOGE("Unable to locate device to partition.\n");
1846 return false;
1847 }
1848 if (!SDCard->UnMount(true))
1849 return false;
1850 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1851 if (SDext != NULL) {
1852 if (!SDext->UnMount(true))
1853 return false;
1854 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001855 string result;
1856 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"", result);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001857 Device = SDCard->Actual_Block_Device;
1858 // Just use the root block device
1859 Device.resize(strlen("/dev/block/mmcblkX"));
1860
1861 // Find the size of the block device:
1862 fp = fopen("/proc/partitions", "rt");
1863 if (fp == NULL) {
1864 LOGE("Unable to open /proc/partitions\n");
1865 return false;
1866 }
1867
1868 while (fgets(line, sizeof(line), fp) != NULL)
1869 {
1870 unsigned long major, minor, blocks;
1871 char device[512];
1872 char tmpString[64];
1873
1874 if (strlen(line) < 7 || line[0] == 'm') continue;
1875 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1876
1877 tmpdevice = "/dev/block/";
1878 tmpdevice += device;
1879 if (tmpdevice == Device) {
1880 // Adjust block size to byte size
1881 total_size = (int)(blocks * 1024ULL / 1000000LLU);
1882 break;
1883 }
1884 }
1885 fclose(fp);
1886
1887 DataManager::GetValue("tw_sdext_size", ext);
1888 DataManager::GetValue("tw_swap_size", swap);
1889 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1890 fat_size = total_size - ext - swap;
1891 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);
1892 memset(temp, 0, sizeof(temp));
1893 sprintf(temp, "%i", fat_size);
1894 fat_str = temp;
1895 memset(temp, 0, sizeof(temp));
1896 sprintf(temp, "%i", fat_size + ext);
1897 ext_str = temp;
1898 memset(temp, 0, sizeof(temp));
1899 sprintf(temp, "%i", fat_size + ext + swap);
1900 swap_str = temp;
1901 if (ext + swap > total_size) {
1902 LOGE("EXT + Swap size is larger than sdcard size.\n");
1903 return false;
1904 }
1905 ui_print("Removing partition table...\n");
1906 Command = "parted -s " + Device + " mklabel msdos";
1907 LOGI("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001908 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001909 LOGE("Unable to remove partition table.\n");
1910 Update_System_Details();
1911 return false;
1912 }
1913 ui_print("Creating FAT32 partition...\n");
1914 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
1915 LOGI("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001916 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001917 LOGE("Unable to create FAT32 partition.\n");
1918 return false;
1919 }
1920 if (ext > 0) {
1921 ui_print("Creating EXT partition...\n");
1922 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
1923 LOGI("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001924 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001925 LOGE("Unable to create EXT partition.\n");
1926 Update_System_Details();
1927 return false;
1928 }
1929 }
1930 if (swap > 0) {
1931 ui_print("Creating swap partition...\n");
1932 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
1933 LOGI("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001934 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001935 LOGE("Unable to create swap partition.\n");
1936 Update_System_Details();
1937 return false;
1938 }
1939 }
1940 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1941#ifdef TW_EXTERNAL_STORAGE_PATH
1942 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1943 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1944 memset(mkdir_path, 0, sizeof(mkdir_path));
1945 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1946#else
1947 Mount_By_Path("/sdcard", 1);
1948 strcpy(mkdir_path, "/sdcard/TWRP");
1949#endif
1950 mkdir(mkdir_path, 0777);
1951 DataManager::Flush();
1952#ifdef TW_EXTERNAL_STORAGE_PATH
1953 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1954 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1955 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1956#else
1957 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1958 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1959 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1960#endif
1961 if (ext > 0) {
1962 if (SDext == NULL) {
1963 LOGE("Unable to locate sd-ext partition.\n");
1964 return false;
1965 }
1966 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
1967 ui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1968 LOGI("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001969 TWFunc::Exec_Cmd(Command, result);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001970 }
1971
1972 Update_System_Details();
1973 ui_print("Partitioning complete.\n");
1974 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001975}