blob: 003dcd1f359239ed8733cb67e1502231e8c5f0bb [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"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060041#include "set_metadata.h"
Dees_Troy38bd7602012-09-14 13:33:53 -040042
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040043#ifdef TW_HAS_MTP
44#include "mtp/mtp_MtpServer.hpp"
45#include "mtp/twrpMtp.hpp"
Ethan Yonker726a0202014-12-16 20:01:38 -060046#include "mtp/MtpMessage.hpp"
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040047#endif
48
Dees Troy6f6441d2014-01-23 02:07:03 +000049extern "C" {
50 #include "cutils/properties.h"
51}
52
Dees_Troy5bf43922012-09-07 16:07:55 -040053#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060054 #include "crypto/lollipop/cryptfs.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040055#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040056
Ethan Yonker6277c792014-09-15 14:54:30 -050057extern bool datamedia;
58
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050059TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040060 mtp_was_enabled = false;
Ethan Yonker726a0202014-12-16 20:01:38 -060061 mtp_write_fd = -1;
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;
Ethan Yonker726a0202014-12-16 20:01:38 -060069 unsigned int storageid = 1 << 16; // upper 16 bits are for physical storage device, we pretend to have only one
Dees_Troy5bf43922012-09-07 16:07:55 -040070
71 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
72 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000073 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040074 return false;
75 }
76
77 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
78 if (fstab_line[0] != '/')
79 continue;
80
Dees_Troy2a923582012-09-20 12:13:34 -040081 if (fstab_line[strlen(fstab_line) - 1] != '\n')
82 fstab_line[strlen(fstab_line)] = '\n';
that9e0593e2014-10-08 00:01:24 +020083 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040084 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040085 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040086
Dees_Troy5bf43922012-09-07 16:07:55 -040087 if (partition->Process_Fstab_Line(line, Display_Error)) {
Ethan Yonker726a0202014-12-16 20:01:38 -060088 if (partition->Is_Storage) {
89 ++storageid;
90 partition->MTP_Storage_ID = storageid;
91 }
Matt Mowered71fa32014-04-16 13:21:47 -050092 if (!settings_partition && partition->Is_Settings_Storage && partition->Is_Present) {
Dees Troy02a64532014-03-19 15:23:32 +000093 settings_partition = partition;
Dees_Troya13d74f2013-03-24 08:54:55 -050094 } else {
95 partition->Is_Settings_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050096 }
Matt Mowered71fa32014-04-16 13:21:47 -050097 if (!andsec_partition && partition->Has_Android_Secure && partition->Is_Present) {
Matt Mowerbf4efa32014-04-14 23:25:26 -050098 andsec_partition = partition;
99 } else {
100 partition->Has_Android_Secure = false;
101 }
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500102 Partitions.push_back(partition);
Dees_Troy5bf43922012-09-07 16:07:55 -0400103 } else {
104 delete partition;
105 }
106 }
107 fclose(fstabFile);
Ethan Yonker6277c792014-09-15 14:54:30 -0500108 if (!datamedia && !settings_partition && Find_Partition_By_Path("/sdcard") == NULL && Find_Partition_By_Path("/internal_sd") == NULL && Find_Partition_By_Path("/internal_sdcard") == NULL && Find_Partition_By_Path("/emmc") == NULL) {
109 // Attempt to automatically identify /data/media emulated storage devices
110 TWPartition* Dat = Find_Partition_By_Path("/data");
111 if (Dat) {
112 LOGINFO("Using automatic handling for /data/media emulated storage device.\n");
113 datamedia = true;
that9e0593e2014-10-08 00:01:24 +0200114 Dat->Setup_Data_Media();
Ethan Yonker6277c792014-09-15 14:54:30 -0500115 settings_partition = Dat;
Ethan Yonker726a0202014-12-16 20:01:38 -0600116 // Since /data was not considered a storage partition earlier, we still need to assign an MTP ID
117 ++storageid;
118 Dat->MTP_Storage_ID = storageid;
Ethan Yonker6277c792014-09-15 14:54:30 -0500119 }
120 }
Dees Troy02a64532014-03-19 15:23:32 +0000121 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500122 std::vector<TWPartition*>::iterator iter;
123 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
124 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000125 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500126 break;
127 }
128 }
Dees Troy02a64532014-03-19 15:23:32 +0000129 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000130 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500131 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400132 if (!Write_Fstab()) {
133 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000134 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400135 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000136 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400137 }
Matt Mowered71fa32014-04-16 13:21:47 -0500138
Matt Mowerbf4efa32014-04-14 23:25:26 -0500139 if (andsec_partition) {
140 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500141 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500142 Setup_Android_Secure_Location(settings_partition);
143 }
Matt Mowered71fa32014-04-16 13:21:47 -0500144 if (settings_partition) {
145 Setup_Settings_Storage_Partition(settings_partition);
146 }
Ethan Yonker253368a2014-11-25 15:00:52 -0600147#ifdef TW_INCLUDE_CRYPTO
Ethan Yonkercceebb82014-11-18 10:17:59 -0600148 TWPartition* Decrypt_Data = Find_Partition_By_Path("/data");
149 if (Decrypt_Data && Decrypt_Data->Is_Encrypted && !Decrypt_Data->Is_Decrypted) {
150 int password_type = cryptfs_get_password_type();
151 if (password_type == CRYPT_TYPE_DEFAULT) {
152 LOGINFO("Device is encrypted with the default password, attempting to decrypt.\n");
153 if (Decrypt_Device("default_password") == 0) {
154 gui_print("Successfully decrypted with default password.\n");
155 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
156 } else {
157 LOGERR("Unable to decrypt with default password.");
158 LOGERR("You may need to perform a Format Data.\n");
159 }
160 } else {
161 DataManager::SetValue("TW_CRYPTO_TYPE", password_type);
162 }
163 }
164#endif
Dees_Troy51127312012-09-08 13:08:49 -0400165 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400166 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400167 return true;
168}
169
170int TWPartitionManager::Write_Fstab(void) {
171 FILE *fp;
172 std::vector<TWPartition*>::iterator iter;
173 string Line;
174
175 fp = fopen("/etc/fstab", "w");
176 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000177 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400178 return false;
179 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400180 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400181 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400182 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400183 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000184 }
185 // Handle subpartition tracking
186 if ((*iter)->Is_SubPartition) {
187 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
188 if (ParentPartition)
189 ParentPartition->Has_SubPartition = true;
190 else
191 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 -0400192 }
193 }
194 fclose(fp);
195 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400196}
197
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500198void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500199 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
200 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
201 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
202}
203
Matt Mowerbf4efa32014-04-14 23:25:26 -0500204void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
205 if (Part->Has_Android_Secure)
206 Part->Setup_AndSec();
Ethan Yonker6277c792014-09-15 14:54:30 -0500207 else if (!datamedia)
Matt Mowerbf4efa32014-04-14 23:25:26 -0500208 Part->Setup_AndSec();
Matt Mowerbf4efa32014-04-14 23:25:26 -0500209}
210
Dees_Troy8170a922012-09-18 15:40:25 -0400211void TWPartitionManager::Output_Partition_Logging(void) {
212 std::vector<TWPartition*>::iterator iter;
213
214 printf("\n\nPartition Logs:\n");
215 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
216 Output_Partition((*iter));
217}
218
219void TWPartitionManager::Output_Partition(TWPartition* Part) {
220 unsigned long long mb = 1048576;
221
Gary Peck004d48b2012-11-21 16:28:18 -0800222 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 -0400223 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800224 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 -0400225 }
Gary Peck004d48b2012-11-21 16:28:18 -0800226 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500227 if (Part->Can_Be_Mounted)
228 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800229 if (Part->Can_Be_Wiped)
230 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700231 if (Part->Use_Rm_Rf)
232 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500233 if (Part->Can_Be_Backed_Up)
234 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800235 if (Part->Wipe_During_Factory_Reset)
236 printf("Wipe_During_Factory_Reset ");
237 if (Part->Wipe_Available_in_GUI)
238 printf("Wipe_Available_in_GUI ");
239 if (Part->Is_SubPartition)
240 printf("Is_SubPartition ");
241 if (Part->Has_SubPartition)
242 printf("Has_SubPartition ");
243 if (Part->Removable)
244 printf("Removable ");
245 if (Part->Is_Present)
246 printf("IsPresent ");
247 if (Part->Can_Be_Encrypted)
248 printf("Can_Be_Encrypted ");
249 if (Part->Is_Encrypted)
250 printf("Is_Encrypted ");
251 if (Part->Is_Decrypted)
252 printf("Is_Decrypted ");
253 if (Part->Has_Data_Media)
254 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000255 if (Part->Can_Encrypt_Backup)
256 printf("Can_Encrypt_Backup ");
257 if (Part->Use_Userdata_Encryption)
258 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800259 if (Part->Has_Android_Secure)
260 printf("Has_Android_Secure ");
261 if (Part->Is_Storage)
262 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500263 if (Part->Is_Settings_Storage)
264 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000265 if (Part->Ignore_Blkid)
266 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000267 if (Part->Retain_Layout_Version)
268 printf("Retain_Layout_Version ");
Ethan Yonker253368a2014-11-25 15:00:52 -0600269 if (Part->Mount_To_Decrypt)
270 printf("Mount_To_Decrypt ");
Gary Peck004d48b2012-11-21 16:28:18 -0800271 printf("\n");
272 if (!Part->SubPartition_Of.empty())
273 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
274 if (!Part->Symlink_Path.empty())
275 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
276 if (!Part->Symlink_Mount_Point.empty())
277 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
278 if (!Part->Primary_Block_Device.empty())
279 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
280 if (!Part->Alternate_Block_Device.empty())
281 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
282 if (!Part->Decrypted_Block_Device.empty())
283 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
Ethan Yonker253368a2014-11-25 15:00:52 -0600284 if (!Part->Crypto_Key_Location.empty() && Part->Crypto_Key_Location != "footer")
285 printf(" Crypto_Key_Location: %s\n", Part->Crypto_Key_Location.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800286 if (Part->Length != 0)
287 printf(" Length: %i\n", Part->Length);
288 if (!Part->Display_Name.empty())
289 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500290 if (!Part->Storage_Name.empty())
291 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800292 if (!Part->Backup_Path.empty())
293 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
294 if (!Part->Backup_Name.empty())
295 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500296 if (!Part->Backup_Display_Name.empty())
297 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800298 if (!Part->Backup_FileName.empty())
299 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
300 if (!Part->Storage_Path.empty())
301 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
302 if (!Part->Current_File_System.empty())
303 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
304 if (!Part->Fstab_File_System.empty())
305 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
306 if (Part->Format_Block_Size != 0)
307 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400308 if (!Part->MTD_Name.empty())
309 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400310 string back_meth = Part->Backup_Method_By_Name();
Ethan Yonker6277c792014-09-15 14:54:30 -0500311 printf(" Backup_Method: %s\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800312 if (Part->Mount_Flags || !Part->Mount_Options.empty())
313 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -0600314 if (Part->MTP_Storage_ID)
315 printf(" MTP_Storage_ID: %i\n", Part->MTP_Storage_ID);
Ethan Yonker6277c792014-09-15 14:54:30 -0500316 printf("\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400317}
318
Dees_Troy51a0e822012-09-05 15:24:24 -0400319int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400320 std::vector<TWPartition*>::iterator iter;
321 int ret = false;
322 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400323 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400324
Dees_Troyd93bda52013-07-03 19:55:19 +0000325 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400326 return true;
327
Dees_Troy5bf43922012-09-07 16:07:55 -0400328 // Iterate through all partitions
329 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400330 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400331 ret = (*iter)->Mount(Display_Error);
332 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400333 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400334 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400335 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400336 }
337 if (found) {
338 return ret;
339 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000340 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400341 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000342 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400343 }
344 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400345}
346
Dees_Troy51a0e822012-09-05 15:24:24 -0400347int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400348 std::vector<TWPartition*>::iterator iter;
349 int ret = false;
350 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400351 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400352
353 // Iterate through all partitions
354 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400355 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400356 ret = (*iter)->UnMount(Display_Error);
357 found = true;
358 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
359 (*iter)->UnMount(Display_Error);
360 }
361 }
362 if (found) {
363 return ret;
364 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000365 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400366 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000367 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400368 }
369 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400370}
371
Dees_Troy51a0e822012-09-05 15:24:24 -0400372int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400373 TWPartition* Part = Find_Partition_By_Path(Path);
374
375 if (Part)
376 return Part->Is_Mounted();
377 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000378 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400379 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400380}
381
Dees_Troy5bf43922012-09-07 16:07:55 -0400382int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400383 string current_storage_path = DataManager::GetCurrentStoragePath();
384
385 if (Mount_By_Path(current_storage_path, Display_Error)) {
386 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
387 if (FreeStorage)
388 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
389 return true;
390 }
391 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400392}
393
Dees_Troy5bf43922012-09-07 16:07:55 -0400394int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
395 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
396}
397
398TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
399 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400400 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400401
402 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400403 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400404 return (*iter);
405 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400406 return NULL;
407}
408
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400409int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
410 // Check the backup name to ensure that it is the correct size and contains only valid characters
411 // and that a backup with that name doesn't already exist
412 char backup_name[MAX_BACKUP_NAME_LEN];
413 char backup_loc[255], tw_image_dir[255];
414 int copy_size;
415 int index, cur_char;
416 string Backup_Name, Backup_Loc;
417
418 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
419 copy_size = Backup_Name.size();
420 // Check size
421 if (copy_size > MAX_BACKUP_NAME_LEN) {
422 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000423 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400424 return -2;
425 }
426
427 // Check each character
428 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500429 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400430 return 0; // A "0" (zero) means to use the current timestamp for the backup name
431 for (index=0; index<copy_size; index++) {
432 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500433 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 -0400434 // These are valid characters
435 // Numbers
436 // Upper case letters
437 // Lower case letters
438 // Space
439 // and -_.{}[]
440 } else {
441 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000442 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400443 return -3;
444 }
445 }
446
447 // Check to make sure that a backup with this name doesn't already exist
448 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
449 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500450 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500451 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400452 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000453 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400454 return -4;
455 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400456 // No problems found, return 0
457 return 0;
458}
459
Dees_Troy43d8b002012-09-17 16:00:01 -0400460bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
461{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500462 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400463 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500464 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500465 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400466
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500467 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400468 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400469
Dees_Troyb46a6842012-09-25 11:06:46 -0400470 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000471 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400472
473 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500474 md5sum.setfn(Backup_Folder + Backup_Filename);
475 if (md5sum.computeMD5() == 0)
476 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000477 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500478 else
479 return -1;
480 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000481 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400482 } else {
483 char filename[512];
484 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500485 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400486 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500487 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000488 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400489 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000490 if (TWFunc::Path_Exists(filename)) {
491 if (md5sum.computeMD5() == 0) {
492 if (md5sum.write_md5digest() != 0)
493 {
494 gui_print(" * MD5 Error.\n");
495 return false;
496 }
497 } else {
498 gui_print(" * Error computing MD5.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500499 return false;
500 }
501 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400502 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400503 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500504 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400505 }
506 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000507 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400508 return false;
509 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000510 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400511 }
512 return true;
513}
514
Dees_Troy093b7642012-09-21 15:59:38 -0400515bool 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 -0400516 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500517 int img_bps;
518 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400519 unsigned long total_time, remain_time, section_time;
520 int use_compression, backup_time;
521 float pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500522 unsigned long long total_size, current_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400523
524 if (Part == NULL)
525 return true;
526
Dees_Troy093b7642012-09-21 15:59:38 -0400527 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
528
529 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
530 if (use_compression)
531 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
532 else
533 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
534
535 // We know the speed for both, how far into the whole backup are we, based on time
536 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
537 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
538
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500539 //pos = (total_time - remain_time) / (float) total_time;
540 total_size = *file_bytes + *img_bytes;
541 current_size = *file_bytes + *img_bytes - *file_bytes_remaining - *img_bytes_remaining;
542 pos = ((float)(current_size) / (float)(total_size));
Dees_Troy2673cec2013-04-02 20:22:16 +0000543 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400544
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500545 LOGINFO("Estimated total time: %lu\nEstimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400546
547 // And get the time
548 if (Part->Backup_Method == 1)
549 section_time = Part->Backup_Size / file_bps;
550 else
551 section_time = Part->Backup_Size / img_bps;
552
553 // Set the position
554 pos = section_time / (float) total_time;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500555 //DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400556
Tom Hite5a926722014-09-15 01:31:03 +0000557 TWFunc::SetPerformanceMode(true);
Dees_Troy43d8b002012-09-17 16:00:01 -0400558 time(&start);
559
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500560 if (Part->Backup(Backup_Folder, &total_size, &current_size)) {
Tom Hite5a926722014-09-15 01:31:03 +0000561 bool md5Success = false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500562 current_size += Part->Backup_Size;
563 pos = (float)((float)(current_size) / (float)(total_size));
564 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400565 if (Part->Has_SubPartition) {
566 std::vector<TWPartition*>::iterator subpart;
567
568 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500569 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Tom Hite5a926722014-09-15 01:31:03 +0000570 if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size)) {
571 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400572 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000573 }
Dees_Troy2727b992013-08-14 20:09:30 +0000574 sync();
575 sync();
Tom Hite5a926722014-09-15 01:31:03 +0000576 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName)) {
577 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400578 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000579 }
Dees_Troy093b7642012-09-21 15:59:38 -0400580 if (Part->Backup_Method == 1) {
581 *file_bytes_remaining -= (*subpart)->Backup_Size;
582 } else {
583 *img_bytes_remaining -= (*subpart)->Backup_Size;
584 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500585 current_size += Part->Backup_Size;
586 pos = (float)(current_size / total_size);
587 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400588 }
589 }
590 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400591 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400592 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000593 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400594 if (Part->Backup_Method == 1) {
595 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400596 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400597 } else {
598 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400599 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400600 }
Tom Hite5a926722014-09-15 01:31:03 +0000601
602 md5Success = Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
603 TWFunc::SetPerformanceMode(false);
604 return md5Success;
Dees_Troy43d8b002012-09-17 16:00:01 -0400605 } else {
Tom Hite5a926722014-09-15 01:31:03 +0000606 TWFunc::SetPerformanceMode(false);
Dees_Troy43d8b002012-09-17 16:00:01 -0400607 return false;
608 }
609}
610
611int TWPartitionManager::Run_Backup(void) {
612 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500613 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400614 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 -0400615 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500616 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400617 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400618 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400619 struct tm *t;
620 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500621 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400622 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500623 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400624
625 time(&total_start);
626
627 Update_System_Details();
628
629 if (!Mount_Current_Storage(true))
630 return false;
631
632 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400633 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400634 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400635 else
636 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400637
Dees_Troy43d8b002012-09-17 16:00:01 -0400638 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
639 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000640 if (Backup_Name == "(Current Date)") {
641 Backup_Name = TWFunc::Get_Current_Date();
642 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
643 TWFunc::Auto_Generate_Backup_Name();
644 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400645 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000646 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400647 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000648 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400649
Dees_Troy2673cec2013-04-02 20:22:16 +0000650 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500651 DataManager::GetValue("tw_backup_list", Backup_List);
652 if (!Backup_List.empty()) {
653 end_pos = Backup_List.find(";", start_pos);
654 while (end_pos != string::npos && start_pos < Backup_List.size()) {
655 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
656 backup_part = Find_Partition_By_Path(backup_path);
657 if (backup_part != NULL) {
658 partition_count++;
659 if (backup_part->Backup_Method == 1)
660 file_bytes += backup_part->Backup_Size;
661 else
662 img_bytes += backup_part->Backup_Size;
663 if (backup_part->Has_SubPartition) {
664 std::vector<TWPartition*>::iterator subpart;
665
666 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000667 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 -0500668 partition_count++;
669 if ((*subpart)->Backup_Method == 1)
670 file_bytes += (*subpart)->Backup_Size;
671 else
672 img_bytes += (*subpart)->Backup_Size;
673 }
674 }
Dees_Troy8170a922012-09-18 15:40:25 -0400675 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500676 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000677 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400678 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500679 start_pos = end_pos + 1;
680 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400681 }
682 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400683
684 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000685 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400686 return false;
687 }
688 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000689 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500690 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400691 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
692 if (storage != NULL) {
693 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000694 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400695 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000696 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400697 return false;
698 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000699 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400700 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000701 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400702 return false;
703 }
704 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500705 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400706
Dees_Troy2673cec2013-04-02 20:22:16 +0000707 gui_print("\n[BACKUP STARTED]\n");
708 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000709 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000710 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000711 return false;
712 }
713
Dees_Troy2673cec2013-04-02 20:22:16 +0000714 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400715
Dees_Troya13d74f2013-03-24 08:54:55 -0500716 start_pos = 0;
717 end_pos = Backup_List.find(";", start_pos);
718 while (end_pos != string::npos && start_pos < Backup_List.size()) {
719 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
720 backup_part = Find_Partition_By_Path(backup_path);
721 if (backup_part != NULL) {
722 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
723 return false;
724 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000725 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500726 }
727 start_pos = end_pos + 1;
728 end_pos = Backup_List.find(";", start_pos);
729 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400730
731 // Average BPS
732 if (img_time == 0)
733 img_time = 1;
734 if (file_time == 0)
735 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400736 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500737 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400738
Dees_Troy2673cec2013-04-02 20:22:16 +0000739 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
740 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400741
742 time(&total_stop);
743 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500744 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500745 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400746
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500747 int prev_img_bps, use_compression;
748 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400749 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
750 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500751 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400752
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500753 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400754 if (use_compression)
755 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500756 else
Dees_Troy093b7642012-09-21 15:59:38 -0400757 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
758 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500759 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400760
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500761 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400762 if (use_compression)
763 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
764 else
765 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
766
Dees_Troy2673cec2013-04-02 20:22:16 +0000767 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400768 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400769 UnMount_Main_Partitions();
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500770 gui_print_color("highlight", "[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000771 string backup_log = Full_Backup_Path + "recovery.log";
772 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600773 tw_set_default_metadata(backup_log.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000774 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400775}
776
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500777bool 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 -0400778 time_t Start, Stop;
Tom Hite5a926722014-09-15 01:31:03 +0000779 TWFunc::SetPerformanceMode(true);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400780 time(&Start);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500781 //DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Tom Hite5a926722014-09-15 01:31:03 +0000782 if (!Part->Restore(Restore_Name, total_restore_size, already_restored_size)) {
783 TWFunc::SetPerformanceMode(false);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400784 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000785 }
Dees_Troy8170a922012-09-18 15:40:25 -0400786 if (Part->Has_SubPartition) {
787 std::vector<TWPartition*>::iterator subpart;
788
789 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
790 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Tom Hite5a926722014-09-15 01:31:03 +0000791 if (!(*subpart)->Restore(Restore_Name, total_restore_size, already_restored_size)) {
792 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400793 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000794 }
Dees_Troy8170a922012-09-18 15:40:25 -0400795 }
796 }
797 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400798 time(&Stop);
Tom Hite5a926722014-09-15 01:31:03 +0000799 TWFunc::SetPerformanceMode(false);
Dees_Troy2673cec2013-04-02 20:22:16 +0000800 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 -0400801 return true;
802}
803
Dees_Troy51a0e822012-09-05 15:24:24 -0400804int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400805 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500806 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400807 time_t rStart, rStop;
808 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500809 string Restore_List, restore_path;
810 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500811 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400812
Dees_Troy2673cec2013-04-02 20:22:16 +0000813 gui_print("\n[RESTORE STARTED]\n\n");
814 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400815
Dees_Troy4a2a1262012-09-18 09:33:47 -0400816 if (!Mount_Current_Storage(true))
817 return false;
818
819 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500820 if (check_md5 > 0) {
821 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
822 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000823 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500824 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000825 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500826 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500827 gui_print("Calculating restore details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500828 DataManager::GetValue("tw_restore_selected", Restore_List);
829 if (!Restore_List.empty()) {
830 end_pos = Restore_List.find(";", start_pos);
831 while (end_pos != string::npos && start_pos < Restore_List.size()) {
832 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
833 restore_part = Find_Partition_By_Path(restore_path);
834 if (restore_part != NULL) {
835 partition_count++;
836 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
837 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500838 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500839 if (restore_part->Has_SubPartition) {
840 std::vector<TWPartition*>::iterator subpart;
841
842 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
843 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400844 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500845 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500846 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500847 }
848 }
849 }
850 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500851 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500852 }
853 start_pos = end_pos + 1;
854 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400855 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400856 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400857
858 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000859 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400860 return false;
861 }
862
Dees_Troy2673cec2013-04-02 20:22:16 +0000863 gui_print("Restoring %i partitions...\n", partition_count);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500864 gui_print("Total restore size is %lluMB\n", total_restore_size / 1048576);
Dees_Troy2673cec2013-04-02 20:22:16 +0000865 DataManager::SetProgress(0.0);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500866
Dees_Troya13d74f2013-03-24 08:54:55 -0500867 start_pos = 0;
868 if (!Restore_List.empty()) {
869 end_pos = Restore_List.find(";", start_pos);
870 while (end_pos != string::npos && start_pos < Restore_List.size()) {
871 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
872 restore_part = Find_Partition_By_Path(restore_path);
873 if (restore_part != NULL) {
874 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500875 if (!Restore_Partition(restore_part, Restore_Name, partition_count, &total_restore_size, &already_restored_size))
Dees_Troya13d74f2013-03-24 08:54:55 -0500876 return false;
877 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000878 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500879 }
880 start_pos = end_pos + 1;
881 end_pos = Restore_List.find(";", start_pos);
882 }
883 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400884 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400885 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400886 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400887 time(&rStop);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500888 gui_print_color("highlight", "[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500889 DataManager::SetValue("tw_file_progress", "");
Dees_Troy63c8df72012-09-10 14:02:05 -0400890 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400891}
892
893void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400894 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500895 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000896 bool get_date = true, check_encryption = true;
897
898 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400899
900 DIR* d;
901 d = opendir(Restore_Name.c_str());
902 if (d == NULL)
903 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000904 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400905 return;
906 }
907
908 struct dirent* de;
909 while ((de = readdir(d)) != NULL)
910 {
911 // Strip off three components
912 char str[256];
913 char* label;
914 char* fstype = NULL;
915 char* extn = NULL;
916 char* ptr;
917
918 strcpy(str, de->d_name);
919 if (strlen(str) <= 2)
920 continue;
921
922 if (get_date) {
923 char file_path[255];
924 struct stat st;
925
926 strcpy(file_path, Restore_Name.c_str());
927 strcat(file_path, "/");
928 strcat(file_path, str);
929 stat(file_path, &st);
930 string backup_date = ctime((const time_t*)(&st.st_mtime));
931 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
932 get_date = false;
933 }
934
935 label = str;
936 ptr = label;
937 while (*ptr && *ptr != '.') ptr++;
938 if (*ptr == '.')
939 {
940 *ptr = 0x00;
941 ptr++;
942 fstype = ptr;
943 }
944 while (*ptr && *ptr != '.') ptr++;
945 if (*ptr == '.')
946 {
947 *ptr = 0x00;
948 ptr++;
949 extn = ptr;
950 }
951
Dees_Troy83bd4832013-05-04 12:39:56 +0000952 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -0500953 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +0000954 if (extnlength != 3 && extnlength != 6) continue;
955 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
956 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
957
958 if (check_encryption) {
959 string filename = Restore_Name + "/";
960 filename += de->d_name;
961 if (TWFunc::Get_File_Type(filename) == 2) {
962 LOGINFO("'%s' is encrypted\n", filename.c_str());
963 DataManager::SetValue("tw_restore_encrypted", 1);
964 }
965 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500966 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -0400967
968 TWPartition* Part = Find_Partition_By_Path(label);
969 if (Part == NULL)
970 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000971 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -0400972 continue;
973 }
974
975 Part->Backup_FileName = de->d_name;
976 if (strlen(extn) > 3) {
977 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
978 }
979
Dees_Troya13d74f2013-03-24 08:54:55 -0500980 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -0400981 }
982 closedir(d);
983
Dees_Troya13d74f2013-03-24 08:54:55 -0500984 // Set the final value
985 DataManager::SetValue("tw_restore_list", Restore_List);
986 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -0400987 return;
988}
989
990int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400991 std::vector<TWPartition*>::iterator iter;
992 int ret = false;
993 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400994 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -0400995
996 // Iterate through all partitions
997 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400998 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -0400999 if (Path == "/and-sec")
1000 ret = (*iter)->Wipe_AndSec();
1001 else
1002 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001003 found = true;
1004 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1005 (*iter)->Wipe();
1006 }
1007 }
1008 if (found) {
1009 return ret;
1010 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001011 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001012 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001013}
1014
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001015int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1016 std::vector<TWPartition*>::iterator iter;
1017 int ret = false;
1018 bool found = false;
1019 string Local_Path = TWFunc::Get_Root_Path(Path);
1020
1021 // Iterate through all partitions
1022 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1023 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1024 if (Path == "/and-sec")
1025 ret = (*iter)->Wipe_AndSec();
1026 else
1027 ret = (*iter)->Wipe(New_File_System);
1028 found = true;
1029 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1030 (*iter)->Wipe(New_File_System);
1031 }
1032 }
1033 if (found) {
1034 return ret;
1035 } else
1036 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1037 return false;
1038}
1039
Dees_Troy51a0e822012-09-05 15:24:24 -04001040int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001041 std::vector<TWPartition*>::iterator iter;
1042 int ret = true;
1043
1044 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001045 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001046 if (!(*iter)->Wipe())
1047 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001048 } else if ((*iter)->Has_Android_Secure) {
1049 if (!(*iter)->Wipe_AndSec())
1050 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001051 }
1052 }
1053 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001054}
1055
Dees_Troy38bd7602012-09-14 13:33:53 -04001056int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1057 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001058 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001059
1060 if (!Mount_By_Path("/data", true))
1061 return false;
1062
1063 if (!Mount_By_Path("/cache", true))
1064 return false;
1065
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001066 dir.push_back("/data/dalvik-cache");
1067 dir.push_back("/cache/dalvik-cache");
1068 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001069 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001070 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001071 if (stat(dir.at(i).c_str(), &st) == 0) {
1072 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001073 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001074 }
1075 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001076 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001077 if (sdext && sdext->Is_Present && sdext->Mount(false))
1078 {
1079 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1080 {
1081 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001082 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001083 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001084 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001085 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001086 return true;
1087}
1088
1089int TWPartitionManager::Wipe_Rotate_Data(void) {
1090 if (!Mount_By_Path("/data", true))
1091 return false;
1092
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001093 unlink("/data/misc/akmd*");
1094 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001095 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001096 return true;
1097}
1098
1099int TWPartitionManager::Wipe_Battery_Stats(void) {
1100 struct stat st;
1101
1102 if (!Mount_By_Path("/data", true))
1103 return false;
1104
1105 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001106 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001107 } else {
1108 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001109 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001110 }
1111 return true;
1112}
1113
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001114int TWPartitionManager::Wipe_Android_Secure(void) {
1115 std::vector<TWPartition*>::iterator iter;
1116 int ret = false;
1117 bool found = false;
1118
1119 // Iterate through all partitions
1120 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1121 if ((*iter)->Has_Android_Secure) {
1122 ret = (*iter)->Wipe_AndSec();
1123 found = true;
1124 }
1125 }
1126 if (found) {
1127 return ret;
1128 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001129 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001130 }
1131 return false;
1132}
1133
Dees_Troy38bd7602012-09-14 13:33:53 -04001134int TWPartitionManager::Format_Data(void) {
1135 TWPartition* dat = Find_Partition_By_Path("/data");
1136
1137 if (dat != NULL) {
1138 if (!dat->UnMount(true))
1139 return false;
1140
1141 return dat->Wipe_Encryption();
1142 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001143 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001144 return false;
1145 }
1146 return false;
1147}
1148
1149int TWPartitionManager::Wipe_Media_From_Data(void) {
1150 TWPartition* dat = Find_Partition_By_Path("/data");
1151
1152 if (dat != NULL) {
1153 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001154 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001155 return false;
1156 }
1157 if (!dat->Mount(true))
1158 return false;
1159
Dees_Troy2673cec2013-04-02 20:22:16 +00001160 gui_print("Wiping internal storage -- /data/media...\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06001161 Remove_MTP_Storage(dat->MTP_Storage_ID);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001162 TWFunc::removeDir("/data/media", false);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001163 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001164 Add_MTP_Storage(dat->MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001165 return false;
1166 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001167 if (dat->Has_Data_Media) {
1168 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001169 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1170 dat->UnMount(false);
1171 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001172 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001173 Add_MTP_Storage(dat->MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001174 return true;
1175 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001176 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001177 return false;
1178 }
1179 return false;
1180}
1181
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001182int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1183 std::vector<TWPartition*>::iterator iter;
1184 int ret = false;
1185 bool found = false;
1186 string Local_Path = TWFunc::Get_Root_Path(Path);
1187
1188 if (Local_Path == "/tmp" || Local_Path == "/")
1189 return true;
1190
1191 // Iterate through all partitions
1192 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1193 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1194 ret = (*iter)->Repair();
1195 found = true;
1196 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1197 (*iter)->Repair();
1198 }
1199 }
1200 if (found) {
1201 return ret;
1202 } else if (Display_Error) {
1203 LOGERR("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1204 } else {
1205 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1206 }
1207 return false;
1208}
1209
Dees_Troy51a0e822012-09-05 15:24:24 -04001210void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001211 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001212 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001213
Dees_Troy2673cec2013-04-02 20:22:16 +00001214 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001215 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001216 if ((*iter)->Can_Be_Mounted) {
1217 (*iter)->Update_Size(true);
1218 if ((*iter)->Mount_Point == "/system") {
1219 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1220 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1221 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1222 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1223 } else if ((*iter)->Mount_Point == "/cache") {
1224 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1225 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1226 } else if ((*iter)->Mount_Point == "/sd-ext") {
1227 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1228 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1229 if ((*iter)->Backup_Size == 0) {
1230 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1231 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1232 } else
1233 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001234 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001235 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001236 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001237 if ((*iter)->Backup_Size == 0) {
1238 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1239 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1240 } else
1241 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001242 } else if ((*iter)->Mount_Point == "/boot") {
1243 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1244 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1245 if ((*iter)->Backup_Size == 0) {
1246 DataManager::SetValue("tw_has_boot_partition", 0);
1247 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1248 } else
1249 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001250 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001251#ifdef SP1_NAME
1252 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1253 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1254 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1255 }
1256#endif
1257#ifdef SP2_NAME
1258 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1259 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1260 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1261 }
1262#endif
1263#ifdef SP3_NAME
1264 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1265 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1266 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1267 }
1268#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001269 } else {
1270 // Handle unmountable partitions in case we reset defaults
1271 if ((*iter)->Mount_Point == "/boot") {
1272 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1273 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1274 if ((*iter)->Backup_Size == 0) {
1275 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1276 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1277 } else
1278 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1279 } else if ((*iter)->Mount_Point == "/recovery") {
1280 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1281 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1282 if ((*iter)->Backup_Size == 0) {
1283 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1284 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1285 } else
1286 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001287 } else if ((*iter)->Mount_Point == "/data") {
1288 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001289 }
1290#ifdef SP1_NAME
1291 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1292 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1293 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1294 }
1295#endif
1296#ifdef SP2_NAME
1297 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1298 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1299 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1300 }
1301#endif
1302#ifdef SP3_NAME
1303 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1304 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1305 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1306 }
1307#endif
Dees_Troy51127312012-09-08 13:08:49 -04001308 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001309 }
that39b7c162013-11-22 22:07:33 +01001310 gui_print("...done\n");
Dees_Troy51127312012-09-08 13:08:49 -04001311 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1312 string current_storage_path = DataManager::GetCurrentStoragePath();
1313 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001314 if (FreeStorage != NULL) {
1315 // Attempt to mount storage
1316 if (!FreeStorage->Mount(false)) {
1317 // We couldn't mount storage... check to see if we have dual storage
1318 int has_dual_storage;
1319 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1320 if (has_dual_storage == 1) {
1321 // We have dual storage, see if we're using the internal storage that should always be present
1322 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001323 if (!FreeStorage->Is_Encrypted) {
1324 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001325 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001326 }
Dees_Troy8170a922012-09-18 15:40:25 -04001327 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1328 } else {
1329 // We were using external, flip to internal
1330 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1331 current_storage_path = DataManager::GetCurrentStoragePath();
1332 FreeStorage = Find_Partition_By_Path(current_storage_path);
1333 if (FreeStorage != NULL) {
1334 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1335 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001336 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001337 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1338 }
1339 }
1340 } else {
1341 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001342 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001343 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1344 }
1345 } else {
1346 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1347 }
1348 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001349 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001350 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001351 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001352 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001353 return;
1354}
1355
1356int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001357#ifdef TW_INCLUDE_CRYPTO
1358 int ret_val, password_len;
1359 char crypto_blkdev[255], cPassword[255];
1360 size_t result;
Ethan Yonker253368a2014-11-25 15:00:52 -06001361 std::vector<TWPartition*>::iterator iter;
Dees_Troy5bf43922012-09-07 16:07:55 -04001362
1363 property_set("ro.crypto.state", "encrypted");
a39552696ff55ce2013-01-08 16:14:56 +00001364
Ethan Yonker253368a2014-11-25 15:00:52 -06001365 // Mount any partitions that need to be mounted for decrypt
1366 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1367 if ((*iter)->Mount_To_Decrypt) {
1368 (*iter)->Mount(true);
1369 }
a39552696ff55ce2013-01-08 16:14:56 +00001370 }
a39552696ff55ce2013-01-08 16:14:56 +00001371
Dees_Troy5bf43922012-09-07 16:07:55 -04001372 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001373 int pwret = cryptfs_check_passwd(cPassword);
1374
Ethan Yonker253368a2014-11-25 15:00:52 -06001375 // Unmount any partitions that were needed for decrypt
1376 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1377 if ((*iter)->Mount_To_Decrypt) {
1378 (*iter)->UnMount(false);
1379 }
1380 }
1381
a39552696ff55ce2013-01-08 16:14:56 +00001382 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001383 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001384 return -1;
1385 }
a39552696ff55ce2013-01-08 16:14:56 +00001386
Dees_Troy5bf43922012-09-07 16:07:55 -04001387 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1388 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001389 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001390 } else {
1391 TWPartition* dat = Find_Partition_By_Path("/data");
1392 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001393 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001394 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1395 dat->Is_Decrypted = true;
1396 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001397 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001398 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 +00001399 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001400
Dees_Troy5bf43922012-09-07 16:07:55 -04001401 // Sleep for a bit so that the device will be ready
1402 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001403 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001404 dat->Storage_Path = "/data/media/0";
1405 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001406 DataManager::SetValue("tw_storage_path", "/data/media/0");
Ethan Yonkerec0fa4a2014-11-20 09:51:03 -06001407 DataManager::SetValue("tw_settings_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001408 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001409 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001410 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001411 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001412 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001413 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001414 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001415 }
1416 return 0;
1417#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001418 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001419 return -1;
1420#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001421 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001422}
1423
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001424int TWPartitionManager::Fix_Permissions(void) {
1425 int result = 0;
1426 if (!Mount_By_Path("/data", true))
1427 return false;
1428
1429 if (!Mount_By_Path("/system", true))
1430 return false;
1431
1432 Mount_By_Path("/sd-ext", false);
1433
1434 fixPermissions perms;
1435 result = perms.fixPerms(true, false);
thata3d31fb2014-12-21 22:27:40 +01001436#ifdef HAVE_SELINUX
1437 if (result == 0 && DataManager::GetIntValue("tw_fixperms_restorecon") == 1)
1438 result = perms.fixContexts();
1439#endif
Dees_Troy1a650e62012-10-19 20:54:32 -04001440 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001441 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001442 return result;
1443}
1444
Ethan Yonker47360be2014-04-01 10:34:34 -05001445TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1446 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1447
1448 if (!Path.empty()) {
1449 string Search_Path = TWFunc::Get_Root_Path(Path);
1450 for (; iter != Partitions.end(); iter++) {
1451 if ((*iter)->Mount_Point == Search_Path) {
1452 iter++;
1453 break;
1454 }
1455 }
1456 }
1457
1458 for (; iter != Partitions.end(); iter++) {
1459 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) {
1460 return (*iter);
1461 }
1462 }
1463
1464 return NULL;
1465}
1466
Dees_Troyd21618c2012-10-14 18:48:49 -04001467int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001468 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1469
1470 if (Part == NULL) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001471 LOGERR("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
Dees_Troyd21618c2012-10-14 18:48:49 -04001472 return false;
1473 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001474 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1475 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001476 return false;
1477
Dees_Troy2673cec2013-04-02 20:22:16 +00001478 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1479 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001480 return false;
1481 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001482 return true;
1483}
1484
Dees_Troy8170a922012-09-18 15:40:25 -04001485int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001486 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001487 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001488 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001489
Dees_Troy8170a922012-09-18 15:40:25 -04001490 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001491 string Lun_File_str = CUSTOM_LUN_FILE;
1492 size_t found = Lun_File_str.find("%");
1493 if (found != string::npos) {
1494 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1495 if (TWFunc::Path_Exists(lun_file))
1496 has_multiple_lun = true;
1497 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001498 mtp_was_enabled = TWFunc::Toggle_MTP(false); // Must disable MTP for USB Storage
Ethan Yonker47360be2014-04-01 10:34:34 -05001499 if (!has_multiple_lun) {
1500 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1501 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1502 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
1503 TWPartition* Mount = Find_Next_Storage("", "/data");
1504 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001505 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1506 goto error_handle;
1507 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001508 } else {
1509 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001510 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001511 }
1512 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001513 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001514 }
Dees_Troy8170a922012-09-18 15:40:25 -04001515 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001516 LOGINFO("Device has multiple lun files\n");
1517 TWPartition* Mount1;
1518 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001519 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker47360be2014-04-01 10:34:34 -05001520 Mount1 = Find_Next_Storage("", "/data");
1521 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001522 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1523 goto error_handle;
1524 }
Matt Moweree717062014-04-26 01:46:46 -05001525 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker47360be2014-04-01 10:34:34 -05001526 Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data");
1527 if (Mount2) {
Matt Moweree717062014-04-26 01:46:46 -05001528 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001529 }
1530 } else {
1531 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001532 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001533 }
Dees_Troy8170a922012-09-18 15:40:25 -04001534 }
Matt Mowerb6ef4782014-11-06 02:24:36 -06001535 property_set("sys.storage.ums_enabled", "1");
Dees_Troy8170a922012-09-18 15:40:25 -04001536 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001537error_handle:
1538 if (mtp_was_enabled)
1539 if (!Enable_MTP())
1540 Disable_MTP();
1541 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001542}
1543
1544int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001545 int index, ret;
1546 char lun_file[255], ch[2] = {0, 0};
1547 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001548
1549 for (index=0; index<2; index++) {
1550 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001551 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001552 if (ret < 0) {
1553 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001554 }
Dees_Troy8170a922012-09-18 15:40:25 -04001555 }
Dees_Troye58d5262012-09-21 12:27:57 -04001556 Mount_All_Storage();
1557 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001558 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001559 property_set("sys.storage.ums_enabled", "0");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001560 if (mtp_was_enabled)
1561 if (!Enable_MTP())
1562 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001563 if (ret < 0 && index == 0) {
1564 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1565 return false;
1566 } else {
1567 return true;
1568 }
Dees_Troy8170a922012-09-18 15:40:25 -04001569 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001570}
1571
1572void TWPartitionManager::Mount_All_Storage(void) {
1573 std::vector<TWPartition*>::iterator iter;
1574
1575 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1576 if ((*iter)->Is_Storage)
1577 (*iter)->Mount(false);
1578 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001579}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001580
Dees_Troyd0384ef2012-10-12 12:15:42 -04001581void TWPartitionManager::UnMount_Main_Partitions(void) {
1582 // Unmounts system and data if data is not data/media
1583 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001584 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001585
1586 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1587
1588 UnMount_By_Path("/system", true);
Ethan Yonker6277c792014-09-15 14:54:30 -05001589 if (!datamedia)
1590 UnMount_By_Path("/data", true);
1591
Dees_Troyd0384ef2012-10-12 12:15:42 -04001592 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1593 Boot_Partition->UnMount(true);
1594}
1595
Dees_Troy9350b8d2012-09-27 12:38:38 -04001596int TWPartitionManager::Partition_SDCard(void) {
1597 char mkdir_path[255], temp[255], line[512];
1598 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1599 int ext, swap, total_size = 0, fat_size;
1600 FILE* fp;
1601
Dees_Troy2673cec2013-04-02 20:22:16 +00001602 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001603#ifdef TW_EXTERNAL_STORAGE_PATH
1604 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1605#else
1606 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1607#endif
Ethan Yonker1eff6cd2014-09-15 13:30:42 -05001608 if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001609 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001610 return false;
1611 }
1612 if (!SDCard->UnMount(true))
1613 return false;
1614 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1615 if (SDext != NULL) {
1616 if (!SDext->UnMount(true))
1617 return false;
1618 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001619
1620 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001621 Device = SDCard->Actual_Block_Device;
1622 // Just use the root block device
1623 Device.resize(strlen("/dev/block/mmcblkX"));
1624
1625 // Find the size of the block device:
1626 fp = fopen("/proc/partitions", "rt");
1627 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001628 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001629 return false;
1630 }
1631
1632 while (fgets(line, sizeof(line), fp) != NULL)
1633 {
1634 unsigned long major, minor, blocks;
1635 char device[512];
1636 char tmpString[64];
1637
1638 if (strlen(line) < 7 || line[0] == 'm') continue;
1639 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1640
1641 tmpdevice = "/dev/block/";
1642 tmpdevice += device;
1643 if (tmpdevice == Device) {
1644 // Adjust block size to byte size
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001645 total_size = (int)(blocks * 1024ULL / 1000000LLU);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001646 break;
1647 }
1648 }
1649 fclose(fp);
1650
1651 DataManager::GetValue("tw_sdext_size", ext);
1652 DataManager::GetValue("tw_swap_size", swap);
1653 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1654 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001655 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 -04001656 memset(temp, 0, sizeof(temp));
1657 sprintf(temp, "%i", fat_size);
1658 fat_str = temp;
1659 memset(temp, 0, sizeof(temp));
1660 sprintf(temp, "%i", fat_size + ext);
1661 ext_str = temp;
1662 memset(temp, 0, sizeof(temp));
1663 sprintf(temp, "%i", fat_size + ext + swap);
1664 swap_str = temp;
1665 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001666 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001667 return false;
1668 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001669 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001670 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001671 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001672 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001673 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001674 Update_System_Details();
1675 return false;
1676 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001677 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001678 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001679 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001680 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001681 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001682 return false;
1683 }
1684 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001685 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001686 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001687 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001688 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001689 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001690 Update_System_Details();
1691 return false;
1692 }
1693 }
1694 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001695 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001696 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001697 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001698 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001699 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001700 Update_System_Details();
1701 return false;
1702 }
1703 }
1704 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1705#ifdef TW_EXTERNAL_STORAGE_PATH
1706 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1707 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1708 memset(mkdir_path, 0, sizeof(mkdir_path));
1709 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1710#else
1711 Mount_By_Path("/sdcard", 1);
1712 strcpy(mkdir_path, "/sdcard/TWRP");
1713#endif
1714 mkdir(mkdir_path, 0777);
1715 DataManager::Flush();
1716#ifdef TW_EXTERNAL_STORAGE_PATH
1717 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1718 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1719 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1720#else
1721 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1722 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1723 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1724#endif
1725 if (ext > 0) {
1726 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001727 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001728 return false;
1729 }
1730 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001731 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1732 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001733 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001734 }
1735
1736 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00001737 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001738 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001739}
Dees_Troya13d74f2013-03-24 08:54:55 -05001740
1741void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1742 std::vector<TWPartition*>::iterator iter;
1743 if (ListType == "mount") {
1744 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1745 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1746 struct PartitionList part;
1747 part.Display_Name = (*iter)->Display_Name;
1748 part.Mount_Point = (*iter)->Mount_Point;
1749 part.selected = (*iter)->Is_Mounted();
1750 Partition_List->push_back(part);
1751 }
1752 }
1753 } else if (ListType == "storage") {
1754 char free_space[255];
1755 string Current_Storage = DataManager::GetCurrentStoragePath();
1756 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1757 if ((*iter)->Is_Storage) {
1758 struct PartitionList part;
1759 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1760 part.Display_Name = (*iter)->Storage_Name + " (";
1761 part.Display_Name += free_space;
1762 part.Display_Name += "MB)";
1763 part.Mount_Point = (*iter)->Storage_Path;
1764 if ((*iter)->Storage_Path == Current_Storage)
1765 part.selected = 1;
1766 else
1767 part.selected = 0;
1768 Partition_List->push_back(part);
1769 }
1770 }
1771 } else if (ListType == "backup") {
1772 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001773 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001774 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001775 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001776 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001777 Backup_Size = (*iter)->Backup_Size;
1778 if ((*iter)->Has_SubPartition) {
1779 std::vector<TWPartition*>::iterator subpart;
1780
1781 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1782 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1783 Backup_Size += (*subpart)->Backup_Size;
1784 }
1785 }
1786 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001787 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1788 part.Display_Name += backup_size;
1789 part.Display_Name += "MB)";
1790 part.Mount_Point = (*iter)->Backup_Path;
1791 part.selected = 0;
1792 Partition_List->push_back(part);
1793 }
1794 }
1795 } else if (ListType == "restore") {
1796 string Restore_List, restore_path;
1797 TWPartition* restore_part = NULL;
1798
1799 DataManager::GetValue("tw_restore_list", Restore_List);
1800 if (!Restore_List.empty()) {
1801 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1802 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1803 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05001804 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00001805 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001806 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05001807 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05001808 } else {
1809 struct PartitionList part;
1810 part.Display_Name = restore_part->Backup_Display_Name;
1811 part.Mount_Point = restore_part->Backup_Path;
1812 part.selected = 1;
1813 Partition_List->push_back(part);
1814 }
1815 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001816 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001817 }
1818 start_pos = end_pos + 1;
1819 end_pos = Restore_List.find(";", start_pos);
1820 }
1821 }
1822 } else if (ListType == "wipe") {
1823 struct PartitionList dalvik;
1824 dalvik.Display_Name = "Dalvik Cache";
1825 dalvik.Mount_Point = "DALVIK";
1826 dalvik.selected = 0;
1827 Partition_List->push_back(dalvik);
1828 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1829 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1830 struct PartitionList part;
1831 part.Display_Name = (*iter)->Display_Name;
1832 part.Mount_Point = (*iter)->Mount_Point;
1833 part.selected = 0;
1834 Partition_List->push_back(part);
1835 }
1836 if ((*iter)->Has_Android_Secure) {
1837 struct PartitionList part;
1838 part.Display_Name = (*iter)->Backup_Display_Name;
1839 part.Mount_Point = (*iter)->Backup_Path;
1840 part.selected = 0;
1841 Partition_List->push_back(part);
1842 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001843 if ((*iter)->Has_Data_Media) {
1844 struct PartitionList datamedia;
1845 datamedia.Display_Name = (*iter)->Storage_Name;
1846 datamedia.Mount_Point = "INTERNAL";
1847 datamedia.selected = 0;
1848 Partition_List->push_back(datamedia);
1849 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001850 }
1851 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001852 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001853 }
1854}
1855
1856int TWPartitionManager::Fstab_Processed(void) {
1857 return Partitions.size();
1858}
Dees_Troyd93bda52013-07-03 19:55:19 +00001859
1860void TWPartitionManager::Output_Storage_Fstab(void) {
1861 std::vector<TWPartition*>::iterator iter;
1862 char storage_partition[255];
1863 string Temp;
1864 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
1865
1866 if (fp == NULL) {
1867 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
1868 return;
1869 }
1870
1871 // Iterate through all partitions
1872 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1873 if ((*iter)->Is_Storage) {
1874 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
1875 strcpy(storage_partition, Temp.c_str());
1876 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
1877 }
1878 }
1879 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001880}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02001881
1882TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
1883{
1884 TWPartition *res = NULL;
1885 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
1886 if(!(*iter)->Is_Storage)
1887 continue;
1888
1889 if((*iter)->Is_Settings_Storage)
1890 return *iter;
1891
1892 if(!res)
1893 res = *iter;
1894 }
1895 return res;
1896}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001897
1898bool TWPartitionManager::Enable_MTP(void) {
1899#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001900 if (mtppid) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001901 LOGERR("MTP already enabled\n");
1902 return true;
1903 }
1904 //Launch MTP Responder
1905 LOGINFO("Starting MTP\n");
1906 char vendor[PROPERTY_VALUE_MAX];
1907 char product[PROPERTY_VALUE_MAX];
1908 int count = 0;
Ethan Yonker726a0202014-12-16 20:01:38 -06001909
1910 int mtppipe[2];
1911
1912 if (pipe(mtppipe) < 0) {
1913 LOGERR("Error creating MTP pipe\n");
1914 return false;
1915 }
1916
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001917 property_set("sys.usb.config", "none");
1918 property_get("usb.vendor", vendor, "18D1");
1919 property_get("usb.product.mtpadb", product, "4EE2");
1920 string vendorstr = vendor;
1921 string productstr = product;
1922 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
1923 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
1924 property_set("sys.usb.config", "mtp,adb");
Ethan Yonker0bc17c62014-12-10 11:46:24 -06001925 usleep(2000); // Short sleep to prevent an occasional kernel panic on some devices
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001926 std::vector<TWPartition*>::iterator iter;
Ethan Yonker6d154c42014-09-03 14:19:43 -05001927 /* To enable MTP debug, use the twrp command line feature to
1928 * twrp set tw_mtp_debug 1
1929 */
1930 twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug"));
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001931 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1932 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false)) {
bigbiff0c532032014-12-21 13:41:26 -05001933 printf("twrp addStorage %s, mtpstorageid: %u, maxFileSize: %lld\n", (*iter)->Storage_Path.c_str(), (*iter)->MTP_Storage_ID, (*iter)->Get_Max_FileSize());
Ethan Yonker726a0202014-12-16 20:01:38 -06001934 mtp->addStorage((*iter)->Storage_Name, (*iter)->Storage_Path, (*iter)->MTP_Storage_ID, (*iter)->Get_Max_FileSize());
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001935 count++;
1936 }
1937 }
1938 if (count) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001939 mtppid = mtp->forkserver(mtppipe);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001940 if (mtppid) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001941 close(mtppipe[0]); // Host closes read side
1942 mtp_write_fd = mtppipe[1];
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001943 DataManager::SetValue("tw_mtp_enabled", 1);
1944 return true;
1945 } else {
Ethan Yonker726a0202014-12-16 20:01:38 -06001946 close(mtppipe[0]);
1947 close(mtppipe[1]);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001948 LOGERR("Failed to enable MTP\n");
1949 return false;
1950 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001951 } else {
1952 close(mtppipe[0]);
1953 close(mtppipe[1]);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001954 }
1955 LOGERR("No valid storage partitions found for MTP.\n");
1956#else
1957 LOGERR("MTP support not included\n");
1958#endif
1959 DataManager::SetValue("tw_mtp_enabled", 0);
1960 return false;
1961}
1962
1963bool TWPartitionManager::Disable_MTP(void) {
1964#ifdef TW_HAS_MTP
1965 char vendor[PROPERTY_VALUE_MAX];
1966 char product[PROPERTY_VALUE_MAX];
1967 property_set("sys.usb.config", "none");
1968 property_get("usb.vendor", vendor, "18D1");
1969 property_get("usb.product.adb", product, "D002");
1970 string vendorstr = vendor;
1971 string productstr = product;
1972 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
1973 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
bigbiffd5c60692014-12-11 18:48:08 -05001974 usleep(2000);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001975 if (mtppid) {
Ethan Yonker8613dc02014-09-11 09:28:20 -05001976 LOGINFO("Disabling MTP\n");
1977 int status;
1978 kill(mtppid, SIGKILL);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001979 mtppid = 0;
Ethan Yonker8613dc02014-09-11 09:28:20 -05001980 // We don't care about the exit value, but this prevents a zombie process
1981 waitpid(mtppid, &status, 0);
Ethan Yonker726a0202014-12-16 20:01:38 -06001982 close(mtp_write_fd);
1983 mtp_write_fd = -1;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001984 }
1985 property_set("sys.usb.config", "adb");
1986 DataManager::SetValue("tw_mtp_enabled", 0);
1987 return true;
1988#else
1989 LOGERR("MTP support not included\n");
1990 DataManager::SetValue("tw_mtp_enabled", 0);
1991 return false;
1992#endif
1993}
Ethan Yonker726a0202014-12-16 20:01:38 -06001994
1995TWPartition* TWPartitionManager::Find_Partition_By_MTP_Storage_ID(unsigned int Storage_ID) {
1996 std::vector<TWPartition*>::iterator iter;
1997
1998 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1999 if ((*iter)->MTP_Storage_ID == Storage_ID)
2000 return (*iter);
2001 }
2002 return NULL;
2003}
2004
2005bool TWPartitionManager::Add_Remove_MTP_Storage(TWPartition* Part, int message_type) {
2006#ifdef TW_HAS_MTP
2007 struct mtpmsg mtp_message;
2008
2009 if (!mtppid)
2010 return false; // MTP is disabled
2011
2012 if (mtp_write_fd < 0) {
2013 LOGERR("MTP: mtp_write_fd is not set\n");
2014 return false;
2015 }
2016
2017 if (Part) {
2018 if (message_type == MTP_MESSAGE_REMOVE_STORAGE) {
2019 mtp_message.message_type = MTP_MESSAGE_REMOVE_STORAGE; // Remove
2020 LOGINFO("sending message to remove %i\n", Part->MTP_Storage_ID);
2021 mtp_message.storage_id = Part->MTP_Storage_ID;
2022 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
2023 LOGERR("error sending message to remove storage %i\n", Part->MTP_Storage_ID);
2024 return false;
2025 } else {
2026 LOGINFO("Message sent, remove storage ID: %i\n", Part->MTP_Storage_ID);
2027 return true;
2028 }
2029 } else if (message_type == MTP_MESSAGE_ADD_STORAGE && Part->Is_Mounted()) {
2030 mtp_message.message_type = MTP_MESSAGE_ADD_STORAGE; // Add
2031 mtp_message.storage_id = Part->MTP_Storage_ID;
2032 mtp_message.path = Part->Storage_Path.c_str();
2033 mtp_message.display = Part->Storage_Name.c_str();
2034 mtp_message.maxFileSize = Part->Get_Max_FileSize();
2035 LOGINFO("sending message to add %i '%s'\n", Part->MTP_Storage_ID, mtp_message.path);
2036 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
2037 LOGERR("error sending message to add storage %i\n", Part->MTP_Storage_ID);
2038 return false;
2039 } else {
2040 LOGINFO("Message sent, add storage ID: %i\n", Part->MTP_Storage_ID);
2041 return true;
2042 }
2043 } else {
2044 LOGERR("Unknown MTP message type: %i\n", message_type);
2045 }
2046 } else {
2047 // This hopefully never happens as the error handling should
2048 // occur in the calling function.
2049 LOGERR("TWPartitionManager::Add_Remove_MTP_Storage NULL partition given\n");
2050 }
2051 return true;
2052#else
2053 LOGERR("MTP support not included\n");
2054 DataManager::SetValue("tw_mtp_enabled", 0);
2055 return false;
2056#endif
2057}
2058
2059bool TWPartitionManager::Add_MTP_Storage(string Mount_Point) {
2060#ifdef TW_HAS_MTP
2061 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2062 if (Part) {
2063 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2064 } else {
2065 LOGERR("TWFunc::Add_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
2066 }
2067#endif
2068 return false;
2069}
2070
2071bool TWPartitionManager::Add_MTP_Storage(unsigned int Storage_ID) {
2072#ifdef TW_HAS_MTP
2073 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2074 if (Part) {
2075 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2076 } else {
2077 LOGERR("TWFunc::Add_MTP_Storage unable to locate partition for %i\n", Storage_ID);
2078 }
2079#endif
2080 return false;
2081}
2082
2083bool TWPartitionManager::Remove_MTP_Storage(string Mount_Point) {
2084#ifdef TW_HAS_MTP
2085 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2086 if (Part) {
2087 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2088 } else {
2089 LOGERR("TWFunc::Remove_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
2090 }
2091#endif
2092 return false;
2093}
2094
2095bool TWPartitionManager::Remove_MTP_Storage(unsigned int Storage_ID) {
2096#ifdef TW_HAS_MTP
2097 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2098 if (Part) {
2099 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2100 } else {
2101 LOGERR("TWFunc::Remove_MTP_Storage unable to locate partition for %i\n", Storage_ID);
2102 }
2103#endif
2104 return false;
2105}