blob: 4e72117dd0422cfcee33289d9ea535b0f8248213 [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_Troy43d8b002012-09-17 16:00:01 -0400904
Dees_Troyb46a6842012-09-25 11:06:46 -0400905 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400906 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400907 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400908 time(&rStop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000909 gui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -0400910 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400911}
912
913void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400914 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500915 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000916 bool get_date = true, check_encryption = true;
917
918 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400919
920 DIR* d;
921 d = opendir(Restore_Name.c_str());
922 if (d == NULL)
923 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000924 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400925 return;
926 }
927
928 struct dirent* de;
929 while ((de = readdir(d)) != NULL)
930 {
931 // Strip off three components
932 char str[256];
933 char* label;
934 char* fstype = NULL;
935 char* extn = NULL;
936 char* ptr;
937
938 strcpy(str, de->d_name);
939 if (strlen(str) <= 2)
940 continue;
941
942 if (get_date) {
943 char file_path[255];
944 struct stat st;
945
946 strcpy(file_path, Restore_Name.c_str());
947 strcat(file_path, "/");
948 strcat(file_path, str);
949 stat(file_path, &st);
950 string backup_date = ctime((const time_t*)(&st.st_mtime));
951 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
952 get_date = false;
953 }
954
955 label = str;
956 ptr = label;
957 while (*ptr && *ptr != '.') ptr++;
958 if (*ptr == '.')
959 {
960 *ptr = 0x00;
961 ptr++;
962 fstype = ptr;
963 }
964 while (*ptr && *ptr != '.') ptr++;
965 if (*ptr == '.')
966 {
967 *ptr = 0x00;
968 ptr++;
969 extn = ptr;
970 }
971
Dees_Troy83bd4832013-05-04 12:39:56 +0000972 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -0500973 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +0000974 if (extnlength != 3 && extnlength != 6) continue;
975 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
976 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
977
978 if (check_encryption) {
979 string filename = Restore_Name + "/";
980 filename += de->d_name;
981 if (TWFunc::Get_File_Type(filename) == 2) {
982 LOGINFO("'%s' is encrypted\n", filename.c_str());
983 DataManager::SetValue("tw_restore_encrypted", 1);
984 }
985 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500986 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -0400987
988 TWPartition* Part = Find_Partition_By_Path(label);
989 if (Part == NULL)
990 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000991 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -0400992 continue;
993 }
994
995 Part->Backup_FileName = de->d_name;
996 if (strlen(extn) > 3) {
997 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
998 }
999
Dees_Troya13d74f2013-03-24 08:54:55 -05001000 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001001 }
1002 closedir(d);
1003
Dees_Troya13d74f2013-03-24 08:54:55 -05001004 // Set the final value
1005 DataManager::SetValue("tw_restore_list", Restore_List);
1006 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001007 return;
1008}
1009
1010int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001011 std::vector<TWPartition*>::iterator iter;
1012 int ret = false;
1013 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001014 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001015
1016 // Iterate through all partitions
1017 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001018 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001019 if (Path == "/and-sec")
1020 ret = (*iter)->Wipe_AndSec();
1021 else
1022 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001023 found = true;
1024 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1025 (*iter)->Wipe();
1026 }
1027 }
1028 if (found) {
1029 return ret;
1030 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001031 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001032 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001033}
1034
1035int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001036 TWPartition* Part = Find_Partition_By_Block(Block);
1037
1038 if (Part) {
1039 if (Part->Has_SubPartition) {
1040 std::vector<TWPartition*>::iterator subpart;
1041
1042 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1043 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1044 (*subpart)->Wipe();
1045 }
1046 return Part->Wipe();
1047 } else
1048 return Part->Wipe();
1049 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001050 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001051 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001052}
1053
1054int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001055 TWPartition* Part = Find_Partition_By_Name(Name);
1056
1057 if (Part) {
1058 if (Part->Has_SubPartition) {
1059 std::vector<TWPartition*>::iterator subpart;
1060
1061 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1062 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1063 (*subpart)->Wipe();
1064 }
1065 return Part->Wipe();
1066 } else
1067 return Part->Wipe();
1068 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001069 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001070 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001071}
1072
1073int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001074 std::vector<TWPartition*>::iterator iter;
1075 int ret = true;
1076
1077 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001078 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001079 if (!(*iter)->Wipe())
1080 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001081 } else if ((*iter)->Has_Android_Secure) {
1082 if (!(*iter)->Wipe_AndSec())
1083 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001084 }
1085 }
1086 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001087}
1088
Dees_Troy38bd7602012-09-14 13:33:53 -04001089int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1090 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001091 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001092
1093 if (!Mount_By_Path("/data", true))
1094 return false;
1095
1096 if (!Mount_By_Path("/cache", true))
1097 return false;
1098
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001099 dir.push_back("/data/dalvik-cache");
1100 dir.push_back("/cache/dalvik-cache");
1101 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001102 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001103 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001104 if (stat(dir.at(i).c_str(), &st) == 0) {
1105 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001106 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001107 }
1108 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001109 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001110 if (sdext && sdext->Is_Present && sdext->Mount(false))
1111 {
1112 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1113 {
1114 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
1115 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001116 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001117 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001118 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001119 return true;
1120}
1121
1122int TWPartitionManager::Wipe_Rotate_Data(void) {
1123 if (!Mount_By_Path("/data", true))
1124 return false;
1125
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001126 unlink("/data/misc/akmd*");
1127 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001128 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001129 return true;
1130}
1131
1132int TWPartitionManager::Wipe_Battery_Stats(void) {
1133 struct stat st;
1134
1135 if (!Mount_By_Path("/data", true))
1136 return false;
1137
1138 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001139 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001140 } else {
1141 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001142 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001143 }
1144 return true;
1145}
1146
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001147int TWPartitionManager::Wipe_Android_Secure(void) {
1148 std::vector<TWPartition*>::iterator iter;
1149 int ret = false;
1150 bool found = false;
1151
1152 // Iterate through all partitions
1153 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1154 if ((*iter)->Has_Android_Secure) {
1155 ret = (*iter)->Wipe_AndSec();
1156 found = true;
1157 }
1158 }
1159 if (found) {
1160 return ret;
1161 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001162 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001163 }
1164 return false;
1165}
1166
Dees_Troy38bd7602012-09-14 13:33:53 -04001167int TWPartitionManager::Format_Data(void) {
1168 TWPartition* dat = Find_Partition_By_Path("/data");
1169
1170 if (dat != NULL) {
1171 if (!dat->UnMount(true))
1172 return false;
1173
1174 return dat->Wipe_Encryption();
1175 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001176 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001177 return false;
1178 }
1179 return false;
1180}
1181
1182int TWPartitionManager::Wipe_Media_From_Data(void) {
1183 TWPartition* dat = Find_Partition_By_Path("/data");
1184
1185 if (dat != NULL) {
1186 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001187 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001188 return false;
1189 }
1190 if (!dat->Mount(true))
1191 return false;
1192
Dees_Troy2673cec2013-04-02 20:22:16 +00001193 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001194 TWFunc::removeDir("/data/media", false);
1195 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
1196 return -1;
Dees_Troy38bd7602012-09-14 13:33:53 -04001197 if (dat->Has_Data_Media) {
1198 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001199 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1200 dat->UnMount(false);
1201 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001202 }
1203 return true;
1204 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001205 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001206 return false;
1207 }
1208 return false;
1209}
1210
Dees_Troy51a0e822012-09-05 15:24:24 -04001211void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001212 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001213 return;
1214}
1215
1216void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001217 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001218 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001219
Dees_Troy2673cec2013-04-02 20:22:16 +00001220 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001221 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001222 if ((*iter)->Can_Be_Mounted) {
1223 (*iter)->Update_Size(true);
1224 if ((*iter)->Mount_Point == "/system") {
1225 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1226 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1227 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1228 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1229 } else if ((*iter)->Mount_Point == "/cache") {
1230 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1231 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1232 } else if ((*iter)->Mount_Point == "/sd-ext") {
1233 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1234 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1235 if ((*iter)->Backup_Size == 0) {
1236 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1237 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1238 } else
1239 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001240 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001241 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001242 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001243 if ((*iter)->Backup_Size == 0) {
1244 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1245 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1246 } else
1247 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001248 } else if ((*iter)->Mount_Point == "/boot") {
1249 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1250 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1251 if ((*iter)->Backup_Size == 0) {
1252 DataManager::SetValue("tw_has_boot_partition", 0);
1253 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1254 } else
1255 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001256 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001257#ifdef SP1_NAME
1258 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1259 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1260 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1261 }
1262#endif
1263#ifdef SP2_NAME
1264 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1265 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1266 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1267 }
1268#endif
1269#ifdef SP3_NAME
1270 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1271 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1272 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1273 }
1274#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001275 } else {
1276 // Handle unmountable partitions in case we reset defaults
1277 if ((*iter)->Mount_Point == "/boot") {
1278 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1279 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1280 if ((*iter)->Backup_Size == 0) {
1281 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1282 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1283 } else
1284 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1285 } else if ((*iter)->Mount_Point == "/recovery") {
1286 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1287 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1288 if ((*iter)->Backup_Size == 0) {
1289 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1290 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1291 } else
1292 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001293 } else if ((*iter)->Mount_Point == "/data") {
1294 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001295 }
1296#ifdef SP1_NAME
1297 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1298 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1299 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1300 }
1301#endif
1302#ifdef SP2_NAME
1303 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1304 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1305 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1306 }
1307#endif
1308#ifdef SP3_NAME
1309 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1310 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1311 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1312 }
1313#endif
Dees_Troy51127312012-09-08 13:08:49 -04001314 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001315 }
Dees_Troy51127312012-09-08 13:08:49 -04001316 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1317 string current_storage_path = DataManager::GetCurrentStoragePath();
1318 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001319 if (FreeStorage != NULL) {
1320 // Attempt to mount storage
1321 if (!FreeStorage->Mount(false)) {
1322 // We couldn't mount storage... check to see if we have dual storage
1323 int has_dual_storage;
1324 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1325 if (has_dual_storage == 1) {
1326 // We have dual storage, see if we're using the internal storage that should always be present
1327 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001328 if (!FreeStorage->Is_Encrypted) {
1329 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001330 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001331 }
Dees_Troy8170a922012-09-18 15:40:25 -04001332 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1333 } else {
1334 // We were using external, flip to internal
1335 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1336 current_storage_path = DataManager::GetCurrentStoragePath();
1337 FreeStorage = Find_Partition_By_Path(current_storage_path);
1338 if (FreeStorage != NULL) {
1339 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1340 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001341 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001342 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1343 }
1344 }
1345 } else {
1346 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001347 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001348 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1349 }
1350 } else {
1351 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1352 }
1353 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001354 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001355 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001356 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001357 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001358 return;
1359}
1360
1361int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001362#ifdef TW_INCLUDE_CRYPTO
1363 int ret_val, password_len;
1364 char crypto_blkdev[255], cPassword[255];
1365 size_t result;
1366
1367 property_set("ro.crypto.state", "encrypted");
1368#ifdef TW_INCLUDE_JB_CRYPTO
1369 // No extra flags needed
1370#else
1371 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1372 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1373 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1374 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1375 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1376 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001377
1378#ifdef CRYPTO_SD_FS_TYPE
1379 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1380 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1381 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001382#endif
a39552696ff55ce2013-01-08 16:14:56 +00001383
1384 property_set("rw.km_fips_status", "ready");
1385
1386#endif
1387
1388 // some samsung devices store "footer" on efs partition
1389 TWPartition *efs = Find_Partition_By_Path("/efs");
1390 if(efs && !efs->Is_Mounted())
1391 efs->Mount(false);
1392 else
1393 efs = 0;
1394#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001395#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001396 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001397 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001398 property_set("ro.crypto.external_encrypted", "1");
1399 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1400 } else {
1401 property_set("ro.crypto.external_encrypted", "0");
1402 }
1403#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001404#endif
a39552696ff55ce2013-01-08 16:14:56 +00001405
Dees_Troy5bf43922012-09-07 16:07:55 -04001406 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001407 int pwret = cryptfs_check_passwd(cPassword);
1408
1409 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001410 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001411 return -1;
1412 }
a39552696ff55ce2013-01-08 16:14:56 +00001413
1414 if(efs)
1415 efs->UnMount(false);
1416
Dees_Troy5bf43922012-09-07 16:07:55 -04001417 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1418 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001419 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001420 } else {
1421 TWPartition* dat = Find_Partition_By_Path("/data");
1422 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001423 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001424 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1425 dat->Is_Decrypted = true;
1426 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001427 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001428 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 +00001429 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001430
1431#ifdef CRYPTO_SD_FS_TYPE
1432 char crypto_blkdev_sd[255];
1433 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1434 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001435 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001436 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001437 emmc->Is_Decrypted = true;
1438 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1439 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001440 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001441 }
a39552696ff55ce2013-01-08 16:14:56 +00001442#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001443#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001444#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001445 char is_external_decrypted[255];
1446 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1447 if (strcmp(is_external_decrypted, "1") == 0) {
1448 sdcard->Is_Decrypted = true;
1449 sdcard->EcryptFS_Password = Password;
1450 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001451 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1452 MetaEcfsFile += "/.MetaEcfsFile";
1453 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1454 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1455 sdcard->UnMount(false);
1456 sdcard->Mount(false);
1457 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001458 } else {
Dees_Troy066eb302013-08-23 17:20:32 +00001459 LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001460 sdcard->Is_Decrypted = false;
1461 sdcard->Decrypted_Block_Device = "";
1462 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001463#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001464#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001465
Dees_Troy5bf43922012-09-07 16:07:55 -04001466 // Sleep for a bit so that the device will be ready
1467 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001468#ifdef RECOVERY_SDCARD_ON_DATA
1469 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1470 dat->Storage_Path = "/data/media/0";
1471 dat->Symlink_Path = dat->Storage_Path;
1472 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
1473 dat->UnMount(false);
1474 DataManager::SetBackupFolder();
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001475 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001476 }
1477#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001478 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001479 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001480 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001481 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001482 }
1483 return 0;
1484#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001485 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001486 return -1;
1487#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001488 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001489}
1490
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001491int TWPartitionManager::Fix_Permissions(void) {
1492 int result = 0;
1493 if (!Mount_By_Path("/data", true))
1494 return false;
1495
1496 if (!Mount_By_Path("/system", true))
1497 return false;
1498
1499 Mount_By_Path("/sd-ext", false);
1500
1501 fixPermissions perms;
1502 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001503 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001504 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001505 return result;
1506}
1507
Dees_Troyd21618c2012-10-14 18:48:49 -04001508int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001509 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1510
1511 if (Part == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001512 LOGERR("Unable to locate volume information for USB storage mode.");
Dees_Troyd21618c2012-10-14 18:48:49 -04001513 return false;
1514 }
1515 if (!Part->UnMount(true))
1516 return false;
1517
Dees_Troy2673cec2013-04-02 20:22:16 +00001518 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1519 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001520 return false;
1521 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001522 return true;
1523}
1524
Dees_Troy8170a922012-09-18 15:40:25 -04001525int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001526 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001527 char lun_file[255];
Dees_Troy8170a922012-09-18 15:40:25 -04001528 string ext_path;
Dees_Troyd21618c2012-10-14 18:48:49 -04001529 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001530
1531 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1532 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1533 if (has_dual == 1 && has_data_media == 0) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001534 string Lun_File_str = CUSTOM_LUN_FILE;
1535 size_t found = Lun_File_str.find("%");
1536 if (found != string::npos) {
1537 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1538 if (TWFunc::Path_Exists(lun_file))
1539 has_multiple_lun = true;
1540 }
1541 if (!has_multiple_lun) {
Dees_Troyf4ca6122012-10-13 22:17:20 -04001542 // Device doesn't have multiple lun files, mount current storage
Dees_Troyf4ca6122012-10-13 22:17:20 -04001543 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001544 return Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001545 } else {
1546 // Device has multiple lun files
Dees_Troyf4ca6122012-10-13 22:17:20 -04001547 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001548 if (!Open_Lun_File(DataManager::GetSettingsStoragePath(), lun_file))
Dees_Troyf4ca6122012-10-13 22:17:20 -04001549 return false;
Dees_Troyf4ca6122012-10-13 22:17:20 -04001550 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001551 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Dees_Troyd21618c2012-10-14 18:48:49 -04001552 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001553 }
Dees_Troy8170a922012-09-18 15:40:25 -04001554 } else {
1555 if (has_data_media == 0)
1556 ext_path = DataManager::GetCurrentStoragePath();
1557 else
1558 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001559 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001560 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001561 }
1562 return true;
1563}
1564
1565int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001566 int index, ret;
1567 char lun_file[255], ch[2] = {0, 0};
1568 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001569
1570 for (index=0; index<2; index++) {
1571 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001572 ret = TWFunc::write_file(lun_file, str);
1573 Mount_All_Storage();
1574 Update_System_Details();
1575 if (ret < 0) {
1576 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001577 }
Dees_Troy8170a922012-09-18 15:40:25 -04001578 }
Dees_Troye58d5262012-09-21 12:27:57 -04001579 Mount_All_Storage();
1580 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001581 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001582 if (ret < 0 && index == 0) {
1583 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1584 return false;
1585 } else {
1586 return true;
1587 }
Dees_Troy8170a922012-09-18 15:40:25 -04001588 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001589}
1590
1591void TWPartitionManager::Mount_All_Storage(void) {
1592 std::vector<TWPartition*>::iterator iter;
1593
1594 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1595 if ((*iter)->Is_Storage)
1596 (*iter)->Mount(false);
1597 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001598}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001599
Dees_Troyd0384ef2012-10-12 12:15:42 -04001600void TWPartitionManager::UnMount_Main_Partitions(void) {
1601 // Unmounts system and data if data is not data/media
1602 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001603 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001604
1605 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1606
1607 UnMount_By_Path("/system", true);
1608#ifndef RECOVERY_SDCARD_ON_DATA
1609 UnMount_By_Path("/data", true);
1610#endif
1611 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1612 Boot_Partition->UnMount(true);
1613}
1614
Dees_Troy9350b8d2012-09-27 12:38:38 -04001615int TWPartitionManager::Partition_SDCard(void) {
1616 char mkdir_path[255], temp[255], line[512];
1617 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1618 int ext, swap, total_size = 0, fat_size;
1619 FILE* fp;
1620
Dees_Troy2673cec2013-04-02 20:22:16 +00001621 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001622#ifdef TW_EXTERNAL_STORAGE_PATH
1623 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1624#else
1625 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1626#endif
1627 if (SDCard == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001628 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001629 return false;
1630 }
1631 if (!SDCard->UnMount(true))
1632 return false;
1633 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1634 if (SDext != NULL) {
1635 if (!SDext->UnMount(true))
1636 return false;
1637 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001638 string result;
1639 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"", result);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001640 Device = SDCard->Actual_Block_Device;
1641 // Just use the root block device
1642 Device.resize(strlen("/dev/block/mmcblkX"));
1643
1644 // Find the size of the block device:
1645 fp = fopen("/proc/partitions", "rt");
1646 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001647 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001648 return false;
1649 }
1650
1651 while (fgets(line, sizeof(line), fp) != NULL)
1652 {
1653 unsigned long major, minor, blocks;
1654 char device[512];
1655 char tmpString[64];
1656
1657 if (strlen(line) < 7 || line[0] == 'm') continue;
1658 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1659
1660 tmpdevice = "/dev/block/";
1661 tmpdevice += device;
1662 if (tmpdevice == Device) {
1663 // Adjust block size to byte size
1664 total_size = (int)(blocks * 1024ULL / 1000000LLU);
1665 break;
1666 }
1667 }
1668 fclose(fp);
1669
1670 DataManager::GetValue("tw_sdext_size", ext);
1671 DataManager::GetValue("tw_swap_size", swap);
1672 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1673 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001674 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 -04001675 memset(temp, 0, sizeof(temp));
1676 sprintf(temp, "%i", fat_size);
1677 fat_str = temp;
1678 memset(temp, 0, sizeof(temp));
1679 sprintf(temp, "%i", fat_size + ext);
1680 ext_str = temp;
1681 memset(temp, 0, sizeof(temp));
1682 sprintf(temp, "%i", fat_size + ext + swap);
1683 swap_str = temp;
1684 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001685 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001686 return false;
1687 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001688 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001689 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001690 LOGINFO("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001691 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001692 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001693 Update_System_Details();
1694 return false;
1695 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001696 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001697 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001698 LOGINFO("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001699 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001700 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001701 return false;
1702 }
1703 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001704 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001705 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001706 LOGINFO("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001707 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001708 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001709 Update_System_Details();
1710 return false;
1711 }
1712 }
1713 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001714 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001715 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001716 LOGINFO("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001717 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001718 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001719 Update_System_Details();
1720 return false;
1721 }
1722 }
1723 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1724#ifdef TW_EXTERNAL_STORAGE_PATH
1725 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1726 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1727 memset(mkdir_path, 0, sizeof(mkdir_path));
1728 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1729#else
1730 Mount_By_Path("/sdcard", 1);
1731 strcpy(mkdir_path, "/sdcard/TWRP");
1732#endif
1733 mkdir(mkdir_path, 0777);
1734 DataManager::Flush();
1735#ifdef TW_EXTERNAL_STORAGE_PATH
1736 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1737 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1738 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1739#else
1740 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1741 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1742 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1743#endif
1744 if (ext > 0) {
1745 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001746 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001747 return false;
1748 }
1749 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001750 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1751 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001752 TWFunc::Exec_Cmd(Command, result);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001753 }
1754
1755 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00001756 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001757 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001758}
Dees_Troya13d74f2013-03-24 08:54:55 -05001759
1760void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1761 std::vector<TWPartition*>::iterator iter;
1762 if (ListType == "mount") {
1763 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1764 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1765 struct PartitionList part;
1766 part.Display_Name = (*iter)->Display_Name;
1767 part.Mount_Point = (*iter)->Mount_Point;
1768 part.selected = (*iter)->Is_Mounted();
1769 Partition_List->push_back(part);
1770 }
1771 }
1772 } else if (ListType == "storage") {
1773 char free_space[255];
1774 string Current_Storage = DataManager::GetCurrentStoragePath();
1775 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1776 if ((*iter)->Is_Storage) {
1777 struct PartitionList part;
1778 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1779 part.Display_Name = (*iter)->Storage_Name + " (";
1780 part.Display_Name += free_space;
1781 part.Display_Name += "MB)";
1782 part.Mount_Point = (*iter)->Storage_Path;
1783 if ((*iter)->Storage_Path == Current_Storage)
1784 part.selected = 1;
1785 else
1786 part.selected = 0;
1787 Partition_List->push_back(part);
1788 }
1789 }
1790 } else if (ListType == "backup") {
1791 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001792 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001793 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001794 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001795 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001796 Backup_Size = (*iter)->Backup_Size;
1797 if ((*iter)->Has_SubPartition) {
1798 std::vector<TWPartition*>::iterator subpart;
1799
1800 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1801 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1802 Backup_Size += (*subpart)->Backup_Size;
1803 }
1804 }
1805 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001806 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1807 part.Display_Name += backup_size;
1808 part.Display_Name += "MB)";
1809 part.Mount_Point = (*iter)->Backup_Path;
1810 part.selected = 0;
1811 Partition_List->push_back(part);
1812 }
1813 }
1814 } else if (ListType == "restore") {
1815 string Restore_List, restore_path;
1816 TWPartition* restore_part = NULL;
1817
1818 DataManager::GetValue("tw_restore_list", Restore_List);
1819 if (!Restore_List.empty()) {
1820 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1821 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1822 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05001823 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
1824 if (restore_part->Backup_Name == "recovery" || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001825 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05001826 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05001827 } else {
1828 struct PartitionList part;
1829 part.Display_Name = restore_part->Backup_Display_Name;
1830 part.Mount_Point = restore_part->Backup_Path;
1831 part.selected = 1;
1832 Partition_List->push_back(part);
1833 }
1834 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001835 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001836 }
1837 start_pos = end_pos + 1;
1838 end_pos = Restore_List.find(";", start_pos);
1839 }
1840 }
1841 } else if (ListType == "wipe") {
1842 struct PartitionList dalvik;
1843 dalvik.Display_Name = "Dalvik Cache";
1844 dalvik.Mount_Point = "DALVIK";
1845 dalvik.selected = 0;
1846 Partition_List->push_back(dalvik);
1847 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1848 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1849 struct PartitionList part;
1850 part.Display_Name = (*iter)->Display_Name;
1851 part.Mount_Point = (*iter)->Mount_Point;
1852 part.selected = 0;
1853 Partition_List->push_back(part);
1854 }
1855 if ((*iter)->Has_Android_Secure) {
1856 struct PartitionList part;
1857 part.Display_Name = (*iter)->Backup_Display_Name;
1858 part.Mount_Point = (*iter)->Backup_Path;
1859 part.selected = 0;
1860 Partition_List->push_back(part);
1861 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001862 if ((*iter)->Has_Data_Media) {
1863 struct PartitionList datamedia;
1864 datamedia.Display_Name = (*iter)->Storage_Name;
1865 datamedia.Mount_Point = "INTERNAL";
1866 datamedia.selected = 0;
1867 Partition_List->push_back(datamedia);
1868 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001869 }
1870 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001871 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001872 }
1873}
1874
1875int TWPartitionManager::Fstab_Processed(void) {
1876 return Partitions.size();
1877}
Dees_Troyd93bda52013-07-03 19:55:19 +00001878
1879void TWPartitionManager::Output_Storage_Fstab(void) {
1880 std::vector<TWPartition*>::iterator iter;
1881 char storage_partition[255];
1882 string Temp;
1883 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
1884
1885 if (fp == NULL) {
1886 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
1887 return;
1888 }
1889
1890 // Iterate through all partitions
1891 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1892 if ((*iter)->Is_Storage) {
1893 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
1894 strcpy(storage_partition, Temp.c_str());
1895 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
1896 }
1897 }
1898 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001899}