blob: 445e4b56341b0cc059fec85d469ee9f5c9a88a2e [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 ");
Hashcodedabfd492013-08-29 22:45:30 -0700165 if (Part->Use_Rm_Rf)
166 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500167 if (Part->Can_Be_Backed_Up)
168 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800169 if (Part->Wipe_During_Factory_Reset)
170 printf("Wipe_During_Factory_Reset ");
171 if (Part->Wipe_Available_in_GUI)
172 printf("Wipe_Available_in_GUI ");
173 if (Part->Is_SubPartition)
174 printf("Is_SubPartition ");
175 if (Part->Has_SubPartition)
176 printf("Has_SubPartition ");
177 if (Part->Removable)
178 printf("Removable ");
179 if (Part->Is_Present)
180 printf("IsPresent ");
181 if (Part->Can_Be_Encrypted)
182 printf("Can_Be_Encrypted ");
183 if (Part->Is_Encrypted)
184 printf("Is_Encrypted ");
185 if (Part->Is_Decrypted)
186 printf("Is_Decrypted ");
187 if (Part->Has_Data_Media)
188 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000189 if (Part->Can_Encrypt_Backup)
190 printf("Can_Encrypt_Backup ");
191 if (Part->Use_Userdata_Encryption)
192 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800193 if (Part->Has_Android_Secure)
194 printf("Has_Android_Secure ");
195 if (Part->Is_Storage)
196 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500197 if (Part->Is_Settings_Storage)
198 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000199 if (Part->Ignore_Blkid)
200 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000201 if (Part->Retain_Layout_Version)
202 printf("Retain_Layout_Version ");
Gary Peck004d48b2012-11-21 16:28:18 -0800203 printf("\n");
204 if (!Part->SubPartition_Of.empty())
205 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
206 if (!Part->Symlink_Path.empty())
207 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
208 if (!Part->Symlink_Mount_Point.empty())
209 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
210 if (!Part->Primary_Block_Device.empty())
211 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
212 if (!Part->Alternate_Block_Device.empty())
213 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
214 if (!Part->Decrypted_Block_Device.empty())
215 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
216 if (Part->Length != 0)
217 printf(" Length: %i\n", Part->Length);
218 if (!Part->Display_Name.empty())
219 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500220 if (!Part->Storage_Name.empty())
221 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800222 if (!Part->Backup_Path.empty())
223 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
224 if (!Part->Backup_Name.empty())
225 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500226 if (!Part->Backup_Display_Name.empty())
227 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800228 if (!Part->Backup_FileName.empty())
229 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
230 if (!Part->Storage_Path.empty())
231 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
232 if (!Part->Current_File_System.empty())
233 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
234 if (!Part->Fstab_File_System.empty())
235 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
236 if (Part->Format_Block_Size != 0)
237 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400238 if (!Part->MTD_Name.empty())
239 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400240 string back_meth = Part->Backup_Method_By_Name();
241 printf(" Backup_Method: %s\n\n", back_meth.c_str());
242}
243
Dees_Troy51a0e822012-09-05 15:24:24 -0400244int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400245 std::vector<TWPartition*>::iterator iter;
246 int ret = false;
247 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400248 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400249
Dees_Troyd93bda52013-07-03 19:55:19 +0000250 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400251 return true;
252
Dees_Troy5bf43922012-09-07 16:07:55 -0400253 // Iterate through all partitions
254 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400255 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400256 ret = (*iter)->Mount(Display_Error);
257 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400258 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400259 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400260 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 }
262 if (found) {
263 return ret;
264 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000265 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400266 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000267 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400268 }
269 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400270}
271
272int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400273 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400274
Dees_Troy51127312012-09-08 13:08:49 -0400275 if (Part) {
276 if (Part->Has_SubPartition) {
277 std::vector<TWPartition*>::iterator subpart;
278
279 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
280 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
281 (*subpart)->Mount(Display_Error);
282 }
283 return Part->Mount(Display_Error);
284 } else
285 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400286 }
287 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000288 LOGERR("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400289 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000290 LOGINFO("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400291 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400292}
293
294int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400295 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400296
Dees_Troy51127312012-09-08 13:08:49 -0400297 if (Part) {
298 if (Part->Has_SubPartition) {
299 std::vector<TWPartition*>::iterator subpart;
300
301 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
302 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
303 (*subpart)->Mount(Display_Error);
304 }
305 return Part->Mount(Display_Error);
306 } else
307 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400308 }
309 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000310 LOGERR("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400311 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000312 LOGINFO("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400313 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400314}
315
316int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400317 std::vector<TWPartition*>::iterator iter;
318 int ret = false;
319 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400320 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400321
322 // Iterate through all partitions
323 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400324 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400325 ret = (*iter)->UnMount(Display_Error);
326 found = true;
327 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
328 (*iter)->UnMount(Display_Error);
329 }
330 }
331 if (found) {
332 return ret;
333 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000334 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400335 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000336 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400337 }
338 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400339}
340
341int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400342 TWPartition* Part = Find_Partition_By_Block(Block);
343
344 if (Part) {
345 if (Part->Has_SubPartition) {
346 std::vector<TWPartition*>::iterator subpart;
347
348 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
349 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
350 (*subpart)->UnMount(Display_Error);
351 }
352 return Part->UnMount(Display_Error);
353 } else
354 return Part->UnMount(Display_Error);
355 }
356 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000357 LOGERR("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400358 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000359 LOGINFO("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400360 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400361}
362
363int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400364 TWPartition* Part = Find_Partition_By_Name(Name);
365
366 if (Part) {
367 if (Part->Has_SubPartition) {
368 std::vector<TWPartition*>::iterator subpart;
369
370 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
371 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
372 (*subpart)->UnMount(Display_Error);
373 }
374 return Part->UnMount(Display_Error);
375 } else
376 return Part->UnMount(Display_Error);
377 }
378 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000379 LOGERR("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400380 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000381 LOGINFO("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400382 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400383}
384
385int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400386 TWPartition* Part = Find_Partition_By_Path(Path);
387
388 if (Part)
389 return Part->Is_Mounted();
390 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000391 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400392 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400393}
394
395int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400396 TWPartition* Part = Find_Partition_By_Block(Block);
397
398 if (Part)
399 return Part->Is_Mounted();
400 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000401 LOGINFO("Is_Mounted: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400402 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400403}
404
405int TWPartitionManager::Is_Mounted_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400406 TWPartition* Part = Find_Partition_By_Name(Name);
407
408 if (Part)
409 return Part->Is_Mounted();
410 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000411 LOGINFO("Is_Mounted: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400412 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400413}
414
Dees_Troy5bf43922012-09-07 16:07:55 -0400415int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400416 string current_storage_path = DataManager::GetCurrentStoragePath();
417
418 if (Mount_By_Path(current_storage_path, Display_Error)) {
419 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
420 if (FreeStorage)
421 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
422 return true;
423 }
424 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400425}
426
Dees_Troy5bf43922012-09-07 16:07:55 -0400427int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
428 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
429}
430
431TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
432 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400433 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400434
435 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400436 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400437 return (*iter);
438 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400439 return NULL;
440}
441
Dees_Troy5bf43922012-09-07 16:07:55 -0400442TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400443 std::vector<TWPartition*>::iterator iter;
444
445 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400446 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 -0400447 return (*iter);
448 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400449 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400450}
451
452TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400453 std::vector<TWPartition*>::iterator iter;
454
455 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
456 if ((*iter)->Display_Name == Name)
457 return (*iter);
458 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400459 return NULL;
460}
Dees_Troy51a0e822012-09-05 15:24:24 -0400461
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400462int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
463 // Check the backup name to ensure that it is the correct size and contains only valid characters
464 // and that a backup with that name doesn't already exist
465 char backup_name[MAX_BACKUP_NAME_LEN];
466 char backup_loc[255], tw_image_dir[255];
467 int copy_size;
468 int index, cur_char;
469 string Backup_Name, Backup_Loc;
470
471 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
472 copy_size = Backup_Name.size();
473 // Check size
474 if (copy_size > MAX_BACKUP_NAME_LEN) {
475 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000476 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400477 return -2;
478 }
479
480 // Check each character
481 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500482 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400483 return 0; // A "0" (zero) means to use the current timestamp for the backup name
484 for (index=0; index<copy_size; index++) {
485 cur_char = (int)backup_name[index];
486 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) {
487 // These are valid characters
488 // Numbers
489 // Upper case letters
490 // Lower case letters
491 // Space
492 // and -_.{}[]
493 } else {
494 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000495 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400496 return -3;
497 }
498 }
499
500 // Check to make sure that a backup with this name doesn't already exist
501 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
502 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500503 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400504 if (TWFunc::Path_Exists(tw_image_dir)) {
505 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000506 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400507 return -4;
508 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400509 // No problems found, return 0
510 return 0;
511}
512
Dees_Troy43d8b002012-09-17 16:00:01 -0400513bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
514{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500515 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400516 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500517 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500518 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400519
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500520 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400521 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400522
Dees_Troyb46a6842012-09-25 11:06:46 -0400523 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000524 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400525
526 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500527 md5sum.setfn(Backup_Folder + Backup_Filename);
528 if (md5sum.computeMD5() == 0)
529 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000530 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500531 else
532 return -1;
533 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000534 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400535 } else {
536 char filename[512];
537 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500538 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400539 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500540 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000541 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400542 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000543 if (TWFunc::Path_Exists(filename)) {
544 if (md5sum.computeMD5() == 0) {
545 if (md5sum.write_md5digest() != 0)
546 {
547 gui_print(" * MD5 Error.\n");
548 return false;
549 }
550 } else {
551 gui_print(" * Error computing MD5.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500552 return false;
553 }
554 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400555 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400556 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500557 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400558 }
559 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000560 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400561 return false;
562 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000563 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400564 }
565 return true;
566}
567
Dees_Troy093b7642012-09-21 15:59:38 -0400568bool 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 -0400569 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500570 int img_bps;
571 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400572 unsigned long total_time, remain_time, section_time;
573 int use_compression, backup_time;
574 float pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400575
576 if (Part == NULL)
577 return true;
578
Dees_Troy093b7642012-09-21 15:59:38 -0400579 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
580
581 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
582 if (use_compression)
583 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
584 else
585 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
586
587 // We know the speed for both, how far into the whole backup are we, based on time
588 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
589 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
590
591 pos = (total_time - remain_time) / (float) total_time;
Dees_Troy2673cec2013-04-02 20:22:16 +0000592 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400593
Dees_Troy2673cec2013-04-02 20:22:16 +0000594 LOGINFO("Estimated Total time: %lu Estimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400595
596 // And get the time
597 if (Part->Backup_Method == 1)
598 section_time = Part->Backup_Size / file_bps;
599 else
600 section_time = Part->Backup_Size / img_bps;
601
602 // Set the position
603 pos = section_time / (float) total_time;
Dees_Troy2673cec2013-04-02 20:22:16 +0000604 DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400605
Dees_Troy43d8b002012-09-17 16:00:01 -0400606 time(&start);
607
608 if (Part->Backup(Backup_Folder)) {
Dees_Troy8170a922012-09-18 15:40:25 -0400609 if (Part->Has_SubPartition) {
610 std::vector<TWPartition*>::iterator subpart;
611
612 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500613 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Dees_Troy8170a922012-09-18 15:40:25 -0400614 if (!(*subpart)->Backup(Backup_Folder))
615 return false;
Dees_Troy2727b992013-08-14 20:09:30 +0000616 sync();
617 sync();
Dees_Troy8170a922012-09-18 15:40:25 -0400618 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
619 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400620 if (Part->Backup_Method == 1) {
621 *file_bytes_remaining -= (*subpart)->Backup_Size;
622 } else {
623 *img_bytes_remaining -= (*subpart)->Backup_Size;
624 }
Dees_Troy8170a922012-09-18 15:40:25 -0400625 }
626 }
627 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400628 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400629 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000630 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400631 if (Part->Backup_Method == 1) {
632 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400633 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400634 } else {
635 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400636 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400637 }
638 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
639 } else {
640 return false;
641 }
642}
643
644int TWPartitionManager::Run_Backup(void) {
645 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500646 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400647 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 -0400648 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500649 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400650 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400651 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400652 struct tm *t;
653 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500654 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400655 seconds = time(0);
656 t = localtime(&seconds);
657
658 time(&total_start);
659
660 Update_System_Details();
661
662 if (!Mount_Current_Storage(true))
663 return false;
664
665 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400666 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400667 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400668 else
669 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400670
Dees_Troy43d8b002012-09-17 16:00:01 -0400671 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
672 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000673 if (Backup_Name == "(Current Date)") {
674 Backup_Name = TWFunc::Get_Current_Date();
675 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
676 TWFunc::Auto_Generate_Backup_Name();
677 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400678 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000679 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400680 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000681 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400682
Dees_Troy2673cec2013-04-02 20:22:16 +0000683 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500684 DataManager::GetValue("tw_backup_list", Backup_List);
685 if (!Backup_List.empty()) {
686 end_pos = Backup_List.find(";", start_pos);
687 while (end_pos != string::npos && start_pos < Backup_List.size()) {
688 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
689 backup_part = Find_Partition_By_Path(backup_path);
690 if (backup_part != NULL) {
691 partition_count++;
692 if (backup_part->Backup_Method == 1)
693 file_bytes += backup_part->Backup_Size;
694 else
695 img_bytes += backup_part->Backup_Size;
696 if (backup_part->Has_SubPartition) {
697 std::vector<TWPartition*>::iterator subpart;
698
699 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000700 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 -0500701 partition_count++;
702 if ((*subpart)->Backup_Method == 1)
703 file_bytes += (*subpart)->Backup_Size;
704 else
705 img_bytes += (*subpart)->Backup_Size;
706 }
707 }
Dees_Troy8170a922012-09-18 15:40:25 -0400708 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500709 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000710 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400711 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500712 start_pos = end_pos + 1;
713 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400714 }
715 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400716
717 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000718 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400719 return false;
720 }
721 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000722 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
723 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400724 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
725 if (storage != NULL) {
726 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000727 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400728 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000729 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400730 return false;
731 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000732 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400733 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000734 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400735 return false;
736 }
737 img_bytes_remaining = img_bytes;
738 file_bytes_remaining = file_bytes;
739
Dees_Troy2673cec2013-04-02 20:22:16 +0000740 gui_print("\n[BACKUP STARTED]\n");
741 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000742 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000743 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000744 return false;
745 }
746
Dees_Troy2673cec2013-04-02 20:22:16 +0000747 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400748
Dees_Troya13d74f2013-03-24 08:54:55 -0500749 start_pos = 0;
750 end_pos = Backup_List.find(";", start_pos);
751 while (end_pos != string::npos && start_pos < Backup_List.size()) {
752 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
753 backup_part = Find_Partition_By_Path(backup_path);
754 if (backup_part != NULL) {
755 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
756 return false;
757 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000758 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500759 }
760 start_pos = end_pos + 1;
761 end_pos = Backup_List.find(";", start_pos);
762 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400763
764 // Average BPS
765 if (img_time == 0)
766 img_time = 1;
767 if (file_time == 0)
768 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400769 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500770 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400771
Dees_Troy2673cec2013-04-02 20:22:16 +0000772 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
773 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400774
775 time(&total_stop);
776 int total_time = (int) difftime(total_stop, total_start);
777 unsigned long long actual_backup_size = TWFunc::Get_Folder_Size(Full_Backup_Path, true);
778 actual_backup_size /= (1024LLU * 1024LLU);
779
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500780 int prev_img_bps, use_compression;
781 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400782 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
783 img_bps += (prev_img_bps * 4);
784 img_bps /= 5;
785
786 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
787 if (use_compression)
788 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
789 else
790 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
791 file_bps += (prev_file_bps * 4);
792 file_bps /= 5;
793
794 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
795 if (use_compression)
796 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
797 else
798 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
799
Dees_Troy2673cec2013-04-02 20:22:16 +0000800 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400801 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400802 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +0000803 gui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000804 string backup_log = Full_Backup_Path + "recovery.log";
805 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
806 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400807}
808
Dees_Troy093b7642012-09-21 15:59:38 -0400809bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400810 time_t Start, Stop;
811 time(&Start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000812 DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400813 if (!Part->Restore(Restore_Name))
814 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400815 if (Part->Has_SubPartition) {
816 std::vector<TWPartition*>::iterator subpart;
817
818 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
819 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
820 if (!(*subpart)->Restore(Restore_Name))
821 return false;
822 }
823 }
824 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400825 time(&Stop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000826 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 -0400827 return true;
828}
829
Dees_Troy51a0e822012-09-05 15:24:24 -0400830int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400831 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500832 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400833 time_t rStart, rStop;
834 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500835 string Restore_List, restore_path;
836 size_t start_pos = 0, end_pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400837
Dees_Troy2673cec2013-04-02 20:22:16 +0000838 gui_print("\n[RESTORE STARTED]\n\n");
839 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400840
Dees_Troy4a2a1262012-09-18 09:33:47 -0400841 if (!Mount_Current_Storage(true))
842 return false;
843
844 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500845 if (check_md5 > 0) {
846 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
847 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000848 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500849 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000850 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500851 }
852 DataManager::GetValue("tw_restore_selected", Restore_List);
853 if (!Restore_List.empty()) {
854 end_pos = Restore_List.find(";", start_pos);
855 while (end_pos != string::npos && start_pos < Restore_List.size()) {
856 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
857 restore_part = Find_Partition_By_Path(restore_path);
858 if (restore_part != NULL) {
859 partition_count++;
860 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
861 return false;
862 if (restore_part->Has_SubPartition) {
863 std::vector<TWPartition*>::iterator subpart;
864
865 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
866 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
867 if (!(*subpart)->Check_MD5(Restore_Name))
868 return false;
869 }
870 }
871 }
872 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500873 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500874 }
875 start_pos = end_pos + 1;
876 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400877 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400878 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400879
880 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000881 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400882 return false;
883 }
884
Dees_Troy2673cec2013-04-02 20:22:16 +0000885 gui_print("Restoring %i partitions...\n", partition_count);
886 DataManager::SetProgress(0.0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500887 start_pos = 0;
888 if (!Restore_List.empty()) {
889 end_pos = Restore_List.find(";", start_pos);
890 while (end_pos != string::npos && start_pos < Restore_List.size()) {
891 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
892 restore_part = Find_Partition_By_Path(restore_path);
893 if (restore_part != NULL) {
894 partition_count++;
895 if (!Restore_Partition(restore_part, Restore_Name, partition_count))
896 return false;
897 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000898 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500899 }
900 start_pos = end_pos + 1;
901 end_pos = Restore_List.find(";", start_pos);
902 }
903 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400904 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400905 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400906 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400907 time(&rStop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000908 gui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -0400909 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400910}
911
912void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400913 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500914 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000915 bool get_date = true, check_encryption = true;
916
917 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400918
919 DIR* d;
920 d = opendir(Restore_Name.c_str());
921 if (d == NULL)
922 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000923 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400924 return;
925 }
926
927 struct dirent* de;
928 while ((de = readdir(d)) != NULL)
929 {
930 // Strip off three components
931 char str[256];
932 char* label;
933 char* fstype = NULL;
934 char* extn = NULL;
935 char* ptr;
936
937 strcpy(str, de->d_name);
938 if (strlen(str) <= 2)
939 continue;
940
941 if (get_date) {
942 char file_path[255];
943 struct stat st;
944
945 strcpy(file_path, Restore_Name.c_str());
946 strcat(file_path, "/");
947 strcat(file_path, str);
948 stat(file_path, &st);
949 string backup_date = ctime((const time_t*)(&st.st_mtime));
950 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
951 get_date = false;
952 }
953
954 label = str;
955 ptr = label;
956 while (*ptr && *ptr != '.') ptr++;
957 if (*ptr == '.')
958 {
959 *ptr = 0x00;
960 ptr++;
961 fstype = ptr;
962 }
963 while (*ptr && *ptr != '.') ptr++;
964 if (*ptr == '.')
965 {
966 *ptr = 0x00;
967 ptr++;
968 extn = ptr;
969 }
970
Dees_Troy83bd4832013-05-04 12:39:56 +0000971 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -0500972 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +0000973 if (extnlength != 3 && extnlength != 6) continue;
974 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
975 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
976
977 if (check_encryption) {
978 string filename = Restore_Name + "/";
979 filename += de->d_name;
980 if (TWFunc::Get_File_Type(filename) == 2) {
981 LOGINFO("'%s' is encrypted\n", filename.c_str());
982 DataManager::SetValue("tw_restore_encrypted", 1);
983 }
984 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500985 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -0400986
987 TWPartition* Part = Find_Partition_By_Path(label);
988 if (Part == NULL)
989 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000990 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -0400991 continue;
992 }
993
994 Part->Backup_FileName = de->d_name;
995 if (strlen(extn) > 3) {
996 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
997 }
998
Dees_Troya13d74f2013-03-24 08:54:55 -0500999 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001000 }
1001 closedir(d);
1002
Dees_Troya13d74f2013-03-24 08:54:55 -05001003 // Set the final value
1004 DataManager::SetValue("tw_restore_list", Restore_List);
1005 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001006 return;
1007}
1008
1009int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001010 std::vector<TWPartition*>::iterator iter;
1011 int ret = false;
1012 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001013 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001014
1015 // Iterate through all partitions
1016 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001017 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001018 if (Path == "/and-sec")
1019 ret = (*iter)->Wipe_AndSec();
1020 else
1021 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001022 found = true;
1023 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1024 (*iter)->Wipe();
1025 }
1026 }
1027 if (found) {
1028 return ret;
1029 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001030 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001031 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001032}
1033
1034int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001035 TWPartition* Part = Find_Partition_By_Block(Block);
1036
1037 if (Part) {
1038 if (Part->Has_SubPartition) {
1039 std::vector<TWPartition*>::iterator subpart;
1040
1041 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1042 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1043 (*subpart)->Wipe();
1044 }
1045 return Part->Wipe();
1046 } else
1047 return Part->Wipe();
1048 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001049 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001050 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001051}
1052
1053int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001054 TWPartition* Part = Find_Partition_By_Name(Name);
1055
1056 if (Part) {
1057 if (Part->Has_SubPartition) {
1058 std::vector<TWPartition*>::iterator subpart;
1059
1060 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1061 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1062 (*subpart)->Wipe();
1063 }
1064 return Part->Wipe();
1065 } else
1066 return Part->Wipe();
1067 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001068 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001069 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001070}
1071
1072int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001073 std::vector<TWPartition*>::iterator iter;
1074 int ret = true;
1075
1076 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001077 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001078 if (!(*iter)->Wipe())
1079 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001080 } else if ((*iter)->Has_Android_Secure) {
1081 if (!(*iter)->Wipe_AndSec())
1082 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001083 }
1084 }
1085 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001086}
1087
Dees_Troy38bd7602012-09-14 13:33:53 -04001088int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1089 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001090 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001091
1092 if (!Mount_By_Path("/data", true))
1093 return false;
1094
1095 if (!Mount_By_Path("/cache", true))
1096 return false;
1097
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001098 dir.push_back("/data/dalvik-cache");
1099 dir.push_back("/cache/dalvik-cache");
1100 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001101 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001102 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001103 if (stat(dir.at(i).c_str(), &st) == 0) {
1104 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001105 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001106 }
1107 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001108 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001109 if (sdext && sdext->Is_Present && sdext->Mount(false))
1110 {
1111 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1112 {
1113 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
1114 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001115 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001116 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001117 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001118 return true;
1119}
1120
1121int TWPartitionManager::Wipe_Rotate_Data(void) {
1122 if (!Mount_By_Path("/data", true))
1123 return false;
1124
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001125 unlink("/data/misc/akmd*");
1126 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001127 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001128 return true;
1129}
1130
1131int TWPartitionManager::Wipe_Battery_Stats(void) {
1132 struct stat st;
1133
1134 if (!Mount_By_Path("/data", true))
1135 return false;
1136
1137 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001138 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001139 } else {
1140 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001141 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001142 }
1143 return true;
1144}
1145
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001146int TWPartitionManager::Wipe_Android_Secure(void) {
1147 std::vector<TWPartition*>::iterator iter;
1148 int ret = false;
1149 bool found = false;
1150
1151 // Iterate through all partitions
1152 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1153 if ((*iter)->Has_Android_Secure) {
1154 ret = (*iter)->Wipe_AndSec();
1155 found = true;
1156 }
1157 }
1158 if (found) {
1159 return ret;
1160 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001161 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001162 }
1163 return false;
1164}
1165
Dees_Troy38bd7602012-09-14 13:33:53 -04001166int TWPartitionManager::Format_Data(void) {
1167 TWPartition* dat = Find_Partition_By_Path("/data");
1168
1169 if (dat != NULL) {
1170 if (!dat->UnMount(true))
1171 return false;
1172
1173 return dat->Wipe_Encryption();
1174 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001175 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001176 return false;
1177 }
1178 return false;
1179}
1180
1181int TWPartitionManager::Wipe_Media_From_Data(void) {
1182 TWPartition* dat = Find_Partition_By_Path("/data");
1183
1184 if (dat != NULL) {
1185 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001186 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001187 return false;
1188 }
1189 if (!dat->Mount(true))
1190 return false;
1191
Dees_Troy2673cec2013-04-02 20:22:16 +00001192 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001193 TWFunc::removeDir("/data/media", false);
1194 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
1195 return -1;
Dees_Troy38bd7602012-09-14 13:33:53 -04001196 if (dat->Has_Data_Media) {
1197 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001198 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1199 dat->UnMount(false);
1200 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001201 }
1202 return true;
1203 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001204 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001205 return false;
1206 }
1207 return false;
1208}
1209
Dees_Troy51a0e822012-09-05 15:24:24 -04001210void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001211 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001212 return;
1213}
1214
1215void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001216 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001217 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001218
Dees_Troy2673cec2013-04-02 20:22:16 +00001219 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001220 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001221 if ((*iter)->Can_Be_Mounted) {
1222 (*iter)->Update_Size(true);
1223 if ((*iter)->Mount_Point == "/system") {
1224 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1225 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1226 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1227 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1228 } else if ((*iter)->Mount_Point == "/cache") {
1229 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1230 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1231 } else if ((*iter)->Mount_Point == "/sd-ext") {
1232 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1233 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1234 if ((*iter)->Backup_Size == 0) {
1235 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1236 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1237 } else
1238 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001239 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001240 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001241 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001242 if ((*iter)->Backup_Size == 0) {
1243 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1244 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1245 } else
1246 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001247 } else if ((*iter)->Mount_Point == "/boot") {
1248 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1249 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1250 if ((*iter)->Backup_Size == 0) {
1251 DataManager::SetValue("tw_has_boot_partition", 0);
1252 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1253 } else
1254 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001255 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001256#ifdef SP1_NAME
1257 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1258 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1259 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1260 }
1261#endif
1262#ifdef SP2_NAME
1263 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1264 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1265 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1266 }
1267#endif
1268#ifdef SP3_NAME
1269 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1270 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1271 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1272 }
1273#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001274 } else {
1275 // Handle unmountable partitions in case we reset defaults
1276 if ((*iter)->Mount_Point == "/boot") {
1277 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1278 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1279 if ((*iter)->Backup_Size == 0) {
1280 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1281 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1282 } else
1283 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1284 } else if ((*iter)->Mount_Point == "/recovery") {
1285 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1286 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1287 if ((*iter)->Backup_Size == 0) {
1288 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1289 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1290 } else
1291 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001292 } else if ((*iter)->Mount_Point == "/data") {
1293 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001294 }
1295#ifdef SP1_NAME
1296 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1297 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1298 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1299 }
1300#endif
1301#ifdef SP2_NAME
1302 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1303 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1304 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1305 }
1306#endif
1307#ifdef SP3_NAME
1308 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1309 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1310 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1311 }
1312#endif
Dees_Troy51127312012-09-08 13:08:49 -04001313 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001314 }
Dees_Troy51127312012-09-08 13:08:49 -04001315 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1316 string current_storage_path = DataManager::GetCurrentStoragePath();
1317 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001318 if (FreeStorage != NULL) {
1319 // Attempt to mount storage
1320 if (!FreeStorage->Mount(false)) {
1321 // We couldn't mount storage... check to see if we have dual storage
1322 int has_dual_storage;
1323 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1324 if (has_dual_storage == 1) {
1325 // We have dual storage, see if we're using the internal storage that should always be present
1326 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001327 if (!FreeStorage->Is_Encrypted) {
1328 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001329 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001330 }
Dees_Troy8170a922012-09-18 15:40:25 -04001331 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1332 } else {
1333 // We were using external, flip to internal
1334 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1335 current_storage_path = DataManager::GetCurrentStoragePath();
1336 FreeStorage = Find_Partition_By_Path(current_storage_path);
1337 if (FreeStorage != NULL) {
1338 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1339 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001340 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001341 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1342 }
1343 }
1344 } else {
1345 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001346 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001347 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1348 }
1349 } else {
1350 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1351 }
1352 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001353 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001354 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001355 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001356 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001357 return;
1358}
1359
1360int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001361#ifdef TW_INCLUDE_CRYPTO
1362 int ret_val, password_len;
1363 char crypto_blkdev[255], cPassword[255];
1364 size_t result;
1365
1366 property_set("ro.crypto.state", "encrypted");
1367#ifdef TW_INCLUDE_JB_CRYPTO
1368 // No extra flags needed
1369#else
1370 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1371 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1372 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1373 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1374 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1375 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001376
1377#ifdef CRYPTO_SD_FS_TYPE
1378 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1379 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1380 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001381#endif
a39552696ff55ce2013-01-08 16:14:56 +00001382
1383 property_set("rw.km_fips_status", "ready");
1384
1385#endif
1386
1387 // some samsung devices store "footer" on efs partition
1388 TWPartition *efs = Find_Partition_By_Path("/efs");
1389 if(efs && !efs->Is_Mounted())
1390 efs->Mount(false);
1391 else
1392 efs = 0;
1393#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001394#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001395 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001396 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001397 property_set("ro.crypto.external_encrypted", "1");
1398 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1399 } else {
1400 property_set("ro.crypto.external_encrypted", "0");
1401 }
1402#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001403#endif
a39552696ff55ce2013-01-08 16:14:56 +00001404
Dees_Troy5bf43922012-09-07 16:07:55 -04001405 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001406 int pwret = cryptfs_check_passwd(cPassword);
1407
1408 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001409 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001410 return -1;
1411 }
a39552696ff55ce2013-01-08 16:14:56 +00001412
1413 if(efs)
1414 efs->UnMount(false);
1415
Dees_Troy5bf43922012-09-07 16:07:55 -04001416 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1417 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001418 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001419 } else {
1420 TWPartition* dat = Find_Partition_By_Path("/data");
1421 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001422 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001423 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1424 dat->Is_Decrypted = true;
1425 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001426 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001427 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 +00001428 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001429
1430#ifdef CRYPTO_SD_FS_TYPE
1431 char crypto_blkdev_sd[255];
1432 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1433 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001434 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001435 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001436 emmc->Is_Decrypted = true;
1437 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1438 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001439 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001440 }
a39552696ff55ce2013-01-08 16:14:56 +00001441#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001442#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001443#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001444 char is_external_decrypted[255];
1445 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1446 if (strcmp(is_external_decrypted, "1") == 0) {
1447 sdcard->Is_Decrypted = true;
1448 sdcard->EcryptFS_Password = Password;
1449 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001450 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1451 MetaEcfsFile += "/.MetaEcfsFile";
1452 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1453 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1454 sdcard->UnMount(false);
1455 sdcard->Mount(false);
1456 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001457 } else {
Dees_Troy066eb302013-08-23 17:20:32 +00001458 LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001459 sdcard->Is_Decrypted = false;
1460 sdcard->Decrypted_Block_Device = "";
1461 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001462#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001463#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001464
Dees_Troy5bf43922012-09-07 16:07:55 -04001465 // Sleep for a bit so that the device will be ready
1466 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001467#ifdef RECOVERY_SDCARD_ON_DATA
1468 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1469 dat->Storage_Path = "/data/media/0";
1470 dat->Symlink_Path = dat->Storage_Path;
1471 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
1472 dat->UnMount(false);
1473 DataManager::SetBackupFolder();
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001474 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001475 }
1476#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001477 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001478 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001479 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001480 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001481 }
1482 return 0;
1483#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001484 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001485 return -1;
1486#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001487 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001488}
1489
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001490int TWPartitionManager::Fix_Permissions(void) {
1491 int result = 0;
1492 if (!Mount_By_Path("/data", true))
1493 return false;
1494
1495 if (!Mount_By_Path("/system", true))
1496 return false;
1497
1498 Mount_By_Path("/sd-ext", false);
1499
1500 fixPermissions perms;
1501 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001502 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001503 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001504 return result;
1505}
1506
Dees_Troyd21618c2012-10-14 18:48:49 -04001507int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001508 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1509
1510 if (Part == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001511 LOGERR("Unable to locate volume information for USB storage mode.");
Dees_Troyd21618c2012-10-14 18:48:49 -04001512 return false;
1513 }
1514 if (!Part->UnMount(true))
1515 return false;
1516
Dees_Troy2673cec2013-04-02 20:22:16 +00001517 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1518 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001519 return false;
1520 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001521 return true;
1522}
1523
Dees_Troy8170a922012-09-18 15:40:25 -04001524int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001525 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001526 char lun_file[255];
Dees_Troy8170a922012-09-18 15:40:25 -04001527 string ext_path;
Dees_Troyd21618c2012-10-14 18:48:49 -04001528 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001529
1530 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1531 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1532 if (has_dual == 1 && has_data_media == 0) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001533 string Lun_File_str = CUSTOM_LUN_FILE;
1534 size_t found = Lun_File_str.find("%");
1535 if (found != string::npos) {
1536 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1537 if (TWFunc::Path_Exists(lun_file))
1538 has_multiple_lun = true;
1539 }
1540 if (!has_multiple_lun) {
Dees_Troyf4ca6122012-10-13 22:17:20 -04001541 // Device doesn't have multiple lun files, mount current storage
Dees_Troyf4ca6122012-10-13 22:17:20 -04001542 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001543 return Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001544 } else {
1545 // Device has multiple lun files
Dees_Troyf4ca6122012-10-13 22:17:20 -04001546 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001547 if (!Open_Lun_File(DataManager::GetSettingsStoragePath(), lun_file))
Dees_Troyf4ca6122012-10-13 22:17:20 -04001548 return false;
Dees_Troyf4ca6122012-10-13 22:17:20 -04001549 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001550 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Dees_Troyd21618c2012-10-14 18:48:49 -04001551 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001552 }
Dees_Troy8170a922012-09-18 15:40:25 -04001553 } else {
1554 if (has_data_media == 0)
1555 ext_path = DataManager::GetCurrentStoragePath();
1556 else
1557 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001558 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001559 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001560 }
1561 return true;
1562}
1563
1564int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001565 int index, ret;
1566 char lun_file[255], ch[2] = {0, 0};
1567 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001568
1569 for (index=0; index<2; index++) {
1570 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001571 ret = TWFunc::write_file(lun_file, str);
1572 Mount_All_Storage();
1573 Update_System_Details();
1574 if (ret < 0) {
1575 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001576 }
Dees_Troy8170a922012-09-18 15:40:25 -04001577 }
Dees_Troye58d5262012-09-21 12:27:57 -04001578 Mount_All_Storage();
1579 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001580 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001581 if (ret < 0 && index == 0) {
1582 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1583 return false;
1584 } else {
1585 return true;
1586 }
Dees_Troy8170a922012-09-18 15:40:25 -04001587 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001588}
1589
1590void TWPartitionManager::Mount_All_Storage(void) {
1591 std::vector<TWPartition*>::iterator iter;
1592
1593 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1594 if ((*iter)->Is_Storage)
1595 (*iter)->Mount(false);
1596 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001597}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001598
Dees_Troyd0384ef2012-10-12 12:15:42 -04001599void TWPartitionManager::UnMount_Main_Partitions(void) {
1600 // Unmounts system and data if data is not data/media
1601 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001602 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001603
1604 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1605
1606 UnMount_By_Path("/system", true);
1607#ifndef RECOVERY_SDCARD_ON_DATA
1608 UnMount_By_Path("/data", true);
1609#endif
1610 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1611 Boot_Partition->UnMount(true);
1612}
1613
Dees_Troy9350b8d2012-09-27 12:38:38 -04001614int TWPartitionManager::Partition_SDCard(void) {
1615 char mkdir_path[255], temp[255], line[512];
1616 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1617 int ext, swap, total_size = 0, fat_size;
1618 FILE* fp;
1619
Dees_Troy2673cec2013-04-02 20:22:16 +00001620 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001621#ifdef TW_EXTERNAL_STORAGE_PATH
1622 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1623#else
1624 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1625#endif
1626 if (SDCard == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001627 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001628 return false;
1629 }
1630 if (!SDCard->UnMount(true))
1631 return false;
1632 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1633 if (SDext != NULL) {
1634 if (!SDext->UnMount(true))
1635 return false;
1636 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001637
1638 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001639 Device = SDCard->Actual_Block_Device;
1640 // Just use the root block device
1641 Device.resize(strlen("/dev/block/mmcblkX"));
1642
1643 // Find the size of the block device:
1644 fp = fopen("/proc/partitions", "rt");
1645 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001646 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001647 return false;
1648 }
1649
1650 while (fgets(line, sizeof(line), fp) != NULL)
1651 {
1652 unsigned long major, minor, blocks;
1653 char device[512];
1654 char tmpString[64];
1655
1656 if (strlen(line) < 7 || line[0] == 'm') continue;
1657 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1658
1659 tmpdevice = "/dev/block/";
1660 tmpdevice += device;
1661 if (tmpdevice == Device) {
1662 // Adjust block size to byte size
1663 total_size = (int)(blocks * 1024ULL / 1000000LLU);
1664 break;
1665 }
1666 }
1667 fclose(fp);
1668
1669 DataManager::GetValue("tw_sdext_size", ext);
1670 DataManager::GetValue("tw_swap_size", swap);
1671 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1672 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001673 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 -04001674 memset(temp, 0, sizeof(temp));
1675 sprintf(temp, "%i", fat_size);
1676 fat_str = temp;
1677 memset(temp, 0, sizeof(temp));
1678 sprintf(temp, "%i", fat_size + ext);
1679 ext_str = temp;
1680 memset(temp, 0, sizeof(temp));
1681 sprintf(temp, "%i", fat_size + ext + swap);
1682 swap_str = temp;
1683 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001684 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001685 return false;
1686 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001687 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001688 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001689 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001690 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001691 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001692 Update_System_Details();
1693 return false;
1694 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001695 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001696 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001697 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001698 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001699 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001700 return false;
1701 }
1702 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001703 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001704 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001705 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001706 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001707 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001708 Update_System_Details();
1709 return false;
1710 }
1711 }
1712 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001713 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001714 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001715 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001716 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001717 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001718 Update_System_Details();
1719 return false;
1720 }
1721 }
1722 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1723#ifdef TW_EXTERNAL_STORAGE_PATH
1724 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1725 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1726 memset(mkdir_path, 0, sizeof(mkdir_path));
1727 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1728#else
1729 Mount_By_Path("/sdcard", 1);
1730 strcpy(mkdir_path, "/sdcard/TWRP");
1731#endif
1732 mkdir(mkdir_path, 0777);
1733 DataManager::Flush();
1734#ifdef TW_EXTERNAL_STORAGE_PATH
1735 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1736 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1737 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1738#else
1739 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1740 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1741 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1742#endif
1743 if (ext > 0) {
1744 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001745 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001746 return false;
1747 }
1748 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001749 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1750 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001751 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001752 }
1753
1754 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00001755 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001756 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001757}
Dees_Troya13d74f2013-03-24 08:54:55 -05001758
1759void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1760 std::vector<TWPartition*>::iterator iter;
1761 if (ListType == "mount") {
1762 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1763 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1764 struct PartitionList part;
1765 part.Display_Name = (*iter)->Display_Name;
1766 part.Mount_Point = (*iter)->Mount_Point;
1767 part.selected = (*iter)->Is_Mounted();
1768 Partition_List->push_back(part);
1769 }
1770 }
1771 } else if (ListType == "storage") {
1772 char free_space[255];
1773 string Current_Storage = DataManager::GetCurrentStoragePath();
1774 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1775 if ((*iter)->Is_Storage) {
1776 struct PartitionList part;
1777 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1778 part.Display_Name = (*iter)->Storage_Name + " (";
1779 part.Display_Name += free_space;
1780 part.Display_Name += "MB)";
1781 part.Mount_Point = (*iter)->Storage_Path;
1782 if ((*iter)->Storage_Path == Current_Storage)
1783 part.selected = 1;
1784 else
1785 part.selected = 0;
1786 Partition_List->push_back(part);
1787 }
1788 }
1789 } else if (ListType == "backup") {
1790 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001791 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001792 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001793 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001794 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001795 Backup_Size = (*iter)->Backup_Size;
1796 if ((*iter)->Has_SubPartition) {
1797 std::vector<TWPartition*>::iterator subpart;
1798
1799 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1800 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1801 Backup_Size += (*subpart)->Backup_Size;
1802 }
1803 }
1804 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001805 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1806 part.Display_Name += backup_size;
1807 part.Display_Name += "MB)";
1808 part.Mount_Point = (*iter)->Backup_Path;
1809 part.selected = 0;
1810 Partition_List->push_back(part);
1811 }
1812 }
1813 } else if (ListType == "restore") {
1814 string Restore_List, restore_path;
1815 TWPartition* restore_part = NULL;
1816
1817 DataManager::GetValue("tw_restore_list", Restore_List);
1818 if (!Restore_List.empty()) {
1819 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1820 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1821 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05001822 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
1823 if (restore_part->Backup_Name == "recovery" || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001824 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05001825 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05001826 } else {
1827 struct PartitionList part;
1828 part.Display_Name = restore_part->Backup_Display_Name;
1829 part.Mount_Point = restore_part->Backup_Path;
1830 part.selected = 1;
1831 Partition_List->push_back(part);
1832 }
1833 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001834 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001835 }
1836 start_pos = end_pos + 1;
1837 end_pos = Restore_List.find(";", start_pos);
1838 }
1839 }
1840 } else if (ListType == "wipe") {
1841 struct PartitionList dalvik;
1842 dalvik.Display_Name = "Dalvik Cache";
1843 dalvik.Mount_Point = "DALVIK";
1844 dalvik.selected = 0;
1845 Partition_List->push_back(dalvik);
1846 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1847 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1848 struct PartitionList part;
1849 part.Display_Name = (*iter)->Display_Name;
1850 part.Mount_Point = (*iter)->Mount_Point;
1851 part.selected = 0;
1852 Partition_List->push_back(part);
1853 }
1854 if ((*iter)->Has_Android_Secure) {
1855 struct PartitionList part;
1856 part.Display_Name = (*iter)->Backup_Display_Name;
1857 part.Mount_Point = (*iter)->Backup_Path;
1858 part.selected = 0;
1859 Partition_List->push_back(part);
1860 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001861 if ((*iter)->Has_Data_Media) {
1862 struct PartitionList datamedia;
1863 datamedia.Display_Name = (*iter)->Storage_Name;
1864 datamedia.Mount_Point = "INTERNAL";
1865 datamedia.selected = 0;
1866 Partition_List->push_back(datamedia);
1867 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001868 }
1869 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001870 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001871 }
1872}
1873
1874int TWPartitionManager::Fstab_Processed(void) {
1875 return Partitions.size();
1876}
Dees_Troyd93bda52013-07-03 19:55:19 +00001877
1878void TWPartitionManager::Output_Storage_Fstab(void) {
1879 std::vector<TWPartition*>::iterator iter;
1880 char storage_partition[255];
1881 string Temp;
1882 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
1883
1884 if (fp == NULL) {
1885 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
1886 return;
1887 }
1888
1889 // Iterate through all partitions
1890 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1891 if ((*iter)->Is_Storage) {
1892 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
1893 strcpy(storage_partition, Temp.c_str());
1894 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
1895 }
1896 }
1897 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001898}