blob: 4cd3e7d5997048327a1e50ac980f903397a95c80 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff7abc5fe2015-01-17 16:53:12 -05002 Copyright 2014 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 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>
Ethan Yonker483e9f42016-01-11 22:21:18 -060033#include <linux/fs.h>
34#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000036#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040037#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040038#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040039#include "twrp-functions.hpp"
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040040#include "fixPermissions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050041#include "twrpDigest.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050042#include "twrpDU.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060043#include "set_metadata.h"
bigbiff7abc5fe2015-01-17 16:53:12 -050044#include "tw_atomic.hpp"
Ethan Yonker74db1572015-10-28 12:44:49 -050045#include "gui/gui.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040046
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040047#ifdef TW_HAS_MTP
48#include "mtp/mtp_MtpServer.hpp"
49#include "mtp/twrpMtp.hpp"
Ethan Yonker726a0202014-12-16 20:01:38 -060050#include "mtp/MtpMessage.hpp"
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040051#endif
52
Dees Troy6f6441d2014-01-23 02:07:03 +000053extern "C" {
54 #include "cutils/properties.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050055 #include "gui/gui.h"
Dees Troy6f6441d2014-01-23 02:07:03 +000056}
57
Dees_Troy5bf43922012-09-07 16:07:55 -040058#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060059 #include "crypto/lollipop/cryptfs.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040060#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040061
Ethan Yonker6277c792014-09-15 14:54:30 -050062extern bool datamedia;
63
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050064TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040065 mtp_was_enabled = false;
Ethan Yonker726a0202014-12-16 20:01:38 -060066 mtp_write_fd = -1;
bigbiff7abc5fe2015-01-17 16:53:12 -050067 stop_backup.set_value(0);
68 tar_fork_pid = 0;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050069}
70
Dees_Troy51a0e822012-09-05 15:24:24 -040071int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040072 FILE *fstabFile;
73 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +000074 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050075 TWPartition* andsec_partition = NULL;
Ethan Yonker726a0202014-12-16 20:01:38 -060076 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 -040077
78 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
79 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000080 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040081 return false;
82 }
83
84 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
85 if (fstab_line[0] != '/')
86 continue;
87
Dees_Troy2a923582012-09-20 12:13:34 -040088 if (fstab_line[strlen(fstab_line) - 1] != '\n')
89 fstab_line[strlen(fstab_line)] = '\n';
that9e0593e2014-10-08 00:01:24 +020090 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040091 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040092 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040093
Dees_Troy5bf43922012-09-07 16:07:55 -040094 if (partition->Process_Fstab_Line(line, Display_Error)) {
Ethan Yonker726a0202014-12-16 20:01:38 -060095 if (partition->Is_Storage) {
96 ++storageid;
97 partition->MTP_Storage_ID = storageid;
98 }
Matt Mowered71fa32014-04-16 13:21:47 -050099 if (!settings_partition && partition->Is_Settings_Storage && partition->Is_Present) {
Dees Troy02a64532014-03-19 15:23:32 +0000100 settings_partition = partition;
Dees_Troya13d74f2013-03-24 08:54:55 -0500101 } else {
102 partition->Is_Settings_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500103 }
Matt Mowered71fa32014-04-16 13:21:47 -0500104 if (!andsec_partition && partition->Has_Android_Secure && partition->Is_Present) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500105 andsec_partition = partition;
106 } else {
107 partition->Has_Android_Secure = false;
108 }
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500109 Partitions.push_back(partition);
Dees_Troy5bf43922012-09-07 16:07:55 -0400110 } else {
111 delete partition;
112 }
113 }
114 fclose(fstabFile);
Ethan Yonker6277c792014-09-15 14:54:30 -0500115 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) {
116 // Attempt to automatically identify /data/media emulated storage devices
117 TWPartition* Dat = Find_Partition_By_Path("/data");
118 if (Dat) {
119 LOGINFO("Using automatic handling for /data/media emulated storage device.\n");
120 datamedia = true;
that9e0593e2014-10-08 00:01:24 +0200121 Dat->Setup_Data_Media();
Ethan Yonker6277c792014-09-15 14:54:30 -0500122 settings_partition = Dat;
Ethan Yonker726a0202014-12-16 20:01:38 -0600123 // Since /data was not considered a storage partition earlier, we still need to assign an MTP ID
124 ++storageid;
125 Dat->MTP_Storage_ID = storageid;
Ethan Yonker6277c792014-09-15 14:54:30 -0500126 }
127 }
Dees Troy02a64532014-03-19 15:23:32 +0000128 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500129 std::vector<TWPartition*>::iterator iter;
130 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
131 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000132 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500133 break;
134 }
135 }
Dees Troy02a64532014-03-19 15:23:32 +0000136 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000137 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500138 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400139 if (!Write_Fstab()) {
140 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000141 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400142 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000143 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400144 }
Matt Mowered71fa32014-04-16 13:21:47 -0500145
Matt Mowerbf4efa32014-04-14 23:25:26 -0500146 if (andsec_partition) {
147 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500148 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500149 Setup_Android_Secure_Location(settings_partition);
150 }
Matt Mowered71fa32014-04-16 13:21:47 -0500151 if (settings_partition) {
152 Setup_Settings_Storage_Partition(settings_partition);
153 }
Ethan Yonker253368a2014-11-25 15:00:52 -0600154#ifdef TW_INCLUDE_CRYPTO
Ethan Yonkercceebb82014-11-18 10:17:59 -0600155 TWPartition* Decrypt_Data = Find_Partition_By_Path("/data");
156 if (Decrypt_Data && Decrypt_Data->Is_Encrypted && !Decrypt_Data->Is_Decrypted) {
157 int password_type = cryptfs_get_password_type();
158 if (password_type == CRYPT_TYPE_DEFAULT) {
159 LOGINFO("Device is encrypted with the default password, attempting to decrypt.\n");
160 if (Decrypt_Device("default_password") == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500161 gui_msg("decrypt_success=Successfully decrypted with default password.");
Ethan Yonkercceebb82014-11-18 10:17:59 -0600162 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
163 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500164 gui_err("unable_to_decrypt=Unable to decrypt with default password.");
Ethan Yonkercceebb82014-11-18 10:17:59 -0600165 }
166 } else {
167 DataManager::SetValue("TW_CRYPTO_TYPE", password_type);
168 }
169 }
170#endif
Dees_Troy51127312012-09-08 13:08:49 -0400171 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400172 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400173 return true;
174}
175
176int TWPartitionManager::Write_Fstab(void) {
177 FILE *fp;
178 std::vector<TWPartition*>::iterator iter;
179 string Line;
180
181 fp = fopen("/etc/fstab", "w");
182 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000183 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400184 return false;
185 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400186 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400187 if ((*iter)->Can_Be_Mounted) {
dianlujitao7d304c72016-01-02 12:15:50 +0800188 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw 0 0\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400189 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000190 }
191 // Handle subpartition tracking
192 if ((*iter)->Is_SubPartition) {
193 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
194 if (ParentPartition)
195 ParentPartition->Has_SubPartition = true;
196 else
197 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 -0400198 }
199 }
200 fclose(fp);
201 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400202}
203
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500204void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500205 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
206 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
207 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
208}
209
Matt Mowerbf4efa32014-04-14 23:25:26 -0500210void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
211 if (Part->Has_Android_Secure)
212 Part->Setup_AndSec();
Ethan Yonker6277c792014-09-15 14:54:30 -0500213 else if (!datamedia)
Matt Mowerbf4efa32014-04-14 23:25:26 -0500214 Part->Setup_AndSec();
Matt Mowerbf4efa32014-04-14 23:25:26 -0500215}
216
Dees_Troy8170a922012-09-18 15:40:25 -0400217void TWPartitionManager::Output_Partition_Logging(void) {
218 std::vector<TWPartition*>::iterator iter;
219
220 printf("\n\nPartition Logs:\n");
221 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
222 Output_Partition((*iter));
223}
224
225void TWPartitionManager::Output_Partition(TWPartition* Part) {
226 unsigned long long mb = 1048576;
227
Gary Peck004d48b2012-11-21 16:28:18 -0800228 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 -0400229 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800230 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 -0400231 }
Gary Peck004d48b2012-11-21 16:28:18 -0800232 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500233 if (Part->Can_Be_Mounted)
234 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800235 if (Part->Can_Be_Wiped)
236 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700237 if (Part->Use_Rm_Rf)
238 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500239 if (Part->Can_Be_Backed_Up)
240 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800241 if (Part->Wipe_During_Factory_Reset)
242 printf("Wipe_During_Factory_Reset ");
243 if (Part->Wipe_Available_in_GUI)
244 printf("Wipe_Available_in_GUI ");
245 if (Part->Is_SubPartition)
246 printf("Is_SubPartition ");
247 if (Part->Has_SubPartition)
248 printf("Has_SubPartition ");
249 if (Part->Removable)
250 printf("Removable ");
251 if (Part->Is_Present)
252 printf("IsPresent ");
253 if (Part->Can_Be_Encrypted)
254 printf("Can_Be_Encrypted ");
255 if (Part->Is_Encrypted)
256 printf("Is_Encrypted ");
257 if (Part->Is_Decrypted)
258 printf("Is_Decrypted ");
259 if (Part->Has_Data_Media)
260 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000261 if (Part->Can_Encrypt_Backup)
262 printf("Can_Encrypt_Backup ");
263 if (Part->Use_Userdata_Encryption)
264 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800265 if (Part->Has_Android_Secure)
266 printf("Has_Android_Secure ");
267 if (Part->Is_Storage)
268 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500269 if (Part->Is_Settings_Storage)
270 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000271 if (Part->Ignore_Blkid)
272 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000273 if (Part->Retain_Layout_Version)
274 printf("Retain_Layout_Version ");
Ethan Yonker253368a2014-11-25 15:00:52 -0600275 if (Part->Mount_To_Decrypt)
276 printf("Mount_To_Decrypt ");
Ethan Yonker96af84a2015-01-05 14:58:36 -0600277 if (Part->Can_Flash_Img)
278 printf("Can_Flash_Img ");
Gary Peck004d48b2012-11-21 16:28:18 -0800279 printf("\n");
280 if (!Part->SubPartition_Of.empty())
281 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
282 if (!Part->Symlink_Path.empty())
283 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
284 if (!Part->Symlink_Mount_Point.empty())
285 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
286 if (!Part->Primary_Block_Device.empty())
287 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
288 if (!Part->Alternate_Block_Device.empty())
289 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
290 if (!Part->Decrypted_Block_Device.empty())
291 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
Ethan Yonker253368a2014-11-25 15:00:52 -0600292 if (!Part->Crypto_Key_Location.empty() && Part->Crypto_Key_Location != "footer")
293 printf(" Crypto_Key_Location: %s\n", Part->Crypto_Key_Location.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800294 if (Part->Length != 0)
295 printf(" Length: %i\n", Part->Length);
296 if (!Part->Display_Name.empty())
297 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500298 if (!Part->Storage_Name.empty())
299 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800300 if (!Part->Backup_Path.empty())
301 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
302 if (!Part->Backup_Name.empty())
303 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500304 if (!Part->Backup_Display_Name.empty())
305 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800306 if (!Part->Backup_FileName.empty())
307 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
308 if (!Part->Storage_Path.empty())
309 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
310 if (!Part->Current_File_System.empty())
311 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
312 if (!Part->Fstab_File_System.empty())
313 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
314 if (Part->Format_Block_Size != 0)
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500315 printf(" Format_Block_Size: %lu\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400316 if (!Part->MTD_Name.empty())
317 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400318 string back_meth = Part->Backup_Method_By_Name();
Ethan Yonker6277c792014-09-15 14:54:30 -0500319 printf(" Backup_Method: %s\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800320 if (Part->Mount_Flags || !Part->Mount_Options.empty())
321 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -0600322 if (Part->MTP_Storage_ID)
323 printf(" MTP_Storage_ID: %i\n", Part->MTP_Storage_ID);
Ethan Yonker6277c792014-09-15 14:54:30 -0500324 printf("\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400325}
326
Dees_Troy51a0e822012-09-05 15:24:24 -0400327int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400328 std::vector<TWPartition*>::iterator iter;
329 int ret = false;
330 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400331 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400332
Dees_Troyd93bda52013-07-03 19:55:19 +0000333 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400334 return true;
335
Dees_Troy5bf43922012-09-07 16:07:55 -0400336 // Iterate through all partitions
337 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400338 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400339 ret = (*iter)->Mount(Display_Error);
340 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400341 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400342 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400343 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400344 }
345 if (found) {
346 return ret;
347 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500348 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Dees_Troy5bf43922012-09-07 16:07:55 -0400349 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000350 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400351 }
352 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400353}
354
Dees_Troy51a0e822012-09-05 15:24:24 -0400355int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400356 std::vector<TWPartition*>::iterator iter;
357 int ret = false;
358 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400359 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400360
361 // Iterate through all partitions
362 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400363 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400364 ret = (*iter)->UnMount(Display_Error);
365 found = true;
366 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
367 (*iter)->UnMount(Display_Error);
368 }
369 }
370 if (found) {
371 return ret;
372 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500373 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Dees_Troy51127312012-09-08 13:08:49 -0400374 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000375 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400376 }
377 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400378}
379
Dees_Troy51a0e822012-09-05 15:24:24 -0400380int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400381 TWPartition* Part = Find_Partition_By_Path(Path);
382
383 if (Part)
384 return Part->Is_Mounted();
385 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000386 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400387 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400388}
389
Dees_Troy5bf43922012-09-07 16:07:55 -0400390int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400391 string current_storage_path = DataManager::GetCurrentStoragePath();
392
393 if (Mount_By_Path(current_storage_path, Display_Error)) {
394 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
395 if (FreeStorage)
396 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
397 return true;
398 }
399 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400400}
401
Dees_Troy5bf43922012-09-07 16:07:55 -0400402int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
403 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
404}
405
406TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
407 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400408 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400409
410 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400411 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400412 return (*iter);
413 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400414 return NULL;
415}
416
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400417int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
418 // Check the backup name to ensure that it is the correct size and contains only valid characters
419 // and that a backup with that name doesn't already exist
420 char backup_name[MAX_BACKUP_NAME_LEN];
421 char backup_loc[255], tw_image_dir[255];
422 int copy_size;
423 int index, cur_char;
424 string Backup_Name, Backup_Loc;
425
426 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
427 copy_size = Backup_Name.size();
428 // Check size
429 if (copy_size > MAX_BACKUP_NAME_LEN) {
430 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500431 gui_err("backup_name_len=Backup name is too long.");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400432 return -2;
433 }
434
435 // Check each character
436 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500437 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400438 return 0; // A "0" (zero) means to use the current timestamp for the backup name
439 for (index=0; index<copy_size; index++) {
440 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500441 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 -0400442 // These are valid characters
443 // Numbers
444 // Upper case letters
445 // Lower case letters
446 // Space
447 // and -_.{}[]
448 } else {
449 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500450 gui_msg(Msg(msg::kError, "backup_name_invalid=Backup name '{1}' contains invalid character: '{1}'")(backup_name)((char)cur_char));
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400451 return -3;
452 }
453 }
454
455 // Check to make sure that a backup with this name doesn't already exist
456 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
457 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500458 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500459 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400460 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500461 gui_err("backup_name_exists=A backup with this name already exists.");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400462 return -4;
463 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400464 // No problems found, return 0
465 return 0;
466}
467
Dees_Troy43d8b002012-09-17 16:00:01 -0400468bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
469{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500470 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400471 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500472 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500473 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400474
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500475 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400476 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400477
Ethan Yonker74db1572015-10-28 12:44:49 -0500478 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, gui_parse_text("{@generating_md51}"));
479 gui_msg("generating_md52= * Generating md5...");
Dees_Troy43d8b002012-09-17 16:00:01 -0400480
481 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500482 md5sum.setfn(Backup_Folder + Backup_Filename);
483 if (md5sum.computeMD5() == 0)
484 if (md5sum.write_md5digest() == 0)
Ethan Yonker74db1572015-10-28 12:44:49 -0500485 gui_msg("md5_created= * MD5 Created.");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500486 else
487 return -1;
488 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500489 gui_err("md5_error= * MD5 Error!");
Dees_Troy43d8b002012-09-17 16:00:01 -0400490 } else {
491 char filename[512];
492 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500493 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400494 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500495 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000496 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400497 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000498 if (TWFunc::Path_Exists(filename)) {
499 if (md5sum.computeMD5() == 0) {
500 if (md5sum.write_md5digest() != 0)
501 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500502 gui_err("md5_error= * MD5 Error!");
Dees_Troy83bd4832013-05-04 12:39:56 +0000503 return false;
504 }
505 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500506 gui_err("md5_compute_error= * Error computing MD5.");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500507 return false;
508 }
509 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400510 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400511 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500512 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400513 }
514 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000515 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400516 return false;
517 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500518 gui_msg("md5_created= * MD5 Created.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400519 }
520 return true;
521}
522
Dees_Troy093b7642012-09-21 15:59:38 -0400523bool 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 -0400524 time_t start, stop;
that203bcc92015-05-09 17:27:33 +0200525 int use_compression;
Dees_Troy093b7642012-09-21 15:59:38 -0400526 float pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500527 unsigned long long total_size, current_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400528
bigbiffbf1d6722015-02-14 16:25:59 -0500529 string backup_log = Backup_Folder + "recovery.log";
530
Dees_Troy43d8b002012-09-17 16:00:01 -0400531 if (Part == NULL)
532 return true;
533
Dees_Troy093b7642012-09-21 15:59:38 -0400534 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400535
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500536 total_size = *file_bytes + *img_bytes;
537 current_size = *file_bytes + *img_bytes - *file_bytes_remaining - *img_bytes_remaining;
that203bcc92015-05-09 17:27:33 +0200538 // Set the position
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500539 pos = ((float)(current_size) / (float)(total_size));
Dees_Troy2673cec2013-04-02 20:22:16 +0000540 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400541
Tom Hite5a926722014-09-15 01:31:03 +0000542 TWFunc::SetPerformanceMode(true);
Dees_Troy43d8b002012-09-17 16:00:01 -0400543 time(&start);
544
bigbiff7abc5fe2015-01-17 16:53:12 -0500545 if (Part->Backup(Backup_Folder, &total_size, &current_size, tar_fork_pid)) {
Tom Hite5a926722014-09-15 01:31:03 +0000546 bool md5Success = false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500547 current_size += Part->Backup_Size;
548 pos = (float)((float)(current_size) / (float)(total_size));
549 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400550 if (Part->Has_SubPartition) {
551 std::vector<TWPartition*>::iterator subpart;
552
553 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500554 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
bigbiff7abc5fe2015-01-17 16:53:12 -0500555 if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size, tar_fork_pid)) {
Tom Hite5a926722014-09-15 01:31:03 +0000556 TWFunc::SetPerformanceMode(false);
bigbiffbf1d6722015-02-14 16:25:59 -0500557 Clean_Backup_Folder(Backup_Folder);
558 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
559 tw_set_default_metadata(backup_log.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400560 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000561 }
Dees_Troy2727b992013-08-14 20:09:30 +0000562 sync();
563 sync();
Tom Hite5a926722014-09-15 01:31:03 +0000564 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName)) {
565 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400566 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000567 }
Dees_Troy093b7642012-09-21 15:59:38 -0400568 if (Part->Backup_Method == 1) {
569 *file_bytes_remaining -= (*subpart)->Backup_Size;
570 } else {
571 *img_bytes_remaining -= (*subpart)->Backup_Size;
572 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500573 current_size += Part->Backup_Size;
574 pos = (float)(current_size / total_size);
575 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400576 }
577 }
578 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400579 time(&stop);
that203bcc92015-05-09 17:27:33 +0200580 int backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000581 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400582 if (Part->Backup_Method == 1) {
583 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400584 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400585 } else {
586 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400587 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400588 }
Tom Hite5a926722014-09-15 01:31:03 +0000589
590 md5Success = Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
591 TWFunc::SetPerformanceMode(false);
592 return md5Success;
Dees_Troy43d8b002012-09-17 16:00:01 -0400593 } else {
bigbiffbf1d6722015-02-14 16:25:59 -0500594 Clean_Backup_Folder(Backup_Folder);
595 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
596 tw_set_default_metadata(backup_log.c_str());
Tom Hite5a926722014-09-15 01:31:03 +0000597 TWFunc::SetPerformanceMode(false);
Dees_Troy43d8b002012-09-17 16:00:01 -0400598 return false;
599 }
bigbiff7abc5fe2015-01-17 16:53:12 -0500600 return 0;
601}
602
bigbiffbf1d6722015-02-14 16:25:59 -0500603void TWPartitionManager::Clean_Backup_Folder(string Backup_Folder) {
604 DIR *d = opendir(Backup_Folder.c_str());
605 struct dirent *p;
606 int r;
607
Ethan Yonker74db1572015-10-28 12:44:49 -0500608 gui_msg("backup_clean=Backup Failed. Cleaning Backup Folder.");
bigbiffbf1d6722015-02-14 16:25:59 -0500609
610 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500611 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Backup_Folder)(strerror(errno)));
bigbiffbf1d6722015-02-14 16:25:59 -0500612 return;
613 }
614
Matt Mower2b18a532015-02-20 16:58:05 -0600615 while ((p = readdir(d))) {
bigbiffbf1d6722015-02-14 16:25:59 -0500616 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
617 continue;
618
619 string path = Backup_Folder + p->d_name;
620
621 size_t dot = path.find_last_of(".") + 1;
622 if (path.substr(dot) == "win" || path.substr(dot) == "md5" || path.substr(dot) == "info") {
623 r = unlink(path.c_str());
624 if (r != 0) {
625 LOGINFO("Unable to unlink '%s: %s'\n", path.c_str(), strerror(errno));
626 }
627 }
628 }
629 closedir(d);
630}
631
bigbiff7abc5fe2015-01-17 16:53:12 -0500632int TWPartitionManager::Cancel_Backup() {
633 string Backup_Folder, Backup_Name, Full_Backup_Path;
634
635 stop_backup.set_value(1);
636
637 if (tar_fork_pid != 0) {
638 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
639 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
640 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
641 LOGINFO("Killing pid: %d\n", tar_fork_pid);
642 kill(tar_fork_pid, SIGUSR2);
643 while (kill(tar_fork_pid, 0) == 0) {
644 usleep(1000);
645 }
646 LOGINFO("Backup_Run stopped and returning false, backup cancelled.\n");
647 LOGINFO("Removing directory %s\n", Full_Backup_Path.c_str());
648 TWFunc::removeDir(Full_Backup_Path, false);
649 tar_fork_pid = 0;
650 }
651
652 return 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400653}
654
655int TWPartitionManager::Run_Backup(void) {
Matt Mower2b18a532015-02-20 16:58:05 -0600656 int check, do_md5, partition_count = 0, disable_free_space_check = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500657 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400658 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 -0400659 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500660 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400661 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400662 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400663 struct tm *t;
664 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500665 size_t start_pos = 0, end_pos = 0;
bigbiff7abc5fe2015-01-17 16:53:12 -0500666 stop_backup.set_value(0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400667 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500668 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400669
670 time(&total_start);
671
672 Update_System_Details();
673
674 if (!Mount_Current_Storage(true))
675 return false;
676
677 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400678 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400679 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400680 else
681 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400682
Dees_Troy43d8b002012-09-17 16:00:01 -0400683 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
684 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker74db1572015-10-28 12:44:49 -0500685 if (Backup_Name == gui_parse_text("{@current_date}")) {
Dees Troyb21cc642013-09-10 17:36:41 +0000686 Backup_Name = TWFunc::Get_Current_Date();
Ethan Yonker74db1572015-10-28 12:44:49 -0500687 } else if (Backup_Name == gui_parse_text("{@auto_generate}") || Backup_Name == "0" || Backup_Name.empty()) {
Dees Troyb21cc642013-09-10 17:36:41 +0000688 TWFunc::Auto_Generate_Backup_Name();
689 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400690 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000691 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400692 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000693 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400694
Dees_Troy2673cec2013-04-02 20:22:16 +0000695 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500696 DataManager::GetValue("tw_backup_list", Backup_List);
697 if (!Backup_List.empty()) {
698 end_pos = Backup_List.find(";", start_pos);
699 while (end_pos != string::npos && start_pos < Backup_List.size()) {
700 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
701 backup_part = Find_Partition_By_Path(backup_path);
702 if (backup_part != NULL) {
703 partition_count++;
704 if (backup_part->Backup_Method == 1)
705 file_bytes += backup_part->Backup_Size;
706 else
707 img_bytes += backup_part->Backup_Size;
708 if (backup_part->Has_SubPartition) {
709 std::vector<TWPartition*>::iterator subpart;
710
711 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000712 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 -0500713 partition_count++;
714 if ((*subpart)->Backup_Method == 1)
715 file_bytes += (*subpart)->Backup_Size;
716 else
717 img_bytes += (*subpart)->Backup_Size;
718 }
719 }
Dees_Troy8170a922012-09-18 15:40:25 -0400720 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500721 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500722 gui_msg(Msg(msg::kError, "unable_to_locate_partition=Unable to locate '{1}' partition for backup calculations.")(backup_path));
Dees_Troy8170a922012-09-18 15:40:25 -0400723 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500724 start_pos = end_pos + 1;
725 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400726 }
727 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400728
729 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500730 gui_msg("no_partition_selected=No partitions selected for backup.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400731 return false;
732 }
733 total_bytes = file_bytes + img_bytes;
Ethan Yonker74db1572015-10-28 12:44:49 -0500734 gui_msg(Msg("total_partitions_backup= * Total number of partitions to back up: {1}")(partition_count));
735 gui_msg(Msg("total_backup_size= * Total size of all data: {1}MB")(total_bytes / 1024 / 1024));
Dees_Troy43d8b002012-09-17 16:00:01 -0400736 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
737 if (storage != NULL) {
738 free_space = storage->Free;
Ethan Yonker74db1572015-10-28 12:44:49 -0500739 gui_msg(Msg("available_space= * Available space: {1}MB")(free_space / 1024 / 1024));
Dees_Troy43d8b002012-09-17 16:00:01 -0400740 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500741 gui_err("unable_locate_storage=Unable to locate storage device.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400742 return false;
743 }
bigbiffbf1d6722015-02-14 16:25:59 -0500744
745 DataManager::GetValue("tw_disable_free_space", disable_free_space_check);
746 if (!disable_free_space_check) {
747 if (free_space - (32 * 1024 * 1024) < total_bytes) {
748 // We require an extra 32MB just in case
Ethan Yonker74db1572015-10-28 12:44:49 -0500749 gui_err("no_space=Not enough free space on storage.");
bigbiffbf1d6722015-02-14 16:25:59 -0500750 return false;
751 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400752 }
753 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500754 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400755
Ethan Yonker74db1572015-10-28 12:44:49 -0500756 gui_msg("backup_started=[BACKUP STARTED]");
757 gui_msg(Msg("backup_folder= * Backup Folder: {1}")(Full_Backup_Path));
Dees_Troyd4b22b02013-01-18 17:17:58 +0000758 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500759 gui_err("fail_backup_folder=Failed to make backup folder.");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000760 return false;
761 }
762
Dees_Troy2673cec2013-04-02 20:22:16 +0000763 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400764
Dees_Troya13d74f2013-03-24 08:54:55 -0500765 start_pos = 0;
766 end_pos = Backup_List.find(";", start_pos);
767 while (end_pos != string::npos && start_pos < Backup_List.size()) {
bigbiff7abc5fe2015-01-17 16:53:12 -0500768 if (stop_backup.get_value() != 0)
769 return -1;
Dees_Troya13d74f2013-03-24 08:54:55 -0500770 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
771 backup_part = Find_Partition_By_Path(backup_path);
772 if (backup_part != NULL) {
773 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
774 return false;
775 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500776 gui_msg(Msg(msg::kError, "unable_to_locate_partition=Unable to locate '{1}' partition for backup calculations.")(backup_path));
Dees_Troya13d74f2013-03-24 08:54:55 -0500777 }
778 start_pos = end_pos + 1;
779 end_pos = Backup_List.find(";", start_pos);
780 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400781
782 // Average BPS
783 if (img_time == 0)
784 img_time = 1;
785 if (file_time == 0)
786 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400787 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500788 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400789
Ethan Yonker74db1572015-10-28 12:44:49 -0500790 gui_msg(Msg("avg_backup_fs=Average backup rate for file systems: {1} MB/sec")(file_bps / (1024 * 1024)));
791 gui_msg(Msg("avg_backup_img=Average backup rate for imaged drives: {1} MB/sec")(img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400792
793 time(&total_stop);
794 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500795 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500796 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400797
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500798 int prev_img_bps, use_compression;
799 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400800 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
801 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500802 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400803
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500804 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400805 if (use_compression)
806 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500807 else
Dees_Troy093b7642012-09-21 15:59:38 -0400808 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
809 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500810 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400811
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500812 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400813 if (use_compression)
814 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
815 else
816 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
817
Ethan Yonker74db1572015-10-28 12:44:49 -0500818 gui_msg(Msg("total_backed_size=[{1} MB TOTAL BACKED UP]")(actual_backup_size));
Dees_Troy43d8b002012-09-17 16:00:01 -0400819 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400820 UnMount_Main_Partitions();
Ethan Yonker56db1c42015-12-20 22:49:00 -0600821 gui_msg(Msg(msg::kHighlight, "backup_completed=[BACKUP COMPLETED IN {1} SECONDS]")(total_time)); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000822 string backup_log = Full_Backup_Path + "recovery.log";
823 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600824 tw_set_default_metadata(backup_log.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000825 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400826}
827
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500828bool 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 -0400829 time_t Start, Stop;
Tom Hite5a926722014-09-15 01:31:03 +0000830 TWFunc::SetPerformanceMode(true);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400831 time(&Start);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500832 //DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Tom Hite5a926722014-09-15 01:31:03 +0000833 if (!Part->Restore(Restore_Name, total_restore_size, already_restored_size)) {
834 TWFunc::SetPerformanceMode(false);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400835 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000836 }
Dees_Troy8170a922012-09-18 15:40:25 -0400837 if (Part->Has_SubPartition) {
838 std::vector<TWPartition*>::iterator subpart;
839
840 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
841 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Tom Hite5a926722014-09-15 01:31:03 +0000842 if (!(*subpart)->Restore(Restore_Name, total_restore_size, already_restored_size)) {
843 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400844 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000845 }
Dees_Troy8170a922012-09-18 15:40:25 -0400846 }
847 }
848 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400849 time(&Stop);
Tom Hite5a926722014-09-15 01:31:03 +0000850 TWFunc::SetPerformanceMode(false);
Ethan Yonker74db1572015-10-28 12:44:49 -0500851 gui_msg(Msg("restort_part_done=[{1} done ({2} seconds)]")(Part->Backup_Display_Name)((int)difftime(Stop, Start)));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400852 return true;
853}
854
Dees_Troy51a0e822012-09-05 15:24:24 -0400855int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400856 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500857 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400858 time_t rStart, rStop;
859 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500860 string Restore_List, restore_path;
861 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500862 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400863
Ethan Yonker74db1572015-10-28 12:44:49 -0500864 gui_msg("restore_started=[RESTORE STARTED]");
865 gui_msg(Msg("restore_folder=Restore folder: '{1}'")(Restore_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -0400866
Dees_Troy4a2a1262012-09-18 09:33:47 -0400867 if (!Mount_Current_Storage(true))
868 return false;
869
870 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500871 if (check_md5 > 0) {
872 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
Ethan Yonker74db1572015-10-28 12:44:49 -0500873 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, gui_parse_text("{@verifying_md5}"));
874 gui_msg("verifying_md5=Verifying MD5");
Dees_Troya13d74f2013-03-24 08:54:55 -0500875 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500876 gui_msg("skip_md5=Skipping MD5 check based on user setting.");
Dees_Troya13d74f2013-03-24 08:54:55 -0500877 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500878 gui_msg("calc_restore=Calculating restore details...");
Dees_Troya13d74f2013-03-24 08:54:55 -0500879 DataManager::GetValue("tw_restore_selected", Restore_List);
880 if (!Restore_List.empty()) {
881 end_pos = Restore_List.find(";", start_pos);
882 while (end_pos != string::npos && start_pos < Restore_List.size()) {
883 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
884 restore_part = Find_Partition_By_Path(restore_path);
885 if (restore_part != NULL) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500886 if (restore_part->Mount_Read_Only) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500887 gui_msg(Msg(msg::kError, "restore_read_only=Cannot restore {1} -- mounted read only.")(restore_part->Backup_Display_Name));
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500888 return false;
889 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500890 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
891 return false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500892 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500893 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500894 if (restore_part->Has_SubPartition) {
895 std::vector<TWPartition*>::iterator subpart;
896
897 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
898 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400899 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500900 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500901 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500902 }
903 }
904 }
905 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500906 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -0500907 }
908 start_pos = end_pos + 1;
909 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400910 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400911 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400912
913 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500914 gui_err("no_part_restore=No partitions selected for restore.");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400915 return false;
916 }
917
Ethan Yonker74db1572015-10-28 12:44:49 -0500918 gui_msg(Msg("restore_part_count=Restoring {1} partitions...")(partition_count));
919 gui_msg(Msg("total_restore_size=Total restore size is {1}MB")(total_restore_size / 1048576));
Dees_Troy2673cec2013-04-02 20:22:16 +0000920 DataManager::SetProgress(0.0);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500921
Dees_Troya13d74f2013-03-24 08:54:55 -0500922 start_pos = 0;
923 if (!Restore_List.empty()) {
924 end_pos = Restore_List.find(";", start_pos);
925 while (end_pos != string::npos && start_pos < Restore_List.size()) {
926 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
927 restore_part = Find_Partition_By_Path(restore_path);
928 if (restore_part != NULL) {
929 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500930 if (!Restore_Partition(restore_part, Restore_Name, partition_count, &total_restore_size, &already_restored_size))
Dees_Troya13d74f2013-03-24 08:54:55 -0500931 return false;
932 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500933 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -0500934 }
935 start_pos = end_pos + 1;
936 end_pos = Restore_List.find(";", start_pos);
937 }
938 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500939 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, gui_parse_text("{@updating_system_details}"));
Dees_Troy43d8b002012-09-17 16:00:01 -0400940 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400941 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400942 time(&rStop);
Ethan Yonker74db1572015-10-28 12:44:49 -0500943 gui_msg(Msg(msg::kHighlight, "restore_complete=[RESTORE COMPLETED IN {1} SECONDS]")((int)difftime(rStop,rStart)));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500944 DataManager::SetValue("tw_file_progress", "");
Dees_Troy63c8df72012-09-10 14:02:05 -0400945 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400946}
947
948void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400949 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500950 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000951 bool get_date = true, check_encryption = true;
952
953 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400954
955 DIR* d;
956 d = opendir(Restore_Name.c_str());
957 if (d == NULL)
958 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500959 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Restore_Name)(strerror(errno)));
Dees_Troy63c8df72012-09-10 14:02:05 -0400960 return;
961 }
962
963 struct dirent* de;
964 while ((de = readdir(d)) != NULL)
965 {
966 // Strip off three components
967 char str[256];
968 char* label;
969 char* fstype = NULL;
970 char* extn = NULL;
971 char* ptr;
972
973 strcpy(str, de->d_name);
974 if (strlen(str) <= 2)
975 continue;
976
977 if (get_date) {
978 char file_path[255];
979 struct stat st;
980
981 strcpy(file_path, Restore_Name.c_str());
982 strcat(file_path, "/");
983 strcat(file_path, str);
984 stat(file_path, &st);
985 string backup_date = ctime((const time_t*)(&st.st_mtime));
986 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
987 get_date = false;
988 }
989
990 label = str;
991 ptr = label;
992 while (*ptr && *ptr != '.') ptr++;
993 if (*ptr == '.')
994 {
995 *ptr = 0x00;
996 ptr++;
997 fstype = ptr;
998 }
999 while (*ptr && *ptr != '.') ptr++;
1000 if (*ptr == '.')
1001 {
1002 *ptr = 0x00;
1003 ptr++;
1004 extn = ptr;
1005 }
1006
Dees_Troy83bd4832013-05-04 12:39:56 +00001007 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -05001008 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +00001009 if (extnlength != 3 && extnlength != 6) continue;
1010 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
1011 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
1012
1013 if (check_encryption) {
1014 string filename = Restore_Name + "/";
1015 filename += de->d_name;
1016 if (TWFunc::Get_File_Type(filename) == 2) {
1017 LOGINFO("'%s' is encrypted\n", filename.c_str());
1018 DataManager::SetValue("tw_restore_encrypted", 1);
1019 }
1020 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001021 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -04001022
1023 TWPartition* Part = Find_Partition_By_Path(label);
1024 if (Part == NULL)
1025 {
Ethan Yonker74db1572015-10-28 12:44:49 -05001026 gui_msg(Msg(msg::kError, "unable_locate_part_backup_name=Unable to locate partition by backup name: '{1}'")(label));
Dees_Troy63c8df72012-09-10 14:02:05 -04001027 continue;
1028 }
1029
1030 Part->Backup_FileName = de->d_name;
1031 if (strlen(extn) > 3) {
1032 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1033 }
1034
Dees_Troya13d74f2013-03-24 08:54:55 -05001035 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001036 }
1037 closedir(d);
1038
Dees_Troya13d74f2013-03-24 08:54:55 -05001039 // Set the final value
1040 DataManager::SetValue("tw_restore_list", Restore_List);
1041 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001042 return;
1043}
1044
1045int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001046 std::vector<TWPartition*>::iterator iter;
1047 int ret = false;
1048 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001049 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001050
1051 // Iterate through all partitions
1052 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001053 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001054 if (Path == "/and-sec")
1055 ret = (*iter)->Wipe_AndSec();
1056 else
1057 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001058 found = true;
1059 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1060 (*iter)->Wipe();
1061 }
1062 }
1063 if (found) {
1064 return ret;
1065 } else
Ethan Yonker74db1572015-10-28 12:44:49 -05001066 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Dees_Troy63c8df72012-09-10 14:02:05 -04001067 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001068}
1069
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001070int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1071 std::vector<TWPartition*>::iterator iter;
1072 int ret = false;
1073 bool found = false;
1074 string Local_Path = TWFunc::Get_Root_Path(Path);
1075
1076 // Iterate through all partitions
1077 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1078 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1079 if (Path == "/and-sec")
1080 ret = (*iter)->Wipe_AndSec();
1081 else
1082 ret = (*iter)->Wipe(New_File_System);
1083 found = true;
1084 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1085 (*iter)->Wipe(New_File_System);
1086 }
1087 }
1088 if (found) {
1089 return ret;
1090 } else
Ethan Yonker74db1572015-10-28 12:44:49 -05001091 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001092 return false;
1093}
1094
Dees_Troy51a0e822012-09-05 15:24:24 -04001095int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001096 std::vector<TWPartition*>::iterator iter;
1097 int ret = true;
1098
1099 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001100 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Ethan Yonker89583ef2015-08-26 09:01:59 -05001101#ifdef TW_OEM_BUILD
1102 if ((*iter)->Mount_Point == "/data") {
1103 if (!(*iter)->Wipe_Encryption())
1104 ret = false;
1105 } else {
1106#endif
1107 if (!(*iter)->Wipe())
1108 ret = false;
1109#ifdef TW_OEM_BUILD
1110 }
1111#endif
Dees_Troy094207a2012-09-26 12:00:39 -04001112 } else if ((*iter)->Has_Android_Secure) {
1113 if (!(*iter)->Wipe_AndSec())
1114 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001115 }
1116 }
1117 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001118}
1119
Dees_Troy38bd7602012-09-14 13:33:53 -04001120int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1121 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001122 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001123
1124 if (!Mount_By_Path("/data", true))
1125 return false;
1126
1127 if (!Mount_By_Path("/cache", true))
1128 return false;
1129
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001130 dir.push_back("/data/dalvik-cache");
1131 dir.push_back("/cache/dalvik-cache");
1132 dir.push_back("/cache/dc");
Ethan Yonker74db1572015-10-28 12:44:49 -05001133 gui_msg("wiping_dalvik=Wiping Dalvik Cache Directories...");
Dees_Troya13d74f2013-03-24 08:54:55 -05001134 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001135 if (stat(dir.at(i).c_str(), &st) == 0) {
1136 TWFunc::removeDir(dir.at(i), false);
Ethan Yonker74db1572015-10-28 12:44:49 -05001137 gui_msg(Msg("cleaned=Cleaned: {1}...")(dir.at(i)));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001138 }
1139 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001140 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001141 if (sdext && sdext->Is_Present && sdext->Mount(false))
1142 {
1143 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1144 {
1145 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Ethan Yonker74db1572015-10-28 12:44:49 -05001146 gui_msg(Msg("cleaned=Cleaned: {1}...")("/sd-ext/dalvik-cache"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001147 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001148 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001149 gui_msg("dalvik_done=-- Dalvik Cache Directories Wipe Complete!");
Dees_Troy38bd7602012-09-14 13:33:53 -04001150 return true;
1151}
1152
1153int TWPartitionManager::Wipe_Rotate_Data(void) {
1154 if (!Mount_By_Path("/data", true))
1155 return false;
1156
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001157 unlink("/data/misc/akmd*");
1158 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001159 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001160 return true;
1161}
1162
1163int TWPartitionManager::Wipe_Battery_Stats(void) {
1164 struct stat st;
1165
1166 if (!Mount_By_Path("/data", true))
1167 return false;
1168
1169 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001170 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001171 } else {
1172 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001173 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001174 }
1175 return true;
1176}
1177
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001178int TWPartitionManager::Wipe_Android_Secure(void) {
1179 std::vector<TWPartition*>::iterator iter;
1180 int ret = false;
1181 bool found = false;
1182
1183 // Iterate through all partitions
1184 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1185 if ((*iter)->Has_Android_Secure) {
1186 ret = (*iter)->Wipe_AndSec();
1187 found = true;
1188 }
1189 }
1190 if (found) {
1191 return ret;
1192 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001193 gui_err("no_andsec=No android secure partitions found.");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001194 }
1195 return false;
1196}
1197
Dees_Troy38bd7602012-09-14 13:33:53 -04001198int TWPartitionManager::Format_Data(void) {
1199 TWPartition* dat = Find_Partition_By_Path("/data");
1200
1201 if (dat != NULL) {
1202 if (!dat->UnMount(true))
1203 return false;
1204
1205 return dat->Wipe_Encryption();
1206 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001207 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1].")("/data"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001208 return false;
1209 }
1210 return false;
1211}
1212
1213int TWPartitionManager::Wipe_Media_From_Data(void) {
1214 TWPartition* dat = Find_Partition_By_Path("/data");
1215
1216 if (dat != NULL) {
1217 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001218 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001219 return false;
1220 }
1221 if (!dat->Mount(true))
1222 return false;
1223
Ethan Yonker74db1572015-10-28 12:44:49 -05001224 gui_msg("wiping_datamedia=Wiping internal storage -- /data/media...");
Ethan Yonker726a0202014-12-16 20:01:38 -06001225 Remove_MTP_Storage(dat->MTP_Storage_ID);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001226 TWFunc::removeDir("/data/media", false);
xiaolu9416f4f2015-06-04 08:22:23 +08001227 dat->Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001228 Add_MTP_Storage(dat->MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001229 return true;
1230 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001231 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1].")("/data"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001232 return false;
1233 }
1234 return false;
1235}
1236
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001237int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1238 std::vector<TWPartition*>::iterator iter;
1239 int ret = false;
1240 bool found = false;
1241 string Local_Path = TWFunc::Get_Root_Path(Path);
1242
1243 if (Local_Path == "/tmp" || Local_Path == "/")
1244 return true;
1245
1246 // Iterate through all partitions
1247 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1248 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1249 ret = (*iter)->Repair();
1250 found = true;
1251 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1252 (*iter)->Repair();
1253 }
1254 }
1255 if (found) {
1256 return ret;
1257 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001258 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001259 } else {
1260 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1261 }
1262 return false;
1263}
1264
Ethan Yonkera2719152015-05-28 09:44:41 -05001265int TWPartitionManager::Resize_By_Path(string Path, bool Display_Error) {
1266 std::vector<TWPartition*>::iterator iter;
1267 int ret = false;
1268 bool found = false;
1269 string Local_Path = TWFunc::Get_Root_Path(Path);
1270
1271 if (Local_Path == "/tmp" || Local_Path == "/")
1272 return true;
1273
1274 // Iterate through all partitions
1275 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1276 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1277 ret = (*iter)->Resize();
1278 found = true;
1279 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1280 (*iter)->Resize();
1281 }
1282 }
1283 if (found) {
1284 return ret;
1285 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001286 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Ethan Yonkera2719152015-05-28 09:44:41 -05001287 } else {
1288 LOGINFO("Resize: Unable to find partition for path '%s'\n", Local_Path.c_str());
1289 }
1290 return false;
1291}
1292
Dees_Troy51a0e822012-09-05 15:24:24 -04001293void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001294 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001295 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001296
Ethan Yonker74db1572015-10-28 12:44:49 -05001297 gui_msg("update_part_details=Updating partition details...");
Dees_Troy5bf43922012-09-07 16:07:55 -04001298 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001299 if ((*iter)->Can_Be_Mounted) {
1300 (*iter)->Update_Size(true);
1301 if ((*iter)->Mount_Point == "/system") {
1302 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1303 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1304 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1305 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1306 } else if ((*iter)->Mount_Point == "/cache") {
1307 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1308 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1309 } else if ((*iter)->Mount_Point == "/sd-ext") {
1310 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1311 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1312 if ((*iter)->Backup_Size == 0) {
1313 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1314 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1315 } else
1316 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001317 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001318 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001319 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001320 if ((*iter)->Backup_Size == 0) {
1321 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1322 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1323 } else
1324 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001325 } else if ((*iter)->Mount_Point == "/boot") {
1326 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1327 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1328 if ((*iter)->Backup_Size == 0) {
1329 DataManager::SetValue("tw_has_boot_partition", 0);
1330 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1331 } else
1332 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001333 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001334#ifdef SP1_NAME
1335 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1336 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1337 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1338 }
1339#endif
1340#ifdef SP2_NAME
1341 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1342 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1343 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1344 }
1345#endif
1346#ifdef SP3_NAME
1347 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1348 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1349 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1350 }
1351#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001352 } else {
1353 // Handle unmountable partitions in case we reset defaults
1354 if ((*iter)->Mount_Point == "/boot") {
1355 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1356 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1357 if ((*iter)->Backup_Size == 0) {
1358 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1359 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1360 } else
1361 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1362 } else if ((*iter)->Mount_Point == "/recovery") {
1363 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1364 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1365 if ((*iter)->Backup_Size == 0) {
1366 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1367 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1368 } else
1369 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001370 } else if ((*iter)->Mount_Point == "/data") {
1371 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001372 }
1373#ifdef SP1_NAME
1374 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1375 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1376 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1377 }
1378#endif
1379#ifdef SP2_NAME
1380 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1381 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1382 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1383 }
1384#endif
1385#ifdef SP3_NAME
1386 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1387 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1388 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1389 }
1390#endif
Dees_Troy51127312012-09-08 13:08:49 -04001391 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001392 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001393 gui_msg("update_part_details_done=...done");
Dees_Troy51127312012-09-08 13:08:49 -04001394 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1395 string current_storage_path = DataManager::GetCurrentStoragePath();
1396 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001397 if (FreeStorage != NULL) {
1398 // Attempt to mount storage
1399 if (!FreeStorage->Mount(false)) {
Matt Mower2d50cad2015-12-03 22:26:55 -06001400 gui_msg(Msg(msg::kError, "unable_to_mount_storage=Unable to mount storage"));
1401 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
Dees_Troy8170a922012-09-18 15:40:25 -04001402 } else {
1403 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1404 }
1405 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001406 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001407 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001408 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001409 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001410 return;
1411}
1412
1413int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001414#ifdef TW_INCLUDE_CRYPTO
1415 int ret_val, password_len;
Ethan Yonkera1de1492015-11-04 11:58:27 -06001416 char crypto_state[PROPERTY_VALUE_MAX], crypto_blkdev[PROPERTY_VALUE_MAX], cPassword[255];
Dees_Troy5bf43922012-09-07 16:07:55 -04001417 size_t result;
Ethan Yonker253368a2014-11-25 15:00:52 -06001418 std::vector<TWPartition*>::iterator iter;
Dees_Troy5bf43922012-09-07 16:07:55 -04001419
Ethan Yonker253368a2014-11-25 15:00:52 -06001420 // Mount any partitions that need to be mounted for decrypt
1421 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1422 if ((*iter)->Mount_To_Decrypt) {
1423 (*iter)->Mount(true);
1424 }
a39552696ff55ce2013-01-08 16:14:56 +00001425 }
a39552696ff55ce2013-01-08 16:14:56 +00001426
Ethan Yonkera1de1492015-11-04 11:58:27 -06001427 property_get("ro.crypto.state", crypto_state, "error");
1428 if (strcmp(crypto_state, "error") == 0) {
1429 property_set("ro.crypto.state", "encrypted");
1430 // Sleep for a bit so that services can start if needed
1431 sleep(1);
1432 }
1433
Dees_Troy5bf43922012-09-07 16:07:55 -04001434 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001435 int pwret = cryptfs_check_passwd(cPassword);
1436
Ethan Yonker253368a2014-11-25 15:00:52 -06001437 // Unmount any partitions that were needed for decrypt
1438 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1439 if ((*iter)->Mount_To_Decrypt) {
1440 (*iter)->UnMount(false);
1441 }
1442 }
1443
a39552696ff55ce2013-01-08 16:14:56 +00001444 if (pwret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001445 gui_err("fail_decrypt=Failed to decrypt data.");
Dees_Troy5bf43922012-09-07 16:07:55 -04001446 return -1;
1447 }
a39552696ff55ce2013-01-08 16:14:56 +00001448
Dees_Troy5bf43922012-09-07 16:07:55 -04001449 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1450 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001451 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001452 } else {
1453 TWPartition* dat = Find_Partition_By_Path("/data");
1454 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001455 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001456 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1457 dat->Is_Decrypted = true;
1458 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001459 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001460 dat->Current_File_System = dat->Fstab_File_System; // Needed if we're ignoring blkid because encrypted devices start out as emmc
Ethan Yonker74db1572015-10-28 12:44:49 -05001461 gui_msg(Msg("decrypt_success=Data successfully decrypted, new block device: '{1}'")(crypto_blkdev));
a39552696ff55ce2013-01-08 16:14:56 +00001462
Dees_Troy5bf43922012-09-07 16:07:55 -04001463 // Sleep for a bit so that the device will be ready
1464 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001465 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001466 dat->Storage_Path = "/data/media/0";
1467 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001468 DataManager::SetValue("tw_storage_path", "/data/media/0");
Ethan Yonkerec0fa4a2014-11-20 09:51:03 -06001469 DataManager::SetValue("tw_settings_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001470 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001471 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001472 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001473 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001474 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001475 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001476 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001477 }
1478 return 0;
1479#else
Ethan Yonker74db1572015-10-28 12:44:49 -05001480 gui_err("no_crypto_support=No crypto support was compiled into this build.");
Dees_Troy5bf43922012-09-07 16:07:55 -04001481 return -1;
1482#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001483 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001484}
1485
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001486int TWPartitionManager::Fix_Permissions(void) {
1487 int result = 0;
1488 if (!Mount_By_Path("/data", true))
1489 return false;
1490
1491 if (!Mount_By_Path("/system", true))
1492 return false;
1493
1494 Mount_By_Path("/sd-ext", false);
1495
1496 fixPermissions perms;
1497 result = perms.fixPerms(true, false);
thata3d31fb2014-12-21 22:27:40 +01001498#ifdef HAVE_SELINUX
1499 if (result == 0 && DataManager::GetIntValue("tw_fixperms_restorecon") == 1)
1500 result = perms.fixContexts();
1501#endif
Dees_Troy1a650e62012-10-19 20:54:32 -04001502 UnMount_Main_Partitions();
Ethan Yonker74db1572015-10-28 12:44:49 -05001503 gui_msg("done=Done.");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001504 return result;
1505}
1506
Ethan Yonker47360be2014-04-01 10:34:34 -05001507TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1508 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1509
1510 if (!Path.empty()) {
1511 string Search_Path = TWFunc::Get_Root_Path(Path);
1512 for (; iter != Partitions.end(); iter++) {
1513 if ((*iter)->Mount_Point == Search_Path) {
1514 iter++;
1515 break;
1516 }
1517 }
1518 }
1519
1520 for (; iter != Partitions.end(); iter++) {
1521 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) {
1522 return (*iter);
1523 }
1524 }
1525
1526 return NULL;
1527}
1528
Dees_Troyd21618c2012-10-14 18:48:49 -04001529int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001530 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1531
1532 if (Part == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001533 LOGINFO("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
1534 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Partition_Path));
Dees_Troyd21618c2012-10-14 18:48:49 -04001535 return false;
1536 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001537 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1538 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001539 return false;
1540
Dees_Troy2673cec2013-04-02 20:22:16 +00001541 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1542 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001543 return false;
1544 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001545 return true;
1546}
1547
Dees_Troy8170a922012-09-18 15:40:25 -04001548int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001549 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001550 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001551 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001552
Dees_Troy8170a922012-09-18 15:40:25 -04001553 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001554 string Lun_File_str = CUSTOM_LUN_FILE;
1555 size_t found = Lun_File_str.find("%");
1556 if (found != string::npos) {
1557 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1558 if (TWFunc::Path_Exists(lun_file))
1559 has_multiple_lun = true;
1560 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001561 mtp_was_enabled = TWFunc::Toggle_MTP(false); // Must disable MTP for USB Storage
Ethan Yonker47360be2014-04-01 10:34:34 -05001562 if (!has_multiple_lun) {
1563 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1564 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1565 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
1566 TWPartition* Mount = Find_Next_Storage("", "/data");
1567 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001568 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1569 goto error_handle;
1570 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001571 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001572 gui_err("unable_locate_storage=Unable to locate storage device.");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001573 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001574 }
1575 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001576 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001577 }
Dees_Troy8170a922012-09-18 15:40:25 -04001578 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001579 LOGINFO("Device has multiple lun files\n");
1580 TWPartition* Mount1;
1581 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001582 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker47360be2014-04-01 10:34:34 -05001583 Mount1 = Find_Next_Storage("", "/data");
1584 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001585 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1586 goto error_handle;
1587 }
Matt Moweree717062014-04-26 01:46:46 -05001588 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker47360be2014-04-01 10:34:34 -05001589 Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data");
1590 if (Mount2) {
Matt Moweree717062014-04-26 01:46:46 -05001591 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001592 }
1593 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001594 gui_err("unable_locate_storage=Unable to locate storage device.");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001595 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001596 }
Dees_Troy8170a922012-09-18 15:40:25 -04001597 }
Matt Mowerb6ef4782014-11-06 02:24:36 -06001598 property_set("sys.storage.ums_enabled", "1");
HandyMenny37d42992014-10-26 22:15:59 +01001599 property_set("sys.usb.config", "mass_storage,adb");
Dees_Troy8170a922012-09-18 15:40:25 -04001600 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001601error_handle:
1602 if (mtp_was_enabled)
1603 if (!Enable_MTP())
1604 Disable_MTP();
1605 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001606}
1607
1608int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001609 int index, ret;
1610 char lun_file[255], ch[2] = {0, 0};
1611 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001612
1613 for (index=0; index<2; index++) {
1614 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001615 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001616 if (ret < 0) {
1617 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001618 }
Dees_Troy8170a922012-09-18 15:40:25 -04001619 }
Dees_Troye58d5262012-09-21 12:27:57 -04001620 Mount_All_Storage();
1621 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001622 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001623 property_set("sys.storage.ums_enabled", "0");
HandyMenny37d42992014-10-26 22:15:59 +01001624 property_set("sys.usb.config", "adb");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001625 if (mtp_was_enabled)
1626 if (!Enable_MTP())
1627 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001628 if (ret < 0 && index == 0) {
1629 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1630 return false;
1631 } else {
1632 return true;
1633 }
Dees_Troy8170a922012-09-18 15:40:25 -04001634 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001635}
1636
1637void TWPartitionManager::Mount_All_Storage(void) {
1638 std::vector<TWPartition*>::iterator iter;
1639
1640 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1641 if ((*iter)->Is_Storage)
1642 (*iter)->Mount(false);
1643 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001644}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001645
Dees_Troyd0384ef2012-10-12 12:15:42 -04001646void TWPartitionManager::UnMount_Main_Partitions(void) {
1647 // Unmounts system and data if data is not data/media
1648 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001649 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001650
1651 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1652
1653 UnMount_By_Path("/system", true);
Ethan Yonker6277c792014-09-15 14:54:30 -05001654 if (!datamedia)
1655 UnMount_By_Path("/data", true);
1656
Dees_Troyd0384ef2012-10-12 12:15:42 -04001657 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1658 Boot_Partition->UnMount(true);
1659}
1660
Dees_Troy9350b8d2012-09-27 12:38:38 -04001661int TWPartitionManager::Partition_SDCard(void) {
1662 char mkdir_path[255], temp[255], line[512];
Ethan Yonker483e9f42016-01-11 22:21:18 -06001663 string Storage_Path, Command, Device, fat_str, ext_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
Dees_Troy9350b8d2012-09-27 12:38:38 -04001664 int ext, swap, total_size = 0, fat_size;
Dees_Troy9350b8d2012-09-27 12:38:38 -04001665
Ethan Yonker74db1572015-10-28 12:44:49 -05001666 gui_msg("start_partition_sd=Partitioning SD Card...");
Ethan Yonker483e9f42016-01-11 22:21:18 -06001667
1668 // Locate and validate device to partition
1669 TWPartition* SDCard = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
1670
Ethan Yonker1eff6cd2014-09-15 13:30:42 -05001671 if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001672 gui_err("partition_sd_locate=Unable to locate device to partition.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001673 return false;
1674 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001675
1676 // Unmount everything
Dees_Troy9350b8d2012-09-27 12:38:38 -04001677 if (!SDCard->UnMount(true))
1678 return false;
1679 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1680 if (SDext != NULL) {
1681 if (!SDext->UnMount(true))
1682 return false;
1683 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001684 char* swappath = getenv("SWAPPATH");
1685 if (swappath != NULL) {
1686 LOGINFO("Unmounting swap at '%s'\n", swappath);
1687 umount(swappath);
1688 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001689
Ethan Yonker483e9f42016-01-11 22:21:18 -06001690 // Determine block device
1691 if (SDCard->Alternate_Block_Device.empty()) {
1692 SDCard->Find_Actual_Block_Device();
1693 Device = SDCard->Actual_Block_Device;
1694 // Just use the root block device
1695 Device.resize(strlen("/dev/block/mmcblkX"));
1696 } else {
1697 Device = SDCard->Alternate_Block_Device;
1698 }
Dees_Troy9350b8d2012-09-27 12:38:38 -04001699
1700 // Find the size of the block device:
Ethan Yonker483e9f42016-01-11 22:21:18 -06001701 total_size = (int)(TWFunc::IOCTL_Get_Block_Size(Device.c_str()) / (1048576));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001702
1703 DataManager::GetValue("tw_sdext_size", ext);
1704 DataManager::GetValue("tw_swap_size", swap);
1705 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1706 fat_size = total_size - ext - swap;
Ethan Yonker483e9f42016-01-11 22:21:18 -06001707 LOGINFO("sd card mount point %s block device is '%s', sdcard size is: %iMB, fat size: %iMB, ext size: %iMB, ext system: '%s', swap size: %iMB\n", DataManager::GetCurrentStoragePath().c_str(), Device.c_str(), total_size, fat_size, ext, ext_format.c_str(), swap);
1708
1709 // Determine partition sizes
1710 if (swap == 0 && ext == 0) {
1711 fat_str = "-0";
1712 } else {
1713 memset(temp, 0, sizeof(temp));
1714 sprintf(temp, "%i", fat_size);
1715 fat_str = temp;
1716 fat_str += "MB";
1717 }
1718 if (swap == 0) {
1719 ext_str = "-0";
1720 } else {
1721 memset(temp, 0, sizeof(temp));
1722 sprintf(temp, "%i", ext);
1723 ext_str = "+";
1724 ext_str += temp;
1725 ext_str += "MB";
1726 }
1727
Dees_Troy9350b8d2012-09-27 12:38:38 -04001728 if (ext + swap > total_size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001729 gui_err("ext_swap_size=EXT + Swap size is larger than sdcard size.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001730 return false;
1731 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001732
Ethan Yonker74db1572015-10-28 12:44:49 -05001733 gui_msg("remove_part_table=Removing partition table...");
Ethan Yonker483e9f42016-01-11 22:21:18 -06001734 Command = "sgdisk --zap-all " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001735 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001736 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001737 gui_err("unable_rm_part=Unable to remove partition table.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001738 Update_System_Details();
1739 return false;
1740 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001741 gui_msg(Msg("create_part=Creating {1} partition...")("FAT32"));
Ethan Yonker483e9f42016-01-11 22:21:18 -06001742 Command = "sgdisk --new=0:0:" + fat_str + " --change-name=0:\"Microsoft basic data\" " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001743 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001744 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001745 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("FAT32"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001746 return false;
1747 }
1748 if (ext > 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001749 gui_msg(Msg("create_part=Creating {1} partition...")("EXT"));
Ethan Yonker483e9f42016-01-11 22:21:18 -06001750 Command = "sgdisk --new=0:0:" + ext_str + " --change-name=0:\"Linux filesystem\" " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001751 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001752 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001753 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("EXT"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001754 Update_System_Details();
1755 return false;
1756 }
1757 }
1758 if (swap > 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001759 gui_msg(Msg("create_part=Creating {1} partition...")("swap"));
Ethan Yonker483e9f42016-01-11 22:21:18 -06001760 Command = "sgdisk --new=0:0:-0 --change-name=0:\"Linux swap\" --typecode=0:0657FD6D-A4AB-43C4-84E5-0933C84B4F4F " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001761 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001762 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001763 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("swap"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001764 Update_System_Details();
1765 return false;
1766 }
1767 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001768
1769 // Convert GPT to MBR
1770 Command = "sgdisk --gpttombr " + Device;
1771 if (TWFunc::Exec_Cmd(Command) != 0)
1772 LOGINFO("Failed to covert partition GPT to MBR\n");
1773
1774 // Tell the kernel to rescan the partition table
1775 int fd = open(Device.c_str(), O_RDONLY);
1776 ioctl(fd, BLKRRPART, 0);
1777 close(fd);
1778
1779 string format_device = Device;
1780 if (Device.substr(0, 17) == "/dev/block/mmcblk")
1781 format_device += "p";
1782
1783 // Format new partitions to proper file system
1784 if (fat_size > 0) {
1785 Command = "mkfs.fat " + format_device + "1";
1786 TWFunc::Exec_Cmd(Command);
1787 }
Dees_Troy9350b8d2012-09-27 12:38:38 -04001788 if (ext > 0) {
1789 if (SDext == NULL) {
Ethan Yonker483e9f42016-01-11 22:21:18 -06001790 Command = "mke2fs -t " + ext_format + " -m 0 " + format_device + "2";
1791 gui_msg(Msg("format_sdext_as=Formatting sd-ext as {1}...")(ext_format));
1792 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
1793 TWFunc::Exec_Cmd(Command);
1794 } else {
1795 SDext->Wipe(ext_format);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001796 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001797 }
1798 if (swap > 0) {
1799 Command = "mkswap " + format_device;
1800 if (ext > 0)
1801 Command += "3";
1802 else
1803 Command += "2";
Vojtech Bocek05534202013-09-11 08:11:56 +02001804 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001805 }
1806
Ethan Yonker483e9f42016-01-11 22:21:18 -06001807 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1808 if (SDCard->Mount(true)) {
1809 string TWRP_Folder = SDCard->Mount_Point + "/TWRP";
1810 mkdir(TWRP_Folder.c_str(), 0777);
1811 DataManager::Flush();
1812 }
1813
Dees_Troy9350b8d2012-09-27 12:38:38 -04001814 Update_System_Details();
Ethan Yonker74db1572015-10-28 12:44:49 -05001815 gui_msg("part_complete=Partitioning complete.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001816 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001817}
Dees_Troya13d74f2013-03-24 08:54:55 -05001818
1819void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1820 std::vector<TWPartition*>::iterator iter;
1821 if (ListType == "mount") {
1822 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1823 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1824 struct PartitionList part;
1825 part.Display_Name = (*iter)->Display_Name;
1826 part.Mount_Point = (*iter)->Mount_Point;
1827 part.selected = (*iter)->Is_Mounted();
1828 Partition_List->push_back(part);
1829 }
1830 }
1831 } else if (ListType == "storage") {
1832 char free_space[255];
1833 string Current_Storage = DataManager::GetCurrentStoragePath();
1834 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1835 if ((*iter)->Is_Storage) {
1836 struct PartitionList part;
1837 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1838 part.Display_Name = (*iter)->Storage_Name + " (";
1839 part.Display_Name += free_space;
1840 part.Display_Name += "MB)";
1841 part.Mount_Point = (*iter)->Storage_Path;
1842 if ((*iter)->Storage_Path == Current_Storage)
1843 part.selected = 1;
1844 else
1845 part.selected = 0;
1846 Partition_List->push_back(part);
1847 }
1848 }
1849 } else if (ListType == "backup") {
1850 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001851 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001852 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001853 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001854 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001855 Backup_Size = (*iter)->Backup_Size;
1856 if ((*iter)->Has_SubPartition) {
1857 std::vector<TWPartition*>::iterator subpart;
1858
1859 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1860 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1861 Backup_Size += (*subpart)->Backup_Size;
1862 }
1863 }
1864 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001865 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1866 part.Display_Name += backup_size;
1867 part.Display_Name += "MB)";
1868 part.Mount_Point = (*iter)->Backup_Path;
1869 part.selected = 0;
1870 Partition_List->push_back(part);
1871 }
1872 }
1873 } else if (ListType == "restore") {
1874 string Restore_List, restore_path;
1875 TWPartition* restore_part = NULL;
1876
1877 DataManager::GetValue("tw_restore_list", Restore_List);
1878 if (!Restore_List.empty()) {
1879 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1880 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1881 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05001882 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00001883 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001884 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05001885 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05001886 } else {
1887 struct PartitionList part;
1888 part.Display_Name = restore_part->Backup_Display_Name;
1889 part.Mount_Point = restore_part->Backup_Path;
1890 part.selected = 1;
1891 Partition_List->push_back(part);
1892 }
1893 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001894 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -05001895 }
1896 start_pos = end_pos + 1;
1897 end_pos = Restore_List.find(";", start_pos);
1898 }
1899 }
1900 } else if (ListType == "wipe") {
1901 struct PartitionList dalvik;
Ethan Yonker74db1572015-10-28 12:44:49 -05001902 dalvik.Display_Name = gui_parse_text("{@dalvik}");
Dees_Troya13d74f2013-03-24 08:54:55 -05001903 dalvik.Mount_Point = "DALVIK";
1904 dalvik.selected = 0;
1905 Partition_List->push_back(dalvik);
1906 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1907 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1908 struct PartitionList part;
1909 part.Display_Name = (*iter)->Display_Name;
1910 part.Mount_Point = (*iter)->Mount_Point;
1911 part.selected = 0;
1912 Partition_List->push_back(part);
1913 }
1914 if ((*iter)->Has_Android_Secure) {
1915 struct PartitionList part;
1916 part.Display_Name = (*iter)->Backup_Display_Name;
1917 part.Mount_Point = (*iter)->Backup_Path;
1918 part.selected = 0;
1919 Partition_List->push_back(part);
1920 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001921 if ((*iter)->Has_Data_Media) {
1922 struct PartitionList datamedia;
1923 datamedia.Display_Name = (*iter)->Storage_Name;
1924 datamedia.Mount_Point = "INTERNAL";
1925 datamedia.selected = 0;
1926 Partition_List->push_back(datamedia);
1927 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001928 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06001929 } else if (ListType == "flashimg") {
1930 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1931 if ((*iter)->Can_Flash_Img && (*iter)->Is_Present) {
1932 struct PartitionList part;
1933 part.Display_Name = (*iter)->Backup_Display_Name;
1934 part.Mount_Point = (*iter)->Backup_Path;
1935 part.selected = 0;
1936 Partition_List->push_back(part);
1937 }
1938 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001939 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001940 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001941 }
1942}
1943
1944int TWPartitionManager::Fstab_Processed(void) {
1945 return Partitions.size();
1946}
Dees_Troyd93bda52013-07-03 19:55:19 +00001947
1948void TWPartitionManager::Output_Storage_Fstab(void) {
1949 std::vector<TWPartition*>::iterator iter;
1950 char storage_partition[255];
1951 string Temp;
1952 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
1953
1954 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001955 gui_msg(Msg(msg::kError, "unable_to_open=Unable to open '{1}'.")("/cache/recovery/storage.fstab"));
Dees_Troyd93bda52013-07-03 19:55:19 +00001956 return;
1957 }
1958
1959 // Iterate through all partitions
1960 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1961 if ((*iter)->Is_Storage) {
1962 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
1963 strcpy(storage_partition, Temp.c_str());
1964 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
1965 }
1966 }
1967 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001968}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02001969
1970TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
1971{
1972 TWPartition *res = NULL;
1973 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
1974 if(!(*iter)->Is_Storage)
1975 continue;
1976
1977 if((*iter)->Is_Settings_Storage)
1978 return *iter;
1979
1980 if(!res)
1981 res = *iter;
1982 }
1983 return res;
1984}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001985
1986bool TWPartitionManager::Enable_MTP(void) {
1987#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001988 if (mtppid) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001989 gui_err("mtp_already_enabled=MTP already enabled");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001990 return true;
1991 }
1992 //Launch MTP Responder
1993 LOGINFO("Starting MTP\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06001994
1995 int mtppipe[2];
1996
1997 if (pipe(mtppipe) < 0) {
1998 LOGERR("Error creating MTP pipe\n");
1999 return false;
2000 }
2001
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002002 char old_value[PROPERTY_VALUE_MAX];
2003 property_get("sys.usb.config", old_value, "error");
2004 if (strcmp(old_value, "error") != 0 && strcmp(old_value, "mtp,adb") != 0) {
2005 char vendor[PROPERTY_VALUE_MAX];
2006 char product[PROPERTY_VALUE_MAX];
2007 property_set("sys.usb.config", "none");
2008 property_get("usb.vendor", vendor, "18D1");
2009 property_get("usb.product.mtpadb", product, "4EE2");
2010 string vendorstr = vendor;
2011 string productstr = product;
2012 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2013 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
2014 property_set("sys.usb.config", "mtp,adb");
2015 }
Ethan Yonker6d154c42014-09-03 14:19:43 -05002016 /* To enable MTP debug, use the twrp command line feature to
2017 * twrp set tw_mtp_debug 1
2018 */
2019 twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug"));
Ethan Yonker1b039202015-01-30 10:08:48 -06002020 mtppid = mtp->forkserver(mtppipe);
2021 if (mtppid) {
2022 close(mtppipe[0]); // Host closes read side
2023 mtp_write_fd = mtppipe[1];
2024 DataManager::SetValue("tw_mtp_enabled", 1);
2025 Add_All_MTP_Storage();
2026 return true;
Ethan Yonker726a0202014-12-16 20:01:38 -06002027 } else {
2028 close(mtppipe[0]);
2029 close(mtppipe[1]);
Ethan Yonker74db1572015-10-28 12:44:49 -05002030 gui_err("mtp_fail=Failed to enable MTP");
Ethan Yonker1b039202015-01-30 10:08:48 -06002031 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002032 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002033#else
Ethan Yonker74db1572015-10-28 12:44:49 -05002034 gui_err("no_mtp=MTP support not included");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002035#endif
2036 DataManager::SetValue("tw_mtp_enabled", 0);
2037 return false;
2038}
2039
Ethan Yonker1b039202015-01-30 10:08:48 -06002040void TWPartitionManager::Add_All_MTP_Storage(void) {
2041#ifdef TW_HAS_MTP
2042 std::vector<TWPartition*>::iterator iter;
2043
2044 if (!mtppid)
2045 return; // MTP is not enabled
2046
2047 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2048 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false))
2049 Add_Remove_MTP_Storage((*iter), MTP_MESSAGE_ADD_STORAGE);
2050 }
2051#else
2052 return;
2053#endif
2054}
2055
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002056bool TWPartitionManager::Disable_MTP(void) {
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002057 char old_value[PROPERTY_VALUE_MAX];
2058 property_get("sys.usb.config", old_value, "error");
2059 if (strcmp(old_value, "adb") != 0) {
2060 char vendor[PROPERTY_VALUE_MAX];
2061 char product[PROPERTY_VALUE_MAX];
2062 property_set("sys.usb.config", "none");
2063 property_get("usb.vendor", vendor, "18D1");
2064 property_get("usb.product.adb", product, "D002");
2065 string vendorstr = vendor;
2066 string productstr = product;
2067 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2068 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
2069 usleep(2000);
2070 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002071#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002072 if (mtppid) {
Ethan Yonker8613dc02014-09-11 09:28:20 -05002073 LOGINFO("Disabling MTP\n");
2074 int status;
2075 kill(mtppid, SIGKILL);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002076 mtppid = 0;
Ethan Yonker8613dc02014-09-11 09:28:20 -05002077 // We don't care about the exit value, but this prevents a zombie process
2078 waitpid(mtppid, &status, 0);
Ethan Yonker726a0202014-12-16 20:01:38 -06002079 close(mtp_write_fd);
2080 mtp_write_fd = -1;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002081 }
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002082#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002083 property_set("sys.usb.config", "adb");
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002084#ifdef TW_HAS_MTP
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002085 DataManager::SetValue("tw_mtp_enabled", 0);
2086 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002087#endif
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002088 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002089}
Ethan Yonker726a0202014-12-16 20:01:38 -06002090
2091TWPartition* TWPartitionManager::Find_Partition_By_MTP_Storage_ID(unsigned int Storage_ID) {
2092 std::vector<TWPartition*>::iterator iter;
2093
2094 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2095 if ((*iter)->MTP_Storage_ID == Storage_ID)
2096 return (*iter);
2097 }
2098 return NULL;
2099}
2100
2101bool TWPartitionManager::Add_Remove_MTP_Storage(TWPartition* Part, int message_type) {
2102#ifdef TW_HAS_MTP
2103 struct mtpmsg mtp_message;
2104
2105 if (!mtppid)
2106 return false; // MTP is disabled
2107
2108 if (mtp_write_fd < 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002109 LOGINFO("MTP: mtp_write_fd is not set\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06002110 return false;
2111 }
2112
2113 if (Part) {
Ethan Yonker4bfabab2014-12-29 09:15:37 -06002114 if (Part->MTP_Storage_ID == 0)
2115 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002116 if (message_type == MTP_MESSAGE_REMOVE_STORAGE) {
2117 mtp_message.message_type = MTP_MESSAGE_REMOVE_STORAGE; // Remove
2118 LOGINFO("sending message to remove %i\n", Part->MTP_Storage_ID);
2119 mtp_message.storage_id = Part->MTP_Storage_ID;
2120 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002121 LOGINFO("error sending message to remove storage %i\n", Part->MTP_Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002122 return false;
2123 } else {
2124 LOGINFO("Message sent, remove storage ID: %i\n", Part->MTP_Storage_ID);
2125 return true;
2126 }
2127 } else if (message_type == MTP_MESSAGE_ADD_STORAGE && Part->Is_Mounted()) {
2128 mtp_message.message_type = MTP_MESSAGE_ADD_STORAGE; // Add
2129 mtp_message.storage_id = Part->MTP_Storage_ID;
2130 mtp_message.path = Part->Storage_Path.c_str();
2131 mtp_message.display = Part->Storage_Name.c_str();
2132 mtp_message.maxFileSize = Part->Get_Max_FileSize();
Ethan Yonker1b039202015-01-30 10:08:48 -06002133 LOGINFO("sending message to add %i '%s' '%s'\n", mtp_message.storage_id, mtp_message.path, mtp_message.display);
Ethan Yonker726a0202014-12-16 20:01:38 -06002134 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002135 LOGINFO("error sending message to add storage %i\n", Part->MTP_Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002136 return false;
2137 } else {
2138 LOGINFO("Message sent, add storage ID: %i\n", Part->MTP_Storage_ID);
2139 return true;
2140 }
2141 } else {
2142 LOGERR("Unknown MTP message type: %i\n", message_type);
2143 }
2144 } else {
2145 // This hopefully never happens as the error handling should
2146 // occur in the calling function.
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002147 LOGINFO("TWPartitionManager::Add_Remove_MTP_Storage NULL partition given\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06002148 }
2149 return true;
2150#else
Ethan Yonker74db1572015-10-28 12:44:49 -05002151 gui_err("no_mtp=MTP support not included");
Ethan Yonker726a0202014-12-16 20:01:38 -06002152 DataManager::SetValue("tw_mtp_enabled", 0);
2153 return false;
2154#endif
2155}
2156
2157bool TWPartitionManager::Add_MTP_Storage(string Mount_Point) {
2158#ifdef TW_HAS_MTP
2159 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2160 if (Part) {
2161 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2162 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002163 LOGINFO("TWFunc::Add_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -06002164 }
2165#endif
2166 return false;
2167}
2168
2169bool TWPartitionManager::Add_MTP_Storage(unsigned int Storage_ID) {
2170#ifdef TW_HAS_MTP
2171 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2172 if (Part) {
2173 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2174 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002175 LOGINFO("TWFunc::Add_MTP_Storage unable to locate partition for %i\n", Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002176 }
2177#endif
2178 return false;
2179}
2180
2181bool TWPartitionManager::Remove_MTP_Storage(string Mount_Point) {
2182#ifdef TW_HAS_MTP
2183 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2184 if (Part) {
2185 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2186 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002187 LOGINFO("TWFunc::Remove_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -06002188 }
2189#endif
2190 return false;
2191}
2192
2193bool TWPartitionManager::Remove_MTP_Storage(unsigned int Storage_ID) {
2194#ifdef TW_HAS_MTP
2195 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2196 if (Part) {
2197 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2198 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002199 LOGINFO("TWFunc::Remove_MTP_Storage unable to locate partition for %i\n", Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002200 }
2201#endif
2202 return false;
2203}
Ethan Yonker96af84a2015-01-05 14:58:36 -06002204
2205bool TWPartitionManager::Flash_Image(string Filename) {
2206 int check, partition_count = 0;
2207 TWPartition* flash_part = NULL;
2208 string Flash_List, flash_path;
2209 size_t start_pos = 0, end_pos = 0;
2210
Ethan Yonker74db1572015-10-28 12:44:49 -05002211 gui_msg("image_flash_start=[IMAGE FLASH STARTED]");
2212 gui_msg(Msg("img_to_flash=Image to flash: '{1}'")(Filename));
Ethan Yonker96af84a2015-01-05 14:58:36 -06002213
2214 if (!Mount_Current_Storage(true))
2215 return false;
2216
Ethan Yonker74db1572015-10-28 12:44:49 -05002217 gui_msg("calc_restore=Calculating restore details...");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002218 DataManager::GetValue("tw_flash_partition", Flash_List);
2219 if (!Flash_List.empty()) {
2220 end_pos = Flash_List.find(";", start_pos);
2221 while (end_pos != string::npos && start_pos < Flash_List.size()) {
2222 flash_path = Flash_List.substr(start_pos, end_pos - start_pos);
2223 flash_part = Find_Partition_By_Path(flash_path);
2224 if (flash_part != NULL) {
2225 partition_count++;
2226 if (partition_count > 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002227 gui_err("too_many_flash=Too many partitions selected for flashing.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002228 return false;
2229 }
2230 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002231 gui_msg(Msg(msg::kError, "flash_unable_locate=Unable to locate '{1}' partition for flashing.")(flash_path));
Ethan Yonker96af84a2015-01-05 14:58:36 -06002232 return false;
2233 }
2234 start_pos = end_pos + 1;
2235 end_pos = Flash_List.find(";", start_pos);
2236 }
2237 }
2238
2239 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002240 gui_err("no_part_flash=No partitions selected for flashing.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002241 return false;
2242 }
2243
2244 DataManager::SetProgress(0.0);
2245 if (flash_part) {
2246 if (!flash_part->Flash_Image(Filename))
2247 return false;
2248 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002249 gui_err("invalid_flash=Invalid flash partition specified.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002250 return false;
2251 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002252 gui_highlight("flash_done=IMAGE FLASH COMPLETED]");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002253 return true;
2254}
Ethan Yonker74db1572015-10-28 12:44:49 -05002255
2256void TWPartitionManager::Translate_Partition(const char* path, const char* resource_name, const char* default_value) {
2257 TWPartition* part = PartitionManager.Find_Partition_By_Path(path);
2258 if (part) {
2259 part->Display_Name = gui_lookup(resource_name, default_value);
2260 part->Backup_Display_Name = part->Display_Name;
2261 }
2262}
2263
2264void TWPartitionManager::Translate_Partition(const char* path, const char* resource_name, const char* default_value, const char* storage_resource_name, const char* storage_default_value) {
2265 TWPartition* part = PartitionManager.Find_Partition_By_Path(path);
2266 if (part) {
2267 part->Display_Name = gui_lookup(resource_name, default_value);
2268 part->Backup_Display_Name = part->Display_Name;
2269 if (part->Is_Storage)
2270 part->Storage_Name = gui_lookup(storage_resource_name, storage_default_value);
2271 }
2272}
2273
2274void TWPartitionManager::Translate_Partition_Display_Names() {
2275 Translate_Partition("/system", "system", "System");
2276 Translate_Partition("/system_image", "system_image", "System Image");
2277 Translate_Partition("/vendor", "vendor", "Vendor");
2278 Translate_Partition("/vendor_image", "vendor_image", "Vendor Image");
2279 Translate_Partition("/cache", "cache", "Cache");
2280 Translate_Partition("/data", "data", "Data", "internal", "Internal Storage");
2281 Translate_Partition("/boot", "boot", "Boot");
2282 Translate_Partition("/recovery", "recovery", "Recovery");
2283 if (!datamedia) {
2284 Translate_Partition("/sdcard", "sdcard", "SDCard", "sdcard", "SDCard");
2285 Translate_Partition("/internal_sd", "sdcard", "SDCard", "sdcard", "SDCard");
2286 Translate_Partition("/internal_sdcard", "sdcard", "SDCard", "sdcard", "SDCard");
2287 Translate_Partition("/emmc", "sdcard", "SDCard", "sdcard", "SDCard");
2288 }
2289 Translate_Partition("/external_sd", "microsd", "Micro SDCard", "microsd", "Micro SDCard");
2290 Translate_Partition("/external_sdcard", "microsd", "Micro SDCard", "microsd", "Micro SDCard");
2291 Translate_Partition("/usb-otg", "usbotg", "USB OTG", "usbotg", "USB OTG");
2292 Translate_Partition("/sd-ext", "sdext", "SD-EXT");
2293
2294 // Android secure is a special case
2295 TWPartition* part = PartitionManager.Find_Partition_By_Path("/and-sec");
2296 if (part)
2297 part->Backup_Display_Name = gui_lookup("android_secure", "Android Secure");
2298
2299 // This updates the text on all of the storage selection buttons in the GUI
2300 DataManager::SetBackupFolder();
2301}