blob: 09ae5d2bea94bf6c1535a1da17e7fa9b6872bc72 [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 ");
Ethan Yonker96af84a2015-01-05 14:58:36 -0600271 if (Part->Can_Flash_Img)
272 printf("Can_Flash_Img ");
Gary Peck004d48b2012-11-21 16:28:18 -0800273 printf("\n");
274 if (!Part->SubPartition_Of.empty())
275 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
276 if (!Part->Symlink_Path.empty())
277 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
278 if (!Part->Symlink_Mount_Point.empty())
279 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
280 if (!Part->Primary_Block_Device.empty())
281 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
282 if (!Part->Alternate_Block_Device.empty())
283 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
284 if (!Part->Decrypted_Block_Device.empty())
285 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
Ethan Yonker253368a2014-11-25 15:00:52 -0600286 if (!Part->Crypto_Key_Location.empty() && Part->Crypto_Key_Location != "footer")
287 printf(" Crypto_Key_Location: %s\n", Part->Crypto_Key_Location.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800288 if (Part->Length != 0)
289 printf(" Length: %i\n", Part->Length);
290 if (!Part->Display_Name.empty())
291 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500292 if (!Part->Storage_Name.empty())
293 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800294 if (!Part->Backup_Path.empty())
295 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
296 if (!Part->Backup_Name.empty())
297 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500298 if (!Part->Backup_Display_Name.empty())
299 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800300 if (!Part->Backup_FileName.empty())
301 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
302 if (!Part->Storage_Path.empty())
303 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
304 if (!Part->Current_File_System.empty())
305 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
306 if (!Part->Fstab_File_System.empty())
307 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
308 if (Part->Format_Block_Size != 0)
309 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400310 if (!Part->MTD_Name.empty())
311 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400312 string back_meth = Part->Backup_Method_By_Name();
Ethan Yonker6277c792014-09-15 14:54:30 -0500313 printf(" Backup_Method: %s\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800314 if (Part->Mount_Flags || !Part->Mount_Options.empty())
315 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -0600316 if (Part->MTP_Storage_ID)
317 printf(" MTP_Storage_ID: %i\n", Part->MTP_Storage_ID);
Ethan Yonker6277c792014-09-15 14:54:30 -0500318 printf("\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400319}
320
Dees_Troy51a0e822012-09-05 15:24:24 -0400321int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400322 std::vector<TWPartition*>::iterator iter;
323 int ret = false;
324 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400325 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400326
Dees_Troyd93bda52013-07-03 19:55:19 +0000327 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400328 return true;
329
Dees_Troy5bf43922012-09-07 16:07:55 -0400330 // Iterate through all partitions
331 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400332 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400333 ret = (*iter)->Mount(Display_Error);
334 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400335 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400336 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400337 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400338 }
339 if (found) {
340 return ret;
341 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000342 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400343 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000344 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400345 }
346 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400347}
348
Dees_Troy51a0e822012-09-05 15:24:24 -0400349int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400350 std::vector<TWPartition*>::iterator iter;
351 int ret = false;
352 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400353 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400354
355 // Iterate through all partitions
356 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400357 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400358 ret = (*iter)->UnMount(Display_Error);
359 found = true;
360 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
361 (*iter)->UnMount(Display_Error);
362 }
363 }
364 if (found) {
365 return ret;
366 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000367 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400368 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000369 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400370 }
371 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400372}
373
Dees_Troy51a0e822012-09-05 15:24:24 -0400374int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400375 TWPartition* Part = Find_Partition_By_Path(Path);
376
377 if (Part)
378 return Part->Is_Mounted();
379 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000380 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400381 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400382}
383
Dees_Troy5bf43922012-09-07 16:07:55 -0400384int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400385 string current_storage_path = DataManager::GetCurrentStoragePath();
386
387 if (Mount_By_Path(current_storage_path, Display_Error)) {
388 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
389 if (FreeStorage)
390 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
391 return true;
392 }
393 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400394}
395
Dees_Troy5bf43922012-09-07 16:07:55 -0400396int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
397 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
398}
399
400TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
401 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400402 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400403
404 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400405 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400406 return (*iter);
407 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400408 return NULL;
409}
410
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400411int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
412 // Check the backup name to ensure that it is the correct size and contains only valid characters
413 // and that a backup with that name doesn't already exist
414 char backup_name[MAX_BACKUP_NAME_LEN];
415 char backup_loc[255], tw_image_dir[255];
416 int copy_size;
417 int index, cur_char;
418 string Backup_Name, Backup_Loc;
419
420 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
421 copy_size = Backup_Name.size();
422 // Check size
423 if (copy_size > MAX_BACKUP_NAME_LEN) {
424 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000425 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400426 return -2;
427 }
428
429 // Check each character
430 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500431 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400432 return 0; // A "0" (zero) means to use the current timestamp for the backup name
433 for (index=0; index<copy_size; index++) {
434 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500435 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 -0400436 // These are valid characters
437 // Numbers
438 // Upper case letters
439 // Lower case letters
440 // Space
441 // and -_.{}[]
442 } else {
443 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000444 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400445 return -3;
446 }
447 }
448
449 // Check to make sure that a backup with this name doesn't already exist
450 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
451 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500452 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500453 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400454 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000455 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400456 return -4;
457 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400458 // No problems found, return 0
459 return 0;
460}
461
Dees_Troy43d8b002012-09-17 16:00:01 -0400462bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
463{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500464 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400465 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500466 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500467 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400468
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500469 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400470 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400471
Dees_Troyb46a6842012-09-25 11:06:46 -0400472 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000473 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400474
475 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500476 md5sum.setfn(Backup_Folder + Backup_Filename);
477 if (md5sum.computeMD5() == 0)
478 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000479 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500480 else
481 return -1;
482 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000483 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400484 } else {
485 char filename[512];
486 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500487 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400488 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500489 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000490 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400491 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000492 if (TWFunc::Path_Exists(filename)) {
493 if (md5sum.computeMD5() == 0) {
494 if (md5sum.write_md5digest() != 0)
495 {
496 gui_print(" * MD5 Error.\n");
497 return false;
498 }
499 } else {
500 gui_print(" * Error computing MD5.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500501 return false;
502 }
503 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400504 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400505 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500506 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400507 }
508 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000509 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400510 return false;
511 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000512 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400513 }
514 return true;
515}
516
Dees_Troy093b7642012-09-21 15:59:38 -0400517bool 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 -0400518 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500519 int img_bps;
520 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400521 unsigned long total_time, remain_time, section_time;
522 int use_compression, backup_time;
523 float pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500524 unsigned long long total_size, current_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400525
526 if (Part == NULL)
527 return true;
528
Dees_Troy093b7642012-09-21 15:59:38 -0400529 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
530
531 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
532 if (use_compression)
533 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
534 else
535 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
536
537 // We know the speed for both, how far into the whole backup are we, based on time
538 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
539 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
540
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500541 //pos = (total_time - remain_time) / (float) total_time;
542 total_size = *file_bytes + *img_bytes;
543 current_size = *file_bytes + *img_bytes - *file_bytes_remaining - *img_bytes_remaining;
544 pos = ((float)(current_size) / (float)(total_size));
Dees_Troy2673cec2013-04-02 20:22:16 +0000545 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400546
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500547 LOGINFO("Estimated total time: %lu\nEstimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400548
549 // And get the time
550 if (Part->Backup_Method == 1)
551 section_time = Part->Backup_Size / file_bps;
552 else
553 section_time = Part->Backup_Size / img_bps;
554
555 // Set the position
556 pos = section_time / (float) total_time;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500557 //DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400558
Tom Hite5a926722014-09-15 01:31:03 +0000559 TWFunc::SetPerformanceMode(true);
Dees_Troy43d8b002012-09-17 16:00:01 -0400560 time(&start);
561
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500562 if (Part->Backup(Backup_Folder, &total_size, &current_size)) {
Tom Hite5a926722014-09-15 01:31:03 +0000563 bool md5Success = false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500564 current_size += Part->Backup_Size;
565 pos = (float)((float)(current_size) / (float)(total_size));
566 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400567 if (Part->Has_SubPartition) {
568 std::vector<TWPartition*>::iterator subpart;
569
570 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500571 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Tom Hite5a926722014-09-15 01:31:03 +0000572 if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size)) {
573 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400574 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000575 }
Dees_Troy2727b992013-08-14 20:09:30 +0000576 sync();
577 sync();
Tom Hite5a926722014-09-15 01:31:03 +0000578 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName)) {
579 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400580 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000581 }
Dees_Troy093b7642012-09-21 15:59:38 -0400582 if (Part->Backup_Method == 1) {
583 *file_bytes_remaining -= (*subpart)->Backup_Size;
584 } else {
585 *img_bytes_remaining -= (*subpart)->Backup_Size;
586 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500587 current_size += Part->Backup_Size;
588 pos = (float)(current_size / total_size);
589 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400590 }
591 }
592 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400593 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400594 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000595 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400596 if (Part->Backup_Method == 1) {
597 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400598 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400599 } else {
600 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400601 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400602 }
Tom Hite5a926722014-09-15 01:31:03 +0000603
604 md5Success = Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
605 TWFunc::SetPerformanceMode(false);
606 return md5Success;
Dees_Troy43d8b002012-09-17 16:00:01 -0400607 } else {
Tom Hite5a926722014-09-15 01:31:03 +0000608 TWFunc::SetPerformanceMode(false);
Dees_Troy43d8b002012-09-17 16:00:01 -0400609 return false;
610 }
611}
612
613int TWPartitionManager::Run_Backup(void) {
614 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500615 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400616 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 -0400617 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500618 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400619 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400620 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400621 struct tm *t;
622 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500623 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400624 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500625 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400626
627 time(&total_start);
628
629 Update_System_Details();
630
631 if (!Mount_Current_Storage(true))
632 return false;
633
634 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400635 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400636 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400637 else
638 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400639
Dees_Troy43d8b002012-09-17 16:00:01 -0400640 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
641 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000642 if (Backup_Name == "(Current Date)") {
643 Backup_Name = TWFunc::Get_Current_Date();
644 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
645 TWFunc::Auto_Generate_Backup_Name();
646 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400647 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000648 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400649 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000650 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400651
Dees_Troy2673cec2013-04-02 20:22:16 +0000652 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500653 DataManager::GetValue("tw_backup_list", Backup_List);
654 if (!Backup_List.empty()) {
655 end_pos = Backup_List.find(";", start_pos);
656 while (end_pos != string::npos && start_pos < Backup_List.size()) {
657 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
658 backup_part = Find_Partition_By_Path(backup_path);
659 if (backup_part != NULL) {
660 partition_count++;
661 if (backup_part->Backup_Method == 1)
662 file_bytes += backup_part->Backup_Size;
663 else
664 img_bytes += backup_part->Backup_Size;
665 if (backup_part->Has_SubPartition) {
666 std::vector<TWPartition*>::iterator subpart;
667
668 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000669 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 -0500670 partition_count++;
671 if ((*subpart)->Backup_Method == 1)
672 file_bytes += (*subpart)->Backup_Size;
673 else
674 img_bytes += (*subpart)->Backup_Size;
675 }
676 }
Dees_Troy8170a922012-09-18 15:40:25 -0400677 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500678 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000679 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400680 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500681 start_pos = end_pos + 1;
682 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400683 }
684 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400685
686 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000687 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400688 return false;
689 }
690 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000691 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500692 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400693 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
694 if (storage != NULL) {
695 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000696 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400697 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000698 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400699 return false;
700 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000701 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400702 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000703 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400704 return false;
705 }
706 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500707 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400708
Dees_Troy2673cec2013-04-02 20:22:16 +0000709 gui_print("\n[BACKUP STARTED]\n");
710 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000711 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000712 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000713 return false;
714 }
715
Dees_Troy2673cec2013-04-02 20:22:16 +0000716 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400717
Dees_Troya13d74f2013-03-24 08:54:55 -0500718 start_pos = 0;
719 end_pos = Backup_List.find(";", start_pos);
720 while (end_pos != string::npos && start_pos < Backup_List.size()) {
721 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
722 backup_part = Find_Partition_By_Path(backup_path);
723 if (backup_part != NULL) {
724 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
725 return false;
726 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000727 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500728 }
729 start_pos = end_pos + 1;
730 end_pos = Backup_List.find(";", start_pos);
731 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400732
733 // Average BPS
734 if (img_time == 0)
735 img_time = 1;
736 if (file_time == 0)
737 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400738 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500739 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400740
Dees_Troy2673cec2013-04-02 20:22:16 +0000741 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
742 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400743
744 time(&total_stop);
745 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500746 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500747 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400748
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500749 int prev_img_bps, use_compression;
750 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400751 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
752 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500753 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400754
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500755 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400756 if (use_compression)
757 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500758 else
Dees_Troy093b7642012-09-21 15:59:38 -0400759 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
760 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500761 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400762
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500763 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400764 if (use_compression)
765 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
766 else
767 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
768
Dees_Troy2673cec2013-04-02 20:22:16 +0000769 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400770 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400771 UnMount_Main_Partitions();
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500772 gui_print_color("highlight", "[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000773 string backup_log = Full_Backup_Path + "recovery.log";
774 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600775 tw_set_default_metadata(backup_log.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000776 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400777}
778
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500779bool 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 -0400780 time_t Start, Stop;
Tom Hite5a926722014-09-15 01:31:03 +0000781 TWFunc::SetPerformanceMode(true);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400782 time(&Start);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500783 //DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Tom Hite5a926722014-09-15 01:31:03 +0000784 if (!Part->Restore(Restore_Name, total_restore_size, already_restored_size)) {
785 TWFunc::SetPerformanceMode(false);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400786 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000787 }
Dees_Troy8170a922012-09-18 15:40:25 -0400788 if (Part->Has_SubPartition) {
789 std::vector<TWPartition*>::iterator subpart;
790
791 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
792 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Tom Hite5a926722014-09-15 01:31:03 +0000793 if (!(*subpart)->Restore(Restore_Name, total_restore_size, already_restored_size)) {
794 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400795 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000796 }
Dees_Troy8170a922012-09-18 15:40:25 -0400797 }
798 }
799 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400800 time(&Stop);
Tom Hite5a926722014-09-15 01:31:03 +0000801 TWFunc::SetPerformanceMode(false);
Dees_Troy2673cec2013-04-02 20:22:16 +0000802 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 -0400803 return true;
804}
805
Dees_Troy51a0e822012-09-05 15:24:24 -0400806int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400807 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500808 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400809 time_t rStart, rStop;
810 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500811 string Restore_List, restore_path;
812 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500813 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400814
Dees_Troy2673cec2013-04-02 20:22:16 +0000815 gui_print("\n[RESTORE STARTED]\n\n");
816 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400817
Dees_Troy4a2a1262012-09-18 09:33:47 -0400818 if (!Mount_Current_Storage(true))
819 return false;
820
821 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500822 if (check_md5 > 0) {
823 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
824 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000825 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500826 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000827 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500828 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500829 gui_print("Calculating restore details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500830 DataManager::GetValue("tw_restore_selected", Restore_List);
831 if (!Restore_List.empty()) {
832 end_pos = Restore_List.find(";", start_pos);
833 while (end_pos != string::npos && start_pos < Restore_List.size()) {
834 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
835 restore_part = Find_Partition_By_Path(restore_path);
836 if (restore_part != NULL) {
837 partition_count++;
838 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
839 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500840 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500841 if (restore_part->Has_SubPartition) {
842 std::vector<TWPartition*>::iterator subpart;
843
844 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
845 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400846 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500847 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500848 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500849 }
850 }
851 }
852 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500853 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500854 }
855 start_pos = end_pos + 1;
856 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400857 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400858 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400859
860 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000861 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400862 return false;
863 }
864
Dees_Troy2673cec2013-04-02 20:22:16 +0000865 gui_print("Restoring %i partitions...\n", partition_count);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500866 gui_print("Total restore size is %lluMB\n", total_restore_size / 1048576);
Dees_Troy2673cec2013-04-02 20:22:16 +0000867 DataManager::SetProgress(0.0);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500868
Dees_Troya13d74f2013-03-24 08:54:55 -0500869 start_pos = 0;
870 if (!Restore_List.empty()) {
871 end_pos = Restore_List.find(";", start_pos);
872 while (end_pos != string::npos && start_pos < Restore_List.size()) {
873 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
874 restore_part = Find_Partition_By_Path(restore_path);
875 if (restore_part != NULL) {
876 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500877 if (!Restore_Partition(restore_part, Restore_Name, partition_count, &total_restore_size, &already_restored_size))
Dees_Troya13d74f2013-03-24 08:54:55 -0500878 return false;
879 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000880 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500881 }
882 start_pos = end_pos + 1;
883 end_pos = Restore_List.find(";", start_pos);
884 }
885 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400886 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400887 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400888 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400889 time(&rStop);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500890 gui_print_color("highlight", "[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500891 DataManager::SetValue("tw_file_progress", "");
Dees_Troy63c8df72012-09-10 14:02:05 -0400892 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400893}
894
895void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400896 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500897 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000898 bool get_date = true, check_encryption = true;
899
900 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400901
902 DIR* d;
903 d = opendir(Restore_Name.c_str());
904 if (d == NULL)
905 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000906 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400907 return;
908 }
909
910 struct dirent* de;
911 while ((de = readdir(d)) != NULL)
912 {
913 // Strip off three components
914 char str[256];
915 char* label;
916 char* fstype = NULL;
917 char* extn = NULL;
918 char* ptr;
919
920 strcpy(str, de->d_name);
921 if (strlen(str) <= 2)
922 continue;
923
924 if (get_date) {
925 char file_path[255];
926 struct stat st;
927
928 strcpy(file_path, Restore_Name.c_str());
929 strcat(file_path, "/");
930 strcat(file_path, str);
931 stat(file_path, &st);
932 string backup_date = ctime((const time_t*)(&st.st_mtime));
933 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
934 get_date = false;
935 }
936
937 label = str;
938 ptr = label;
939 while (*ptr && *ptr != '.') ptr++;
940 if (*ptr == '.')
941 {
942 *ptr = 0x00;
943 ptr++;
944 fstype = ptr;
945 }
946 while (*ptr && *ptr != '.') ptr++;
947 if (*ptr == '.')
948 {
949 *ptr = 0x00;
950 ptr++;
951 extn = ptr;
952 }
953
Dees_Troy83bd4832013-05-04 12:39:56 +0000954 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -0500955 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +0000956 if (extnlength != 3 && extnlength != 6) continue;
957 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
958 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
959
960 if (check_encryption) {
961 string filename = Restore_Name + "/";
962 filename += de->d_name;
963 if (TWFunc::Get_File_Type(filename) == 2) {
964 LOGINFO("'%s' is encrypted\n", filename.c_str());
965 DataManager::SetValue("tw_restore_encrypted", 1);
966 }
967 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500968 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -0400969
970 TWPartition* Part = Find_Partition_By_Path(label);
971 if (Part == NULL)
972 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000973 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -0400974 continue;
975 }
976
977 Part->Backup_FileName = de->d_name;
978 if (strlen(extn) > 3) {
979 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
980 }
981
Dees_Troya13d74f2013-03-24 08:54:55 -0500982 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -0400983 }
984 closedir(d);
985
Dees_Troya13d74f2013-03-24 08:54:55 -0500986 // Set the final value
987 DataManager::SetValue("tw_restore_list", Restore_List);
988 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -0400989 return;
990}
991
992int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400993 std::vector<TWPartition*>::iterator iter;
994 int ret = false;
995 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400996 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -0400997
998 // Iterate through all partitions
999 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001000 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001001 if (Path == "/and-sec")
1002 ret = (*iter)->Wipe_AndSec();
1003 else
1004 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001005 found = true;
1006 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1007 (*iter)->Wipe();
1008 }
1009 }
1010 if (found) {
1011 return ret;
1012 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001013 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001014 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001015}
1016
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001017int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1018 std::vector<TWPartition*>::iterator iter;
1019 int ret = false;
1020 bool found = false;
1021 string Local_Path = TWFunc::Get_Root_Path(Path);
1022
1023 // Iterate through all partitions
1024 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1025 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1026 if (Path == "/and-sec")
1027 ret = (*iter)->Wipe_AndSec();
1028 else
1029 ret = (*iter)->Wipe(New_File_System);
1030 found = true;
1031 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1032 (*iter)->Wipe(New_File_System);
1033 }
1034 }
1035 if (found) {
1036 return ret;
1037 } else
1038 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1039 return false;
1040}
1041
Dees_Troy51a0e822012-09-05 15:24:24 -04001042int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001043 std::vector<TWPartition*>::iterator iter;
1044 int ret = true;
1045
1046 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001047 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001048 if (!(*iter)->Wipe())
1049 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001050 } else if ((*iter)->Has_Android_Secure) {
1051 if (!(*iter)->Wipe_AndSec())
1052 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001053 }
1054 }
1055 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001056}
1057
Dees_Troy38bd7602012-09-14 13:33:53 -04001058int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1059 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001060 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001061
1062 if (!Mount_By_Path("/data", true))
1063 return false;
1064
1065 if (!Mount_By_Path("/cache", true))
1066 return false;
1067
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001068 dir.push_back("/data/dalvik-cache");
1069 dir.push_back("/cache/dalvik-cache");
1070 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001071 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001072 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001073 if (stat(dir.at(i).c_str(), &st) == 0) {
1074 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001075 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001076 }
1077 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001078 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001079 if (sdext && sdext->Is_Present && sdext->Mount(false))
1080 {
1081 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1082 {
1083 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001084 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001085 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001086 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001087 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001088 return true;
1089}
1090
1091int TWPartitionManager::Wipe_Rotate_Data(void) {
1092 if (!Mount_By_Path("/data", true))
1093 return false;
1094
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001095 unlink("/data/misc/akmd*");
1096 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001097 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001098 return true;
1099}
1100
1101int TWPartitionManager::Wipe_Battery_Stats(void) {
1102 struct stat st;
1103
1104 if (!Mount_By_Path("/data", true))
1105 return false;
1106
1107 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001108 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001109 } else {
1110 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001111 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001112 }
1113 return true;
1114}
1115
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001116int TWPartitionManager::Wipe_Android_Secure(void) {
1117 std::vector<TWPartition*>::iterator iter;
1118 int ret = false;
1119 bool found = false;
1120
1121 // Iterate through all partitions
1122 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1123 if ((*iter)->Has_Android_Secure) {
1124 ret = (*iter)->Wipe_AndSec();
1125 found = true;
1126 }
1127 }
1128 if (found) {
1129 return ret;
1130 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001131 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001132 }
1133 return false;
1134}
1135
Dees_Troy38bd7602012-09-14 13:33:53 -04001136int TWPartitionManager::Format_Data(void) {
1137 TWPartition* dat = Find_Partition_By_Path("/data");
1138
1139 if (dat != NULL) {
1140 if (!dat->UnMount(true))
1141 return false;
1142
1143 return dat->Wipe_Encryption();
1144 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001145 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001146 return false;
1147 }
1148 return false;
1149}
1150
1151int TWPartitionManager::Wipe_Media_From_Data(void) {
1152 TWPartition* dat = Find_Partition_By_Path("/data");
1153
1154 if (dat != NULL) {
1155 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001156 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001157 return false;
1158 }
1159 if (!dat->Mount(true))
1160 return false;
1161
Dees_Troy2673cec2013-04-02 20:22:16 +00001162 gui_print("Wiping internal storage -- /data/media...\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06001163 Remove_MTP_Storage(dat->MTP_Storage_ID);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001164 TWFunc::removeDir("/data/media", false);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001165 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001166 Add_MTP_Storage(dat->MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001167 return false;
1168 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001169 if (dat->Has_Data_Media) {
1170 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001171 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1172 dat->UnMount(false);
1173 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001174 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001175 Add_MTP_Storage(dat->MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001176 return true;
1177 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001178 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001179 return false;
1180 }
1181 return false;
1182}
1183
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001184int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1185 std::vector<TWPartition*>::iterator iter;
1186 int ret = false;
1187 bool found = false;
1188 string Local_Path = TWFunc::Get_Root_Path(Path);
1189
1190 if (Local_Path == "/tmp" || Local_Path == "/")
1191 return true;
1192
1193 // Iterate through all partitions
1194 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1195 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1196 ret = (*iter)->Repair();
1197 found = true;
1198 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1199 (*iter)->Repair();
1200 }
1201 }
1202 if (found) {
1203 return ret;
1204 } else if (Display_Error) {
1205 LOGERR("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1206 } else {
1207 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1208 }
1209 return false;
1210}
1211
Dees_Troy51a0e822012-09-05 15:24:24 -04001212void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001213 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001214 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001215
Dees_Troy2673cec2013-04-02 20:22:16 +00001216 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001217 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001218 if ((*iter)->Can_Be_Mounted) {
1219 (*iter)->Update_Size(true);
1220 if ((*iter)->Mount_Point == "/system") {
1221 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1222 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1223 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1224 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1225 } else if ((*iter)->Mount_Point == "/cache") {
1226 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1227 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1228 } else if ((*iter)->Mount_Point == "/sd-ext") {
1229 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1230 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1231 if ((*iter)->Backup_Size == 0) {
1232 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1233 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1234 } else
1235 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001236 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001237 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001238 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001239 if ((*iter)->Backup_Size == 0) {
1240 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1241 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1242 } else
1243 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001244 } else if ((*iter)->Mount_Point == "/boot") {
1245 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1246 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1247 if ((*iter)->Backup_Size == 0) {
1248 DataManager::SetValue("tw_has_boot_partition", 0);
1249 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1250 } else
1251 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001252 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001253#ifdef SP1_NAME
1254 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1255 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1256 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1257 }
1258#endif
1259#ifdef SP2_NAME
1260 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1261 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1262 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1263 }
1264#endif
1265#ifdef SP3_NAME
1266 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1267 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1268 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1269 }
1270#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001271 } else {
1272 // Handle unmountable partitions in case we reset defaults
1273 if ((*iter)->Mount_Point == "/boot") {
1274 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1275 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1276 if ((*iter)->Backup_Size == 0) {
1277 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1278 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1279 } else
1280 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1281 } else if ((*iter)->Mount_Point == "/recovery") {
1282 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1283 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1284 if ((*iter)->Backup_Size == 0) {
1285 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1286 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1287 } else
1288 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001289 } else if ((*iter)->Mount_Point == "/data") {
1290 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001291 }
1292#ifdef SP1_NAME
1293 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1294 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1295 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1296 }
1297#endif
1298#ifdef SP2_NAME
1299 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1300 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1301 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1302 }
1303#endif
1304#ifdef SP3_NAME
1305 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1306 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1307 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1308 }
1309#endif
Dees_Troy51127312012-09-08 13:08:49 -04001310 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001311 }
that39b7c162013-11-22 22:07:33 +01001312 gui_print("...done\n");
Dees_Troy51127312012-09-08 13:08:49 -04001313 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1314 string current_storage_path = DataManager::GetCurrentStoragePath();
1315 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001316 if (FreeStorage != NULL) {
1317 // Attempt to mount storage
1318 if (!FreeStorage->Mount(false)) {
1319 // We couldn't mount storage... check to see if we have dual storage
1320 int has_dual_storage;
1321 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1322 if (has_dual_storage == 1) {
1323 // We have dual storage, see if we're using the internal storage that should always be present
1324 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001325 if (!FreeStorage->Is_Encrypted) {
1326 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001327 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001328 }
Dees_Troy8170a922012-09-18 15:40:25 -04001329 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1330 } else {
1331 // We were using external, flip to internal
1332 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1333 current_storage_path = DataManager::GetCurrentStoragePath();
1334 FreeStorage = Find_Partition_By_Path(current_storage_path);
1335 if (FreeStorage != NULL) {
1336 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1337 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001338 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001339 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1340 }
1341 }
1342 } else {
1343 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001344 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001345 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1346 }
1347 } else {
1348 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1349 }
1350 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001351 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001352 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001353 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001354 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001355 return;
1356}
1357
1358int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001359#ifdef TW_INCLUDE_CRYPTO
1360 int ret_val, password_len;
1361 char crypto_blkdev[255], cPassword[255];
1362 size_t result;
Ethan Yonker253368a2014-11-25 15:00:52 -06001363 std::vector<TWPartition*>::iterator iter;
Dees_Troy5bf43922012-09-07 16:07:55 -04001364
1365 property_set("ro.crypto.state", "encrypted");
a39552696ff55ce2013-01-08 16:14:56 +00001366
Ethan Yonker253368a2014-11-25 15:00:52 -06001367 // Mount any partitions that need to be mounted for decrypt
1368 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1369 if ((*iter)->Mount_To_Decrypt) {
1370 (*iter)->Mount(true);
1371 }
a39552696ff55ce2013-01-08 16:14:56 +00001372 }
a39552696ff55ce2013-01-08 16:14:56 +00001373
Dees_Troy5bf43922012-09-07 16:07:55 -04001374 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001375 int pwret = cryptfs_check_passwd(cPassword);
1376
Ethan Yonker253368a2014-11-25 15:00:52 -06001377 // Unmount any partitions that were needed for decrypt
1378 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1379 if ((*iter)->Mount_To_Decrypt) {
1380 (*iter)->UnMount(false);
1381 }
1382 }
1383
a39552696ff55ce2013-01-08 16:14:56 +00001384 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001385 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001386 return -1;
1387 }
a39552696ff55ce2013-01-08 16:14:56 +00001388
Dees_Troy5bf43922012-09-07 16:07:55 -04001389 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1390 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001391 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001392 } else {
1393 TWPartition* dat = Find_Partition_By_Path("/data");
1394 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001395 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001396 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1397 dat->Is_Decrypted = true;
1398 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001399 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001400 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 +00001401 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001402
Dees_Troy5bf43922012-09-07 16:07:55 -04001403 // Sleep for a bit so that the device will be ready
1404 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001405 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001406 dat->Storage_Path = "/data/media/0";
1407 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001408 DataManager::SetValue("tw_storage_path", "/data/media/0");
Ethan Yonkerec0fa4a2014-11-20 09:51:03 -06001409 DataManager::SetValue("tw_settings_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001410 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001411 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001412 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001413 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001414 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001415 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001416 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001417 }
1418 return 0;
1419#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001420 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001421 return -1;
1422#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001423 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001424}
1425
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001426int TWPartitionManager::Fix_Permissions(void) {
1427 int result = 0;
1428 if (!Mount_By_Path("/data", true))
1429 return false;
1430
1431 if (!Mount_By_Path("/system", true))
1432 return false;
1433
1434 Mount_By_Path("/sd-ext", false);
1435
1436 fixPermissions perms;
1437 result = perms.fixPerms(true, false);
thata3d31fb2014-12-21 22:27:40 +01001438#ifdef HAVE_SELINUX
1439 if (result == 0 && DataManager::GetIntValue("tw_fixperms_restorecon") == 1)
1440 result = perms.fixContexts();
1441#endif
Dees_Troy1a650e62012-10-19 20:54:32 -04001442 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001443 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001444 return result;
1445}
1446
Ethan Yonker47360be2014-04-01 10:34:34 -05001447TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1448 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1449
1450 if (!Path.empty()) {
1451 string Search_Path = TWFunc::Get_Root_Path(Path);
1452 for (; iter != Partitions.end(); iter++) {
1453 if ((*iter)->Mount_Point == Search_Path) {
1454 iter++;
1455 break;
1456 }
1457 }
1458 }
1459
1460 for (; iter != Partitions.end(); iter++) {
1461 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) {
1462 return (*iter);
1463 }
1464 }
1465
1466 return NULL;
1467}
1468
Dees_Troyd21618c2012-10-14 18:48:49 -04001469int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001470 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1471
1472 if (Part == NULL) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001473 LOGERR("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
Dees_Troyd21618c2012-10-14 18:48:49 -04001474 return false;
1475 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001476 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1477 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001478 return false;
1479
Dees_Troy2673cec2013-04-02 20:22:16 +00001480 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1481 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001482 return false;
1483 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001484 return true;
1485}
1486
Dees_Troy8170a922012-09-18 15:40:25 -04001487int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001488 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001489 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001490 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001491
Dees_Troy8170a922012-09-18 15:40:25 -04001492 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001493 string Lun_File_str = CUSTOM_LUN_FILE;
1494 size_t found = Lun_File_str.find("%");
1495 if (found != string::npos) {
1496 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1497 if (TWFunc::Path_Exists(lun_file))
1498 has_multiple_lun = true;
1499 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001500 mtp_was_enabled = TWFunc::Toggle_MTP(false); // Must disable MTP for USB Storage
Ethan Yonker47360be2014-04-01 10:34:34 -05001501 if (!has_multiple_lun) {
1502 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1503 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1504 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
1505 TWPartition* Mount = Find_Next_Storage("", "/data");
1506 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001507 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1508 goto error_handle;
1509 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001510 } else {
1511 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001512 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001513 }
1514 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001515 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001516 }
Dees_Troy8170a922012-09-18 15:40:25 -04001517 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001518 LOGINFO("Device has multiple lun files\n");
1519 TWPartition* Mount1;
1520 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001521 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker47360be2014-04-01 10:34:34 -05001522 Mount1 = Find_Next_Storage("", "/data");
1523 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001524 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1525 goto error_handle;
1526 }
Matt Moweree717062014-04-26 01:46:46 -05001527 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker47360be2014-04-01 10:34:34 -05001528 Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data");
1529 if (Mount2) {
Matt Moweree717062014-04-26 01:46:46 -05001530 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001531 }
1532 } else {
1533 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001534 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001535 }
Dees_Troy8170a922012-09-18 15:40:25 -04001536 }
Matt Mowerb6ef4782014-11-06 02:24:36 -06001537 property_set("sys.storage.ums_enabled", "1");
Dees_Troy8170a922012-09-18 15:40:25 -04001538 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001539error_handle:
1540 if (mtp_was_enabled)
1541 if (!Enable_MTP())
1542 Disable_MTP();
1543 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001544}
1545
1546int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001547 int index, ret;
1548 char lun_file[255], ch[2] = {0, 0};
1549 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001550
1551 for (index=0; index<2; index++) {
1552 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001553 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001554 if (ret < 0) {
1555 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001556 }
Dees_Troy8170a922012-09-18 15:40:25 -04001557 }
Dees_Troye58d5262012-09-21 12:27:57 -04001558 Mount_All_Storage();
1559 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001560 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001561 property_set("sys.storage.ums_enabled", "0");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001562 if (mtp_was_enabled)
1563 if (!Enable_MTP())
1564 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001565 if (ret < 0 && index == 0) {
1566 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1567 return false;
1568 } else {
1569 return true;
1570 }
Dees_Troy8170a922012-09-18 15:40:25 -04001571 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001572}
1573
1574void TWPartitionManager::Mount_All_Storage(void) {
1575 std::vector<TWPartition*>::iterator iter;
1576
1577 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1578 if ((*iter)->Is_Storage)
1579 (*iter)->Mount(false);
1580 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001581}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001582
Dees_Troyd0384ef2012-10-12 12:15:42 -04001583void TWPartitionManager::UnMount_Main_Partitions(void) {
1584 // Unmounts system and data if data is not data/media
1585 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001586 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001587
1588 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1589
1590 UnMount_By_Path("/system", true);
Ethan Yonker6277c792014-09-15 14:54:30 -05001591 if (!datamedia)
1592 UnMount_By_Path("/data", true);
1593
Dees_Troyd0384ef2012-10-12 12:15:42 -04001594 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1595 Boot_Partition->UnMount(true);
1596}
1597
Dees_Troy9350b8d2012-09-27 12:38:38 -04001598int TWPartitionManager::Partition_SDCard(void) {
1599 char mkdir_path[255], temp[255], line[512];
1600 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1601 int ext, swap, total_size = 0, fat_size;
1602 FILE* fp;
1603
Dees_Troy2673cec2013-04-02 20:22:16 +00001604 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001605#ifdef TW_EXTERNAL_STORAGE_PATH
1606 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1607#else
1608 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1609#endif
Ethan Yonker1eff6cd2014-09-15 13:30:42 -05001610 if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001611 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001612 return false;
1613 }
1614 if (!SDCard->UnMount(true))
1615 return false;
1616 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1617 if (SDext != NULL) {
1618 if (!SDext->UnMount(true))
1619 return false;
1620 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001621
1622 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001623 Device = SDCard->Actual_Block_Device;
1624 // Just use the root block device
1625 Device.resize(strlen("/dev/block/mmcblkX"));
1626
1627 // Find the size of the block device:
1628 fp = fopen("/proc/partitions", "rt");
1629 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001630 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001631 return false;
1632 }
1633
1634 while (fgets(line, sizeof(line), fp) != NULL)
1635 {
1636 unsigned long major, minor, blocks;
1637 char device[512];
1638 char tmpString[64];
1639
1640 if (strlen(line) < 7 || line[0] == 'm') continue;
1641 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1642
1643 tmpdevice = "/dev/block/";
1644 tmpdevice += device;
1645 if (tmpdevice == Device) {
1646 // Adjust block size to byte size
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001647 total_size = (int)(blocks * 1024ULL / 1000000LLU);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001648 break;
1649 }
1650 }
1651 fclose(fp);
1652
1653 DataManager::GetValue("tw_sdext_size", ext);
1654 DataManager::GetValue("tw_swap_size", swap);
1655 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1656 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001657 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 -04001658 memset(temp, 0, sizeof(temp));
1659 sprintf(temp, "%i", fat_size);
1660 fat_str = temp;
1661 memset(temp, 0, sizeof(temp));
1662 sprintf(temp, "%i", fat_size + ext);
1663 ext_str = temp;
1664 memset(temp, 0, sizeof(temp));
1665 sprintf(temp, "%i", fat_size + ext + swap);
1666 swap_str = temp;
1667 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001668 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001669 return false;
1670 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001671 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001672 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001673 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001674 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001675 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001676 Update_System_Details();
1677 return false;
1678 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001679 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001680 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001681 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001682 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001683 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001684 return false;
1685 }
1686 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001687 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001688 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001689 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001690 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001691 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001692 Update_System_Details();
1693 return false;
1694 }
1695 }
1696 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001697 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001698 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001699 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001700 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001701 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001702 Update_System_Details();
1703 return false;
1704 }
1705 }
1706 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1707#ifdef TW_EXTERNAL_STORAGE_PATH
1708 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1709 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1710 memset(mkdir_path, 0, sizeof(mkdir_path));
1711 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1712#else
1713 Mount_By_Path("/sdcard", 1);
1714 strcpy(mkdir_path, "/sdcard/TWRP");
1715#endif
1716 mkdir(mkdir_path, 0777);
1717 DataManager::Flush();
1718#ifdef TW_EXTERNAL_STORAGE_PATH
1719 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1720 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1721 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1722#else
1723 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1724 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1725 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1726#endif
1727 if (ext > 0) {
1728 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001729 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001730 return false;
1731 }
1732 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001733 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1734 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001735 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001736 }
1737
1738 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00001739 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001740 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001741}
Dees_Troya13d74f2013-03-24 08:54:55 -05001742
1743void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1744 std::vector<TWPartition*>::iterator iter;
1745 if (ListType == "mount") {
1746 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1747 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1748 struct PartitionList part;
1749 part.Display_Name = (*iter)->Display_Name;
1750 part.Mount_Point = (*iter)->Mount_Point;
1751 part.selected = (*iter)->Is_Mounted();
1752 Partition_List->push_back(part);
1753 }
1754 }
1755 } else if (ListType == "storage") {
1756 char free_space[255];
1757 string Current_Storage = DataManager::GetCurrentStoragePath();
1758 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1759 if ((*iter)->Is_Storage) {
1760 struct PartitionList part;
1761 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1762 part.Display_Name = (*iter)->Storage_Name + " (";
1763 part.Display_Name += free_space;
1764 part.Display_Name += "MB)";
1765 part.Mount_Point = (*iter)->Storage_Path;
1766 if ((*iter)->Storage_Path == Current_Storage)
1767 part.selected = 1;
1768 else
1769 part.selected = 0;
1770 Partition_List->push_back(part);
1771 }
1772 }
1773 } else if (ListType == "backup") {
1774 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001775 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001776 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001777 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001778 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001779 Backup_Size = (*iter)->Backup_Size;
1780 if ((*iter)->Has_SubPartition) {
1781 std::vector<TWPartition*>::iterator subpart;
1782
1783 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1784 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1785 Backup_Size += (*subpart)->Backup_Size;
1786 }
1787 }
1788 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001789 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1790 part.Display_Name += backup_size;
1791 part.Display_Name += "MB)";
1792 part.Mount_Point = (*iter)->Backup_Path;
1793 part.selected = 0;
1794 Partition_List->push_back(part);
1795 }
1796 }
1797 } else if (ListType == "restore") {
1798 string Restore_List, restore_path;
1799 TWPartition* restore_part = NULL;
1800
1801 DataManager::GetValue("tw_restore_list", Restore_List);
1802 if (!Restore_List.empty()) {
1803 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1804 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1805 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05001806 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00001807 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001808 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05001809 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05001810 } else {
1811 struct PartitionList part;
1812 part.Display_Name = restore_part->Backup_Display_Name;
1813 part.Mount_Point = restore_part->Backup_Path;
1814 part.selected = 1;
1815 Partition_List->push_back(part);
1816 }
1817 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001818 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001819 }
1820 start_pos = end_pos + 1;
1821 end_pos = Restore_List.find(";", start_pos);
1822 }
1823 }
1824 } else if (ListType == "wipe") {
1825 struct PartitionList dalvik;
1826 dalvik.Display_Name = "Dalvik Cache";
1827 dalvik.Mount_Point = "DALVIK";
1828 dalvik.selected = 0;
1829 Partition_List->push_back(dalvik);
1830 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1831 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1832 struct PartitionList part;
1833 part.Display_Name = (*iter)->Display_Name;
1834 part.Mount_Point = (*iter)->Mount_Point;
1835 part.selected = 0;
1836 Partition_List->push_back(part);
1837 }
1838 if ((*iter)->Has_Android_Secure) {
1839 struct PartitionList part;
1840 part.Display_Name = (*iter)->Backup_Display_Name;
1841 part.Mount_Point = (*iter)->Backup_Path;
1842 part.selected = 0;
1843 Partition_List->push_back(part);
1844 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001845 if ((*iter)->Has_Data_Media) {
1846 struct PartitionList datamedia;
1847 datamedia.Display_Name = (*iter)->Storage_Name;
1848 datamedia.Mount_Point = "INTERNAL";
1849 datamedia.selected = 0;
1850 Partition_List->push_back(datamedia);
1851 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001852 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06001853 } else if (ListType == "flashimg") {
1854 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1855 if ((*iter)->Can_Flash_Img && (*iter)->Is_Present) {
1856 struct PartitionList part;
1857 part.Display_Name = (*iter)->Backup_Display_Name;
1858 part.Mount_Point = (*iter)->Backup_Path;
1859 part.selected = 0;
1860 Partition_List->push_back(part);
1861 }
1862 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001863 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001864 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001865 }
1866}
1867
1868int TWPartitionManager::Fstab_Processed(void) {
1869 return Partitions.size();
1870}
Dees_Troyd93bda52013-07-03 19:55:19 +00001871
1872void TWPartitionManager::Output_Storage_Fstab(void) {
1873 std::vector<TWPartition*>::iterator iter;
1874 char storage_partition[255];
1875 string Temp;
1876 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
1877
1878 if (fp == NULL) {
1879 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
1880 return;
1881 }
1882
1883 // Iterate through all partitions
1884 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1885 if ((*iter)->Is_Storage) {
1886 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
1887 strcpy(storage_partition, Temp.c_str());
1888 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
1889 }
1890 }
1891 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001892}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02001893
1894TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
1895{
1896 TWPartition *res = NULL;
1897 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
1898 if(!(*iter)->Is_Storage)
1899 continue;
1900
1901 if((*iter)->Is_Settings_Storage)
1902 return *iter;
1903
1904 if(!res)
1905 res = *iter;
1906 }
1907 return res;
1908}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001909
1910bool TWPartitionManager::Enable_MTP(void) {
1911#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001912 if (mtppid) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001913 LOGERR("MTP already enabled\n");
1914 return true;
1915 }
1916 //Launch MTP Responder
1917 LOGINFO("Starting MTP\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001918 int count = 0;
Ethan Yonker726a0202014-12-16 20:01:38 -06001919
1920 int mtppipe[2];
1921
1922 if (pipe(mtppipe) < 0) {
1923 LOGERR("Error creating MTP pipe\n");
1924 return false;
1925 }
1926
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06001927 char old_value[PROPERTY_VALUE_MAX];
1928 property_get("sys.usb.config", old_value, "error");
1929 if (strcmp(old_value, "error") != 0 && strcmp(old_value, "mtp,adb") != 0) {
1930 char vendor[PROPERTY_VALUE_MAX];
1931 char product[PROPERTY_VALUE_MAX];
1932 property_set("sys.usb.config", "none");
1933 property_get("usb.vendor", vendor, "18D1");
1934 property_get("usb.product.mtpadb", product, "4EE2");
1935 string vendorstr = vendor;
1936 string productstr = product;
1937 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
1938 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
1939 property_set("sys.usb.config", "mtp,adb");
1940 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001941 std::vector<TWPartition*>::iterator iter;
Ethan Yonker6d154c42014-09-03 14:19:43 -05001942 /* To enable MTP debug, use the twrp command line feature to
1943 * twrp set tw_mtp_debug 1
1944 */
1945 twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug"));
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001946 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1947 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false)) {
bigbiff0c532032014-12-21 13:41:26 -05001948 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 -06001949 mtp->addStorage((*iter)->Storage_Name, (*iter)->Storage_Path, (*iter)->MTP_Storage_ID, (*iter)->Get_Max_FileSize());
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001950 count++;
1951 }
1952 }
1953 if (count) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001954 mtppid = mtp->forkserver(mtppipe);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001955 if (mtppid) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001956 close(mtppipe[0]); // Host closes read side
1957 mtp_write_fd = mtppipe[1];
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001958 DataManager::SetValue("tw_mtp_enabled", 1);
1959 return true;
1960 } else {
Ethan Yonker726a0202014-12-16 20:01:38 -06001961 close(mtppipe[0]);
1962 close(mtppipe[1]);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001963 LOGERR("Failed to enable MTP\n");
1964 return false;
1965 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001966 } else {
1967 close(mtppipe[0]);
1968 close(mtppipe[1]);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001969 }
1970 LOGERR("No valid storage partitions found for MTP.\n");
1971#else
1972 LOGERR("MTP support not included\n");
1973#endif
1974 DataManager::SetValue("tw_mtp_enabled", 0);
1975 return false;
1976}
1977
1978bool TWPartitionManager::Disable_MTP(void) {
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06001979 char old_value[PROPERTY_VALUE_MAX];
1980 property_get("sys.usb.config", old_value, "error");
1981 if (strcmp(old_value, "adb") != 0) {
1982 char vendor[PROPERTY_VALUE_MAX];
1983 char product[PROPERTY_VALUE_MAX];
1984 property_set("sys.usb.config", "none");
1985 property_get("usb.vendor", vendor, "18D1");
1986 property_get("usb.product.adb", product, "D002");
1987 string vendorstr = vendor;
1988 string productstr = product;
1989 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
1990 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
1991 usleep(2000);
1992 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001993#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001994 if (mtppid) {
Ethan Yonker8613dc02014-09-11 09:28:20 -05001995 LOGINFO("Disabling MTP\n");
1996 int status;
1997 kill(mtppid, SIGKILL);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001998 mtppid = 0;
Ethan Yonker8613dc02014-09-11 09:28:20 -05001999 // We don't care about the exit value, but this prevents a zombie process
2000 waitpid(mtppid, &status, 0);
Ethan Yonker726a0202014-12-16 20:01:38 -06002001 close(mtp_write_fd);
2002 mtp_write_fd = -1;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002003 }
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002004#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002005 property_set("sys.usb.config", "adb");
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002006#ifdef TW_HAS_MTP
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002007 DataManager::SetValue("tw_mtp_enabled", 0);
2008 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002009#endif
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002010 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002011}
Ethan Yonker726a0202014-12-16 20:01:38 -06002012
2013TWPartition* TWPartitionManager::Find_Partition_By_MTP_Storage_ID(unsigned int Storage_ID) {
2014 std::vector<TWPartition*>::iterator iter;
2015
2016 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2017 if ((*iter)->MTP_Storage_ID == Storage_ID)
2018 return (*iter);
2019 }
2020 return NULL;
2021}
2022
2023bool TWPartitionManager::Add_Remove_MTP_Storage(TWPartition* Part, int message_type) {
2024#ifdef TW_HAS_MTP
2025 struct mtpmsg mtp_message;
2026
2027 if (!mtppid)
2028 return false; // MTP is disabled
2029
2030 if (mtp_write_fd < 0) {
2031 LOGERR("MTP: mtp_write_fd is not set\n");
2032 return false;
2033 }
2034
2035 if (Part) {
Ethan Yonker4bfabab2014-12-29 09:15:37 -06002036 if (Part->MTP_Storage_ID == 0)
2037 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002038 if (message_type == MTP_MESSAGE_REMOVE_STORAGE) {
2039 mtp_message.message_type = MTP_MESSAGE_REMOVE_STORAGE; // Remove
2040 LOGINFO("sending message to remove %i\n", Part->MTP_Storage_ID);
2041 mtp_message.storage_id = Part->MTP_Storage_ID;
2042 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
2043 LOGERR("error sending message to remove storage %i\n", Part->MTP_Storage_ID);
2044 return false;
2045 } else {
2046 LOGINFO("Message sent, remove storage ID: %i\n", Part->MTP_Storage_ID);
2047 return true;
2048 }
2049 } else if (message_type == MTP_MESSAGE_ADD_STORAGE && Part->Is_Mounted()) {
2050 mtp_message.message_type = MTP_MESSAGE_ADD_STORAGE; // Add
2051 mtp_message.storage_id = Part->MTP_Storage_ID;
2052 mtp_message.path = Part->Storage_Path.c_str();
2053 mtp_message.display = Part->Storage_Name.c_str();
2054 mtp_message.maxFileSize = Part->Get_Max_FileSize();
2055 LOGINFO("sending message to add %i '%s'\n", Part->MTP_Storage_ID, mtp_message.path);
2056 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
2057 LOGERR("error sending message to add storage %i\n", Part->MTP_Storage_ID);
2058 return false;
2059 } else {
2060 LOGINFO("Message sent, add storage ID: %i\n", Part->MTP_Storage_ID);
2061 return true;
2062 }
2063 } else {
2064 LOGERR("Unknown MTP message type: %i\n", message_type);
2065 }
2066 } else {
2067 // This hopefully never happens as the error handling should
2068 // occur in the calling function.
2069 LOGERR("TWPartitionManager::Add_Remove_MTP_Storage NULL partition given\n");
2070 }
2071 return true;
2072#else
2073 LOGERR("MTP support not included\n");
2074 DataManager::SetValue("tw_mtp_enabled", 0);
2075 return false;
2076#endif
2077}
2078
2079bool TWPartitionManager::Add_MTP_Storage(string Mount_Point) {
2080#ifdef TW_HAS_MTP
2081 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2082 if (Part) {
2083 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2084 } else {
2085 LOGERR("TWFunc::Add_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
2086 }
2087#endif
2088 return false;
2089}
2090
2091bool TWPartitionManager::Add_MTP_Storage(unsigned int Storage_ID) {
2092#ifdef TW_HAS_MTP
2093 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2094 if (Part) {
2095 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2096 } else {
2097 LOGERR("TWFunc::Add_MTP_Storage unable to locate partition for %i\n", Storage_ID);
2098 }
2099#endif
2100 return false;
2101}
2102
2103bool TWPartitionManager::Remove_MTP_Storage(string Mount_Point) {
2104#ifdef TW_HAS_MTP
2105 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2106 if (Part) {
2107 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2108 } else {
2109 LOGERR("TWFunc::Remove_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
2110 }
2111#endif
2112 return false;
2113}
2114
2115bool TWPartitionManager::Remove_MTP_Storage(unsigned int Storage_ID) {
2116#ifdef TW_HAS_MTP
2117 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2118 if (Part) {
2119 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2120 } else {
2121 LOGERR("TWFunc::Remove_MTP_Storage unable to locate partition for %i\n", Storage_ID);
2122 }
2123#endif
2124 return false;
2125}
Ethan Yonker96af84a2015-01-05 14:58:36 -06002126
2127bool TWPartitionManager::Flash_Image(string Filename) {
2128 int check, partition_count = 0;
2129 TWPartition* flash_part = NULL;
2130 string Flash_List, flash_path;
2131 size_t start_pos = 0, end_pos = 0;
2132
2133 gui_print("\n[IMAGE FLASH STARTED]\n\n");
2134 gui_print("Image to flash: '%s'\n", Filename.c_str());
2135
2136 if (!Mount_Current_Storage(true))
2137 return false;
2138
2139 gui_print("Calculating restore details...\n");
2140 DataManager::GetValue("tw_flash_partition", Flash_List);
2141 if (!Flash_List.empty()) {
2142 end_pos = Flash_List.find(";", start_pos);
2143 while (end_pos != string::npos && start_pos < Flash_List.size()) {
2144 flash_path = Flash_List.substr(start_pos, end_pos - start_pos);
2145 flash_part = Find_Partition_By_Path(flash_path);
2146 if (flash_part != NULL) {
2147 partition_count++;
2148 if (partition_count > 1) {
2149 LOGERR("Too many partitions selected for flashing.\n");
2150 return false;
2151 }
2152 } else {
2153 LOGERR("Unable to locate '%s' partition for flashing (flash list).\n", flash_path.c_str());
2154 return false;
2155 }
2156 start_pos = end_pos + 1;
2157 end_pos = Flash_List.find(";", start_pos);
2158 }
2159 }
2160
2161 if (partition_count == 0) {
2162 LOGERR("No partitions selected for flashing.\n");
2163 return false;
2164 }
2165
2166 DataManager::SetProgress(0.0);
2167 if (flash_part) {
2168 if (!flash_part->Flash_Image(Filename))
2169 return false;
2170 } else {
2171 LOGERR("Invalid flash partition specified.\n");
2172 return false;
2173 }
2174 gui_print_color("highlight", "[IMAGE FLASH COMPLETED]\n\n");
2175 return true;
2176}