blob: d7f1600efe358f3c77cc11b23a06ad4d7e479e49 [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"
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "twcommon.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"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050042#include "twrpDigest.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
53int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040054 FILE *fstabFile;
55 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -050056 bool Found_Settings_Storage = false;
Dees_Troy5bf43922012-09-07 16:07:55 -040057
58 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
59 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000060 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040061 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)) {
Dees_Troya13d74f2013-03-24 08:54:55 -050076 if (!Found_Settings_Storage && partition->Is_Settings_Storage) {
77 Found_Settings_Storage = true;
78 Partitions.push_back(partition);
79 DataManager::SetValue("tw_settings_path", partition->Storage_Path);
80 DataManager::SetValue("tw_storage_path", partition->Storage_Path);
Dees_Troy2673cec2013-04-02 20:22:16 +000081 LOGINFO("Settings storage is '%s'\n", partition->Storage_Path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -050082 } else {
83 partition->Is_Settings_Storage = false;
84 Partitions.push_back(partition);
85 }
Dees_Troy5bf43922012-09-07 16:07:55 -040086 } else {
87 delete partition;
88 }
89 }
90 fclose(fstabFile);
Dees_Troya13d74f2013-03-24 08:54:55 -050091 if (!Found_Settings_Storage) {
92 std::vector<TWPartition*>::iterator iter;
93 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
94 if ((*iter)->Is_Storage) {
95 (*iter)->Is_Settings_Storage = true;
96 Found_Settings_Storage = true;
97 DataManager::SetValue("tw_settings_path", (*iter)->Storage_Path);
98 DataManager::SetValue("tw_storage_path", (*iter)->Storage_Path);
Dees_Troy2673cec2013-04-02 20:22:16 +000099 LOGINFO("Settings storage is '%s'\n", (*iter)->Storage_Path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500100 break;
101 }
102 }
103 if (!Found_Settings_Storage)
Dees_Troy2673cec2013-04-02 20:22:16 +0000104 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500105 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400106 if (!Write_Fstab()) {
107 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000108 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400109 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000110 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400111 }
Dees_Troy51127312012-09-08 13:08:49 -0400112 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400113 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400114 return true;
115}
116
117int TWPartitionManager::Write_Fstab(void) {
118 FILE *fp;
119 std::vector<TWPartition*>::iterator iter;
120 string Line;
121
122 fp = fopen("/etc/fstab", "w");
123 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000124 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400125 return false;
126 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400127 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400128 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400129 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400130 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000131 }
132 // Handle subpartition tracking
133 if ((*iter)->Is_SubPartition) {
134 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
135 if (ParentPartition)
136 ParentPartition->Has_SubPartition = true;
137 else
138 LOGERR("Unable to locate parent partition '%s' of '%s'\n", (*iter)->SubPartition_Of.c_str(), (*iter)->Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400139 }
140 }
141 fclose(fp);
142 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400143}
144
Dees_Troy8170a922012-09-18 15:40:25 -0400145void TWPartitionManager::Output_Partition_Logging(void) {
146 std::vector<TWPartition*>::iterator iter;
147
148 printf("\n\nPartition Logs:\n");
149 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
150 Output_Partition((*iter));
151}
152
153void TWPartitionManager::Output_Partition(TWPartition* Part) {
154 unsigned long long mb = 1048576;
155
Gary Peck004d48b2012-11-21 16:28:18 -0800156 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 -0400157 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800158 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 -0400159 }
Gary Peck004d48b2012-11-21 16:28:18 -0800160 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500161 if (Part->Can_Be_Mounted)
162 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800163 if (Part->Can_Be_Wiped)
164 printf("Can_Be_Wiped ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500165 if (Part->Can_Be_Backed_Up)
166 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800167 if (Part->Wipe_During_Factory_Reset)
168 printf("Wipe_During_Factory_Reset ");
169 if (Part->Wipe_Available_in_GUI)
170 printf("Wipe_Available_in_GUI ");
171 if (Part->Is_SubPartition)
172 printf("Is_SubPartition ");
173 if (Part->Has_SubPartition)
174 printf("Has_SubPartition ");
175 if (Part->Removable)
176 printf("Removable ");
177 if (Part->Is_Present)
178 printf("IsPresent ");
179 if (Part->Can_Be_Encrypted)
180 printf("Can_Be_Encrypted ");
181 if (Part->Is_Encrypted)
182 printf("Is_Encrypted ");
183 if (Part->Is_Decrypted)
184 printf("Is_Decrypted ");
185 if (Part->Has_Data_Media)
186 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000187 if (Part->Can_Encrypt_Backup)
188 printf("Can_Encrypt_Backup ");
189 if (Part->Use_Userdata_Encryption)
190 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800191 if (Part->Has_Android_Secure)
192 printf("Has_Android_Secure ");
193 if (Part->Is_Storage)
194 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500195 if (Part->Is_Settings_Storage)
196 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000197 if (Part->Ignore_Blkid)
198 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000199 if (Part->Retain_Layout_Version)
200 printf("Retain_Layout_Version ");
Gary Peck004d48b2012-11-21 16:28:18 -0800201 printf("\n");
202 if (!Part->SubPartition_Of.empty())
203 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
204 if (!Part->Symlink_Path.empty())
205 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
206 if (!Part->Symlink_Mount_Point.empty())
207 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
208 if (!Part->Primary_Block_Device.empty())
209 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
210 if (!Part->Alternate_Block_Device.empty())
211 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
212 if (!Part->Decrypted_Block_Device.empty())
213 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
214 if (Part->Length != 0)
215 printf(" Length: %i\n", Part->Length);
216 if (!Part->Display_Name.empty())
217 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500218 if (!Part->Storage_Name.empty())
219 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800220 if (!Part->Backup_Path.empty())
221 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
222 if (!Part->Backup_Name.empty())
223 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500224 if (!Part->Backup_Display_Name.empty())
225 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800226 if (!Part->Backup_FileName.empty())
227 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
228 if (!Part->Storage_Path.empty())
229 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
230 if (!Part->Current_File_System.empty())
231 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
232 if (!Part->Fstab_File_System.empty())
233 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
234 if (Part->Format_Block_Size != 0)
235 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400236 if (!Part->MTD_Name.empty())
237 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400238 string back_meth = Part->Backup_Method_By_Name();
239 printf(" Backup_Method: %s\n\n", back_meth.c_str());
240}
241
Dees_Troy51a0e822012-09-05 15:24:24 -0400242int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400243 std::vector<TWPartition*>::iterator iter;
244 int ret = false;
245 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400246 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400247
Dees_Troy43d8b002012-09-17 16:00:01 -0400248 if (Local_Path == "/tmp")
249 return true;
250
Dees_Troy5bf43922012-09-07 16:07:55 -0400251 // Iterate through all partitions
252 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400253 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400254 ret = (*iter)->Mount(Display_Error);
255 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400256 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400257 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400258 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400259 }
260 if (found) {
261 return ret;
262 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000263 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400264 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000265 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400266 }
267 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400268}
269
270int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400271 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400272
Dees_Troy51127312012-09-08 13:08:49 -0400273 if (Part) {
274 if (Part->Has_SubPartition) {
275 std::vector<TWPartition*>::iterator subpart;
276
277 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
278 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
279 (*subpart)->Mount(Display_Error);
280 }
281 return Part->Mount(Display_Error);
282 } else
283 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400284 }
285 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000286 LOGERR("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400287 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000288 LOGINFO("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400289 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400290}
291
292int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400293 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400294
Dees_Troy51127312012-09-08 13:08:49 -0400295 if (Part) {
296 if (Part->Has_SubPartition) {
297 std::vector<TWPartition*>::iterator subpart;
298
299 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
300 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
301 (*subpart)->Mount(Display_Error);
302 }
303 return Part->Mount(Display_Error);
304 } else
305 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400306 }
307 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000308 LOGERR("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400309 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000310 LOGINFO("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400311 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400312}
313
314int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400315 std::vector<TWPartition*>::iterator iter;
316 int ret = false;
317 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400318 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400319
320 // Iterate through all partitions
321 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400322 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400323 ret = (*iter)->UnMount(Display_Error);
324 found = true;
325 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
326 (*iter)->UnMount(Display_Error);
327 }
328 }
329 if (found) {
330 return ret;
331 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000332 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400333 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000334 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400335 }
336 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400337}
338
339int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400340 TWPartition* Part = Find_Partition_By_Block(Block);
341
342 if (Part) {
343 if (Part->Has_SubPartition) {
344 std::vector<TWPartition*>::iterator subpart;
345
346 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
347 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
348 (*subpart)->UnMount(Display_Error);
349 }
350 return Part->UnMount(Display_Error);
351 } else
352 return Part->UnMount(Display_Error);
353 }
354 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000355 LOGERR("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400356 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000357 LOGINFO("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400358 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400359}
360
361int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400362 TWPartition* Part = Find_Partition_By_Name(Name);
363
364 if (Part) {
365 if (Part->Has_SubPartition) {
366 std::vector<TWPartition*>::iterator subpart;
367
368 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
369 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
370 (*subpart)->UnMount(Display_Error);
371 }
372 return Part->UnMount(Display_Error);
373 } else
374 return Part->UnMount(Display_Error);
375 }
376 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000377 LOGERR("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400378 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000379 LOGINFO("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400380 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400381}
382
383int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400384 TWPartition* Part = Find_Partition_By_Path(Path);
385
386 if (Part)
387 return Part->Is_Mounted();
388 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000389 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400390 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400391}
392
393int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400394 TWPartition* Part = Find_Partition_By_Block(Block);
395
396 if (Part)
397 return Part->Is_Mounted();
398 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000399 LOGINFO("Is_Mounted: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400400 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400401}
402
403int TWPartitionManager::Is_Mounted_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400404 TWPartition* Part = Find_Partition_By_Name(Name);
405
406 if (Part)
407 return Part->Is_Mounted();
408 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000409 LOGINFO("Is_Mounted: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400410 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400411}
412
Dees_Troy5bf43922012-09-07 16:07:55 -0400413int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400414 string current_storage_path = DataManager::GetCurrentStoragePath();
415
416 if (Mount_By_Path(current_storage_path, Display_Error)) {
417 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
418 if (FreeStorage)
419 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
420 return true;
421 }
422 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400423}
424
Dees_Troy5bf43922012-09-07 16:07:55 -0400425int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
426 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
427}
428
429TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
430 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400431 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400432
433 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400434 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400435 return (*iter);
436 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400437 return NULL;
438}
439
Dees_Troy5bf43922012-09-07 16:07:55 -0400440TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400441 std::vector<TWPartition*>::iterator iter;
442
443 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400444 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 -0400445 return (*iter);
446 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400447 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400448}
449
450TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400451 std::vector<TWPartition*>::iterator iter;
452
453 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
454 if ((*iter)->Display_Name == Name)
455 return (*iter);
456 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400457 return NULL;
458}
Dees_Troy51a0e822012-09-05 15:24:24 -0400459
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400460int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
461 // Check the backup name to ensure that it is the correct size and contains only valid characters
462 // and that a backup with that name doesn't already exist
463 char backup_name[MAX_BACKUP_NAME_LEN];
464 char backup_loc[255], tw_image_dir[255];
465 int copy_size;
466 int index, cur_char;
467 string Backup_Name, Backup_Loc;
468
469 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
470 copy_size = Backup_Name.size();
471 // Check size
472 if (copy_size > MAX_BACKUP_NAME_LEN) {
473 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000474 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400475 return -2;
476 }
477
478 // Check each character
479 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500480 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400481 return 0; // A "0" (zero) means to use the current timestamp for the backup name
482 for (index=0; index<copy_size; index++) {
483 cur_char = (int)backup_name[index];
484 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) {
485 // These are valid characters
486 // Numbers
487 // Upper case letters
488 // Lower case letters
489 // Space
490 // and -_.{}[]
491 } else {
492 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000493 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400494 return -3;
495 }
496 }
497
498 // Check to make sure that a backup with this name doesn't already exist
499 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
500 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500501 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400502 if (TWFunc::Path_Exists(tw_image_dir)) {
503 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000504 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400505 return -4;
506 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400507 // No problems found, return 0
508 return 0;
509}
510
Dees_Troy43d8b002012-09-17 16:00:01 -0400511bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
512{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500513 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400514 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500515 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500516 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400517
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500518 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400519 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400520
Dees_Troyb46a6842012-09-25 11:06:46 -0400521 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000522 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400523
524 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500525 md5sum.setfn(Backup_Folder + Backup_Filename);
526 if (md5sum.computeMD5() == 0)
527 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000528 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500529 else
530 return -1;
531 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000532 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400533 } else {
534 char filename[512];
535 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500536 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400537 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500538 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000539 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400540 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000541 if (TWFunc::Path_Exists(filename)) {
542 if (md5sum.computeMD5() == 0) {
543 if (md5sum.write_md5digest() != 0)
544 {
545 gui_print(" * MD5 Error.\n");
546 return false;
547 }
548 } else {
549 gui_print(" * Error computing MD5.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500550 return false;
551 }
552 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400553 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400554 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500555 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400556 }
557 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000558 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400559 return false;
560 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000561 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400562 }
563 return true;
564}
565
Dees_Troy093b7642012-09-21 15:59:38 -0400566bool 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 -0400567 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500568 int img_bps;
569 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400570 unsigned long total_time, remain_time, section_time;
571 int use_compression, backup_time;
572 float pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400573
574 if (Part == NULL)
575 return true;
576
Dees_Troy093b7642012-09-21 15:59:38 -0400577 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
578
579 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
580 if (use_compression)
581 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
582 else
583 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
584
585 // We know the speed for both, how far into the whole backup are we, based on time
586 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
587 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
588
589 pos = (total_time - remain_time) / (float) total_time;
Dees_Troy2673cec2013-04-02 20:22:16 +0000590 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400591
Dees_Troy2673cec2013-04-02 20:22:16 +0000592 LOGINFO("Estimated Total time: %lu Estimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400593
594 // And get the time
595 if (Part->Backup_Method == 1)
596 section_time = Part->Backup_Size / file_bps;
597 else
598 section_time = Part->Backup_Size / img_bps;
599
600 // Set the position
601 pos = section_time / (float) total_time;
Dees_Troy2673cec2013-04-02 20:22:16 +0000602 DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400603
Dees_Troy43d8b002012-09-17 16:00:01 -0400604 time(&start);
605
606 if (Part->Backup(Backup_Folder)) {
Dees_Troy8170a922012-09-18 15:40:25 -0400607 if (Part->Has_SubPartition) {
608 std::vector<TWPartition*>::iterator subpart;
609
610 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500611 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Dees_Troy8170a922012-09-18 15:40:25 -0400612 if (!(*subpart)->Backup(Backup_Folder))
613 return false;
614 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
615 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400616 if (Part->Backup_Method == 1) {
617 *file_bytes_remaining -= (*subpart)->Backup_Size;
618 } else {
619 *img_bytes_remaining -= (*subpart)->Backup_Size;
620 }
Dees_Troy8170a922012-09-18 15:40:25 -0400621 }
622 }
623 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400624 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400625 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000626 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400627 if (Part->Backup_Method == 1) {
628 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400629 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400630 } else {
631 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400632 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400633 }
634 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
635 } else {
636 return false;
637 }
638}
639
640int TWPartitionManager::Run_Backup(void) {
641 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500642 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400643 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 -0400644 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500645 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400646 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400647 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400648 struct tm *t;
649 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500650 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400651 seconds = time(0);
652 t = localtime(&seconds);
653
654 time(&total_start);
655
656 Update_System_Details();
657
658 if (!Mount_Current_Storage(true))
659 return false;
660
661 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400662 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400663 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400664 else
665 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400666
Dees_Troy43d8b002012-09-17 16:00:01 -0400667 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
668 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400669 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name.empty()) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400670 char timestamp[255];
671 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);
672 Backup_Name = timestamp;
673 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000674 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400675 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000676 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400677
Dees_Troy2673cec2013-04-02 20:22:16 +0000678 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500679 DataManager::GetValue("tw_backup_list", Backup_List);
680 if (!Backup_List.empty()) {
681 end_pos = Backup_List.find(";", start_pos);
682 while (end_pos != string::npos && start_pos < Backup_List.size()) {
683 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
684 backup_part = Find_Partition_By_Path(backup_path);
685 if (backup_part != NULL) {
686 partition_count++;
687 if (backup_part->Backup_Method == 1)
688 file_bytes += backup_part->Backup_Size;
689 else
690 img_bytes += backup_part->Backup_Size;
691 if (backup_part->Has_SubPartition) {
692 std::vector<TWPartition*>::iterator subpart;
693
694 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000695 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == backup_part->Mount_Point) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500696 partition_count++;
697 if ((*subpart)->Backup_Method == 1)
698 file_bytes += (*subpart)->Backup_Size;
699 else
700 img_bytes += (*subpart)->Backup_Size;
701 }
702 }
Dees_Troy8170a922012-09-18 15:40:25 -0400703 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500704 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000705 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400706 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500707 start_pos = end_pos + 1;
708 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400709 }
710 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400711
712 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000713 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400714 return false;
715 }
716 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000717 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
718 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400719 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
720 if (storage != NULL) {
721 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000722 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400723 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000724 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400725 return false;
726 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000727 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400728 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000729 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400730 return false;
731 }
732 img_bytes_remaining = img_bytes;
733 file_bytes_remaining = file_bytes;
734
Dees_Troy2673cec2013-04-02 20:22:16 +0000735 gui_print("\n[BACKUP STARTED]\n");
736 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000737 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000738 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000739 return false;
740 }
741
Dees_Troy2673cec2013-04-02 20:22:16 +0000742 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400743
Dees_Troya13d74f2013-03-24 08:54:55 -0500744 start_pos = 0;
745 end_pos = Backup_List.find(";", start_pos);
746 while (end_pos != string::npos && start_pos < Backup_List.size()) {
747 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
748 backup_part = Find_Partition_By_Path(backup_path);
749 if (backup_part != NULL) {
750 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
751 return false;
752 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000753 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500754 }
755 start_pos = end_pos + 1;
756 end_pos = Backup_List.find(";", start_pos);
757 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400758
759 // Average BPS
760 if (img_time == 0)
761 img_time = 1;
762 if (file_time == 0)
763 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400764 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500765 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400766
Dees_Troy2673cec2013-04-02 20:22:16 +0000767 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
768 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400769
770 time(&total_stop);
771 int total_time = (int) difftime(total_stop, total_start);
772 unsigned long long actual_backup_size = TWFunc::Get_Folder_Size(Full_Backup_Path, true);
773 actual_backup_size /= (1024LLU * 1024LLU);
774
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500775 int prev_img_bps, use_compression;
776 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400777 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
778 img_bps += (prev_img_bps * 4);
779 img_bps /= 5;
780
781 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
782 if (use_compression)
783 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
784 else
785 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
786 file_bps += (prev_file_bps * 4);
787 file_bps /= 5;
788
789 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
790 if (use_compression)
791 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
792 else
793 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
794
Dees_Troy2673cec2013-04-02 20:22:16 +0000795 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400796 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400797 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +0000798 gui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000799 string backup_log = Full_Backup_Path + "recovery.log";
800 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
801 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400802}
803
Dees_Troy093b7642012-09-21 15:59:38 -0400804bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400805 time_t Start, Stop;
806 time(&Start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000807 DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400808 if (!Part->Restore(Restore_Name))
809 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400810 if (Part->Has_SubPartition) {
811 std::vector<TWPartition*>::iterator subpart;
812
813 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
814 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
815 if (!(*subpart)->Restore(Restore_Name))
816 return false;
817 }
818 }
819 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400820 time(&Stop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000821 gui_print("[%s done (%d seconds)]\n\n", Part->Backup_Display_Name.c_str(), (int)difftime(Stop, Start));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400822 return true;
823}
824
Dees_Troy51a0e822012-09-05 15:24:24 -0400825int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400826 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500827 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400828 time_t rStart, rStop;
829 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500830 string Restore_List, restore_path;
831 size_t start_pos = 0, end_pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400832
Dees_Troy2673cec2013-04-02 20:22:16 +0000833 gui_print("\n[RESTORE STARTED]\n\n");
834 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400835
Dees_Troy4a2a1262012-09-18 09:33:47 -0400836 if (!Mount_Current_Storage(true))
837 return false;
838
839 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500840 if (check_md5 > 0) {
841 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
842 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000843 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500844 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000845 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500846 }
847 DataManager::GetValue("tw_restore_selected", Restore_List);
848 if (!Restore_List.empty()) {
849 end_pos = Restore_List.find(";", start_pos);
850 while (end_pos != string::npos && start_pos < Restore_List.size()) {
851 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
852 restore_part = Find_Partition_By_Path(restore_path);
853 if (restore_part != NULL) {
854 partition_count++;
855 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
856 return false;
857 if (restore_part->Has_SubPartition) {
858 std::vector<TWPartition*>::iterator subpart;
859
860 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
861 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
862 if (!(*subpart)->Check_MD5(Restore_Name))
863 return false;
864 }
865 }
866 }
867 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000868 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500869 }
870 start_pos = end_pos + 1;
871 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400872 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400873 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400874
875 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000876 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400877 return false;
878 }
879
Dees_Troy2673cec2013-04-02 20:22:16 +0000880 gui_print("Restoring %i partitions...\n", partition_count);
881 DataManager::SetProgress(0.0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500882 start_pos = 0;
883 if (!Restore_List.empty()) {
884 end_pos = Restore_List.find(";", start_pos);
885 while (end_pos != string::npos && start_pos < Restore_List.size()) {
886 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
887 restore_part = Find_Partition_By_Path(restore_path);
888 if (restore_part != NULL) {
889 partition_count++;
890 if (!Restore_Partition(restore_part, Restore_Name, partition_count))
891 return false;
892 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000893 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500894 }
895 start_pos = end_pos + 1;
896 end_pos = Restore_List.find(";", start_pos);
897 }
898 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400899
Dees_Troyb46a6842012-09-25 11:06:46 -0400900 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400901 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400902 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400903 time(&rStop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000904 gui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -0400905 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400906}
907
908void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400909 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500910 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000911 bool get_date = true, check_encryption = true;
912
913 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400914
915 DIR* d;
916 d = opendir(Restore_Name.c_str());
917 if (d == NULL)
918 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000919 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400920 return;
921 }
922
923 struct dirent* de;
924 while ((de = readdir(d)) != NULL)
925 {
926 // Strip off three components
927 char str[256];
928 char* label;
929 char* fstype = NULL;
930 char* extn = NULL;
931 char* ptr;
932
933 strcpy(str, de->d_name);
934 if (strlen(str) <= 2)
935 continue;
936
937 if (get_date) {
938 char file_path[255];
939 struct stat st;
940
941 strcpy(file_path, Restore_Name.c_str());
942 strcat(file_path, "/");
943 strcat(file_path, str);
944 stat(file_path, &st);
945 string backup_date = ctime((const time_t*)(&st.st_mtime));
946 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
947 get_date = false;
948 }
949
950 label = str;
951 ptr = label;
952 while (*ptr && *ptr != '.') ptr++;
953 if (*ptr == '.')
954 {
955 *ptr = 0x00;
956 ptr++;
957 fstype = ptr;
958 }
959 while (*ptr && *ptr != '.') ptr++;
960 if (*ptr == '.')
961 {
962 *ptr = 0x00;
963 ptr++;
964 extn = ptr;
965 }
966
Dees_Troy83bd4832013-05-04 12:39:56 +0000967 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -0500968 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +0000969 if (extnlength != 3 && extnlength != 6) continue;
970 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
971 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
972
973 if (check_encryption) {
974 string filename = Restore_Name + "/";
975 filename += de->d_name;
976 if (TWFunc::Get_File_Type(filename) == 2) {
977 LOGINFO("'%s' is encrypted\n", filename.c_str());
978 DataManager::SetValue("tw_restore_encrypted", 1);
979 }
980 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500981 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -0400982
983 TWPartition* Part = Find_Partition_By_Path(label);
984 if (Part == NULL)
985 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000986 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -0400987 continue;
988 }
989
990 Part->Backup_FileName = de->d_name;
991 if (strlen(extn) > 3) {
992 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
993 }
994
Dees_Troya13d74f2013-03-24 08:54:55 -0500995 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -0400996 }
997 closedir(d);
998
Dees_Troya13d74f2013-03-24 08:54:55 -0500999 // Set the final value
1000 DataManager::SetValue("tw_restore_list", Restore_List);
1001 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001002 return;
1003}
1004
1005int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001006 std::vector<TWPartition*>::iterator iter;
1007 int ret = false;
1008 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001009 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001010
1011 // Iterate through all partitions
1012 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001013 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001014 if (Path == "/and-sec")
1015 ret = (*iter)->Wipe_AndSec();
1016 else
1017 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001018 found = true;
1019 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1020 (*iter)->Wipe();
1021 }
1022 }
1023 if (found) {
1024 return ret;
1025 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001026 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001027 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001028}
1029
1030int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001031 TWPartition* Part = Find_Partition_By_Block(Block);
1032
1033 if (Part) {
1034 if (Part->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 == Part->Mount_Point)
1039 (*subpart)->Wipe();
1040 }
1041 return Part->Wipe();
1042 } else
1043 return Part->Wipe();
1044 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001045 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001046 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001047}
1048
1049int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001050 TWPartition* Part = Find_Partition_By_Name(Name);
1051
1052 if (Part) {
1053 if (Part->Has_SubPartition) {
1054 std::vector<TWPartition*>::iterator subpart;
1055
1056 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1057 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1058 (*subpart)->Wipe();
1059 }
1060 return Part->Wipe();
1061 } else
1062 return Part->Wipe();
1063 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001064 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001065 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001066}
1067
1068int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001069 std::vector<TWPartition*>::iterator iter;
1070 int ret = true;
1071
1072 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001073 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001074 if (!(*iter)->Wipe())
1075 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001076 } else if ((*iter)->Has_Android_Secure) {
1077 if (!(*iter)->Wipe_AndSec())
1078 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001079 }
1080 }
1081 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001082}
1083
Dees_Troy38bd7602012-09-14 13:33:53 -04001084int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1085 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001086 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001087
1088 if (!Mount_By_Path("/data", true))
1089 return false;
1090
1091 if (!Mount_By_Path("/cache", true))
1092 return false;
1093
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001094 dir.push_back("/data/dalvik-cache");
1095 dir.push_back("/cache/dalvik-cache");
1096 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001097 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001098 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001099 if (stat(dir.at(i).c_str(), &st) == 0) {
1100 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001101 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001102 }
1103 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001104 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
1105 if (sdext != NULL) {
1106 if (sdext->Is_Present && sdext->Mount(false)) {
1107 if (stat("/sd-ext/dalvik-cache", &st) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001108 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001109 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001110 }
1111 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001112 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001113 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001114 return true;
1115}
1116
1117int TWPartitionManager::Wipe_Rotate_Data(void) {
1118 if (!Mount_By_Path("/data", true))
1119 return false;
1120
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001121 unlink("/data/misc/akmd*");
1122 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001123 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001124 return true;
1125}
1126
1127int TWPartitionManager::Wipe_Battery_Stats(void) {
1128 struct stat st;
1129
1130 if (!Mount_By_Path("/data", true))
1131 return false;
1132
1133 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001134 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001135 } else {
1136 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001137 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001138 }
1139 return true;
1140}
1141
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001142int TWPartitionManager::Wipe_Android_Secure(void) {
1143 std::vector<TWPartition*>::iterator iter;
1144 int ret = false;
1145 bool found = false;
1146
1147 // Iterate through all partitions
1148 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1149 if ((*iter)->Has_Android_Secure) {
1150 ret = (*iter)->Wipe_AndSec();
1151 found = true;
1152 }
1153 }
1154 if (found) {
1155 return ret;
1156 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001157 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001158 }
1159 return false;
1160}
1161
Dees_Troy38bd7602012-09-14 13:33:53 -04001162int TWPartitionManager::Format_Data(void) {
1163 TWPartition* dat = Find_Partition_By_Path("/data");
1164
1165 if (dat != NULL) {
1166 if (!dat->UnMount(true))
1167 return false;
1168
1169 return dat->Wipe_Encryption();
1170 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001171 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001172 return false;
1173 }
1174 return false;
1175}
1176
1177int TWPartitionManager::Wipe_Media_From_Data(void) {
1178 TWPartition* dat = Find_Partition_By_Path("/data");
1179
1180 if (dat != NULL) {
1181 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001182 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001183 return false;
1184 }
1185 if (!dat->Mount(true))
1186 return false;
1187
Dees_Troy2673cec2013-04-02 20:22:16 +00001188 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001189 TWFunc::removeDir("/data/media", false);
1190 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
1191 return -1;
Dees_Troy38bd7602012-09-14 13:33:53 -04001192 if (dat->Has_Data_Media) {
1193 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001194 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1195 dat->UnMount(false);
1196 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001197 }
1198 return true;
1199 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001200 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001201 return false;
1202 }
1203 return false;
1204}
1205
Dees_Troy51a0e822012-09-05 15:24:24 -04001206void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001207 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001208 return;
1209}
1210
1211void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001212 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001213 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001214
Dees_Troy2673cec2013-04-02 20:22:16 +00001215 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001216 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001217 if ((*iter)->Can_Be_Mounted) {
1218 (*iter)->Update_Size(true);
1219 if ((*iter)->Mount_Point == "/system") {
1220 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1221 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1222 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1223 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1224 } else if ((*iter)->Mount_Point == "/cache") {
1225 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1226 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1227 } else if ((*iter)->Mount_Point == "/sd-ext") {
1228 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1229 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1230 if ((*iter)->Backup_Size == 0) {
1231 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1232 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1233 } else
1234 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001235 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001236 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001237 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001238 if ((*iter)->Backup_Size == 0) {
1239 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1240 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1241 } else
1242 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001243 } else if ((*iter)->Mount_Point == "/boot") {
1244 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1245 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1246 if ((*iter)->Backup_Size == 0) {
1247 DataManager::SetValue("tw_has_boot_partition", 0);
1248 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1249 } else
1250 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001251 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001252#ifdef SP1_NAME
1253 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1254 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1255 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1256 }
1257#endif
1258#ifdef SP2_NAME
1259 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1260 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1261 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1262 }
1263#endif
1264#ifdef SP3_NAME
1265 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1266 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1267 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1268 }
1269#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001270 } else {
1271 // Handle unmountable partitions in case we reset defaults
1272 if ((*iter)->Mount_Point == "/boot") {
1273 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1274 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1275 if ((*iter)->Backup_Size == 0) {
1276 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1277 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1278 } else
1279 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1280 } else if ((*iter)->Mount_Point == "/recovery") {
1281 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1282 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1283 if ((*iter)->Backup_Size == 0) {
1284 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1285 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1286 } else
1287 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001288 } else if ((*iter)->Mount_Point == "/data") {
1289 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001290 }
1291#ifdef SP1_NAME
1292 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1293 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1294 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1295 }
1296#endif
1297#ifdef SP2_NAME
1298 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1299 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1300 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1301 }
1302#endif
1303#ifdef SP3_NAME
1304 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1305 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1306 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1307 }
1308#endif
Dees_Troy51127312012-09-08 13:08:49 -04001309 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001310 }
Dees_Troy51127312012-09-08 13:08:49 -04001311 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1312 string current_storage_path = DataManager::GetCurrentStoragePath();
1313 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001314 if (FreeStorage != NULL) {
1315 // Attempt to mount storage
1316 if (!FreeStorage->Mount(false)) {
1317 // We couldn't mount storage... check to see if we have dual storage
1318 int has_dual_storage;
1319 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1320 if (has_dual_storage == 1) {
1321 // We have dual storage, see if we're using the internal storage that should always be present
1322 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001323 if (!FreeStorage->Is_Encrypted) {
1324 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001325 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001326 }
Dees_Troy8170a922012-09-18 15:40:25 -04001327 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1328 } else {
1329 // We were using external, flip to internal
1330 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1331 current_storage_path = DataManager::GetCurrentStoragePath();
1332 FreeStorage = Find_Partition_By_Path(current_storage_path);
1333 if (FreeStorage != NULL) {
1334 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1335 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001336 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001337 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1338 }
1339 }
1340 } else {
1341 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001342 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001343 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1344 }
1345 } else {
1346 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1347 }
1348 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001349 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001350 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001351 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001352 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001353 return;
1354}
1355
1356int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001357#ifdef TW_INCLUDE_CRYPTO
1358 int ret_val, password_len;
1359 char crypto_blkdev[255], cPassword[255];
1360 size_t result;
1361
1362 property_set("ro.crypto.state", "encrypted");
1363#ifdef TW_INCLUDE_JB_CRYPTO
1364 // No extra flags needed
1365#else
1366 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1367 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1368 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1369 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1370 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1371 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001372
1373#ifdef CRYPTO_SD_FS_TYPE
1374 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1375 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1376 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001377#endif
a39552696ff55ce2013-01-08 16:14:56 +00001378
1379 property_set("rw.km_fips_status", "ready");
1380
1381#endif
1382
1383 // some samsung devices store "footer" on efs partition
1384 TWPartition *efs = Find_Partition_By_Path("/efs");
1385 if(efs && !efs->Is_Mounted())
1386 efs->Mount(false);
1387 else
1388 efs = 0;
1389#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001390#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001391 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001392 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001393 property_set("ro.crypto.external_encrypted", "1");
1394 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1395 } else {
1396 property_set("ro.crypto.external_encrypted", "0");
1397 }
1398#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001399#endif
a39552696ff55ce2013-01-08 16:14:56 +00001400
Dees_Troy5bf43922012-09-07 16:07:55 -04001401 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001402 int pwret = cryptfs_check_passwd(cPassword);
1403
1404 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001405 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001406 return -1;
1407 }
a39552696ff55ce2013-01-08 16:14:56 +00001408
1409 if(efs)
1410 efs->UnMount(false);
1411
Dees_Troy5bf43922012-09-07 16:07:55 -04001412 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1413 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001414 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001415 } else {
1416 TWPartition* dat = Find_Partition_By_Path("/data");
1417 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001418 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001419 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1420 dat->Is_Decrypted = true;
1421 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001422 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001423 dat->Current_File_System = dat->Fstab_File_System; // Needed if we're ignoring blkid because encrypted devices start out as emmc
Dees_Troy2673cec2013-04-02 20:22:16 +00001424 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001425
1426#ifdef CRYPTO_SD_FS_TYPE
1427 char crypto_blkdev_sd[255];
1428 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1429 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001430 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001431 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001432 emmc->Is_Decrypted = true;
1433 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1434 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001435 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001436 }
a39552696ff55ce2013-01-08 16:14:56 +00001437#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001438#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001439#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001440 char is_external_decrypted[255];
1441 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1442 if (strcmp(is_external_decrypted, "1") == 0) {
1443 sdcard->Is_Decrypted = true;
1444 sdcard->EcryptFS_Password = Password;
1445 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001446 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1447 MetaEcfsFile += "/.MetaEcfsFile";
1448 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1449 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1450 sdcard->UnMount(false);
1451 sdcard->Mount(false);
1452 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001453 } else {
1454 sdcard->Is_Decrypted = false;
1455 sdcard->Decrypted_Block_Device = "";
1456 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001457#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001458#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001459
Dees_Troy5bf43922012-09-07 16:07:55 -04001460 // Sleep for a bit so that the device will be ready
1461 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001462#ifdef RECOVERY_SDCARD_ON_DATA
1463 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1464 dat->Storage_Path = "/data/media/0";
1465 dat->Symlink_Path = dat->Storage_Path;
1466 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
1467 dat->UnMount(false);
1468 DataManager::SetBackupFolder();
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001469 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001470 }
1471#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001472 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001473 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001474 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001475 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001476 }
1477 return 0;
1478#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001479 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001480 return -1;
1481#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001482 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001483}
1484
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001485int TWPartitionManager::Fix_Permissions(void) {
1486 int result = 0;
1487 if (!Mount_By_Path("/data", true))
1488 return false;
1489
1490 if (!Mount_By_Path("/system", true))
1491 return false;
1492
1493 Mount_By_Path("/sd-ext", false);
1494
1495 fixPermissions perms;
1496 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001497 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001498 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001499 return result;
1500}
1501
Dees_Troyd21618c2012-10-14 18:48:49 -04001502int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001503 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1504
1505 if (Part == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001506 LOGERR("Unable to locate volume information for USB storage mode.");
Dees_Troyd21618c2012-10-14 18:48:49 -04001507 return false;
1508 }
1509 if (!Part->UnMount(true))
1510 return false;
1511
Dees_Troy2673cec2013-04-02 20:22:16 +00001512 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1513 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001514 return false;
1515 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001516 return true;
1517}
1518
Dees_Troy8170a922012-09-18 15:40:25 -04001519int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001520 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001521 char lun_file[255];
Dees_Troy8170a922012-09-18 15:40:25 -04001522 string ext_path;
Dees_Troyd21618c2012-10-14 18:48:49 -04001523 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001524
1525 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1526 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1527 if (has_dual == 1 && has_data_media == 0) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001528 string Lun_File_str = CUSTOM_LUN_FILE;
1529 size_t found = Lun_File_str.find("%");
1530 if (found != string::npos) {
1531 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1532 if (TWFunc::Path_Exists(lun_file))
1533 has_multiple_lun = true;
1534 }
1535 if (!has_multiple_lun) {
Dees_Troyf4ca6122012-10-13 22:17:20 -04001536 // Device doesn't have multiple lun files, mount current storage
Dees_Troyf4ca6122012-10-13 22:17:20 -04001537 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001538 return Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001539 } else {
1540 // Device has multiple lun files
Dees_Troyf4ca6122012-10-13 22:17:20 -04001541 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001542 if (!Open_Lun_File(DataManager::GetSettingsStoragePath(), lun_file))
Dees_Troyf4ca6122012-10-13 22:17:20 -04001543 return false;
Dees_Troyf4ca6122012-10-13 22:17:20 -04001544 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001545 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Dees_Troyd21618c2012-10-14 18:48:49 -04001546 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001547 }
Dees_Troy8170a922012-09-18 15:40:25 -04001548 } else {
1549 if (has_data_media == 0)
1550 ext_path = DataManager::GetCurrentStoragePath();
1551 else
1552 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001553 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001554 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001555 }
1556 return true;
1557}
1558
1559int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001560 int index, ret;
1561 char lun_file[255], ch[2] = {0, 0};
1562 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001563
1564 for (index=0; index<2; index++) {
1565 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001566 ret = TWFunc::write_file(lun_file, str);
1567 Mount_All_Storage();
1568 Update_System_Details();
1569 if (ret < 0) {
1570 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001571 }
Dees_Troy8170a922012-09-18 15:40:25 -04001572 }
Dees_Troye58d5262012-09-21 12:27:57 -04001573 Mount_All_Storage();
1574 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001575 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001576 if (ret < 0 && index == 0) {
1577 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1578 return false;
1579 } else {
1580 return true;
1581 }
Dees_Troy8170a922012-09-18 15:40:25 -04001582 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001583}
1584
1585void TWPartitionManager::Mount_All_Storage(void) {
1586 std::vector<TWPartition*>::iterator iter;
1587
1588 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1589 if ((*iter)->Is_Storage)
1590 (*iter)->Mount(false);
1591 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001592}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001593
Dees_Troyd0384ef2012-10-12 12:15:42 -04001594void TWPartitionManager::UnMount_Main_Partitions(void) {
1595 // Unmounts system and data if data is not data/media
1596 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001597 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001598
1599 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1600
1601 UnMount_By_Path("/system", true);
1602#ifndef RECOVERY_SDCARD_ON_DATA
1603 UnMount_By_Path("/data", true);
1604#endif
1605 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1606 Boot_Partition->UnMount(true);
1607}
1608
Dees_Troy9350b8d2012-09-27 12:38:38 -04001609int TWPartitionManager::Partition_SDCard(void) {
1610 char mkdir_path[255], temp[255], line[512];
1611 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1612 int ext, swap, total_size = 0, fat_size;
1613 FILE* fp;
1614
Dees_Troy2673cec2013-04-02 20:22:16 +00001615 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001616#ifdef TW_EXTERNAL_STORAGE_PATH
1617 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1618#else
1619 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1620#endif
1621 if (SDCard == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001622 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001623 return false;
1624 }
1625 if (!SDCard->UnMount(true))
1626 return false;
1627 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1628 if (SDext != NULL) {
1629 if (!SDext->UnMount(true))
1630 return false;
1631 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001632 string result;
1633 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"", result);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001634 Device = SDCard->Actual_Block_Device;
1635 // Just use the root block device
1636 Device.resize(strlen("/dev/block/mmcblkX"));
1637
1638 // Find the size of the block device:
1639 fp = fopen("/proc/partitions", "rt");
1640 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001641 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001642 return false;
1643 }
1644
1645 while (fgets(line, sizeof(line), fp) != NULL)
1646 {
1647 unsigned long major, minor, blocks;
1648 char device[512];
1649 char tmpString[64];
1650
1651 if (strlen(line) < 7 || line[0] == 'm') continue;
1652 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1653
1654 tmpdevice = "/dev/block/";
1655 tmpdevice += device;
1656 if (tmpdevice == Device) {
1657 // Adjust block size to byte size
1658 total_size = (int)(blocks * 1024ULL / 1000000LLU);
1659 break;
1660 }
1661 }
1662 fclose(fp);
1663
1664 DataManager::GetValue("tw_sdext_size", ext);
1665 DataManager::GetValue("tw_swap_size", swap);
1666 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1667 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001668 LOGINFO("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);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001669 memset(temp, 0, sizeof(temp));
1670 sprintf(temp, "%i", fat_size);
1671 fat_str = temp;
1672 memset(temp, 0, sizeof(temp));
1673 sprintf(temp, "%i", fat_size + ext);
1674 ext_str = temp;
1675 memset(temp, 0, sizeof(temp));
1676 sprintf(temp, "%i", fat_size + ext + swap);
1677 swap_str = temp;
1678 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001679 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001680 return false;
1681 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001682 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001683 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001684 LOGINFO("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001685 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001686 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001687 Update_System_Details();
1688 return false;
1689 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001690 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001691 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001692 LOGINFO("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001693 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001694 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001695 return false;
1696 }
1697 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001698 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001699 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001700 LOGINFO("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001701 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001702 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001703 Update_System_Details();
1704 return false;
1705 }
1706 }
1707 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001708 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001709 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001710 LOGINFO("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001711 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001712 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001713 Update_System_Details();
1714 return false;
1715 }
1716 }
1717 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1718#ifdef TW_EXTERNAL_STORAGE_PATH
1719 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1720 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1721 memset(mkdir_path, 0, sizeof(mkdir_path));
1722 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1723#else
1724 Mount_By_Path("/sdcard", 1);
1725 strcpy(mkdir_path, "/sdcard/TWRP");
1726#endif
1727 mkdir(mkdir_path, 0777);
1728 DataManager::Flush();
1729#ifdef TW_EXTERNAL_STORAGE_PATH
1730 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1731 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1732 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1733#else
1734 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1735 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1736 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1737#endif
1738 if (ext > 0) {
1739 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001740 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001741 return false;
1742 }
1743 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001744 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1745 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001746 TWFunc::Exec_Cmd(Command, result);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001747 }
1748
1749 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00001750 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001751 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001752}
Dees_Troya13d74f2013-03-24 08:54:55 -05001753
1754void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1755 std::vector<TWPartition*>::iterator iter;
1756 if (ListType == "mount") {
1757 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1758 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1759 struct PartitionList part;
1760 part.Display_Name = (*iter)->Display_Name;
1761 part.Mount_Point = (*iter)->Mount_Point;
1762 part.selected = (*iter)->Is_Mounted();
1763 Partition_List->push_back(part);
1764 }
1765 }
1766 } else if (ListType == "storage") {
1767 char free_space[255];
1768 string Current_Storage = DataManager::GetCurrentStoragePath();
1769 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1770 if ((*iter)->Is_Storage) {
1771 struct PartitionList part;
1772 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1773 part.Display_Name = (*iter)->Storage_Name + " (";
1774 part.Display_Name += free_space;
1775 part.Display_Name += "MB)";
1776 part.Mount_Point = (*iter)->Storage_Path;
1777 if ((*iter)->Storage_Path == Current_Storage)
1778 part.selected = 1;
1779 else
1780 part.selected = 0;
1781 Partition_List->push_back(part);
1782 }
1783 }
1784 } else if (ListType == "backup") {
1785 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001786 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001787 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001788 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001789 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001790 Backup_Size = (*iter)->Backup_Size;
1791 if ((*iter)->Has_SubPartition) {
1792 std::vector<TWPartition*>::iterator subpart;
1793
1794 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1795 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1796 Backup_Size += (*subpart)->Backup_Size;
1797 }
1798 }
1799 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001800 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1801 part.Display_Name += backup_size;
1802 part.Display_Name += "MB)";
1803 part.Mount_Point = (*iter)->Backup_Path;
1804 part.selected = 0;
1805 Partition_List->push_back(part);
1806 }
1807 }
1808 } else if (ListType == "restore") {
1809 string Restore_List, restore_path;
1810 TWPartition* restore_part = NULL;
1811
1812 DataManager::GetValue("tw_restore_list", Restore_List);
1813 if (!Restore_List.empty()) {
1814 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1815 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1816 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
1817 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL && !restore_part->Is_SubPartition) {
1818 if (restore_part->Backup_Name == "recovery") {
1819 // Don't allow restore of recovery (causes problems on some devices)
1820 } else {
1821 struct PartitionList part;
1822 part.Display_Name = restore_part->Backup_Display_Name;
1823 part.Mount_Point = restore_part->Backup_Path;
1824 part.selected = 1;
1825 Partition_List->push_back(part);
1826 }
1827 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001828 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001829 }
1830 start_pos = end_pos + 1;
1831 end_pos = Restore_List.find(";", start_pos);
1832 }
1833 }
1834 } else if (ListType == "wipe") {
1835 struct PartitionList dalvik;
1836 dalvik.Display_Name = "Dalvik Cache";
1837 dalvik.Mount_Point = "DALVIK";
1838 dalvik.selected = 0;
1839 Partition_List->push_back(dalvik);
1840 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1841 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1842 struct PartitionList part;
1843 part.Display_Name = (*iter)->Display_Name;
1844 part.Mount_Point = (*iter)->Mount_Point;
1845 part.selected = 0;
1846 Partition_List->push_back(part);
1847 }
1848 if ((*iter)->Has_Android_Secure) {
1849 struct PartitionList part;
1850 part.Display_Name = (*iter)->Backup_Display_Name;
1851 part.Mount_Point = (*iter)->Backup_Path;
1852 part.selected = 0;
1853 Partition_List->push_back(part);
1854 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001855 if ((*iter)->Has_Data_Media) {
1856 struct PartitionList datamedia;
1857 datamedia.Display_Name = (*iter)->Storage_Name;
1858 datamedia.Mount_Point = "INTERNAL";
1859 datamedia.selected = 0;
1860 Partition_List->push_back(datamedia);
1861 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001862 }
1863 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001864 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001865 }
1866}
1867
1868int TWPartitionManager::Fstab_Processed(void) {
1869 return Partitions.size();
1870}