blob: d83518b058180b46fc6703536f6c46e67ffb6337 [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>
Ethan Yonker8613dc02014-09-11 09:28:20 -050032#include <sys/wait.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040033#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000034#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040036#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040037#include "twrp-functions.hpp"
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040038#include "fixPermissions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050039#include "twrpDigest.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050040#include "twrpDU.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040041
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040042#ifdef TW_HAS_MTP
43#include "mtp/mtp_MtpServer.hpp"
44#include "mtp/twrpMtp.hpp"
45#endif
46
Dees Troy6f6441d2014-01-23 02:07:03 +000047extern "C" {
48 #include "cutils/properties.h"
49}
50
Dees_Troy5bf43922012-09-07 16:07:55 -040051#ifdef TW_INCLUDE_CRYPTO
52 #ifdef TW_INCLUDE_JB_CRYPTO
53 #include "crypto/jb/cryptfs.h"
54 #else
55 #include "crypto/ics/cryptfs.h"
56 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -040057#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040058
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050059TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040060 mtpid = 100;
61 mtp_was_enabled = false;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050062}
63
Dees_Troy51a0e822012-09-05 15:24:24 -040064int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040065 FILE *fstabFile;
66 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +000067 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050068 TWPartition* andsec_partition = NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -040069
70 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
71 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000072 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040073 return false;
74 }
75
76 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
77 if (fstab_line[0] != '/')
78 continue;
79
Dees_Troy2a923582012-09-20 12:13:34 -040080 if (fstab_line[strlen(fstab_line) - 1] != '\n')
81 fstab_line[strlen(fstab_line)] = '\n';
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040082 TWPartition* partition = new TWPartition(&mtpid);
Dees_Troy2a923582012-09-20 12:13:34 -040083 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040084 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040085
Dees_Troy5bf43922012-09-07 16:07:55 -040086 if (partition->Process_Fstab_Line(line, Display_Error)) {
Matt Mowered71fa32014-04-16 13:21:47 -050087 if (!settings_partition && partition->Is_Settings_Storage && partition->Is_Present) {
Dees Troy02a64532014-03-19 15:23:32 +000088 settings_partition = partition;
Dees_Troya13d74f2013-03-24 08:54:55 -050089 } else {
90 partition->Is_Settings_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050091 }
Matt Mowered71fa32014-04-16 13:21:47 -050092 if (!andsec_partition && partition->Has_Android_Secure && partition->Is_Present) {
Matt Mowerbf4efa32014-04-14 23:25:26 -050093 andsec_partition = partition;
94 } else {
95 partition->Has_Android_Secure = false;
96 }
Ethan Yonkerc05c5982014-03-13 09:19:56 -050097 Partitions.push_back(partition);
Dees_Troy5bf43922012-09-07 16:07:55 -040098 } else {
99 delete partition;
100 }
101 }
102 fclose(fstabFile);
Dees Troy02a64532014-03-19 15:23:32 +0000103 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500104 std::vector<TWPartition*>::iterator iter;
105 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
106 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000107 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500108 break;
109 }
110 }
Dees Troy02a64532014-03-19 15:23:32 +0000111 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000112 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500113 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400114 if (!Write_Fstab()) {
115 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000116 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400117 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000118 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400119 }
Matt Mowered71fa32014-04-16 13:21:47 -0500120
Matt Mowerbf4efa32014-04-14 23:25:26 -0500121 if (andsec_partition) {
122 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500123 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500124 Setup_Android_Secure_Location(settings_partition);
125 }
Matt Mowered71fa32014-04-16 13:21:47 -0500126 if (settings_partition) {
127 Setup_Settings_Storage_Partition(settings_partition);
128 }
Dees_Troy51127312012-09-08 13:08:49 -0400129 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400130 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400131 return true;
132}
133
134int TWPartitionManager::Write_Fstab(void) {
135 FILE *fp;
136 std::vector<TWPartition*>::iterator iter;
137 string Line;
138
139 fp = fopen("/etc/fstab", "w");
140 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000141 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400142 return false;
143 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400144 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400145 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400146 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400147 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000148 }
149 // Handle subpartition tracking
150 if ((*iter)->Is_SubPartition) {
151 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
152 if (ParentPartition)
153 ParentPartition->Has_SubPartition = true;
154 else
155 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 -0400156 }
157 }
158 fclose(fp);
159 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400160}
161
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500162void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500163 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
164 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
165 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
166}
167
Matt Mowerbf4efa32014-04-14 23:25:26 -0500168void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
169 if (Part->Has_Android_Secure)
170 Part->Setup_AndSec();
171#ifndef RECOVERY_SDCARD_ON_DATA
172 else
173 Part->Setup_AndSec();
174#endif
175}
176
Dees_Troy8170a922012-09-18 15:40:25 -0400177void TWPartitionManager::Output_Partition_Logging(void) {
178 std::vector<TWPartition*>::iterator iter;
179
180 printf("\n\nPartition Logs:\n");
181 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
182 Output_Partition((*iter));
183}
184
185void TWPartitionManager::Output_Partition(TWPartition* Part) {
186 unsigned long long mb = 1048576;
187
Gary Peck004d48b2012-11-21 16:28:18 -0800188 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 -0400189 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800190 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 -0400191 }
Gary Peck004d48b2012-11-21 16:28:18 -0800192 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500193 if (Part->Can_Be_Mounted)
194 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800195 if (Part->Can_Be_Wiped)
196 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700197 if (Part->Use_Rm_Rf)
198 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500199 if (Part->Can_Be_Backed_Up)
200 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800201 if (Part->Wipe_During_Factory_Reset)
202 printf("Wipe_During_Factory_Reset ");
203 if (Part->Wipe_Available_in_GUI)
204 printf("Wipe_Available_in_GUI ");
205 if (Part->Is_SubPartition)
206 printf("Is_SubPartition ");
207 if (Part->Has_SubPartition)
208 printf("Has_SubPartition ");
209 if (Part->Removable)
210 printf("Removable ");
211 if (Part->Is_Present)
212 printf("IsPresent ");
213 if (Part->Can_Be_Encrypted)
214 printf("Can_Be_Encrypted ");
215 if (Part->Is_Encrypted)
216 printf("Is_Encrypted ");
217 if (Part->Is_Decrypted)
218 printf("Is_Decrypted ");
219 if (Part->Has_Data_Media)
220 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000221 if (Part->Can_Encrypt_Backup)
222 printf("Can_Encrypt_Backup ");
223 if (Part->Use_Userdata_Encryption)
224 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800225 if (Part->Has_Android_Secure)
226 printf("Has_Android_Secure ");
227 if (Part->Is_Storage)
228 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500229 if (Part->Is_Settings_Storage)
230 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000231 if (Part->Ignore_Blkid)
232 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000233 if (Part->Retain_Layout_Version)
234 printf("Retain_Layout_Version ");
Gary Peck004d48b2012-11-21 16:28:18 -0800235 printf("\n");
236 if (!Part->SubPartition_Of.empty())
237 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
238 if (!Part->Symlink_Path.empty())
239 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
240 if (!Part->Symlink_Mount_Point.empty())
241 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
242 if (!Part->Primary_Block_Device.empty())
243 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
244 if (!Part->Alternate_Block_Device.empty())
245 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
246 if (!Part->Decrypted_Block_Device.empty())
247 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
248 if (Part->Length != 0)
249 printf(" Length: %i\n", Part->Length);
250 if (!Part->Display_Name.empty())
251 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500252 if (!Part->Storage_Name.empty())
253 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800254 if (!Part->Backup_Path.empty())
255 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
256 if (!Part->Backup_Name.empty())
257 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500258 if (!Part->Backup_Display_Name.empty())
259 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800260 if (!Part->Backup_FileName.empty())
261 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
262 if (!Part->Storage_Path.empty())
263 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
264 if (!Part->Current_File_System.empty())
265 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
266 if (!Part->Fstab_File_System.empty())
267 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
268 if (Part->Format_Block_Size != 0)
269 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400270 if (!Part->MTD_Name.empty())
271 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400272 string back_meth = Part->Backup_Method_By_Name();
273 printf(" Backup_Method: %s\n\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800274 if (Part->Mount_Flags || !Part->Mount_Options.empty())
275 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400276}
277
Dees_Troy51a0e822012-09-05 15:24:24 -0400278int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400279 std::vector<TWPartition*>::iterator iter;
280 int ret = false;
281 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400282 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400283
Dees_Troyd93bda52013-07-03 19:55:19 +0000284 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400285 return true;
286
Dees_Troy5bf43922012-09-07 16:07:55 -0400287 // Iterate through all partitions
288 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400289 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400290 ret = (*iter)->Mount(Display_Error);
291 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400292 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400293 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400294 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400295 }
296 if (found) {
297 return ret;
298 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000299 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400300 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000301 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400302 }
303 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400304}
305
306int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400307 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400308
Dees_Troy51127312012-09-08 13:08:49 -0400309 if (Part) {
310 if (Part->Has_SubPartition) {
311 std::vector<TWPartition*>::iterator subpart;
312
313 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
314 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
315 (*subpart)->Mount(Display_Error);
316 }
317 return Part->Mount(Display_Error);
318 } else
319 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400320 }
321 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000322 LOGERR("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400323 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000324 LOGINFO("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400325 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400326}
327
328int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400329 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400330
Dees_Troy51127312012-09-08 13:08:49 -0400331 if (Part) {
332 if (Part->Has_SubPartition) {
333 std::vector<TWPartition*>::iterator subpart;
334
335 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
336 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
337 (*subpart)->Mount(Display_Error);
338 }
339 return Part->Mount(Display_Error);
340 } else
341 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400342 }
343 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000344 LOGERR("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400345 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000346 LOGINFO("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400347 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400348}
349
350int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400351 std::vector<TWPartition*>::iterator iter;
352 int ret = false;
353 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400354 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400355
356 // Iterate through all partitions
357 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400358 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400359 ret = (*iter)->UnMount(Display_Error);
360 found = true;
361 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
362 (*iter)->UnMount(Display_Error);
363 }
364 }
365 if (found) {
366 return ret;
367 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000368 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400369 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000370 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400371 }
372 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400373}
374
375int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400376 TWPartition* Part = Find_Partition_By_Block(Block);
377
378 if (Part) {
379 if (Part->Has_SubPartition) {
380 std::vector<TWPartition*>::iterator subpart;
381
382 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
383 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
384 (*subpart)->UnMount(Display_Error);
385 }
386 return Part->UnMount(Display_Error);
387 } else
388 return Part->UnMount(Display_Error);
389 }
390 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000391 LOGERR("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400392 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000393 LOGINFO("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400394 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400395}
396
397int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400398 TWPartition* Part = Find_Partition_By_Name(Name);
399
400 if (Part) {
401 if (Part->Has_SubPartition) {
402 std::vector<TWPartition*>::iterator subpart;
403
404 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
405 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
406 (*subpart)->UnMount(Display_Error);
407 }
408 return Part->UnMount(Display_Error);
409 } else
410 return Part->UnMount(Display_Error);
411 }
412 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000413 LOGERR("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400414 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000415 LOGINFO("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400416 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400417}
418
419int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400420 TWPartition* Part = Find_Partition_By_Path(Path);
421
422 if (Part)
423 return Part->Is_Mounted();
424 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000425 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400426 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400427}
428
429int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400430 TWPartition* Part = Find_Partition_By_Block(Block);
431
432 if (Part)
433 return Part->Is_Mounted();
434 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000435 LOGINFO("Is_Mounted: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400436 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400437}
438
439int TWPartitionManager::Is_Mounted_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400440 TWPartition* Part = Find_Partition_By_Name(Name);
441
442 if (Part)
443 return Part->Is_Mounted();
444 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000445 LOGINFO("Is_Mounted: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400446 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400447}
448
Dees_Troy5bf43922012-09-07 16:07:55 -0400449int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400450 string current_storage_path = DataManager::GetCurrentStoragePath();
451
452 if (Mount_By_Path(current_storage_path, Display_Error)) {
453 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
454 if (FreeStorage)
455 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
456 return true;
457 }
458 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400459}
460
Dees_Troy5bf43922012-09-07 16:07:55 -0400461int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
462 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
463}
464
465TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
466 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400467 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400468
469 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400470 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400471 return (*iter);
472 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400473 return NULL;
474}
475
Dees_Troy5bf43922012-09-07 16:07:55 -0400476TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400477 std::vector<TWPartition*>::iterator iter;
478
479 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400480 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 -0400481 return (*iter);
482 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400483 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400484}
485
486TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400487 std::vector<TWPartition*>::iterator iter;
488
489 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
490 if ((*iter)->Display_Name == Name)
491 return (*iter);
492 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400493 return NULL;
494}
Dees_Troy51a0e822012-09-05 15:24:24 -0400495
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400496int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
497 // Check the backup name to ensure that it is the correct size and contains only valid characters
498 // and that a backup with that name doesn't already exist
499 char backup_name[MAX_BACKUP_NAME_LEN];
500 char backup_loc[255], tw_image_dir[255];
501 int copy_size;
502 int index, cur_char;
503 string Backup_Name, Backup_Loc;
504
505 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
506 copy_size = Backup_Name.size();
507 // Check size
508 if (copy_size > MAX_BACKUP_NAME_LEN) {
509 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000510 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400511 return -2;
512 }
513
514 // Check each character
515 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500516 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400517 return 0; // A "0" (zero) means to use the current timestamp for the backup name
518 for (index=0; index<copy_size; index++) {
519 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500520 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 -0400521 // These are valid characters
522 // Numbers
523 // Upper case letters
524 // Lower case letters
525 // Space
526 // and -_.{}[]
527 } else {
528 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000529 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400530 return -3;
531 }
532 }
533
534 // Check to make sure that a backup with this name doesn't already exist
535 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
536 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500537 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500538 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400539 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000540 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400541 return -4;
542 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400543 // No problems found, return 0
544 return 0;
545}
546
Dees_Troy43d8b002012-09-17 16:00:01 -0400547bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
548{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500549 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400550 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500551 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500552 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400553
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500554 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400555 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400556
Dees_Troyb46a6842012-09-25 11:06:46 -0400557 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000558 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400559
560 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500561 md5sum.setfn(Backup_Folder + Backup_Filename);
562 if (md5sum.computeMD5() == 0)
563 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000564 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500565 else
566 return -1;
567 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000568 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400569 } else {
570 char filename[512];
571 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500572 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400573 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500574 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000575 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400576 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000577 if (TWFunc::Path_Exists(filename)) {
578 if (md5sum.computeMD5() == 0) {
579 if (md5sum.write_md5digest() != 0)
580 {
581 gui_print(" * MD5 Error.\n");
582 return false;
583 }
584 } else {
585 gui_print(" * Error computing MD5.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500586 return false;
587 }
588 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400589 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400590 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500591 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400592 }
593 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000594 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400595 return false;
596 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000597 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400598 }
599 return true;
600}
601
Dees_Troy093b7642012-09-21 15:59:38 -0400602bool 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 -0400603 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500604 int img_bps;
605 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400606 unsigned long total_time, remain_time, section_time;
607 int use_compression, backup_time;
608 float pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500609 unsigned long long total_size, current_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400610
611 if (Part == NULL)
612 return true;
613
Dees_Troy093b7642012-09-21 15:59:38 -0400614 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
615
616 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
617 if (use_compression)
618 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
619 else
620 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
621
622 // We know the speed for both, how far into the whole backup are we, based on time
623 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
624 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
625
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500626 //pos = (total_time - remain_time) / (float) total_time;
627 total_size = *file_bytes + *img_bytes;
628 current_size = *file_bytes + *img_bytes - *file_bytes_remaining - *img_bytes_remaining;
629 pos = ((float)(current_size) / (float)(total_size));
Dees_Troy2673cec2013-04-02 20:22:16 +0000630 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400631
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500632 LOGINFO("Estimated total time: %lu\nEstimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400633
634 // And get the time
635 if (Part->Backup_Method == 1)
636 section_time = Part->Backup_Size / file_bps;
637 else
638 section_time = Part->Backup_Size / img_bps;
639
640 // Set the position
641 pos = section_time / (float) total_time;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500642 //DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400643
Dees_Troy43d8b002012-09-17 16:00:01 -0400644 time(&start);
645
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500646 if (Part->Backup(Backup_Folder, &total_size, &current_size)) {
647 current_size += Part->Backup_Size;
648 pos = (float)((float)(current_size) / (float)(total_size));
649 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400650 if (Part->Has_SubPartition) {
651 std::vector<TWPartition*>::iterator subpart;
652
653 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500654 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500655 if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size))
Dees_Troy8170a922012-09-18 15:40:25 -0400656 return false;
Dees_Troy2727b992013-08-14 20:09:30 +0000657 sync();
658 sync();
Dees_Troy8170a922012-09-18 15:40:25 -0400659 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
660 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400661 if (Part->Backup_Method == 1) {
662 *file_bytes_remaining -= (*subpart)->Backup_Size;
663 } else {
664 *img_bytes_remaining -= (*subpart)->Backup_Size;
665 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500666 current_size += Part->Backup_Size;
667 pos = (float)(current_size / total_size);
668 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400669 }
670 }
671 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400672 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400673 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000674 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400675 if (Part->Backup_Method == 1) {
676 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400677 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400678 } else {
679 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400680 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400681 }
682 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
683 } else {
684 return false;
685 }
686}
687
688int TWPartitionManager::Run_Backup(void) {
689 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500690 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400691 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 -0400692 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500693 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400694 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400695 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400696 struct tm *t;
697 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500698 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400699 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500700 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400701
702 time(&total_start);
703
704 Update_System_Details();
705
706 if (!Mount_Current_Storage(true))
707 return false;
708
709 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400710 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400711 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400712 else
713 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400714
Dees_Troy43d8b002012-09-17 16:00:01 -0400715 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
716 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000717 if (Backup_Name == "(Current Date)") {
718 Backup_Name = TWFunc::Get_Current_Date();
719 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
720 TWFunc::Auto_Generate_Backup_Name();
721 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400722 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000723 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400724 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000725 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400726
Dees_Troy2673cec2013-04-02 20:22:16 +0000727 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500728 DataManager::GetValue("tw_backup_list", Backup_List);
729 if (!Backup_List.empty()) {
730 end_pos = Backup_List.find(";", start_pos);
731 while (end_pos != string::npos && start_pos < Backup_List.size()) {
732 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
733 backup_part = Find_Partition_By_Path(backup_path);
734 if (backup_part != NULL) {
735 partition_count++;
736 if (backup_part->Backup_Method == 1)
737 file_bytes += backup_part->Backup_Size;
738 else
739 img_bytes += backup_part->Backup_Size;
740 if (backup_part->Has_SubPartition) {
741 std::vector<TWPartition*>::iterator subpart;
742
743 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000744 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 -0500745 partition_count++;
746 if ((*subpart)->Backup_Method == 1)
747 file_bytes += (*subpart)->Backup_Size;
748 else
749 img_bytes += (*subpart)->Backup_Size;
750 }
751 }
Dees_Troy8170a922012-09-18 15:40:25 -0400752 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500753 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000754 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400755 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500756 start_pos = end_pos + 1;
757 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400758 }
759 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400760
761 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000762 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400763 return false;
764 }
765 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000766 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500767 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400768 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
769 if (storage != NULL) {
770 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000771 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400772 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000773 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400774 return false;
775 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000776 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400777 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000778 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400779 return false;
780 }
781 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500782 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400783
Dees_Troy2673cec2013-04-02 20:22:16 +0000784 gui_print("\n[BACKUP STARTED]\n");
785 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000786 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000787 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000788 return false;
789 }
790
Dees_Troy2673cec2013-04-02 20:22:16 +0000791 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400792
Dees_Troya13d74f2013-03-24 08:54:55 -0500793 start_pos = 0;
794 end_pos = Backup_List.find(";", start_pos);
795 while (end_pos != string::npos && start_pos < Backup_List.size()) {
796 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
797 backup_part = Find_Partition_By_Path(backup_path);
798 if (backup_part != NULL) {
799 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
800 return false;
801 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000802 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500803 }
804 start_pos = end_pos + 1;
805 end_pos = Backup_List.find(";", start_pos);
806 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400807
808 // Average BPS
809 if (img_time == 0)
810 img_time = 1;
811 if (file_time == 0)
812 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400813 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500814 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400815
Dees_Troy2673cec2013-04-02 20:22:16 +0000816 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
817 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400818
819 time(&total_stop);
820 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500821 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500822 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400823
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500824 int prev_img_bps, use_compression;
825 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400826 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
827 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500828 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400829
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500830 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400831 if (use_compression)
832 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500833 else
Dees_Troy093b7642012-09-21 15:59:38 -0400834 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
835 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500836 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400837
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500838 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400839 if (use_compression)
840 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
841 else
842 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
843
Dees_Troy2673cec2013-04-02 20:22:16 +0000844 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400845 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400846 UnMount_Main_Partitions();
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500847 gui_print_color("highlight", "[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000848 string backup_log = Full_Backup_Path + "recovery.log";
849 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
850 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400851}
852
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500853bool 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 -0400854 time_t Start, Stop;
855 time(&Start);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500856 //DataManager::ShowProgress(1.0 / (float)partition_count, 150);
857 if (!Part->Restore(Restore_Name, total_restore_size, already_restored_size))
Dees_Troy4a2a1262012-09-18 09:33:47 -0400858 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400859 if (Part->Has_SubPartition) {
860 std::vector<TWPartition*>::iterator subpart;
861
862 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
863 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500864 if (!(*subpart)->Restore(Restore_Name, total_restore_size, already_restored_size))
Dees_Troy8170a922012-09-18 15:40:25 -0400865 return false;
866 }
867 }
868 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400869 time(&Stop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000870 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 -0400871 return true;
872}
873
Dees_Troy51a0e822012-09-05 15:24:24 -0400874int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400875 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500876 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400877 time_t rStart, rStop;
878 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500879 string Restore_List, restore_path;
880 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500881 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400882
Dees_Troy2673cec2013-04-02 20:22:16 +0000883 gui_print("\n[RESTORE STARTED]\n\n");
884 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400885
Dees_Troy4a2a1262012-09-18 09:33:47 -0400886 if (!Mount_Current_Storage(true))
887 return false;
888
889 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500890 if (check_md5 > 0) {
891 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
892 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000893 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500894 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000895 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500896 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500897 gui_print("Calculating restore details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500898 DataManager::GetValue("tw_restore_selected", Restore_List);
899 if (!Restore_List.empty()) {
900 end_pos = Restore_List.find(";", start_pos);
901 while (end_pos != string::npos && start_pos < Restore_List.size()) {
902 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
903 restore_part = Find_Partition_By_Path(restore_path);
904 if (restore_part != NULL) {
905 partition_count++;
906 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
907 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500908 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500909 if (restore_part->Has_SubPartition) {
910 std::vector<TWPartition*>::iterator subpart;
911
912 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
913 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400914 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500915 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500916 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500917 }
918 }
919 }
920 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500921 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500922 }
923 start_pos = end_pos + 1;
924 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400925 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400926 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400927
928 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000929 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400930 return false;
931 }
932
Dees_Troy2673cec2013-04-02 20:22:16 +0000933 gui_print("Restoring %i partitions...\n", partition_count);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500934 gui_print("Total restore size is %lluMB\n", total_restore_size / 1048576);
Dees_Troy2673cec2013-04-02 20:22:16 +0000935 DataManager::SetProgress(0.0);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500936
Dees_Troya13d74f2013-03-24 08:54:55 -0500937 start_pos = 0;
938 if (!Restore_List.empty()) {
939 end_pos = Restore_List.find(";", start_pos);
940 while (end_pos != string::npos && start_pos < Restore_List.size()) {
941 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
942 restore_part = Find_Partition_By_Path(restore_path);
943 if (restore_part != NULL) {
944 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500945 if (!Restore_Partition(restore_part, Restore_Name, partition_count, &total_restore_size, &already_restored_size))
Dees_Troya13d74f2013-03-24 08:54:55 -0500946 return false;
947 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000948 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500949 }
950 start_pos = end_pos + 1;
951 end_pos = Restore_List.find(";", start_pos);
952 }
953 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400954 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400955 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400956 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400957 time(&rStop);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500958 gui_print_color("highlight", "[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500959 DataManager::SetValue("tw_file_progress", "");
Dees_Troy63c8df72012-09-10 14:02:05 -0400960 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400961}
962
963void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400964 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500965 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000966 bool get_date = true, check_encryption = true;
967
968 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400969
970 DIR* d;
971 d = opendir(Restore_Name.c_str());
972 if (d == NULL)
973 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000974 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400975 return;
976 }
977
978 struct dirent* de;
979 while ((de = readdir(d)) != NULL)
980 {
981 // Strip off three components
982 char str[256];
983 char* label;
984 char* fstype = NULL;
985 char* extn = NULL;
986 char* ptr;
987
988 strcpy(str, de->d_name);
989 if (strlen(str) <= 2)
990 continue;
991
992 if (get_date) {
993 char file_path[255];
994 struct stat st;
995
996 strcpy(file_path, Restore_Name.c_str());
997 strcat(file_path, "/");
998 strcat(file_path, str);
999 stat(file_path, &st);
1000 string backup_date = ctime((const time_t*)(&st.st_mtime));
1001 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
1002 get_date = false;
1003 }
1004
1005 label = str;
1006 ptr = label;
1007 while (*ptr && *ptr != '.') ptr++;
1008 if (*ptr == '.')
1009 {
1010 *ptr = 0x00;
1011 ptr++;
1012 fstype = ptr;
1013 }
1014 while (*ptr && *ptr != '.') ptr++;
1015 if (*ptr == '.')
1016 {
1017 *ptr = 0x00;
1018 ptr++;
1019 extn = ptr;
1020 }
1021
Dees_Troy83bd4832013-05-04 12:39:56 +00001022 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -05001023 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +00001024 if (extnlength != 3 && extnlength != 6) continue;
1025 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
1026 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
1027
1028 if (check_encryption) {
1029 string filename = Restore_Name + "/";
1030 filename += de->d_name;
1031 if (TWFunc::Get_File_Type(filename) == 2) {
1032 LOGINFO("'%s' is encrypted\n", filename.c_str());
1033 DataManager::SetValue("tw_restore_encrypted", 1);
1034 }
1035 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001036 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -04001037
1038 TWPartition* Part = Find_Partition_By_Path(label);
1039 if (Part == NULL)
1040 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001041 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -04001042 continue;
1043 }
1044
1045 Part->Backup_FileName = de->d_name;
1046 if (strlen(extn) > 3) {
1047 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1048 }
1049
Dees_Troya13d74f2013-03-24 08:54:55 -05001050 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001051 }
1052 closedir(d);
1053
Dees_Troya13d74f2013-03-24 08:54:55 -05001054 // Set the final value
1055 DataManager::SetValue("tw_restore_list", Restore_List);
1056 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001057 return;
1058}
1059
1060int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001061 std::vector<TWPartition*>::iterator iter;
1062 int ret = false;
1063 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001064 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001065
1066 // Iterate through all partitions
1067 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001068 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001069 if (Path == "/and-sec")
1070 ret = (*iter)->Wipe_AndSec();
1071 else
1072 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001073 found = true;
1074 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1075 (*iter)->Wipe();
1076 }
1077 }
1078 if (found) {
1079 return ret;
1080 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001081 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001082 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001083}
1084
1085int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001086 TWPartition* Part = Find_Partition_By_Block(Block);
1087
1088 if (Part) {
1089 if (Part->Has_SubPartition) {
1090 std::vector<TWPartition*>::iterator subpart;
1091
1092 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1093 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1094 (*subpart)->Wipe();
1095 }
1096 return Part->Wipe();
1097 } else
1098 return Part->Wipe();
1099 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001100 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001101 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001102}
1103
1104int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001105 TWPartition* Part = Find_Partition_By_Name(Name);
1106
1107 if (Part) {
1108 if (Part->Has_SubPartition) {
1109 std::vector<TWPartition*>::iterator subpart;
1110
1111 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1112 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1113 (*subpart)->Wipe();
1114 }
1115 return Part->Wipe();
1116 } else
1117 return Part->Wipe();
1118 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001119 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001120 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001121}
1122
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001123int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1124 std::vector<TWPartition*>::iterator iter;
1125 int ret = false;
1126 bool found = false;
1127 string Local_Path = TWFunc::Get_Root_Path(Path);
1128
1129 // Iterate through all partitions
1130 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1131 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1132 if (Path == "/and-sec")
1133 ret = (*iter)->Wipe_AndSec();
1134 else
1135 ret = (*iter)->Wipe(New_File_System);
1136 found = true;
1137 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1138 (*iter)->Wipe(New_File_System);
1139 }
1140 }
1141 if (found) {
1142 return ret;
1143 } else
1144 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1145 return false;
1146}
1147
1148int TWPartitionManager::Wipe_By_Block(string Block, string New_File_System) {
1149 TWPartition* Part = Find_Partition_By_Block(Block);
1150
1151 if (Part) {
1152 if (Part->Has_SubPartition) {
1153 std::vector<TWPartition*>::iterator subpart;
1154
1155 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1156 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1157 (*subpart)->Wipe(New_File_System);
1158 }
1159 return Part->Wipe(New_File_System);
1160 } else
1161 return Part->Wipe(New_File_System);
1162 }
1163 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
1164 return false;
1165}
1166
1167int TWPartitionManager::Wipe_By_Name(string Name, string New_File_System) {
1168 TWPartition* Part = Find_Partition_By_Name(Name);
1169
1170 if (Part) {
1171 if (Part->Has_SubPartition) {
1172 std::vector<TWPartition*>::iterator subpart;
1173
1174 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1175 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1176 (*subpart)->Wipe();
1177 }
1178 return Part->Wipe(New_File_System);
1179 } else
1180 return Part->Wipe(New_File_System);
1181 }
1182 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
1183 return false;
1184}
1185
Dees_Troy51a0e822012-09-05 15:24:24 -04001186int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001187 std::vector<TWPartition*>::iterator iter;
1188 int ret = true;
1189
1190 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001191 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001192 if (!(*iter)->Wipe())
1193 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001194 } else if ((*iter)->Has_Android_Secure) {
1195 if (!(*iter)->Wipe_AndSec())
1196 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001197 }
1198 }
1199 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001200}
1201
Dees_Troy38bd7602012-09-14 13:33:53 -04001202int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1203 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001204 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001205
1206 if (!Mount_By_Path("/data", true))
1207 return false;
1208
1209 if (!Mount_By_Path("/cache", true))
1210 return false;
1211
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001212 dir.push_back("/data/dalvik-cache");
1213 dir.push_back("/cache/dalvik-cache");
1214 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001215 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001216 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001217 if (stat(dir.at(i).c_str(), &st) == 0) {
1218 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001219 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001220 }
1221 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001222 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001223 if (sdext && sdext->Is_Present && sdext->Mount(false))
1224 {
1225 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1226 {
1227 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001228 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001229 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001230 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001231 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001232 return true;
1233}
1234
1235int TWPartitionManager::Wipe_Rotate_Data(void) {
1236 if (!Mount_By_Path("/data", true))
1237 return false;
1238
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001239 unlink("/data/misc/akmd*");
1240 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001241 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001242 return true;
1243}
1244
1245int TWPartitionManager::Wipe_Battery_Stats(void) {
1246 struct stat st;
1247
1248 if (!Mount_By_Path("/data", true))
1249 return false;
1250
1251 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001252 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001253 } else {
1254 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001255 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001256 }
1257 return true;
1258}
1259
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001260int TWPartitionManager::Wipe_Android_Secure(void) {
1261 std::vector<TWPartition*>::iterator iter;
1262 int ret = false;
1263 bool found = false;
1264
1265 // Iterate through all partitions
1266 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1267 if ((*iter)->Has_Android_Secure) {
1268 ret = (*iter)->Wipe_AndSec();
1269 found = true;
1270 }
1271 }
1272 if (found) {
1273 return ret;
1274 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001275 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001276 }
1277 return false;
1278}
1279
Dees_Troy38bd7602012-09-14 13:33:53 -04001280int TWPartitionManager::Format_Data(void) {
1281 TWPartition* dat = Find_Partition_By_Path("/data");
1282
1283 if (dat != NULL) {
1284 if (!dat->UnMount(true))
1285 return false;
1286
1287 return dat->Wipe_Encryption();
1288 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001289 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001290 return false;
1291 }
1292 return false;
1293}
1294
1295int TWPartitionManager::Wipe_Media_From_Data(void) {
1296 TWPartition* dat = Find_Partition_By_Path("/data");
1297
1298 if (dat != NULL) {
1299 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001300 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001301 return false;
1302 }
1303 if (!dat->Mount(true))
1304 return false;
1305
Dees_Troy2673cec2013-04-02 20:22:16 +00001306 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001307 mtp_was_enabled = TWFunc::Toggle_MTP(false);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001308 TWFunc::removeDir("/data/media", false);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001309 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
1310 if (mtp_was_enabled) {
1311 if (!Enable_MTP())
1312 Disable_MTP();
1313 }
1314 return false;
1315 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001316 if (dat->Has_Data_Media) {
1317 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001318 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1319 dat->UnMount(false);
1320 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001321 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001322 if (mtp_was_enabled) {
1323 if (!Enable_MTP())
1324 Disable_MTP();
1325 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001326 return true;
1327 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001328 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001329 return false;
1330 }
1331 return false;
1332}
1333
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001334int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1335 std::vector<TWPartition*>::iterator iter;
1336 int ret = false;
1337 bool found = false;
1338 string Local_Path = TWFunc::Get_Root_Path(Path);
1339
1340 if (Local_Path == "/tmp" || Local_Path == "/")
1341 return true;
1342
1343 // Iterate through all partitions
1344 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1345 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1346 ret = (*iter)->Repair();
1347 found = true;
1348 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1349 (*iter)->Repair();
1350 }
1351 }
1352 if (found) {
1353 return ret;
1354 } else if (Display_Error) {
1355 LOGERR("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1356 } else {
1357 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1358 }
1359 return false;
1360}
1361
1362int TWPartitionManager::Repair_By_Block(string Block, bool Display_Error) {
1363 TWPartition* Part = Find_Partition_By_Block(Block);
1364
1365 if (Part) {
1366 if (Part->Has_SubPartition) {
1367 std::vector<TWPartition*>::iterator subpart;
1368
1369 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1370 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1371 (*subpart)->Repair();
1372 }
1373 return Part->Repair();
1374 } else
1375 return Part->Repair();
1376 }
1377 if (Display_Error)
1378 LOGERR("Repair: Unable to find partition for block '%s'\n", Block.c_str());
1379 else
1380 LOGINFO("Repair: Unable to find partition for block '%s'\n", Block.c_str());
1381 return false;
1382}
1383
1384int TWPartitionManager::Repair_By_Name(string Name, bool Display_Error) {
1385 TWPartition* Part = Find_Partition_By_Name(Name);
1386
1387 if (Part) {
1388 if (Part->Has_SubPartition) {
1389 std::vector<TWPartition*>::iterator subpart;
1390
1391 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1392 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1393 (*subpart)->Repair();
1394 }
1395 return Part->Repair();
1396 } else
1397 return Part->Repair();
1398 }
1399 if (Display_Error)
1400 LOGERR("Repair: Unable to find partition for name '%s'\n", Name.c_str());
1401 else
1402 LOGINFO("Repair: Unable to find partition for name '%s'\n", Name.c_str());
1403 return false;
1404}
1405
Dees_Troy51a0e822012-09-05 15:24:24 -04001406void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001407 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001408 return;
1409}
1410
1411void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001412 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001413 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001414
Dees_Troy2673cec2013-04-02 20:22:16 +00001415 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001416 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001417 if ((*iter)->Can_Be_Mounted) {
1418 (*iter)->Update_Size(true);
1419 if ((*iter)->Mount_Point == "/system") {
1420 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1421 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1422 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1423 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1424 } else if ((*iter)->Mount_Point == "/cache") {
1425 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1426 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1427 } else if ((*iter)->Mount_Point == "/sd-ext") {
1428 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1429 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1430 if ((*iter)->Backup_Size == 0) {
1431 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1432 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1433 } else
1434 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001435 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001436 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001437 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001438 if ((*iter)->Backup_Size == 0) {
1439 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1440 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1441 } else
1442 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001443 } else if ((*iter)->Mount_Point == "/boot") {
1444 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1445 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1446 if ((*iter)->Backup_Size == 0) {
1447 DataManager::SetValue("tw_has_boot_partition", 0);
1448 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1449 } else
1450 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001451 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001452#ifdef SP1_NAME
1453 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1454 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1455 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1456 }
1457#endif
1458#ifdef SP2_NAME
1459 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1460 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1461 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1462 }
1463#endif
1464#ifdef SP3_NAME
1465 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1466 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1467 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1468 }
1469#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001470 } else {
1471 // Handle unmountable partitions in case we reset defaults
1472 if ((*iter)->Mount_Point == "/boot") {
1473 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1474 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1475 if ((*iter)->Backup_Size == 0) {
1476 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1477 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1478 } else
1479 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1480 } else if ((*iter)->Mount_Point == "/recovery") {
1481 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1482 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1483 if ((*iter)->Backup_Size == 0) {
1484 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1485 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1486 } else
1487 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001488 } else if ((*iter)->Mount_Point == "/data") {
1489 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001490 }
1491#ifdef SP1_NAME
1492 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1493 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1494 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1495 }
1496#endif
1497#ifdef SP2_NAME
1498 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1499 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1500 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1501 }
1502#endif
1503#ifdef SP3_NAME
1504 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1505 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1506 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1507 }
1508#endif
Dees_Troy51127312012-09-08 13:08:49 -04001509 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001510 }
Dees_Troy51127312012-09-08 13:08:49 -04001511 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1512 string current_storage_path = DataManager::GetCurrentStoragePath();
1513 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001514 if (FreeStorage != NULL) {
1515 // Attempt to mount storage
1516 if (!FreeStorage->Mount(false)) {
1517 // We couldn't mount storage... check to see if we have dual storage
1518 int has_dual_storage;
1519 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1520 if (has_dual_storage == 1) {
1521 // We have dual storage, see if we're using the internal storage that should always be present
1522 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001523 if (!FreeStorage->Is_Encrypted) {
1524 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001525 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001526 }
Dees_Troy8170a922012-09-18 15:40:25 -04001527 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1528 } else {
1529 // We were using external, flip to internal
1530 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1531 current_storage_path = DataManager::GetCurrentStoragePath();
1532 FreeStorage = Find_Partition_By_Path(current_storage_path);
1533 if (FreeStorage != NULL) {
1534 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1535 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001536 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001537 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1538 }
1539 }
1540 } else {
1541 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001542 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001543 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1544 }
1545 } else {
1546 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1547 }
1548 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001549 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001550 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001551 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001552 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001553 return;
1554}
1555
1556int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001557#ifdef TW_INCLUDE_CRYPTO
1558 int ret_val, password_len;
1559 char crypto_blkdev[255], cPassword[255];
1560 size_t result;
1561
1562 property_set("ro.crypto.state", "encrypted");
1563#ifdef TW_INCLUDE_JB_CRYPTO
1564 // No extra flags needed
1565#else
1566 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1567 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1568 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1569 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1570 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1571 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001572
1573#ifdef CRYPTO_SD_FS_TYPE
Ethan Yonker71413f42014-02-26 13:36:08 -06001574 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1575 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1576 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001577#endif
a39552696ff55ce2013-01-08 16:14:56 +00001578
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001579 property_set("rw.km_fips_status", "ready");
a39552696ff55ce2013-01-08 16:14:56 +00001580
1581#endif
1582
1583 // some samsung devices store "footer" on efs partition
1584 TWPartition *efs = Find_Partition_By_Path("/efs");
1585 if(efs && !efs->Is_Mounted())
1586 efs->Mount(false);
1587 else
1588 efs = 0;
1589#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001590#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001591 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001592 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001593 property_set("ro.crypto.external_encrypted", "1");
1594 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1595 } else {
1596 property_set("ro.crypto.external_encrypted", "0");
1597 }
1598#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001599#endif
a39552696ff55ce2013-01-08 16:14:56 +00001600
Dees_Troy5bf43922012-09-07 16:07:55 -04001601 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001602 int pwret = cryptfs_check_passwd(cPassword);
1603
1604 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001605 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001606 return -1;
1607 }
a39552696ff55ce2013-01-08 16:14:56 +00001608
1609 if(efs)
1610 efs->UnMount(false);
1611
Dees_Troy5bf43922012-09-07 16:07:55 -04001612 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1613 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001614 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001615 } else {
1616 TWPartition* dat = Find_Partition_By_Path("/data");
1617 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001618 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001619 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1620 dat->Is_Decrypted = true;
1621 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001622 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001623 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 +00001624 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001625
1626#ifdef CRYPTO_SD_FS_TYPE
1627 char crypto_blkdev_sd[255];
1628 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1629 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001630 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001631 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001632 emmc->Is_Decrypted = true;
1633 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1634 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001635 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001636 }
a39552696ff55ce2013-01-08 16:14:56 +00001637#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001638#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001639#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001640 char is_external_decrypted[255];
1641 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1642 if (strcmp(is_external_decrypted, "1") == 0) {
1643 sdcard->Is_Decrypted = true;
1644 sdcard->EcryptFS_Password = Password;
1645 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001646 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1647 MetaEcfsFile += "/.MetaEcfsFile";
1648 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1649 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1650 sdcard->UnMount(false);
1651 sdcard->Mount(false);
1652 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001653 } else {
Dees_Troy066eb302013-08-23 17:20:32 +00001654 LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001655 sdcard->Is_Decrypted = false;
1656 sdcard->Decrypted_Block_Device = "";
1657 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001658#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001659#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001660
Dees_Troy5bf43922012-09-07 16:07:55 -04001661 // Sleep for a bit so that the device will be ready
1662 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001663#ifdef RECOVERY_SDCARD_ON_DATA
1664 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1665 dat->Storage_Path = "/data/media/0";
1666 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001667 DataManager::SetValue("tw_storage_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001668 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001669 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001670 }
1671#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001672 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001673 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001674 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001675 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001676 }
1677 return 0;
1678#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001679 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001680 return -1;
1681#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001682 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001683}
1684
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001685int TWPartitionManager::Fix_Permissions(void) {
1686 int result = 0;
1687 if (!Mount_By_Path("/data", true))
1688 return false;
1689
1690 if (!Mount_By_Path("/system", true))
1691 return false;
1692
1693 Mount_By_Path("/sd-ext", false);
1694
1695 fixPermissions perms;
1696 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001697 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001698 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001699 return result;
1700}
1701
Ethan Yonker47360be2014-04-01 10:34:34 -05001702TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1703 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1704
1705 if (!Path.empty()) {
1706 string Search_Path = TWFunc::Get_Root_Path(Path);
1707 for (; iter != Partitions.end(); iter++) {
1708 if ((*iter)->Mount_Point == Search_Path) {
1709 iter++;
1710 break;
1711 }
1712 }
1713 }
1714
1715 for (; iter != Partitions.end(); iter++) {
1716 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) {
1717 return (*iter);
1718 }
1719 }
1720
1721 return NULL;
1722}
1723
Dees_Troyd21618c2012-10-14 18:48:49 -04001724int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001725 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1726
1727 if (Part == NULL) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001728 LOGERR("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
Dees_Troyd21618c2012-10-14 18:48:49 -04001729 return false;
1730 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001731 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1732 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001733 return false;
1734
Dees_Troy2673cec2013-04-02 20:22:16 +00001735 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1736 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001737 return false;
1738 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001739 return true;
1740}
1741
Dees_Troy8170a922012-09-18 15:40:25 -04001742int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001743 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001744 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001745 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001746
Dees_Troy8170a922012-09-18 15:40:25 -04001747 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001748 string Lun_File_str = CUSTOM_LUN_FILE;
1749 size_t found = Lun_File_str.find("%");
1750 if (found != string::npos) {
1751 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1752 if (TWFunc::Path_Exists(lun_file))
1753 has_multiple_lun = true;
1754 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001755 mtp_was_enabled = TWFunc::Toggle_MTP(false);
Ethan Yonker47360be2014-04-01 10:34:34 -05001756 if (!has_multiple_lun) {
1757 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1758 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1759 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
1760 TWPartition* Mount = Find_Next_Storage("", "/data");
1761 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001762 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1763 goto error_handle;
1764 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001765 } else {
1766 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001767 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001768 }
1769 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001770 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001771 }
Dees_Troy8170a922012-09-18 15:40:25 -04001772 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001773 LOGINFO("Device has multiple lun files\n");
1774 TWPartition* Mount1;
1775 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001776 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker47360be2014-04-01 10:34:34 -05001777 Mount1 = Find_Next_Storage("", "/data");
1778 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001779 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1780 goto error_handle;
1781 }
Matt Moweree717062014-04-26 01:46:46 -05001782 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker47360be2014-04-01 10:34:34 -05001783 Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data");
1784 if (Mount2) {
Matt Moweree717062014-04-26 01:46:46 -05001785 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001786 }
1787 } else {
1788 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001789 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001790 }
Dees_Troy8170a922012-09-18 15:40:25 -04001791 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001792 property_set("sys.storage.ums_enabled", "1");
Dees_Troy8170a922012-09-18 15:40:25 -04001793 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001794error_handle:
1795 if (mtp_was_enabled)
1796 if (!Enable_MTP())
1797 Disable_MTP();
1798 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001799}
1800
1801int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001802 int index, ret;
1803 char lun_file[255], ch[2] = {0, 0};
1804 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001805
1806 for (index=0; index<2; index++) {
1807 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001808 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001809 if (ret < 0) {
1810 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001811 }
Dees_Troy8170a922012-09-18 15:40:25 -04001812 }
Dees_Troye58d5262012-09-21 12:27:57 -04001813 Mount_All_Storage();
1814 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001815 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001816 property_set("sys.storage.ums_enabled", "0");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001817 if (mtp_was_enabled)
1818 if (!Enable_MTP())
1819 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001820 if (ret < 0 && index == 0) {
1821 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1822 return false;
1823 } else {
1824 return true;
1825 }
Dees_Troy8170a922012-09-18 15:40:25 -04001826 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001827}
1828
1829void TWPartitionManager::Mount_All_Storage(void) {
1830 std::vector<TWPartition*>::iterator iter;
1831
1832 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1833 if ((*iter)->Is_Storage)
1834 (*iter)->Mount(false);
1835 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001836}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001837
Dees_Troyd0384ef2012-10-12 12:15:42 -04001838void TWPartitionManager::UnMount_Main_Partitions(void) {
1839 // Unmounts system and data if data is not data/media
1840 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001841 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001842
1843 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1844
1845 UnMount_By_Path("/system", true);
1846#ifndef RECOVERY_SDCARD_ON_DATA
1847 UnMount_By_Path("/data", true);
1848#endif
1849 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1850 Boot_Partition->UnMount(true);
1851}
1852
Dees_Troy9350b8d2012-09-27 12:38:38 -04001853int TWPartitionManager::Partition_SDCard(void) {
1854 char mkdir_path[255], temp[255], line[512];
1855 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1856 int ext, swap, total_size = 0, fat_size;
1857 FILE* fp;
1858
Dees_Troy2673cec2013-04-02 20:22:16 +00001859 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001860#ifdef TW_EXTERNAL_STORAGE_PATH
1861 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1862#else
1863 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1864#endif
1865 if (SDCard == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001866 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001867 return false;
1868 }
1869 if (!SDCard->UnMount(true))
1870 return false;
1871 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1872 if (SDext != NULL) {
1873 if (!SDext->UnMount(true))
1874 return false;
1875 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001876
1877 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001878 Device = SDCard->Actual_Block_Device;
1879 // Just use the root block device
1880 Device.resize(strlen("/dev/block/mmcblkX"));
1881
1882 // Find the size of the block device:
1883 fp = fopen("/proc/partitions", "rt");
1884 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001885 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001886 return false;
1887 }
1888
1889 while (fgets(line, sizeof(line), fp) != NULL)
1890 {
1891 unsigned long major, minor, blocks;
1892 char device[512];
1893 char tmpString[64];
1894
1895 if (strlen(line) < 7 || line[0] == 'm') continue;
1896 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1897
1898 tmpdevice = "/dev/block/";
1899 tmpdevice += device;
1900 if (tmpdevice == Device) {
1901 // Adjust block size to byte size
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001902 total_size = (int)(blocks * 1024ULL / 1000000LLU);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001903 break;
1904 }
1905 }
1906 fclose(fp);
1907
1908 DataManager::GetValue("tw_sdext_size", ext);
1909 DataManager::GetValue("tw_swap_size", swap);
1910 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1911 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001912 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 -04001913 memset(temp, 0, sizeof(temp));
1914 sprintf(temp, "%i", fat_size);
1915 fat_str = temp;
1916 memset(temp, 0, sizeof(temp));
1917 sprintf(temp, "%i", fat_size + ext);
1918 ext_str = temp;
1919 memset(temp, 0, sizeof(temp));
1920 sprintf(temp, "%i", fat_size + ext + swap);
1921 swap_str = temp;
1922 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001923 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001924 return false;
1925 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001926 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001927 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001928 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001929 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001930 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001931 Update_System_Details();
1932 return false;
1933 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001934 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001935 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001936 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001937 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001938 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001939 return false;
1940 }
1941 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001942 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001943 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001944 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001945 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001946 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001947 Update_System_Details();
1948 return false;
1949 }
1950 }
1951 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001952 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001953 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001954 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001955 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001956 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001957 Update_System_Details();
1958 return false;
1959 }
1960 }
1961 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1962#ifdef TW_EXTERNAL_STORAGE_PATH
1963 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1964 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1965 memset(mkdir_path, 0, sizeof(mkdir_path));
1966 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1967#else
1968 Mount_By_Path("/sdcard", 1);
1969 strcpy(mkdir_path, "/sdcard/TWRP");
1970#endif
1971 mkdir(mkdir_path, 0777);
1972 DataManager::Flush();
1973#ifdef TW_EXTERNAL_STORAGE_PATH
1974 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1975 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1976 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1977#else
1978 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1979 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1980 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1981#endif
1982 if (ext > 0) {
1983 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001984 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001985 return false;
1986 }
1987 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001988 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1989 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001990 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001991 }
1992
1993 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00001994 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001995 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001996}
Dees_Troya13d74f2013-03-24 08:54:55 -05001997
1998void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1999 std::vector<TWPartition*>::iterator iter;
2000 if (ListType == "mount") {
2001 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2002 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
2003 struct PartitionList part;
2004 part.Display_Name = (*iter)->Display_Name;
2005 part.Mount_Point = (*iter)->Mount_Point;
2006 part.selected = (*iter)->Is_Mounted();
2007 Partition_List->push_back(part);
2008 }
2009 }
2010 } else if (ListType == "storage") {
2011 char free_space[255];
2012 string Current_Storage = DataManager::GetCurrentStoragePath();
2013 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2014 if ((*iter)->Is_Storage) {
2015 struct PartitionList part;
2016 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
2017 part.Display_Name = (*iter)->Storage_Name + " (";
2018 part.Display_Name += free_space;
2019 part.Display_Name += "MB)";
2020 part.Mount_Point = (*iter)->Storage_Path;
2021 if ((*iter)->Storage_Path == Current_Storage)
2022 part.selected = 1;
2023 else
2024 part.selected = 0;
2025 Partition_List->push_back(part);
2026 }
2027 }
2028 } else if (ListType == "backup") {
2029 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002030 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05002031 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002032 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05002033 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002034 Backup_Size = (*iter)->Backup_Size;
2035 if ((*iter)->Has_SubPartition) {
2036 std::vector<TWPartition*>::iterator subpart;
2037
2038 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
2039 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
2040 Backup_Size += (*subpart)->Backup_Size;
2041 }
2042 }
2043 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05002044 part.Display_Name = (*iter)->Backup_Display_Name + " (";
2045 part.Display_Name += backup_size;
2046 part.Display_Name += "MB)";
2047 part.Mount_Point = (*iter)->Backup_Path;
2048 part.selected = 0;
2049 Partition_List->push_back(part);
2050 }
2051 }
2052 } else if (ListType == "restore") {
2053 string Restore_List, restore_path;
2054 TWPartition* restore_part = NULL;
2055
2056 DataManager::GetValue("tw_restore_list", Restore_List);
2057 if (!Restore_List.empty()) {
2058 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
2059 while (end_pos != string::npos && start_pos < Restore_List.size()) {
2060 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05002061 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00002062 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05002063 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05002064 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05002065 } else {
2066 struct PartitionList part;
2067 part.Display_Name = restore_part->Backup_Display_Name;
2068 part.Mount_Point = restore_part->Backup_Path;
2069 part.selected = 1;
2070 Partition_List->push_back(part);
2071 }
2072 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00002073 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05002074 }
2075 start_pos = end_pos + 1;
2076 end_pos = Restore_List.find(";", start_pos);
2077 }
2078 }
2079 } else if (ListType == "wipe") {
2080 struct PartitionList dalvik;
2081 dalvik.Display_Name = "Dalvik Cache";
2082 dalvik.Mount_Point = "DALVIK";
2083 dalvik.selected = 0;
2084 Partition_List->push_back(dalvik);
2085 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2086 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
2087 struct PartitionList part;
2088 part.Display_Name = (*iter)->Display_Name;
2089 part.Mount_Point = (*iter)->Mount_Point;
2090 part.selected = 0;
2091 Partition_List->push_back(part);
2092 }
2093 if ((*iter)->Has_Android_Secure) {
2094 struct PartitionList part;
2095 part.Display_Name = (*iter)->Backup_Display_Name;
2096 part.Mount_Point = (*iter)->Backup_Path;
2097 part.selected = 0;
2098 Partition_List->push_back(part);
2099 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00002100 if ((*iter)->Has_Data_Media) {
2101 struct PartitionList datamedia;
2102 datamedia.Display_Name = (*iter)->Storage_Name;
2103 datamedia.Mount_Point = "INTERNAL";
2104 datamedia.selected = 0;
2105 Partition_List->push_back(datamedia);
2106 }
Dees_Troya13d74f2013-03-24 08:54:55 -05002107 }
2108 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00002109 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05002110 }
2111}
2112
2113int TWPartitionManager::Fstab_Processed(void) {
2114 return Partitions.size();
2115}
Dees_Troyd93bda52013-07-03 19:55:19 +00002116
2117void TWPartitionManager::Output_Storage_Fstab(void) {
2118 std::vector<TWPartition*>::iterator iter;
2119 char storage_partition[255];
2120 string Temp;
2121 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
2122
2123 if (fp == NULL) {
2124 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
2125 return;
2126 }
2127
2128 // Iterate through all partitions
2129 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2130 if ((*iter)->Is_Storage) {
2131 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
2132 strcpy(storage_partition, Temp.c_str());
2133 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
2134 }
2135 }
2136 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002137}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02002138
2139TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
2140{
2141 TWPartition *res = NULL;
2142 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
2143 if(!(*iter)->Is_Storage)
2144 continue;
2145
2146 if((*iter)->Is_Settings_Storage)
2147 return *iter;
2148
2149 if(!res)
2150 res = *iter;
2151 }
2152 return res;
2153}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002154
2155bool TWPartitionManager::Enable_MTP(void) {
2156#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002157 if (mtppid) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002158 LOGERR("MTP already enabled\n");
2159 return true;
2160 }
2161 //Launch MTP Responder
2162 LOGINFO("Starting MTP\n");
2163 char vendor[PROPERTY_VALUE_MAX];
2164 char product[PROPERTY_VALUE_MAX];
2165 int count = 0;
2166 property_set("sys.usb.config", "none");
2167 property_get("usb.vendor", vendor, "18D1");
2168 property_get("usb.product.mtpadb", product, "4EE2");
2169 string vendorstr = vendor;
2170 string productstr = product;
2171 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2172 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
2173 property_set("sys.usb.config", "mtp,adb");
2174 std::vector<TWPartition*>::iterator iter;
Ethan Yonker6d154c42014-09-03 14:19:43 -05002175 /* To enable MTP debug, use the twrp command line feature to
2176 * twrp set tw_mtp_debug 1
2177 */
2178 twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug"));
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002179 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2180 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false)) {
2181 printf("twrp mtpid: %d\n", (*iter)->mtpid);
2182 mtp->addStorage((*iter)->Storage_Name, (*iter)->Storage_Path, (*iter)->mtpid);
2183 count++;
2184 }
2185 }
2186 if (count) {
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002187 mtppid = mtp->forkserver();
2188 if (mtppid) {
2189 DataManager::SetValue("tw_mtp_enabled", 1);
2190 return true;
2191 } else {
2192 LOGERR("Failed to enable MTP\n");
2193 return false;
2194 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002195 }
2196 LOGERR("No valid storage partitions found for MTP.\n");
2197#else
2198 LOGERR("MTP support not included\n");
2199#endif
2200 DataManager::SetValue("tw_mtp_enabled", 0);
2201 return false;
2202}
2203
2204bool TWPartitionManager::Disable_MTP(void) {
2205#ifdef TW_HAS_MTP
2206 char vendor[PROPERTY_VALUE_MAX];
2207 char product[PROPERTY_VALUE_MAX];
2208 property_set("sys.usb.config", "none");
2209 property_get("usb.vendor", vendor, "18D1");
2210 property_get("usb.product.adb", product, "D002");
2211 string vendorstr = vendor;
2212 string productstr = product;
2213 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2214 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002215 if (mtppid) {
Ethan Yonker8613dc02014-09-11 09:28:20 -05002216 LOGINFO("Disabling MTP\n");
2217 int status;
2218 kill(mtppid, SIGKILL);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002219 mtppid = 0;
Ethan Yonker8613dc02014-09-11 09:28:20 -05002220 // We don't care about the exit value, but this prevents a zombie process
2221 waitpid(mtppid, &status, 0);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002222 }
2223 property_set("sys.usb.config", "adb");
2224 DataManager::SetValue("tw_mtp_enabled", 0);
2225 return true;
2226#else
2227 LOGERR("MTP support not included\n");
2228 DataManager::SetValue("tw_mtp_enabled", 0);
2229 return false;
2230#endif
2231}