blob: 59451e3e7ea4ec244b3473cc64b5077cb2426db7 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
24#include <unistd.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040025#include <vector>
Dees_Troy63c8df72012-09-10 14:02:05 -040026#include <dirent.h>
27#include <time.h>
Dees_Troy8170a922012-09-18 15:40:25 -040028#include <errno.h>
29#include <fcntl.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050030#include <iostream>
31#include <iomanip>
Dees_Troy51a0e822012-09-05 15:24:24 -040032#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000033#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040034#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040035#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040036#include "twrp-functions.hpp"
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040037#include "fixPermissions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050038#include "twrpDigest.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050039#include "twrpDU.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040041#ifdef TW_HAS_MTP
42#include "mtp/mtp_MtpServer.hpp"
43#include "mtp/twrpMtp.hpp"
44#endif
45
Dees Troy6f6441d2014-01-23 02:07:03 +000046extern "C" {
47 #include "cutils/properties.h"
48}
49
Dees_Troy5bf43922012-09-07 16:07:55 -040050#ifdef TW_INCLUDE_CRYPTO
51 #ifdef TW_INCLUDE_JB_CRYPTO
52 #include "crypto/jb/cryptfs.h"
53 #else
54 #include "crypto/ics/cryptfs.h"
55 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -040056#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040057
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050058TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040059 mtpid = 100;
60 mtp_was_enabled = false;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050061}
62
Dees_Troy51a0e822012-09-05 15:24:24 -040063int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040064 FILE *fstabFile;
65 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +000066 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050067 TWPartition* andsec_partition = NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -040068
69 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
70 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000071 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040072 return false;
73 }
74
75 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
76 if (fstab_line[0] != '/')
77 continue;
78
Dees_Troy2a923582012-09-20 12:13:34 -040079 if (fstab_line[strlen(fstab_line) - 1] != '\n')
80 fstab_line[strlen(fstab_line)] = '\n';
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040081 TWPartition* partition = new TWPartition(&mtpid);
Dees_Troy2a923582012-09-20 12:13:34 -040082 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040083 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040084
Dees_Troy5bf43922012-09-07 16:07:55 -040085 if (partition->Process_Fstab_Line(line, Display_Error)) {
Matt Mowered71fa32014-04-16 13:21:47 -050086 if (!settings_partition && partition->Is_Settings_Storage && partition->Is_Present) {
Dees Troy02a64532014-03-19 15:23:32 +000087 settings_partition = partition;
Dees_Troya13d74f2013-03-24 08:54:55 -050088 } else {
89 partition->Is_Settings_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050090 }
Matt Mowered71fa32014-04-16 13:21:47 -050091 if (!andsec_partition && partition->Has_Android_Secure && partition->Is_Present) {
Matt Mowerbf4efa32014-04-14 23:25:26 -050092 andsec_partition = partition;
93 } else {
94 partition->Has_Android_Secure = false;
95 }
Ethan Yonkerc05c5982014-03-13 09:19:56 -050096 Partitions.push_back(partition);
Dees_Troy5bf43922012-09-07 16:07:55 -040097 } else {
98 delete partition;
99 }
100 }
101 fclose(fstabFile);
Dees Troy02a64532014-03-19 15:23:32 +0000102 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500103 std::vector<TWPartition*>::iterator iter;
104 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
105 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000106 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500107 break;
108 }
109 }
Dees Troy02a64532014-03-19 15:23:32 +0000110 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000111 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500112 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400113 if (!Write_Fstab()) {
114 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000115 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400116 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000117 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400118 }
Matt Mowered71fa32014-04-16 13:21:47 -0500119
Matt Mowerbf4efa32014-04-14 23:25:26 -0500120 if (andsec_partition) {
121 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500122 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500123 Setup_Android_Secure_Location(settings_partition);
124 }
Matt Mowered71fa32014-04-16 13:21:47 -0500125 if (settings_partition) {
126 Setup_Settings_Storage_Partition(settings_partition);
127 }
Dees_Troy51127312012-09-08 13:08:49 -0400128 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400129 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400130 return true;
131}
132
133int TWPartitionManager::Write_Fstab(void) {
134 FILE *fp;
135 std::vector<TWPartition*>::iterator iter;
136 string Line;
137
138 fp = fopen("/etc/fstab", "w");
139 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000140 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400141 return false;
142 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400143 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400144 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400145 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400146 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000147 }
148 // Handle subpartition tracking
149 if ((*iter)->Is_SubPartition) {
150 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
151 if (ParentPartition)
152 ParentPartition->Has_SubPartition = true;
153 else
154 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 -0400155 }
156 }
157 fclose(fp);
158 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400159}
160
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500161void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500162 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
163 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
164 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
165}
166
Matt Mowerbf4efa32014-04-14 23:25:26 -0500167void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
168 if (Part->Has_Android_Secure)
169 Part->Setup_AndSec();
170#ifndef RECOVERY_SDCARD_ON_DATA
171 else
172 Part->Setup_AndSec();
173#endif
174}
175
Dees_Troy8170a922012-09-18 15:40:25 -0400176void TWPartitionManager::Output_Partition_Logging(void) {
177 std::vector<TWPartition*>::iterator iter;
178
179 printf("\n\nPartition Logs:\n");
180 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
181 Output_Partition((*iter));
182}
183
184void TWPartitionManager::Output_Partition(TWPartition* Part) {
185 unsigned long long mb = 1048576;
186
Gary Peck004d48b2012-11-21 16:28:18 -0800187 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 -0400188 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800189 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 -0400190 }
Gary Peck004d48b2012-11-21 16:28:18 -0800191 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500192 if (Part->Can_Be_Mounted)
193 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800194 if (Part->Can_Be_Wiped)
195 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700196 if (Part->Use_Rm_Rf)
197 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500198 if (Part->Can_Be_Backed_Up)
199 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800200 if (Part->Wipe_During_Factory_Reset)
201 printf("Wipe_During_Factory_Reset ");
202 if (Part->Wipe_Available_in_GUI)
203 printf("Wipe_Available_in_GUI ");
204 if (Part->Is_SubPartition)
205 printf("Is_SubPartition ");
206 if (Part->Has_SubPartition)
207 printf("Has_SubPartition ");
208 if (Part->Removable)
209 printf("Removable ");
210 if (Part->Is_Present)
211 printf("IsPresent ");
212 if (Part->Can_Be_Encrypted)
213 printf("Can_Be_Encrypted ");
214 if (Part->Is_Encrypted)
215 printf("Is_Encrypted ");
216 if (Part->Is_Decrypted)
217 printf("Is_Decrypted ");
218 if (Part->Has_Data_Media)
219 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000220 if (Part->Can_Encrypt_Backup)
221 printf("Can_Encrypt_Backup ");
222 if (Part->Use_Userdata_Encryption)
223 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800224 if (Part->Has_Android_Secure)
225 printf("Has_Android_Secure ");
226 if (Part->Is_Storage)
227 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500228 if (Part->Is_Settings_Storage)
229 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000230 if (Part->Ignore_Blkid)
231 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000232 if (Part->Retain_Layout_Version)
233 printf("Retain_Layout_Version ");
Gary Peck004d48b2012-11-21 16:28:18 -0800234 printf("\n");
235 if (!Part->SubPartition_Of.empty())
236 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
237 if (!Part->Symlink_Path.empty())
238 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
239 if (!Part->Symlink_Mount_Point.empty())
240 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
241 if (!Part->Primary_Block_Device.empty())
242 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
243 if (!Part->Alternate_Block_Device.empty())
244 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
245 if (!Part->Decrypted_Block_Device.empty())
246 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
247 if (Part->Length != 0)
248 printf(" Length: %i\n", Part->Length);
249 if (!Part->Display_Name.empty())
250 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500251 if (!Part->Storage_Name.empty())
252 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800253 if (!Part->Backup_Path.empty())
254 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
255 if (!Part->Backup_Name.empty())
256 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500257 if (!Part->Backup_Display_Name.empty())
258 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800259 if (!Part->Backup_FileName.empty())
260 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
261 if (!Part->Storage_Path.empty())
262 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
263 if (!Part->Current_File_System.empty())
264 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
265 if (!Part->Fstab_File_System.empty())
266 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
267 if (Part->Format_Block_Size != 0)
268 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400269 if (!Part->MTD_Name.empty())
270 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400271 string back_meth = Part->Backup_Method_By_Name();
272 printf(" Backup_Method: %s\n\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800273 if (Part->Mount_Flags || !Part->Mount_Options.empty())
274 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400275}
276
Dees_Troy51a0e822012-09-05 15:24:24 -0400277int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400278 std::vector<TWPartition*>::iterator iter;
279 int ret = false;
280 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400281 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400282
Dees_Troyd93bda52013-07-03 19:55:19 +0000283 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400284 return true;
285
Dees_Troy5bf43922012-09-07 16:07:55 -0400286 // Iterate through all partitions
287 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400288 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400289 ret = (*iter)->Mount(Display_Error);
290 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400291 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400292 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400293 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400294 }
295 if (found) {
296 return ret;
297 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000298 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400299 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000300 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400301 }
302 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400303}
304
305int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400306 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400307
Dees_Troy51127312012-09-08 13:08:49 -0400308 if (Part) {
309 if (Part->Has_SubPartition) {
310 std::vector<TWPartition*>::iterator subpart;
311
312 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
313 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
314 (*subpart)->Mount(Display_Error);
315 }
316 return Part->Mount(Display_Error);
317 } else
318 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400319 }
320 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000321 LOGERR("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400322 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000323 LOGINFO("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400324 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400325}
326
327int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400328 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400329
Dees_Troy51127312012-09-08 13:08:49 -0400330 if (Part) {
331 if (Part->Has_SubPartition) {
332 std::vector<TWPartition*>::iterator subpart;
333
334 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
335 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
336 (*subpart)->Mount(Display_Error);
337 }
338 return Part->Mount(Display_Error);
339 } else
340 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400341 }
342 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000343 LOGERR("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400344 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000345 LOGINFO("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400346 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400347}
348
349int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400350 std::vector<TWPartition*>::iterator iter;
351 int ret = false;
352 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400353 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400354
355 // Iterate through all partitions
356 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400357 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400358 ret = (*iter)->UnMount(Display_Error);
359 found = true;
360 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
361 (*iter)->UnMount(Display_Error);
362 }
363 }
364 if (found) {
365 return ret;
366 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000367 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400368 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000369 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400370 }
371 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400372}
373
374int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400375 TWPartition* Part = Find_Partition_By_Block(Block);
376
377 if (Part) {
378 if (Part->Has_SubPartition) {
379 std::vector<TWPartition*>::iterator subpart;
380
381 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
382 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
383 (*subpart)->UnMount(Display_Error);
384 }
385 return Part->UnMount(Display_Error);
386 } else
387 return Part->UnMount(Display_Error);
388 }
389 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000390 LOGERR("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400391 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000392 LOGINFO("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400393 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400394}
395
396int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400397 TWPartition* Part = Find_Partition_By_Name(Name);
398
399 if (Part) {
400 if (Part->Has_SubPartition) {
401 std::vector<TWPartition*>::iterator subpart;
402
403 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
404 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
405 (*subpart)->UnMount(Display_Error);
406 }
407 return Part->UnMount(Display_Error);
408 } else
409 return Part->UnMount(Display_Error);
410 }
411 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000412 LOGERR("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400413 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000414 LOGINFO("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400415 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400416}
417
418int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400419 TWPartition* Part = Find_Partition_By_Path(Path);
420
421 if (Part)
422 return Part->Is_Mounted();
423 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000424 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400425 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400426}
427
428int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400429 TWPartition* Part = Find_Partition_By_Block(Block);
430
431 if (Part)
432 return Part->Is_Mounted();
433 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000434 LOGINFO("Is_Mounted: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400435 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400436}
437
438int TWPartitionManager::Is_Mounted_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400439 TWPartition* Part = Find_Partition_By_Name(Name);
440
441 if (Part)
442 return Part->Is_Mounted();
443 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000444 LOGINFO("Is_Mounted: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400445 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400446}
447
Dees_Troy5bf43922012-09-07 16:07:55 -0400448int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400449 string current_storage_path = DataManager::GetCurrentStoragePath();
450
451 if (Mount_By_Path(current_storage_path, Display_Error)) {
452 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
453 if (FreeStorage)
454 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
455 return true;
456 }
457 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400458}
459
Dees_Troy5bf43922012-09-07 16:07:55 -0400460int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
461 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
462}
463
464TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
465 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400466 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400467
468 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400469 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400470 return (*iter);
471 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400472 return NULL;
473}
474
Dees_Troy5bf43922012-09-07 16:07:55 -0400475TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400476 std::vector<TWPartition*>::iterator iter;
477
478 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400479 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 -0400480 return (*iter);
481 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400482 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400483}
484
485TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400486 std::vector<TWPartition*>::iterator iter;
487
488 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
489 if ((*iter)->Display_Name == Name)
490 return (*iter);
491 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400492 return NULL;
493}
Dees_Troy51a0e822012-09-05 15:24:24 -0400494
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400495int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
496 // Check the backup name to ensure that it is the correct size and contains only valid characters
497 // and that a backup with that name doesn't already exist
498 char backup_name[MAX_BACKUP_NAME_LEN];
499 char backup_loc[255], tw_image_dir[255];
500 int copy_size;
501 int index, cur_char;
502 string Backup_Name, Backup_Loc;
503
504 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
505 copy_size = Backup_Name.size();
506 // Check size
507 if (copy_size > MAX_BACKUP_NAME_LEN) {
508 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000509 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400510 return -2;
511 }
512
513 // Check each character
514 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500515 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400516 return 0; // A "0" (zero) means to use the current timestamp for the backup name
517 for (index=0; index<copy_size; index++) {
518 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500519 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) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400520 // These are valid characters
521 // Numbers
522 // Upper case letters
523 // Lower case letters
524 // Space
525 // and -_.{}[]
526 } else {
527 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000528 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400529 return -3;
530 }
531 }
532
533 // Check to make sure that a backup with this name doesn't already exist
534 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
535 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500536 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500537 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400538 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000539 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400540 return -4;
541 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400542 // No problems found, return 0
543 return 0;
544}
545
Dees_Troy43d8b002012-09-17 16:00:01 -0400546bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
547{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500548 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400549 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500550 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500551 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400552
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500553 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400554 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400555
Dees_Troyb46a6842012-09-25 11:06:46 -0400556 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000557 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400558
559 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500560 md5sum.setfn(Backup_Folder + Backup_Filename);
561 if (md5sum.computeMD5() == 0)
562 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000563 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500564 else
565 return -1;
566 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000567 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400568 } else {
569 char filename[512];
570 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500571 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400572 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500573 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000574 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400575 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000576 if (TWFunc::Path_Exists(filename)) {
577 if (md5sum.computeMD5() == 0) {
578 if (md5sum.write_md5digest() != 0)
579 {
580 gui_print(" * MD5 Error.\n");
581 return false;
582 }
583 } else {
584 gui_print(" * Error computing MD5.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500585 return false;
586 }
587 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400588 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400589 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500590 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400591 }
592 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000593 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400594 return false;
595 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000596 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400597 }
598 return true;
599}
600
Dees_Troy093b7642012-09-21 15:59:38 -0400601bool 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 -0400602 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500603 int img_bps;
604 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400605 unsigned long total_time, remain_time, section_time;
606 int use_compression, backup_time;
607 float pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500608 unsigned long long total_size, current_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400609
610 if (Part == NULL)
611 return true;
612
Dees_Troy093b7642012-09-21 15:59:38 -0400613 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
614
615 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
616 if (use_compression)
617 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
618 else
619 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
620
621 // We know the speed for both, how far into the whole backup are we, based on time
622 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
623 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
624
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500625 //pos = (total_time - remain_time) / (float) total_time;
626 total_size = *file_bytes + *img_bytes;
627 current_size = *file_bytes + *img_bytes - *file_bytes_remaining - *img_bytes_remaining;
628 pos = ((float)(current_size) / (float)(total_size));
Dees_Troy2673cec2013-04-02 20:22:16 +0000629 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400630
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500631 LOGINFO("Estimated total time: %lu\nEstimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400632
633 // And get the time
634 if (Part->Backup_Method == 1)
635 section_time = Part->Backup_Size / file_bps;
636 else
637 section_time = Part->Backup_Size / img_bps;
638
639 // Set the position
640 pos = section_time / (float) total_time;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500641 //DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400642
Dees_Troy43d8b002012-09-17 16:00:01 -0400643 time(&start);
644
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500645 if (Part->Backup(Backup_Folder, &total_size, &current_size)) {
646 current_size += Part->Backup_Size;
647 pos = (float)((float)(current_size) / (float)(total_size));
648 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400649 if (Part->Has_SubPartition) {
650 std::vector<TWPartition*>::iterator subpart;
651
652 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500653 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500654 if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size))
Dees_Troy8170a922012-09-18 15:40:25 -0400655 return false;
Dees_Troy2727b992013-08-14 20:09:30 +0000656 sync();
657 sync();
Dees_Troy8170a922012-09-18 15:40:25 -0400658 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
659 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400660 if (Part->Backup_Method == 1) {
661 *file_bytes_remaining -= (*subpart)->Backup_Size;
662 } else {
663 *img_bytes_remaining -= (*subpart)->Backup_Size;
664 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500665 current_size += Part->Backup_Size;
666 pos = (float)(current_size / total_size);
667 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400668 }
669 }
670 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400671 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400672 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000673 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400674 if (Part->Backup_Method == 1) {
675 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400676 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400677 } else {
678 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400679 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400680 }
681 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
682 } else {
683 return false;
684 }
685}
686
687int TWPartitionManager::Run_Backup(void) {
688 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500689 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400690 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 -0400691 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500692 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400693 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400694 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400695 struct tm *t;
696 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500697 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400698 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500699 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400700
701 time(&total_start);
702
703 Update_System_Details();
704
705 if (!Mount_Current_Storage(true))
706 return false;
707
708 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400709 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400710 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400711 else
712 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400713
Dees_Troy43d8b002012-09-17 16:00:01 -0400714 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
715 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000716 if (Backup_Name == "(Current Date)") {
717 Backup_Name = TWFunc::Get_Current_Date();
718 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
719 TWFunc::Auto_Generate_Backup_Name();
720 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400721 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000722 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400723 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000724 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400725
Dees_Troy2673cec2013-04-02 20:22:16 +0000726 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500727 DataManager::GetValue("tw_backup_list", Backup_List);
728 if (!Backup_List.empty()) {
729 end_pos = Backup_List.find(";", start_pos);
730 while (end_pos != string::npos && start_pos < Backup_List.size()) {
731 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
732 backup_part = Find_Partition_By_Path(backup_path);
733 if (backup_part != NULL) {
734 partition_count++;
735 if (backup_part->Backup_Method == 1)
736 file_bytes += backup_part->Backup_Size;
737 else
738 img_bytes += backup_part->Backup_Size;
739 if (backup_part->Has_SubPartition) {
740 std::vector<TWPartition*>::iterator subpart;
741
742 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000743 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 -0500744 partition_count++;
745 if ((*subpart)->Backup_Method == 1)
746 file_bytes += (*subpart)->Backup_Size;
747 else
748 img_bytes += (*subpart)->Backup_Size;
749 }
750 }
Dees_Troy8170a922012-09-18 15:40:25 -0400751 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500752 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000753 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400754 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500755 start_pos = end_pos + 1;
756 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400757 }
758 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400759
760 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000761 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400762 return false;
763 }
764 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000765 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500766 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400767 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
768 if (storage != NULL) {
769 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000770 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400771 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000772 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400773 return false;
774 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000775 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400776 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000777 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400778 return false;
779 }
780 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500781 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400782
Dees_Troy2673cec2013-04-02 20:22:16 +0000783 gui_print("\n[BACKUP STARTED]\n");
784 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000785 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000786 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000787 return false;
788 }
789
Dees_Troy2673cec2013-04-02 20:22:16 +0000790 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400791
Dees_Troya13d74f2013-03-24 08:54:55 -0500792 start_pos = 0;
793 end_pos = Backup_List.find(";", start_pos);
794 while (end_pos != string::npos && start_pos < Backup_List.size()) {
795 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
796 backup_part = Find_Partition_By_Path(backup_path);
797 if (backup_part != NULL) {
798 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
799 return false;
800 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000801 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500802 }
803 start_pos = end_pos + 1;
804 end_pos = Backup_List.find(";", start_pos);
805 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400806
807 // Average BPS
808 if (img_time == 0)
809 img_time = 1;
810 if (file_time == 0)
811 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400812 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500813 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400814
Dees_Troy2673cec2013-04-02 20:22:16 +0000815 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
816 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400817
818 time(&total_stop);
819 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500820 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500821 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400822
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500823 int prev_img_bps, use_compression;
824 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400825 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
826 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500827 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400828
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500829 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400830 if (use_compression)
831 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500832 else
Dees_Troy093b7642012-09-21 15:59:38 -0400833 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
834 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500835 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400836
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500837 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400838 if (use_compression)
839 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
840 else
841 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
842
Dees_Troy2673cec2013-04-02 20:22:16 +0000843 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400844 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400845 UnMount_Main_Partitions();
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500846 gui_print_color("highlight", "[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000847 string backup_log = Full_Backup_Path + "recovery.log";
848 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
849 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400850}
851
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500852bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400853 time_t Start, Stop;
854 time(&Start);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500855 //DataManager::ShowProgress(1.0 / (float)partition_count, 150);
856 if (!Part->Restore(Restore_Name, total_restore_size, already_restored_size))
Dees_Troy4a2a1262012-09-18 09:33:47 -0400857 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400858 if (Part->Has_SubPartition) {
859 std::vector<TWPartition*>::iterator subpart;
860
861 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
862 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500863 if (!(*subpart)->Restore(Restore_Name, total_restore_size, already_restored_size))
Dees_Troy8170a922012-09-18 15:40:25 -0400864 return false;
865 }
866 }
867 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400868 time(&Stop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000869 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 -0400870 return true;
871}
872
Dees_Troy51a0e822012-09-05 15:24:24 -0400873int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400874 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500875 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400876 time_t rStart, rStop;
877 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500878 string Restore_List, restore_path;
879 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500880 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400881
Dees_Troy2673cec2013-04-02 20:22:16 +0000882 gui_print("\n[RESTORE STARTED]\n\n");
883 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400884
Dees_Troy4a2a1262012-09-18 09:33:47 -0400885 if (!Mount_Current_Storage(true))
886 return false;
887
888 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500889 if (check_md5 > 0) {
890 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
891 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000892 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500893 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000894 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500895 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500896 gui_print("Calculating restore details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500897 DataManager::GetValue("tw_restore_selected", Restore_List);
898 if (!Restore_List.empty()) {
899 end_pos = Restore_List.find(";", start_pos);
900 while (end_pos != string::npos && start_pos < Restore_List.size()) {
901 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
902 restore_part = Find_Partition_By_Path(restore_path);
903 if (restore_part != NULL) {
904 partition_count++;
905 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
906 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500907 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500908 if (restore_part->Has_SubPartition) {
909 std::vector<TWPartition*>::iterator subpart;
910
911 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
912 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400913 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500914 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500915 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500916 }
917 }
918 }
919 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500920 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500921 }
922 start_pos = end_pos + 1;
923 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400924 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400925 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400926
927 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000928 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400929 return false;
930 }
931
Dees_Troy2673cec2013-04-02 20:22:16 +0000932 gui_print("Restoring %i partitions...\n", partition_count);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500933 gui_print("Total restore size is %lluMB\n", total_restore_size / 1048576);
Dees_Troy2673cec2013-04-02 20:22:16 +0000934 DataManager::SetProgress(0.0);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500935
Dees_Troya13d74f2013-03-24 08:54:55 -0500936 start_pos = 0;
937 if (!Restore_List.empty()) {
938 end_pos = Restore_List.find(";", start_pos);
939 while (end_pos != string::npos && start_pos < Restore_List.size()) {
940 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
941 restore_part = Find_Partition_By_Path(restore_path);
942 if (restore_part != NULL) {
943 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500944 if (!Restore_Partition(restore_part, Restore_Name, partition_count, &total_restore_size, &already_restored_size))
Dees_Troya13d74f2013-03-24 08:54:55 -0500945 return false;
946 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000947 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500948 }
949 start_pos = end_pos + 1;
950 end_pos = Restore_List.find(";", start_pos);
951 }
952 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400953 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400954 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400955 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400956 time(&rStop);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500957 gui_print_color("highlight", "[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500958 DataManager::SetValue("tw_file_progress", "");
Dees_Troy63c8df72012-09-10 14:02:05 -0400959 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400960}
961
962void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400963 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500964 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000965 bool get_date = true, check_encryption = true;
966
967 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400968
969 DIR* d;
970 d = opendir(Restore_Name.c_str());
971 if (d == NULL)
972 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000973 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400974 return;
975 }
976
977 struct dirent* de;
978 while ((de = readdir(d)) != NULL)
979 {
980 // Strip off three components
981 char str[256];
982 char* label;
983 char* fstype = NULL;
984 char* extn = NULL;
985 char* ptr;
986
987 strcpy(str, de->d_name);
988 if (strlen(str) <= 2)
989 continue;
990
991 if (get_date) {
992 char file_path[255];
993 struct stat st;
994
995 strcpy(file_path, Restore_Name.c_str());
996 strcat(file_path, "/");
997 strcat(file_path, str);
998 stat(file_path, &st);
999 string backup_date = ctime((const time_t*)(&st.st_mtime));
1000 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
1001 get_date = false;
1002 }
1003
1004 label = str;
1005 ptr = label;
1006 while (*ptr && *ptr != '.') ptr++;
1007 if (*ptr == '.')
1008 {
1009 *ptr = 0x00;
1010 ptr++;
1011 fstype = ptr;
1012 }
1013 while (*ptr && *ptr != '.') ptr++;
1014 if (*ptr == '.')
1015 {
1016 *ptr = 0x00;
1017 ptr++;
1018 extn = ptr;
1019 }
1020
Dees_Troy83bd4832013-05-04 12:39:56 +00001021 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -05001022 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +00001023 if (extnlength != 3 && extnlength != 6) continue;
1024 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
1025 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
1026
1027 if (check_encryption) {
1028 string filename = Restore_Name + "/";
1029 filename += de->d_name;
1030 if (TWFunc::Get_File_Type(filename) == 2) {
1031 LOGINFO("'%s' is encrypted\n", filename.c_str());
1032 DataManager::SetValue("tw_restore_encrypted", 1);
1033 }
1034 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001035 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -04001036
1037 TWPartition* Part = Find_Partition_By_Path(label);
1038 if (Part == NULL)
1039 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001040 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -04001041 continue;
1042 }
1043
1044 Part->Backup_FileName = de->d_name;
1045 if (strlen(extn) > 3) {
1046 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1047 }
1048
Dees_Troya13d74f2013-03-24 08:54:55 -05001049 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001050 }
1051 closedir(d);
1052
Dees_Troya13d74f2013-03-24 08:54:55 -05001053 // Set the final value
1054 DataManager::SetValue("tw_restore_list", Restore_List);
1055 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001056 return;
1057}
1058
1059int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001060 std::vector<TWPartition*>::iterator iter;
1061 int ret = false;
1062 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001063 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001064
1065 // Iterate through all partitions
1066 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001067 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001068 if (Path == "/and-sec")
1069 ret = (*iter)->Wipe_AndSec();
1070 else
1071 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001072 found = true;
1073 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1074 (*iter)->Wipe();
1075 }
1076 }
1077 if (found) {
1078 return ret;
1079 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001080 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001081 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001082}
1083
1084int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001085 TWPartition* Part = Find_Partition_By_Block(Block);
1086
1087 if (Part) {
1088 if (Part->Has_SubPartition) {
1089 std::vector<TWPartition*>::iterator subpart;
1090
1091 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1092 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1093 (*subpart)->Wipe();
1094 }
1095 return Part->Wipe();
1096 } else
1097 return Part->Wipe();
1098 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001099 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001100 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001101}
1102
1103int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001104 TWPartition* Part = Find_Partition_By_Name(Name);
1105
1106 if (Part) {
1107 if (Part->Has_SubPartition) {
1108 std::vector<TWPartition*>::iterator subpart;
1109
1110 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1111 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1112 (*subpart)->Wipe();
1113 }
1114 return Part->Wipe();
1115 } else
1116 return Part->Wipe();
1117 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001118 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001119 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001120}
1121
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001122int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1123 std::vector<TWPartition*>::iterator iter;
1124 int ret = false;
1125 bool found = false;
1126 string Local_Path = TWFunc::Get_Root_Path(Path);
1127
1128 // Iterate through all partitions
1129 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1130 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1131 if (Path == "/and-sec")
1132 ret = (*iter)->Wipe_AndSec();
1133 else
1134 ret = (*iter)->Wipe(New_File_System);
1135 found = true;
1136 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1137 (*iter)->Wipe(New_File_System);
1138 }
1139 }
1140 if (found) {
1141 return ret;
1142 } else
1143 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1144 return false;
1145}
1146
1147int TWPartitionManager::Wipe_By_Block(string Block, string New_File_System) {
1148 TWPartition* Part = Find_Partition_By_Block(Block);
1149
1150 if (Part) {
1151 if (Part->Has_SubPartition) {
1152 std::vector<TWPartition*>::iterator subpart;
1153
1154 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1155 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1156 (*subpart)->Wipe(New_File_System);
1157 }
1158 return Part->Wipe(New_File_System);
1159 } else
1160 return Part->Wipe(New_File_System);
1161 }
1162 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
1163 return false;
1164}
1165
1166int TWPartitionManager::Wipe_By_Name(string Name, string New_File_System) {
1167 TWPartition* Part = Find_Partition_By_Name(Name);
1168
1169 if (Part) {
1170 if (Part->Has_SubPartition) {
1171 std::vector<TWPartition*>::iterator subpart;
1172
1173 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1174 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1175 (*subpart)->Wipe();
1176 }
1177 return Part->Wipe(New_File_System);
1178 } else
1179 return Part->Wipe(New_File_System);
1180 }
1181 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
1182 return false;
1183}
1184
Dees_Troy51a0e822012-09-05 15:24:24 -04001185int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001186 std::vector<TWPartition*>::iterator iter;
1187 int ret = true;
1188
1189 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001190 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001191 if (!(*iter)->Wipe())
1192 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001193 } else if ((*iter)->Has_Android_Secure) {
1194 if (!(*iter)->Wipe_AndSec())
1195 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001196 }
1197 }
1198 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001199}
1200
Dees_Troy38bd7602012-09-14 13:33:53 -04001201int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1202 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001203 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001204
1205 if (!Mount_By_Path("/data", true))
1206 return false;
1207
1208 if (!Mount_By_Path("/cache", true))
1209 return false;
1210
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001211 dir.push_back("/data/dalvik-cache");
1212 dir.push_back("/cache/dalvik-cache");
1213 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001214 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001215 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001216 if (stat(dir.at(i).c_str(), &st) == 0) {
1217 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001218 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001219 }
1220 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001221 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001222 if (sdext && sdext->Is_Present && sdext->Mount(false))
1223 {
1224 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1225 {
1226 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001227 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001228 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001229 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001230 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001231 return true;
1232}
1233
1234int TWPartitionManager::Wipe_Rotate_Data(void) {
1235 if (!Mount_By_Path("/data", true))
1236 return false;
1237
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001238 unlink("/data/misc/akmd*");
1239 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001240 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001241 return true;
1242}
1243
1244int TWPartitionManager::Wipe_Battery_Stats(void) {
1245 struct stat st;
1246
1247 if (!Mount_By_Path("/data", true))
1248 return false;
1249
1250 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001251 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001252 } else {
1253 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001254 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001255 }
1256 return true;
1257}
1258
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001259int TWPartitionManager::Wipe_Android_Secure(void) {
1260 std::vector<TWPartition*>::iterator iter;
1261 int ret = false;
1262 bool found = false;
1263
1264 // Iterate through all partitions
1265 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1266 if ((*iter)->Has_Android_Secure) {
1267 ret = (*iter)->Wipe_AndSec();
1268 found = true;
1269 }
1270 }
1271 if (found) {
1272 return ret;
1273 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001274 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001275 }
1276 return false;
1277}
1278
Dees_Troy38bd7602012-09-14 13:33:53 -04001279int TWPartitionManager::Format_Data(void) {
1280 TWPartition* dat = Find_Partition_By_Path("/data");
1281
1282 if (dat != NULL) {
1283 if (!dat->UnMount(true))
1284 return false;
1285
1286 return dat->Wipe_Encryption();
1287 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001288 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001289 return false;
1290 }
1291 return false;
1292}
1293
1294int TWPartitionManager::Wipe_Media_From_Data(void) {
1295 TWPartition* dat = Find_Partition_By_Path("/data");
1296
1297 if (dat != NULL) {
1298 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001299 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001300 return false;
1301 }
1302 if (!dat->Mount(true))
1303 return false;
1304
Dees_Troy2673cec2013-04-02 20:22:16 +00001305 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001306 mtp_was_enabled = TWFunc::Toggle_MTP(false);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001307 TWFunc::removeDir("/data/media", false);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001308 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
1309 if (mtp_was_enabled) {
1310 if (!Enable_MTP())
1311 Disable_MTP();
1312 }
1313 return false;
1314 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001315 if (dat->Has_Data_Media) {
1316 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001317 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1318 dat->UnMount(false);
1319 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001320 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001321 if (mtp_was_enabled) {
1322 if (!Enable_MTP())
1323 Disable_MTP();
1324 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001325 return true;
1326 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001327 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001328 return false;
1329 }
1330 return false;
1331}
1332
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001333int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1334 std::vector<TWPartition*>::iterator iter;
1335 int ret = false;
1336 bool found = false;
1337 string Local_Path = TWFunc::Get_Root_Path(Path);
1338
1339 if (Local_Path == "/tmp" || Local_Path == "/")
1340 return true;
1341
1342 // Iterate through all partitions
1343 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1344 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1345 ret = (*iter)->Repair();
1346 found = true;
1347 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1348 (*iter)->Repair();
1349 }
1350 }
1351 if (found) {
1352 return ret;
1353 } else if (Display_Error) {
1354 LOGERR("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1355 } else {
1356 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1357 }
1358 return false;
1359}
1360
1361int TWPartitionManager::Repair_By_Block(string Block, bool Display_Error) {
1362 TWPartition* Part = Find_Partition_By_Block(Block);
1363
1364 if (Part) {
1365 if (Part->Has_SubPartition) {
1366 std::vector<TWPartition*>::iterator subpart;
1367
1368 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1369 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1370 (*subpart)->Repair();
1371 }
1372 return Part->Repair();
1373 } else
1374 return Part->Repair();
1375 }
1376 if (Display_Error)
1377 LOGERR("Repair: Unable to find partition for block '%s'\n", Block.c_str());
1378 else
1379 LOGINFO("Repair: Unable to find partition for block '%s'\n", Block.c_str());
1380 return false;
1381}
1382
1383int TWPartitionManager::Repair_By_Name(string Name, bool Display_Error) {
1384 TWPartition* Part = Find_Partition_By_Name(Name);
1385
1386 if (Part) {
1387 if (Part->Has_SubPartition) {
1388 std::vector<TWPartition*>::iterator subpart;
1389
1390 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1391 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1392 (*subpart)->Repair();
1393 }
1394 return Part->Repair();
1395 } else
1396 return Part->Repair();
1397 }
1398 if (Display_Error)
1399 LOGERR("Repair: Unable to find partition for name '%s'\n", Name.c_str());
1400 else
1401 LOGINFO("Repair: Unable to find partition for name '%s'\n", Name.c_str());
1402 return false;
1403}
1404
Dees_Troy51a0e822012-09-05 15:24:24 -04001405void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001406 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001407 return;
1408}
1409
1410void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001411 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001412 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001413
Dees_Troy2673cec2013-04-02 20:22:16 +00001414 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001415 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001416 if ((*iter)->Can_Be_Mounted) {
1417 (*iter)->Update_Size(true);
1418 if ((*iter)->Mount_Point == "/system") {
1419 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1420 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1421 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1422 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1423 } else if ((*iter)->Mount_Point == "/cache") {
1424 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1425 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1426 } else if ((*iter)->Mount_Point == "/sd-ext") {
1427 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1428 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1429 if ((*iter)->Backup_Size == 0) {
1430 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1431 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1432 } else
1433 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001434 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001435 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001436 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001437 if ((*iter)->Backup_Size == 0) {
1438 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1439 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1440 } else
1441 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001442 } else if ((*iter)->Mount_Point == "/boot") {
1443 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1444 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1445 if ((*iter)->Backup_Size == 0) {
1446 DataManager::SetValue("tw_has_boot_partition", 0);
1447 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1448 } else
1449 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001450 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001451#ifdef SP1_NAME
1452 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1453 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1454 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1455 }
1456#endif
1457#ifdef SP2_NAME
1458 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1459 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1460 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1461 }
1462#endif
1463#ifdef SP3_NAME
1464 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1465 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1466 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1467 }
1468#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001469 } else {
1470 // Handle unmountable partitions in case we reset defaults
1471 if ((*iter)->Mount_Point == "/boot") {
1472 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1473 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1474 if ((*iter)->Backup_Size == 0) {
1475 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1476 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1477 } else
1478 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1479 } else if ((*iter)->Mount_Point == "/recovery") {
1480 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1481 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1482 if ((*iter)->Backup_Size == 0) {
1483 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1484 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1485 } else
1486 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001487 } else if ((*iter)->Mount_Point == "/data") {
1488 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001489 }
1490#ifdef SP1_NAME
1491 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1492 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1493 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1494 }
1495#endif
1496#ifdef SP2_NAME
1497 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1498 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1499 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1500 }
1501#endif
1502#ifdef SP3_NAME
1503 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1504 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1505 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1506 }
1507#endif
Dees_Troy51127312012-09-08 13:08:49 -04001508 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001509 }
Dees_Troy51127312012-09-08 13:08:49 -04001510 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1511 string current_storage_path = DataManager::GetCurrentStoragePath();
1512 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001513 if (FreeStorage != NULL) {
1514 // Attempt to mount storage
1515 if (!FreeStorage->Mount(false)) {
1516 // We couldn't mount storage... check to see if we have dual storage
1517 int has_dual_storage;
1518 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1519 if (has_dual_storage == 1) {
1520 // We have dual storage, see if we're using the internal storage that should always be present
1521 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001522 if (!FreeStorage->Is_Encrypted) {
1523 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001524 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001525 }
Dees_Troy8170a922012-09-18 15:40:25 -04001526 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1527 } else {
1528 // We were using external, flip to internal
1529 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1530 current_storage_path = DataManager::GetCurrentStoragePath();
1531 FreeStorage = Find_Partition_By_Path(current_storage_path);
1532 if (FreeStorage != NULL) {
1533 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1534 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001535 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001536 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1537 }
1538 }
1539 } else {
1540 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001541 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001542 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1543 }
1544 } else {
1545 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1546 }
1547 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001548 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001549 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001550 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001551 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001552 return;
1553}
1554
1555int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001556#ifdef TW_INCLUDE_CRYPTO
1557 int ret_val, password_len;
1558 char crypto_blkdev[255], cPassword[255];
1559 size_t result;
1560
1561 property_set("ro.crypto.state", "encrypted");
1562#ifdef TW_INCLUDE_JB_CRYPTO
1563 // No extra flags needed
1564#else
1565 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1566 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1567 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1568 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1569 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1570 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001571
1572#ifdef CRYPTO_SD_FS_TYPE
Ethan Yonker71413f42014-02-26 13:36:08 -06001573 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1574 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1575 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001576#endif
a39552696ff55ce2013-01-08 16:14:56 +00001577
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001578 property_set("rw.km_fips_status", "ready");
a39552696ff55ce2013-01-08 16:14:56 +00001579
1580#endif
1581
1582 // some samsung devices store "footer" on efs partition
1583 TWPartition *efs = Find_Partition_By_Path("/efs");
1584 if(efs && !efs->Is_Mounted())
1585 efs->Mount(false);
1586 else
1587 efs = 0;
1588#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001589#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001590 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001591 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001592 property_set("ro.crypto.external_encrypted", "1");
1593 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1594 } else {
1595 property_set("ro.crypto.external_encrypted", "0");
1596 }
1597#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001598#endif
a39552696ff55ce2013-01-08 16:14:56 +00001599
Dees_Troy5bf43922012-09-07 16:07:55 -04001600 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001601 int pwret = cryptfs_check_passwd(cPassword);
1602
1603 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001604 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001605 return -1;
1606 }
a39552696ff55ce2013-01-08 16:14:56 +00001607
1608 if(efs)
1609 efs->UnMount(false);
1610
Dees_Troy5bf43922012-09-07 16:07:55 -04001611 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1612 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001613 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001614 } else {
1615 TWPartition* dat = Find_Partition_By_Path("/data");
1616 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001617 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001618 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1619 dat->Is_Decrypted = true;
1620 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001621 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001622 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 +00001623 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001624
1625#ifdef CRYPTO_SD_FS_TYPE
1626 char crypto_blkdev_sd[255];
1627 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1628 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001629 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001630 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001631 emmc->Is_Decrypted = true;
1632 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1633 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001634 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001635 }
a39552696ff55ce2013-01-08 16:14:56 +00001636#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001637#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001638#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001639 char is_external_decrypted[255];
1640 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1641 if (strcmp(is_external_decrypted, "1") == 0) {
1642 sdcard->Is_Decrypted = true;
1643 sdcard->EcryptFS_Password = Password;
1644 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001645 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1646 MetaEcfsFile += "/.MetaEcfsFile";
1647 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1648 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1649 sdcard->UnMount(false);
1650 sdcard->Mount(false);
1651 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001652 } else {
Dees_Troy066eb302013-08-23 17:20:32 +00001653 LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001654 sdcard->Is_Decrypted = false;
1655 sdcard->Decrypted_Block_Device = "";
1656 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001657#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001658#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001659
Dees_Troy5bf43922012-09-07 16:07:55 -04001660 // Sleep for a bit so that the device will be ready
1661 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001662#ifdef RECOVERY_SDCARD_ON_DATA
1663 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1664 dat->Storage_Path = "/data/media/0";
1665 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001666 DataManager::SetValue("tw_storage_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001667 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001668 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001669 }
1670#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001671 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001672 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001673 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001674 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001675 }
1676 return 0;
1677#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001678 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001679 return -1;
1680#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001681 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001682}
1683
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001684int TWPartitionManager::Fix_Permissions(void) {
1685 int result = 0;
1686 if (!Mount_By_Path("/data", true))
1687 return false;
1688
1689 if (!Mount_By_Path("/system", true))
1690 return false;
1691
1692 Mount_By_Path("/sd-ext", false);
1693
1694 fixPermissions perms;
1695 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001696 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001697 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001698 return result;
1699}
1700
Ethan Yonker47360be2014-04-01 10:34:34 -05001701TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1702 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1703
1704 if (!Path.empty()) {
1705 string Search_Path = TWFunc::Get_Root_Path(Path);
1706 for (; iter != Partitions.end(); iter++) {
1707 if ((*iter)->Mount_Point == Search_Path) {
1708 iter++;
1709 break;
1710 }
1711 }
1712 }
1713
1714 for (; iter != Partitions.end(); iter++) {
1715 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) {
1716 return (*iter);
1717 }
1718 }
1719
1720 return NULL;
1721}
1722
Dees_Troyd21618c2012-10-14 18:48:49 -04001723int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001724 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1725
1726 if (Part == NULL) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001727 LOGERR("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
Dees_Troyd21618c2012-10-14 18:48:49 -04001728 return false;
1729 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001730 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1731 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001732 return false;
1733
Dees_Troy2673cec2013-04-02 20:22:16 +00001734 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1735 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001736 return false;
1737 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001738 return true;
1739}
1740
Dees_Troy8170a922012-09-18 15:40:25 -04001741int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001742 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001743 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001744 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001745
Dees_Troy8170a922012-09-18 15:40:25 -04001746 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001747 string Lun_File_str = CUSTOM_LUN_FILE;
1748 size_t found = Lun_File_str.find("%");
1749 if (found != string::npos) {
1750 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1751 if (TWFunc::Path_Exists(lun_file))
1752 has_multiple_lun = true;
1753 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001754 mtp_was_enabled = TWFunc::Toggle_MTP(false);
Ethan Yonker47360be2014-04-01 10:34:34 -05001755 if (!has_multiple_lun) {
1756 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1757 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1758 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
1759 TWPartition* Mount = Find_Next_Storage("", "/data");
1760 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001761 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1762 goto error_handle;
1763 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001764 } else {
1765 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001766 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001767 }
1768 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001769 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001770 }
Dees_Troy8170a922012-09-18 15:40:25 -04001771 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001772 LOGINFO("Device has multiple lun files\n");
1773 TWPartition* Mount1;
1774 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001775 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker47360be2014-04-01 10:34:34 -05001776 Mount1 = Find_Next_Storage("", "/data");
1777 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001778 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1779 goto error_handle;
1780 }
Matt Moweree717062014-04-26 01:46:46 -05001781 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker47360be2014-04-01 10:34:34 -05001782 Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data");
1783 if (Mount2) {
Matt Moweree717062014-04-26 01:46:46 -05001784 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001785 }
1786 } else {
1787 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001788 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001789 }
Dees_Troy8170a922012-09-18 15:40:25 -04001790 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001791 property_set("sys.storage.ums_enabled", "1");
Dees_Troy8170a922012-09-18 15:40:25 -04001792 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001793error_handle:
1794 if (mtp_was_enabled)
1795 if (!Enable_MTP())
1796 Disable_MTP();
1797 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001798}
1799
1800int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001801 int index, ret;
1802 char lun_file[255], ch[2] = {0, 0};
1803 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001804
1805 for (index=0; index<2; index++) {
1806 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001807 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001808 if (ret < 0) {
1809 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001810 }
Dees_Troy8170a922012-09-18 15:40:25 -04001811 }
Dees_Troye58d5262012-09-21 12:27:57 -04001812 Mount_All_Storage();
1813 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001814 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001815 property_set("sys.storage.ums_enabled", "0");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001816 if (mtp_was_enabled)
1817 if (!Enable_MTP())
1818 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001819 if (ret < 0 && index == 0) {
1820 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1821 return false;
1822 } else {
1823 return true;
1824 }
Dees_Troy8170a922012-09-18 15:40:25 -04001825 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001826}
1827
1828void TWPartitionManager::Mount_All_Storage(void) {
1829 std::vector<TWPartition*>::iterator iter;
1830
1831 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1832 if ((*iter)->Is_Storage)
1833 (*iter)->Mount(false);
1834 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001835}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001836
Dees_Troyd0384ef2012-10-12 12:15:42 -04001837void TWPartitionManager::UnMount_Main_Partitions(void) {
1838 // Unmounts system and data if data is not data/media
1839 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001840 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001841
1842 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1843
1844 UnMount_By_Path("/system", true);
1845#ifndef RECOVERY_SDCARD_ON_DATA
1846 UnMount_By_Path("/data", true);
1847#endif
1848 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1849 Boot_Partition->UnMount(true);
1850}
1851
Dees_Troy9350b8d2012-09-27 12:38:38 -04001852int TWPartitionManager::Partition_SDCard(void) {
1853 char mkdir_path[255], temp[255], line[512];
1854 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1855 int ext, swap, total_size = 0, fat_size;
1856 FILE* fp;
1857
Dees_Troy2673cec2013-04-02 20:22:16 +00001858 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001859#ifdef TW_EXTERNAL_STORAGE_PATH
1860 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1861#else
1862 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1863#endif
1864 if (SDCard == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001865 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001866 return false;
1867 }
1868 if (!SDCard->UnMount(true))
1869 return false;
1870 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1871 if (SDext != NULL) {
1872 if (!SDext->UnMount(true))
1873 return false;
1874 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001875
1876 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001877 Device = SDCard->Actual_Block_Device;
1878 // Just use the root block device
1879 Device.resize(strlen("/dev/block/mmcblkX"));
1880
1881 // Find the size of the block device:
1882 fp = fopen("/proc/partitions", "rt");
1883 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001884 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001885 return false;
1886 }
1887
1888 while (fgets(line, sizeof(line), fp) != NULL)
1889 {
1890 unsigned long major, minor, blocks;
1891 char device[512];
1892 char tmpString[64];
1893
1894 if (strlen(line) < 7 || line[0] == 'm') continue;
1895 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1896
1897 tmpdevice = "/dev/block/";
1898 tmpdevice += device;
1899 if (tmpdevice == Device) {
1900 // Adjust block size to byte size
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001901 total_size = (int)(blocks * 1024ULL / 1000000LLU);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001902 break;
1903 }
1904 }
1905 fclose(fp);
1906
1907 DataManager::GetValue("tw_sdext_size", ext);
1908 DataManager::GetValue("tw_swap_size", swap);
1909 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1910 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001911 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 -04001912 memset(temp, 0, sizeof(temp));
1913 sprintf(temp, "%i", fat_size);
1914 fat_str = temp;
1915 memset(temp, 0, sizeof(temp));
1916 sprintf(temp, "%i", fat_size + ext);
1917 ext_str = temp;
1918 memset(temp, 0, sizeof(temp));
1919 sprintf(temp, "%i", fat_size + ext + swap);
1920 swap_str = temp;
1921 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001922 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001923 return false;
1924 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001925 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001926 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001927 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001928 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001929 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001930 Update_System_Details();
1931 return false;
1932 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001933 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001934 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001935 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001936 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001937 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001938 return false;
1939 }
1940 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001941 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001942 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001943 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001944 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001945 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001946 Update_System_Details();
1947 return false;
1948 }
1949 }
1950 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001951 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001952 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001953 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001954 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001955 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001956 Update_System_Details();
1957 return false;
1958 }
1959 }
1960 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1961#ifdef TW_EXTERNAL_STORAGE_PATH
1962 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1963 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1964 memset(mkdir_path, 0, sizeof(mkdir_path));
1965 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1966#else
1967 Mount_By_Path("/sdcard", 1);
1968 strcpy(mkdir_path, "/sdcard/TWRP");
1969#endif
1970 mkdir(mkdir_path, 0777);
1971 DataManager::Flush();
1972#ifdef TW_EXTERNAL_STORAGE_PATH
1973 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1974 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1975 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1976#else
1977 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1978 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1979 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1980#endif
1981 if (ext > 0) {
1982 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001983 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001984 return false;
1985 }
1986 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001987 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1988 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001989 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001990 }
1991
1992 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00001993 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001994 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001995}
Dees_Troya13d74f2013-03-24 08:54:55 -05001996
1997void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1998 std::vector<TWPartition*>::iterator iter;
1999 if (ListType == "mount") {
2000 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2001 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
2002 struct PartitionList part;
2003 part.Display_Name = (*iter)->Display_Name;
2004 part.Mount_Point = (*iter)->Mount_Point;
2005 part.selected = (*iter)->Is_Mounted();
2006 Partition_List->push_back(part);
2007 }
2008 }
2009 } else if (ListType == "storage") {
2010 char free_space[255];
2011 string Current_Storage = DataManager::GetCurrentStoragePath();
2012 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2013 if ((*iter)->Is_Storage) {
2014 struct PartitionList part;
2015 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
2016 part.Display_Name = (*iter)->Storage_Name + " (";
2017 part.Display_Name += free_space;
2018 part.Display_Name += "MB)";
2019 part.Mount_Point = (*iter)->Storage_Path;
2020 if ((*iter)->Storage_Path == Current_Storage)
2021 part.selected = 1;
2022 else
2023 part.selected = 0;
2024 Partition_List->push_back(part);
2025 }
2026 }
2027 } else if (ListType == "backup") {
2028 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002029 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05002030 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002031 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05002032 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002033 Backup_Size = (*iter)->Backup_Size;
2034 if ((*iter)->Has_SubPartition) {
2035 std::vector<TWPartition*>::iterator subpart;
2036
2037 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
2038 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
2039 Backup_Size += (*subpart)->Backup_Size;
2040 }
2041 }
2042 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05002043 part.Display_Name = (*iter)->Backup_Display_Name + " (";
2044 part.Display_Name += backup_size;
2045 part.Display_Name += "MB)";
2046 part.Mount_Point = (*iter)->Backup_Path;
2047 part.selected = 0;
2048 Partition_List->push_back(part);
2049 }
2050 }
2051 } else if (ListType == "restore") {
2052 string Restore_List, restore_path;
2053 TWPartition* restore_part = NULL;
2054
2055 DataManager::GetValue("tw_restore_list", Restore_List);
2056 if (!Restore_List.empty()) {
2057 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
2058 while (end_pos != string::npos && start_pos < Restore_List.size()) {
2059 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05002060 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00002061 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05002062 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05002063 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05002064 } else {
2065 struct PartitionList part;
2066 part.Display_Name = restore_part->Backup_Display_Name;
2067 part.Mount_Point = restore_part->Backup_Path;
2068 part.selected = 1;
2069 Partition_List->push_back(part);
2070 }
2071 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00002072 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05002073 }
2074 start_pos = end_pos + 1;
2075 end_pos = Restore_List.find(";", start_pos);
2076 }
2077 }
2078 } else if (ListType == "wipe") {
2079 struct PartitionList dalvik;
2080 dalvik.Display_Name = "Dalvik Cache";
2081 dalvik.Mount_Point = "DALVIK";
2082 dalvik.selected = 0;
2083 Partition_List->push_back(dalvik);
2084 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2085 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
2086 struct PartitionList part;
2087 part.Display_Name = (*iter)->Display_Name;
2088 part.Mount_Point = (*iter)->Mount_Point;
2089 part.selected = 0;
2090 Partition_List->push_back(part);
2091 }
2092 if ((*iter)->Has_Android_Secure) {
2093 struct PartitionList part;
2094 part.Display_Name = (*iter)->Backup_Display_Name;
2095 part.Mount_Point = (*iter)->Backup_Path;
2096 part.selected = 0;
2097 Partition_List->push_back(part);
2098 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00002099 if ((*iter)->Has_Data_Media) {
2100 struct PartitionList datamedia;
2101 datamedia.Display_Name = (*iter)->Storage_Name;
2102 datamedia.Mount_Point = "INTERNAL";
2103 datamedia.selected = 0;
2104 Partition_List->push_back(datamedia);
2105 }
Dees_Troya13d74f2013-03-24 08:54:55 -05002106 }
2107 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00002108 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05002109 }
2110}
2111
2112int TWPartitionManager::Fstab_Processed(void) {
2113 return Partitions.size();
2114}
Dees_Troyd93bda52013-07-03 19:55:19 +00002115
2116void TWPartitionManager::Output_Storage_Fstab(void) {
2117 std::vector<TWPartition*>::iterator iter;
2118 char storage_partition[255];
2119 string Temp;
2120 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
2121
2122 if (fp == NULL) {
2123 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
2124 return;
2125 }
2126
2127 // Iterate through all partitions
2128 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2129 if ((*iter)->Is_Storage) {
2130 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
2131 strcpy(storage_partition, Temp.c_str());
2132 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
2133 }
2134 }
2135 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002136}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02002137
2138TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
2139{
2140 TWPartition *res = NULL;
2141 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
2142 if(!(*iter)->Is_Storage)
2143 continue;
2144
2145 if((*iter)->Is_Settings_Storage)
2146 return *iter;
2147
2148 if(!res)
2149 res = *iter;
2150 }
2151 return res;
2152}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002153
2154bool TWPartitionManager::Enable_MTP(void) {
2155#ifdef TW_HAS_MTP
2156 if (mtpthread) {
2157 LOGERR("MTP already enabled\n");
2158 return true;
2159 }
2160 //Launch MTP Responder
2161 LOGINFO("Starting MTP\n");
2162 char vendor[PROPERTY_VALUE_MAX];
2163 char product[PROPERTY_VALUE_MAX];
2164 int count = 0;
2165 property_set("sys.usb.config", "none");
2166 property_get("usb.vendor", vendor, "18D1");
2167 property_get("usb.product.mtpadb", product, "4EE2");
2168 string vendorstr = vendor;
2169 string productstr = product;
2170 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2171 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
2172 property_set("sys.usb.config", "mtp,adb");
2173 std::vector<TWPartition*>::iterator iter;
2174 twrpMtp *mtp = new twrpMtp();
2175 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2176 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false)) {
2177 printf("twrp mtpid: %d\n", (*iter)->mtpid);
2178 mtp->addStorage((*iter)->Storage_Name, (*iter)->Storage_Path, (*iter)->mtpid);
2179 count++;
2180 }
2181 }
2182 if (count) {
2183 mtpthread = mtp->runserver();
2184 DataManager::SetValue("tw_mtp_enabled", 1);
2185 return true;
2186 }
2187 LOGERR("No valid storage partitions found for MTP.\n");
2188#else
2189 LOGERR("MTP support not included\n");
2190#endif
2191 DataManager::SetValue("tw_mtp_enabled", 0);
2192 return false;
2193}
2194
2195bool TWPartitionManager::Disable_MTP(void) {
2196#ifdef TW_HAS_MTP
2197 char vendor[PROPERTY_VALUE_MAX];
2198 char product[PROPERTY_VALUE_MAX];
2199 property_set("sys.usb.config", "none");
2200 property_get("usb.vendor", vendor, "18D1");
2201 property_get("usb.product.adb", product, "D002");
2202 string vendorstr = vendor;
2203 string productstr = product;
2204 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2205 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
2206 if (mtpthread) {
2207 pthread_kill(mtpthread, 0);
2208 mtpthread = NULL;
2209 }
2210 property_set("sys.usb.config", "adb");
2211 DataManager::SetValue("tw_mtp_enabled", 0);
2212 return true;
2213#else
2214 LOGERR("MTP support not included\n");
2215 DataManager::SetValue("tw_mtp_enabled", 0);
2216 return false;
2217#endif
2218}