blob: 320944c3f2d9e8f54867c21603da59850fc1157c [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
Ethan Yonker472f5062016-02-25 13:47:30 -06002 Copyright 2014 to 2016 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"
Ethan Yonkerb5fab762016-01-28 14:03:33 -060040#include "fixContexts.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"
Ethan Yonker472f5062016-02-25 13:47:30 -060046#include "progresstracking.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040047
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040048#ifdef TW_HAS_MTP
49#include "mtp/mtp_MtpServer.hpp"
50#include "mtp/twrpMtp.hpp"
Ethan Yonker726a0202014-12-16 20:01:38 -060051#include "mtp/MtpMessage.hpp"
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040052#endif
53
Dees Troy6f6441d2014-01-23 02:07:03 +000054extern "C" {
55 #include "cutils/properties.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050056 #include "gui/gui.h"
Dees Troy6f6441d2014-01-23 02:07:03 +000057}
58
Dees_Troy5bf43922012-09-07 16:07:55 -040059#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060060 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060061 #include "gui/rapidxml.hpp"
62 #include "gui/pages.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040063#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040064
Ethan Yonker6277c792014-09-15 14:54:30 -050065extern bool datamedia;
66
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050067TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040068 mtp_was_enabled = false;
Ethan Yonker726a0202014-12-16 20:01:38 -060069 mtp_write_fd = -1;
bigbiff7abc5fe2015-01-17 16:53:12 -050070 stop_backup.set_value(0);
71 tar_fork_pid = 0;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050072}
73
Dees_Troy51a0e822012-09-05 15:24:24 -040074int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040075 FILE *fstabFile;
76 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +000077 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050078 TWPartition* andsec_partition = NULL;
Ethan Yonker726a0202014-12-16 20:01:38 -060079 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 -040080
81 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
82 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000083 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040084 return false;
85 }
86
87 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
88 if (fstab_line[0] != '/')
89 continue;
90
Dees_Troy2a923582012-09-20 12:13:34 -040091 if (fstab_line[strlen(fstab_line) - 1] != '\n')
92 fstab_line[strlen(fstab_line)] = '\n';
Dees_Troy2a923582012-09-20 12:13:34 -040093
Matt Mower2b2dd152016-04-26 11:24:08 -050094 TWPartition* partition = new TWPartition();
Matt Mower72c87ce2016-04-26 14:34:56 -050095 if (partition->Process_Fstab_Line(fstab_line, Display_Error))
Ethan Yonkerc05c5982014-03-13 09:19:56 -050096 Partitions.push_back(partition);
Matt Mower72c87ce2016-04-26 14:34:56 -050097 else
Dees_Troy5bf43922012-09-07 16:07:55 -040098 delete partition;
Matt Mower2b2dd152016-04-26 11:24:08 -050099
100 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy5bf43922012-09-07 16:07:55 -0400101 }
102 fclose(fstabFile);
Matt Mower72c87ce2016-04-26 14:34:56 -0500103
104 std::vector<TWPartition*>::iterator iter;
105 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
106 (*iter)->Partition_Post_Processing(Display_Error);
107
108 if ((*iter)->Is_Storage) {
109 ++storageid;
110 (*iter)->MTP_Storage_ID = storageid;
111 }
112
113 if (!settings_partition && (*iter)->Is_Settings_Storage && (*iter)->Is_Present)
114 settings_partition = (*iter);
115 else
116 (*iter)->Is_Settings_Storage = false;
117
118 if (!andsec_partition && (*iter)->Has_Android_Secure && (*iter)->Is_Present)
119 andsec_partition = (*iter);
120 else
121 (*iter)->Has_Android_Secure = false;
122 }
123
Ethan Yonker6277c792014-09-15 14:54:30 -0500124 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) {
125 // Attempt to automatically identify /data/media emulated storage devices
126 TWPartition* Dat = Find_Partition_By_Path("/data");
127 if (Dat) {
128 LOGINFO("Using automatic handling for /data/media emulated storage device.\n");
129 datamedia = true;
that9e0593e2014-10-08 00:01:24 +0200130 Dat->Setup_Data_Media();
Ethan Yonker6277c792014-09-15 14:54:30 -0500131 settings_partition = Dat;
Ethan Yonker726a0202014-12-16 20:01:38 -0600132 // Since /data was not considered a storage partition earlier, we still need to assign an MTP ID
133 ++storageid;
134 Dat->MTP_Storage_ID = storageid;
Ethan Yonker6277c792014-09-15 14:54:30 -0500135 }
136 }
Dees Troy02a64532014-03-19 15:23:32 +0000137 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500138 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
139 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000140 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500141 break;
142 }
143 }
Dees Troy02a64532014-03-19 15:23:32 +0000144 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000145 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500146 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400147 if (!Write_Fstab()) {
148 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000149 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400150 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000151 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400152 }
Matt Mowered71fa32014-04-16 13:21:47 -0500153
Matt Mowerbf4efa32014-04-14 23:25:26 -0500154 if (andsec_partition) {
155 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500156 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500157 Setup_Android_Secure_Location(settings_partition);
158 }
Matt Mowered71fa32014-04-16 13:21:47 -0500159 if (settings_partition) {
160 Setup_Settings_Storage_Partition(settings_partition);
161 }
Ethan Yonker253368a2014-11-25 15:00:52 -0600162#ifdef TW_INCLUDE_CRYPTO
Ethan Yonkercceebb82014-11-18 10:17:59 -0600163 TWPartition* Decrypt_Data = Find_Partition_By_Path("/data");
164 if (Decrypt_Data && Decrypt_Data->Is_Encrypted && !Decrypt_Data->Is_Decrypted) {
165 int password_type = cryptfs_get_password_type();
166 if (password_type == CRYPT_TYPE_DEFAULT) {
167 LOGINFO("Device is encrypted with the default password, attempting to decrypt.\n");
168 if (Decrypt_Device("default_password") == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500169 gui_msg("decrypt_success=Successfully decrypted with default password.");
Ethan Yonkercceebb82014-11-18 10:17:59 -0600170 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
171 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500172 gui_err("unable_to_decrypt=Unable to decrypt with default password.");
Ethan Yonkercceebb82014-11-18 10:17:59 -0600173 }
174 } else {
175 DataManager::SetValue("TW_CRYPTO_TYPE", password_type);
176 }
177 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600178 if (Decrypt_Data && (!Decrypt_Data->Is_Encrypted || Decrypt_Data->Is_Decrypted) && Decrypt_Data->Mount(false)) {
179 Decrypt_Adopted();
180 }
Ethan Yonkercceebb82014-11-18 10:17:59 -0600181#endif
Dees_Troy51127312012-09-08 13:08:49 -0400182 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400183 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400184 return true;
185}
186
187int TWPartitionManager::Write_Fstab(void) {
188 FILE *fp;
189 std::vector<TWPartition*>::iterator iter;
190 string Line;
191
192 fp = fopen("/etc/fstab", "w");
193 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000194 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400195 return false;
196 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400197 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400198 if ((*iter)->Can_Be_Mounted) {
dianlujitao7d304c72016-01-02 12:15:50 +0800199 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw 0 0\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400200 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000201 }
202 // Handle subpartition tracking
203 if ((*iter)->Is_SubPartition) {
204 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
205 if (ParentPartition)
206 ParentPartition->Has_SubPartition = true;
207 else
208 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 -0400209 }
210 }
211 fclose(fp);
212 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400213}
214
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500215void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500216 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
217 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
218 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
219}
220
Matt Mowerbf4efa32014-04-14 23:25:26 -0500221void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
222 if (Part->Has_Android_Secure)
223 Part->Setup_AndSec();
Ethan Yonker6277c792014-09-15 14:54:30 -0500224 else if (!datamedia)
Matt Mowerbf4efa32014-04-14 23:25:26 -0500225 Part->Setup_AndSec();
Matt Mowerbf4efa32014-04-14 23:25:26 -0500226}
227
Dees_Troy8170a922012-09-18 15:40:25 -0400228void TWPartitionManager::Output_Partition_Logging(void) {
229 std::vector<TWPartition*>::iterator iter;
230
231 printf("\n\nPartition Logs:\n");
232 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
233 Output_Partition((*iter));
234}
235
236void TWPartitionManager::Output_Partition(TWPartition* Part) {
237 unsigned long long mb = 1048576;
238
Gary Peck004d48b2012-11-21 16:28:18 -0800239 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 -0400240 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800241 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 -0400242 }
Gary Peck004d48b2012-11-21 16:28:18 -0800243 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500244 if (Part->Can_Be_Mounted)
245 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800246 if (Part->Can_Be_Wiped)
247 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700248 if (Part->Use_Rm_Rf)
249 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500250 if (Part->Can_Be_Backed_Up)
251 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800252 if (Part->Wipe_During_Factory_Reset)
253 printf("Wipe_During_Factory_Reset ");
254 if (Part->Wipe_Available_in_GUI)
255 printf("Wipe_Available_in_GUI ");
256 if (Part->Is_SubPartition)
257 printf("Is_SubPartition ");
258 if (Part->Has_SubPartition)
259 printf("Has_SubPartition ");
260 if (Part->Removable)
261 printf("Removable ");
262 if (Part->Is_Present)
263 printf("IsPresent ");
264 if (Part->Can_Be_Encrypted)
265 printf("Can_Be_Encrypted ");
266 if (Part->Is_Encrypted)
267 printf("Is_Encrypted ");
268 if (Part->Is_Decrypted)
269 printf("Is_Decrypted ");
270 if (Part->Has_Data_Media)
271 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000272 if (Part->Can_Encrypt_Backup)
273 printf("Can_Encrypt_Backup ");
274 if (Part->Use_Userdata_Encryption)
275 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800276 if (Part->Has_Android_Secure)
277 printf("Has_Android_Secure ");
278 if (Part->Is_Storage)
279 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500280 if (Part->Is_Settings_Storage)
281 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000282 if (Part->Ignore_Blkid)
283 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000284 if (Part->Retain_Layout_Version)
285 printf("Retain_Layout_Version ");
Ethan Yonker253368a2014-11-25 15:00:52 -0600286 if (Part->Mount_To_Decrypt)
287 printf("Mount_To_Decrypt ");
Ethan Yonker96af84a2015-01-05 14:58:36 -0600288 if (Part->Can_Flash_Img)
289 printf("Can_Flash_Img ");
Ethan Yonker66a19492015-12-10 10:19:45 -0600290 if (Part->Is_Adopted_Storage)
291 printf("Is_Adopted_Storage ");
Gary Peck004d48b2012-11-21 16:28:18 -0800292 printf("\n");
293 if (!Part->SubPartition_Of.empty())
294 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
295 if (!Part->Symlink_Path.empty())
296 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
297 if (!Part->Symlink_Mount_Point.empty())
298 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
299 if (!Part->Primary_Block_Device.empty())
300 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
301 if (!Part->Alternate_Block_Device.empty())
302 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
303 if (!Part->Decrypted_Block_Device.empty())
304 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
Ethan Yonker253368a2014-11-25 15:00:52 -0600305 if (!Part->Crypto_Key_Location.empty() && Part->Crypto_Key_Location != "footer")
306 printf(" Crypto_Key_Location: %s\n", Part->Crypto_Key_Location.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800307 if (Part->Length != 0)
308 printf(" Length: %i\n", Part->Length);
309 if (!Part->Display_Name.empty())
310 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500311 if (!Part->Storage_Name.empty())
312 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800313 if (!Part->Backup_Path.empty())
314 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
315 if (!Part->Backup_Name.empty())
316 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500317 if (!Part->Backup_Display_Name.empty())
318 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800319 if (!Part->Backup_FileName.empty())
320 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
321 if (!Part->Storage_Path.empty())
322 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
323 if (!Part->Current_File_System.empty())
324 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
325 if (!Part->Fstab_File_System.empty())
326 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
327 if (Part->Format_Block_Size != 0)
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500328 printf(" Format_Block_Size: %lu\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400329 if (!Part->MTD_Name.empty())
330 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400331 string back_meth = Part->Backup_Method_By_Name();
Ethan Yonker6277c792014-09-15 14:54:30 -0500332 printf(" Backup_Method: %s\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800333 if (Part->Mount_Flags || !Part->Mount_Options.empty())
Matt Mower4ab42b12016-04-21 13:52:18 -0500334 printf(" Mount_Options: %s\n", Part->Mount_Options.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -0600335 if (Part->MTP_Storage_ID)
336 printf(" MTP_Storage_ID: %i\n", Part->MTP_Storage_ID);
Ethan Yonker6277c792014-09-15 14:54:30 -0500337 printf("\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400338}
339
Dees_Troy51a0e822012-09-05 15:24:24 -0400340int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400341 std::vector<TWPartition*>::iterator iter;
342 int ret = false;
343 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400344 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400345
Dees_Troyd93bda52013-07-03 19:55:19 +0000346 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400347 return true;
348
Dees_Troy5bf43922012-09-07 16:07:55 -0400349 // Iterate through all partitions
350 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400351 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400352 ret = (*iter)->Mount(Display_Error);
353 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400354 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400355 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400356 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400357 }
358 if (found) {
359 return ret;
360 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500361 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 -0400362 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000363 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400364 }
365 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400366}
367
Dees_Troy51a0e822012-09-05 15:24:24 -0400368int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400369 std::vector<TWPartition*>::iterator iter;
370 int ret = false;
371 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400372 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400373
374 // Iterate through all partitions
375 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400376 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400377 ret = (*iter)->UnMount(Display_Error);
378 found = true;
379 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
380 (*iter)->UnMount(Display_Error);
381 }
382 }
383 if (found) {
384 return ret;
385 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500386 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 -0400387 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000388 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400389 }
390 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400391}
392
Dees_Troy51a0e822012-09-05 15:24:24 -0400393int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400394 TWPartition* Part = Find_Partition_By_Path(Path);
395
396 if (Part)
397 return Part->Is_Mounted();
398 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000399 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400400 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400401}
402
Dees_Troy5bf43922012-09-07 16:07:55 -0400403int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400404 string current_storage_path = DataManager::GetCurrentStoragePath();
405
406 if (Mount_By_Path(current_storage_path, Display_Error)) {
407 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
408 if (FreeStorage)
409 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
410 return true;
411 }
412 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400413}
414
Dees_Troy5bf43922012-09-07 16:07:55 -0400415int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
416 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
417}
418
419TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
420 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400421 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400422
423 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400424 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400425 return (*iter);
426 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400427 return NULL;
428}
429
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400430int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
431 // Check the backup name to ensure that it is the correct size and contains only valid characters
432 // and that a backup with that name doesn't already exist
433 char backup_name[MAX_BACKUP_NAME_LEN];
434 char backup_loc[255], tw_image_dir[255];
435 int copy_size;
436 int index, cur_char;
437 string Backup_Name, Backup_Loc;
438
439 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
440 copy_size = Backup_Name.size();
441 // Check size
442 if (copy_size > MAX_BACKUP_NAME_LEN) {
443 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500444 gui_err("backup_name_len=Backup name is too long.");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400445 return -2;
446 }
447
448 // Check each character
449 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500450 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400451 return 0; // A "0" (zero) means to use the current timestamp for the backup name
452 for (index=0; index<copy_size; index++) {
453 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500454 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 -0400455 // These are valid characters
456 // Numbers
457 // Upper case letters
458 // Lower case letters
459 // Space
460 // and -_.{}[]
461 } else {
462 if (Display_Error)
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600463 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 -0400464 return -3;
465 }
466 }
467
468 // Check to make sure that a backup with this name doesn't already exist
469 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
470 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500471 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500472 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400473 if (Display_Error)
Matt Mower3c366972015-12-25 19:28:31 -0600474 gui_err("backup_name_exists=A backup with that name already exists!");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400475 return -4;
476 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400477 // No problems found, return 0
478 return 0;
479}
480
Dees_Troy43d8b002012-09-17 16:00:01 -0400481bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
482{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500483 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400484 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500485 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500486 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400487
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500488 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400489 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400490
Ethan Yonker74db1572015-10-28 12:44:49 -0500491 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, gui_parse_text("{@generating_md51}"));
492 gui_msg("generating_md52= * Generating md5...");
Dees_Troy43d8b002012-09-17 16:00:01 -0400493
494 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500495 md5sum.setfn(Backup_Folder + Backup_Filename);
496 if (md5sum.computeMD5() == 0)
497 if (md5sum.write_md5digest() == 0)
Ethan Yonker74db1572015-10-28 12:44:49 -0500498 gui_msg("md5_created= * MD5 Created.");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500499 else
500 return -1;
501 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500502 gui_err("md5_error= * MD5 Error!");
Dees_Troy43d8b002012-09-17 16:00:01 -0400503 } else {
504 char filename[512];
505 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500506 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400507 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500508 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000509 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400510 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000511 if (TWFunc::Path_Exists(filename)) {
512 if (md5sum.computeMD5() == 0) {
513 if (md5sum.write_md5digest() != 0)
514 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500515 gui_err("md5_error= * MD5 Error!");
Dees_Troy83bd4832013-05-04 12:39:56 +0000516 return false;
517 }
518 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500519 gui_err("md5_compute_error= * Error computing MD5.");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500520 return false;
521 }
522 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400523 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400524 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500525 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400526 }
527 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000528 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400529 return false;
530 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500531 gui_msg("md5_created= * MD5 Created.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400532 }
533 return true;
534}
535
Ethan Yonker472f5062016-02-25 13:47:30 -0600536bool TWPartitionManager::Backup_Partition(TWPartition* Part, const string& Backup_Folder, bool generate_md5, unsigned long *img_time, unsigned long *file_time, ProgressTracking *progress) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400537 time_t start, stop;
that203bcc92015-05-09 17:27:33 +0200538 int use_compression;
Dees_Troy43d8b002012-09-17 16:00:01 -0400539
bigbiffbf1d6722015-02-14 16:25:59 -0500540 string backup_log = Backup_Folder + "recovery.log";
541
Dees_Troy43d8b002012-09-17 16:00:01 -0400542 if (Part == NULL)
543 return true;
544
Dees_Troy093b7642012-09-21 15:59:38 -0400545 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400546
Tom Hite5a926722014-09-15 01:31:03 +0000547 TWFunc::SetPerformanceMode(true);
Dees_Troy43d8b002012-09-17 16:00:01 -0400548 time(&start);
549
Ethan Yonker472f5062016-02-25 13:47:30 -0600550 if (Part->Backup(Backup_Folder, tar_fork_pid, progress)) {
Tom Hite5a926722014-09-15 01:31:03 +0000551 bool md5Success = false;
Dees_Troy8170a922012-09-18 15:40:25 -0400552 if (Part->Has_SubPartition) {
553 std::vector<TWPartition*>::iterator subpart;
554
555 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500556 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Ethan Yonker472f5062016-02-25 13:47:30 -0600557 if (!(*subpart)->Backup(Backup_Folder, tar_fork_pid, progress)) {
Tom Hite5a926722014-09-15 01:31:03 +0000558 TWFunc::SetPerformanceMode(false);
bigbiffbf1d6722015-02-14 16:25:59 -0500559 Clean_Backup_Folder(Backup_Folder);
560 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
561 tw_set_default_metadata(backup_log.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400562 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000563 }
Dees_Troy2727b992013-08-14 20:09:30 +0000564 sync();
565 sync();
Tom Hite5a926722014-09-15 01:31:03 +0000566 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName)) {
567 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400568 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000569 }
Dees_Troy8170a922012-09-18 15:40:25 -0400570 }
571 }
572 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400573 time(&stop);
that203bcc92015-05-09 17:27:33 +0200574 int backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000575 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400576 if (Part->Backup_Method == 1) {
Dees_Troy093b7642012-09-21 15:59:38 -0400577 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400578 } else {
Dees_Troy093b7642012-09-21 15:59:38 -0400579 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400580 }
Tom Hite5a926722014-09-15 01:31:03 +0000581
582 md5Success = Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
583 TWFunc::SetPerformanceMode(false);
584 return md5Success;
Dees_Troy43d8b002012-09-17 16:00:01 -0400585 } else {
bigbiffbf1d6722015-02-14 16:25:59 -0500586 Clean_Backup_Folder(Backup_Folder);
587 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
588 tw_set_default_metadata(backup_log.c_str());
Tom Hite5a926722014-09-15 01:31:03 +0000589 TWFunc::SetPerformanceMode(false);
Dees_Troy43d8b002012-09-17 16:00:01 -0400590 return false;
591 }
bigbiff7abc5fe2015-01-17 16:53:12 -0500592 return 0;
593}
594
bigbiffbf1d6722015-02-14 16:25:59 -0500595void TWPartitionManager::Clean_Backup_Folder(string Backup_Folder) {
596 DIR *d = opendir(Backup_Folder.c_str());
597 struct dirent *p;
598 int r;
599
Ethan Yonker74db1572015-10-28 12:44:49 -0500600 gui_msg("backup_clean=Backup Failed. Cleaning Backup Folder.");
bigbiffbf1d6722015-02-14 16:25:59 -0500601
602 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500603 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Backup_Folder)(strerror(errno)));
bigbiffbf1d6722015-02-14 16:25:59 -0500604 return;
605 }
606
Matt Mower2b18a532015-02-20 16:58:05 -0600607 while ((p = readdir(d))) {
bigbiffbf1d6722015-02-14 16:25:59 -0500608 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
609 continue;
610
611 string path = Backup_Folder + p->d_name;
612
613 size_t dot = path.find_last_of(".") + 1;
614 if (path.substr(dot) == "win" || path.substr(dot) == "md5" || path.substr(dot) == "info") {
615 r = unlink(path.c_str());
616 if (r != 0) {
617 LOGINFO("Unable to unlink '%s: %s'\n", path.c_str(), strerror(errno));
618 }
619 }
620 }
621 closedir(d);
622}
623
Ethan Yonker472f5062016-02-25 13:47:30 -0600624int TWPartitionManager::Check_Backup_Cancel() {
625 return stop_backup.get_value();
626}
627
bigbiff7abc5fe2015-01-17 16:53:12 -0500628int TWPartitionManager::Cancel_Backup() {
629 string Backup_Folder, Backup_Name, Full_Backup_Path;
630
631 stop_backup.set_value(1);
632
633 if (tar_fork_pid != 0) {
634 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
635 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
636 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
637 LOGINFO("Killing pid: %d\n", tar_fork_pid);
638 kill(tar_fork_pid, SIGUSR2);
639 while (kill(tar_fork_pid, 0) == 0) {
640 usleep(1000);
641 }
642 LOGINFO("Backup_Run stopped and returning false, backup cancelled.\n");
643 LOGINFO("Removing directory %s\n", Full_Backup_Path.c_str());
644 TWFunc::removeDir(Full_Backup_Path, false);
645 tar_fork_pid = 0;
646 }
647
648 return 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400649}
650
651int TWPartitionManager::Run_Backup(void) {
Matt Mower2b18a532015-02-20 16:58:05 -0600652 int check, do_md5, partition_count = 0, disable_free_space_check = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500653 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400654 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 -0400655 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500656 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400657 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400658 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400659 struct tm *t;
660 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500661 size_t start_pos = 0, end_pos = 0;
bigbiff7abc5fe2015-01-17 16:53:12 -0500662 stop_backup.set_value(0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400663 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500664 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400665
666 time(&total_start);
667
668 Update_System_Details();
669
670 if (!Mount_Current_Storage(true))
671 return false;
672
673 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400674 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400675 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400676 else
677 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400678
Dees_Troy43d8b002012-09-17 16:00:01 -0400679 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
680 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600681 if (Backup_Name == gui_lookup("curr_date", "(Current Date)")) {
Dees Troyb21cc642013-09-10 17:36:41 +0000682 Backup_Name = TWFunc::Get_Current_Date();
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600683 } else if (Backup_Name == gui_lookup("auto_generate", "(Auto Generate)") || Backup_Name == "0" || Backup_Name.empty()) {
Dees Troyb21cc642013-09-10 17:36:41 +0000684 TWFunc::Auto_Generate_Backup_Name();
685 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400686 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000687 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400688 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000689 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400690
Dees_Troy2673cec2013-04-02 20:22:16 +0000691 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500692 DataManager::GetValue("tw_backup_list", Backup_List);
693 if (!Backup_List.empty()) {
694 end_pos = Backup_List.find(";", start_pos);
695 while (end_pos != string::npos && start_pos < Backup_List.size()) {
696 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
697 backup_part = Find_Partition_By_Path(backup_path);
698 if (backup_part != NULL) {
699 partition_count++;
700 if (backup_part->Backup_Method == 1)
701 file_bytes += backup_part->Backup_Size;
702 else
703 img_bytes += backup_part->Backup_Size;
704 if (backup_part->Has_SubPartition) {
705 std::vector<TWPartition*>::iterator subpart;
706
707 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000708 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 -0500709 partition_count++;
710 if ((*subpart)->Backup_Method == 1)
711 file_bytes += (*subpart)->Backup_Size;
712 else
713 img_bytes += (*subpart)->Backup_Size;
714 }
715 }
Dees_Troy8170a922012-09-18 15:40:25 -0400716 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500717 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500718 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 -0400719 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500720 start_pos = end_pos + 1;
721 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400722 }
723 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400724
725 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500726 gui_msg("no_partition_selected=No partitions selected for backup.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400727 return false;
728 }
729 total_bytes = file_bytes + img_bytes;
Ethan Yonker472f5062016-02-25 13:47:30 -0600730 ProgressTracking progress(total_bytes);
Ethan Yonker74db1572015-10-28 12:44:49 -0500731 gui_msg(Msg("total_partitions_backup= * Total number of partitions to back up: {1}")(partition_count));
732 gui_msg(Msg("total_backup_size= * Total size of all data: {1}MB")(total_bytes / 1024 / 1024));
Dees_Troy43d8b002012-09-17 16:00:01 -0400733 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
734 if (storage != NULL) {
735 free_space = storage->Free;
Ethan Yonker74db1572015-10-28 12:44:49 -0500736 gui_msg(Msg("available_space= * Available space: {1}MB")(free_space / 1024 / 1024));
Dees_Troy43d8b002012-09-17 16:00:01 -0400737 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500738 gui_err("unable_locate_storage=Unable to locate storage device.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400739 return false;
740 }
bigbiffbf1d6722015-02-14 16:25:59 -0500741
742 DataManager::GetValue("tw_disable_free_space", disable_free_space_check);
743 if (!disable_free_space_check) {
744 if (free_space - (32 * 1024 * 1024) < total_bytes) {
745 // We require an extra 32MB just in case
Ethan Yonker74db1572015-10-28 12:44:49 -0500746 gui_err("no_space=Not enough free space on storage.");
bigbiffbf1d6722015-02-14 16:25:59 -0500747 return false;
748 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400749 }
750 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500751 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400752
Ethan Yonker74db1572015-10-28 12:44:49 -0500753 gui_msg("backup_started=[BACKUP STARTED]");
754 gui_msg(Msg("backup_folder= * Backup Folder: {1}")(Full_Backup_Path));
Dees_Troyd4b22b02013-01-18 17:17:58 +0000755 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500756 gui_err("fail_backup_folder=Failed to make backup folder.");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000757 return false;
758 }
759
Dees_Troy2673cec2013-04-02 20:22:16 +0000760 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400761
Dees_Troya13d74f2013-03-24 08:54:55 -0500762 start_pos = 0;
763 end_pos = Backup_List.find(";", start_pos);
764 while (end_pos != string::npos && start_pos < Backup_List.size()) {
bigbiff7abc5fe2015-01-17 16:53:12 -0500765 if (stop_backup.get_value() != 0)
766 return -1;
Dees_Troya13d74f2013-03-24 08:54:55 -0500767 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
768 backup_part = Find_Partition_By_Path(backup_path);
769 if (backup_part != NULL) {
Ethan Yonker472f5062016-02-25 13:47:30 -0600770 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_time, &file_time, &progress))
Dees_Troya13d74f2013-03-24 08:54:55 -0500771 return false;
772 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500773 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 -0500774 }
775 start_pos = end_pos + 1;
776 end_pos = Backup_List.find(";", start_pos);
777 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400778
779 // Average BPS
780 if (img_time == 0)
781 img_time = 1;
782 if (file_time == 0)
783 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400784 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500785 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400786
Ethan Yonker472f5062016-02-25 13:47:30 -0600787 if (file_bytes != 0)
788 gui_msg(Msg("avg_backup_fs=Average backup rate for file systems: {1} MB/sec")(file_bps / (1024 * 1024)));
789 if (img_bytes != 0)
790 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 -0400791
792 time(&total_stop);
793 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500794 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500795 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400796
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500797 int prev_img_bps, use_compression;
798 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400799 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
800 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500801 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400802
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500803 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400804 if (use_compression)
805 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500806 else
Dees_Troy093b7642012-09-21 15:59:38 -0400807 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
808 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500809 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400810
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500811 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400812 if (use_compression)
813 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
814 else
815 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
816
Ethan Yonker74db1572015-10-28 12:44:49 -0500817 gui_msg(Msg("total_backed_size=[{1} MB TOTAL BACKED UP]")(actual_backup_size));
Dees_Troy43d8b002012-09-17 16:00:01 -0400818 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400819 UnMount_Main_Partitions();
Ethan Yonker56db1c42015-12-20 22:49:00 -0600820 gui_msg(Msg(msg::kHighlight, "backup_completed=[BACKUP COMPLETED IN {1} SECONDS]")(total_time)); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000821 string backup_log = Full_Backup_Path + "recovery.log";
822 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600823 tw_set_default_metadata(backup_log.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000824 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400825}
826
Ethan Yonker472f5062016-02-25 13:47:30 -0600827bool TWPartitionManager::Restore_Partition(TWPartition* Part, const string& Restore_Name, ProgressTracking *progress) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400828 time_t Start, Stop;
Tom Hite5a926722014-09-15 01:31:03 +0000829 TWFunc::SetPerformanceMode(true);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400830 time(&Start);
Ethan Yonker472f5062016-02-25 13:47:30 -0600831
832 if (!Part->Restore(Restore_Name, progress)) {
Tom Hite5a926722014-09-15 01:31:03 +0000833 TWFunc::SetPerformanceMode(false);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400834 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000835 }
Dees_Troy8170a922012-09-18 15:40:25 -0400836 if (Part->Has_SubPartition) {
837 std::vector<TWPartition*>::iterator subpart;
838
839 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
840 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Ethan Yonker472f5062016-02-25 13:47:30 -0600841 if (!(*subpart)->Restore(Restore_Name, progress)) {
Tom Hite5a926722014-09-15 01:31:03 +0000842 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400843 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000844 }
Dees_Troy8170a922012-09-18 15:40:25 -0400845 }
846 }
847 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400848 time(&Stop);
Tom Hite5a926722014-09-15 01:31:03 +0000849 TWFunc::SetPerformanceMode(false);
z31s1g2053fe02016-02-04 06:45:18 +0100850 gui_msg(Msg("restore_part_done=[{1} done ({2} seconds)]")(Part->Backup_Display_Name)((int)difftime(Stop, Start)));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400851 return true;
852}
853
Ethan Yonker472f5062016-02-25 13:47:30 -0600854int TWPartitionManager::Run_Restore(const string& Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400855 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500856 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400857 time_t rStart, rStop;
858 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500859 string Restore_List, restore_path;
860 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500861 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400862
Ethan Yonker74db1572015-10-28 12:44:49 -0500863 gui_msg("restore_started=[RESTORE STARTED]");
864 gui_msg(Msg("restore_folder=Restore folder: '{1}'")(Restore_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -0400865
Dees_Troy4a2a1262012-09-18 09:33:47 -0400866 if (!Mount_Current_Storage(true))
867 return false;
868
869 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500870 if (check_md5 > 0) {
871 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
Ethan Yonker74db1572015-10-28 12:44:49 -0500872 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, gui_parse_text("{@verifying_md5}"));
873 gui_msg("verifying_md5=Verifying MD5");
Dees_Troya13d74f2013-03-24 08:54:55 -0500874 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500875 gui_msg("skip_md5=Skipping MD5 check based on user setting.");
Dees_Troya13d74f2013-03-24 08:54:55 -0500876 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500877 gui_msg("calc_restore=Calculating restore details...");
Dees_Troya13d74f2013-03-24 08:54:55 -0500878 DataManager::GetValue("tw_restore_selected", Restore_List);
879 if (!Restore_List.empty()) {
880 end_pos = Restore_List.find(";", start_pos);
881 while (end_pos != string::npos && start_pos < Restore_List.size()) {
882 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
883 restore_part = Find_Partition_By_Path(restore_path);
884 if (restore_part != NULL) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500885 if (restore_part->Mount_Read_Only) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500886 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 -0500887 return false;
888 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500889 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
890 return false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500891 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500892 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500893 if (restore_part->Has_SubPartition) {
894 std::vector<TWPartition*>::iterator subpart;
895
896 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
897 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400898 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500899 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500900 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500901 }
902 }
903 }
904 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500905 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -0500906 }
907 start_pos = end_pos + 1;
908 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400909 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400910 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400911
912 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500913 gui_err("no_part_restore=No partitions selected for restore.");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400914 return false;
915 }
916
Ethan Yonker74db1572015-10-28 12:44:49 -0500917 gui_msg(Msg("restore_part_count=Restoring {1} partitions...")(partition_count));
918 gui_msg(Msg("total_restore_size=Total restore size is {1}MB")(total_restore_size / 1048576));
Dees_Troy2673cec2013-04-02 20:22:16 +0000919 DataManager::SetProgress(0.0);
Ethan Yonker472f5062016-02-25 13:47:30 -0600920 ProgressTracking progress(total_restore_size);
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 Yonker472f5062016-02-25 13:47:30 -0600930 if (!Restore_Partition(restore_part, Restore_Name, &progress))
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);
Matt Mower3c366972015-12-25 19:28:31 -0600943 gui_msg(Msg(msg::kHighlight, "restore_completed=[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
James Christopher Adduonofddbdfa2016-02-29 15:08:11 -05001035 if (!Part->Is_SubPartition)
1036 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001037 }
1038 closedir(d);
1039
Dees_Troya13d74f2013-03-24 08:54:55 -05001040 // Set the final value
1041 DataManager::SetValue("tw_restore_list", Restore_List);
1042 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001043 return;
1044}
1045
1046int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001047 std::vector<TWPartition*>::iterator iter;
1048 int ret = false;
1049 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001050 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001051
1052 // Iterate through all partitions
1053 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001054 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001055 if (Path == "/and-sec")
1056 ret = (*iter)->Wipe_AndSec();
1057 else
1058 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001059 found = true;
1060 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1061 (*iter)->Wipe();
1062 }
1063 }
1064 if (found) {
1065 return ret;
1066 } else
Ethan Yonker74db1572015-10-28 12:44:49 -05001067 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 -04001068 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001069}
1070
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001071int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1072 std::vector<TWPartition*>::iterator iter;
1073 int ret = false;
1074 bool found = false;
1075 string Local_Path = TWFunc::Get_Root_Path(Path);
1076
1077 // Iterate through all partitions
1078 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1079 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1080 if (Path == "/and-sec")
1081 ret = (*iter)->Wipe_AndSec();
1082 else
1083 ret = (*iter)->Wipe(New_File_System);
1084 found = true;
1085 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1086 (*iter)->Wipe(New_File_System);
1087 }
1088 }
1089 if (found) {
1090 return ret;
1091 } else
Ethan Yonker74db1572015-10-28 12:44:49 -05001092 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 -05001093 return false;
1094}
1095
Dees_Troy51a0e822012-09-05 15:24:24 -04001096int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001097 std::vector<TWPartition*>::iterator iter;
1098 int ret = true;
1099
1100 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001101 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Ethan Yonker89583ef2015-08-26 09:01:59 -05001102#ifdef TW_OEM_BUILD
1103 if ((*iter)->Mount_Point == "/data") {
1104 if (!(*iter)->Wipe_Encryption())
1105 ret = false;
1106 } else {
1107#endif
1108 if (!(*iter)->Wipe())
1109 ret = false;
1110#ifdef TW_OEM_BUILD
1111 }
1112#endif
Dees_Troy094207a2012-09-26 12:00:39 -04001113 } else if ((*iter)->Has_Android_Secure) {
1114 if (!(*iter)->Wipe_AndSec())
1115 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001116 }
1117 }
1118 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001119}
1120
Dees_Troy38bd7602012-09-14 13:33:53 -04001121int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1122 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001123 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001124
1125 if (!Mount_By_Path("/data", true))
1126 return false;
1127
1128 if (!Mount_By_Path("/cache", true))
1129 return false;
1130
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001131 dir.push_back("/data/dalvik-cache");
1132 dir.push_back("/cache/dalvik-cache");
1133 dir.push_back("/cache/dc");
Ethan Yonker74db1572015-10-28 12:44:49 -05001134 gui_msg("wiping_dalvik=Wiping Dalvik Cache Directories...");
Dees_Troya13d74f2013-03-24 08:54:55 -05001135 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001136 if (stat(dir.at(i).c_str(), &st) == 0) {
1137 TWFunc::removeDir(dir.at(i), false);
Ethan Yonker74db1572015-10-28 12:44:49 -05001138 gui_msg(Msg("cleaned=Cleaned: {1}...")(dir.at(i)));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001139 }
1140 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001141 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001142 if (sdext && sdext->Is_Present && sdext->Mount(false))
1143 {
1144 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1145 {
1146 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Ethan Yonker74db1572015-10-28 12:44:49 -05001147 gui_msg(Msg("cleaned=Cleaned: {1}...")("/sd-ext/dalvik-cache"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001148 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001149 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001150 gui_msg("dalvik_done=-- Dalvik Cache Directories Wipe Complete!");
Dees_Troy38bd7602012-09-14 13:33:53 -04001151 return true;
1152}
1153
1154int TWPartitionManager::Wipe_Rotate_Data(void) {
1155 if (!Mount_By_Path("/data", true))
1156 return false;
1157
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001158 unlink("/data/misc/akmd*");
1159 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001160 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001161 return true;
1162}
1163
1164int TWPartitionManager::Wipe_Battery_Stats(void) {
1165 struct stat st;
1166
1167 if (!Mount_By_Path("/data", true))
1168 return false;
1169
1170 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001171 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001172 } else {
1173 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001174 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001175 }
1176 return true;
1177}
1178
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001179int TWPartitionManager::Wipe_Android_Secure(void) {
1180 std::vector<TWPartition*>::iterator iter;
1181 int ret = false;
1182 bool found = false;
1183
1184 // Iterate through all partitions
1185 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1186 if ((*iter)->Has_Android_Secure) {
1187 ret = (*iter)->Wipe_AndSec();
1188 found = true;
1189 }
1190 }
1191 if (found) {
1192 return ret;
1193 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001194 gui_err("no_andsec=No android secure partitions found.");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001195 }
1196 return false;
1197}
1198
Dees_Troy38bd7602012-09-14 13:33:53 -04001199int TWPartitionManager::Format_Data(void) {
1200 TWPartition* dat = Find_Partition_By_Path("/data");
1201
1202 if (dat != NULL) {
1203 if (!dat->UnMount(true))
1204 return false;
1205
1206 return dat->Wipe_Encryption();
1207 } else {
Matt Mower3c366972015-12-25 19:28:31 -06001208 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/data"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001209 return false;
1210 }
1211 return false;
1212}
1213
1214int TWPartitionManager::Wipe_Media_From_Data(void) {
1215 TWPartition* dat = Find_Partition_By_Path("/data");
1216
1217 if (dat != NULL) {
1218 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001219 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001220 return false;
1221 }
1222 if (!dat->Mount(true))
1223 return false;
1224
Ethan Yonker74db1572015-10-28 12:44:49 -05001225 gui_msg("wiping_datamedia=Wiping internal storage -- /data/media...");
Ethan Yonker726a0202014-12-16 20:01:38 -06001226 Remove_MTP_Storage(dat->MTP_Storage_ID);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001227 TWFunc::removeDir("/data/media", false);
xiaolu9416f4f2015-06-04 08:22:23 +08001228 dat->Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001229 Add_MTP_Storage(dat->MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001230 return true;
1231 } else {
Matt Mower3c366972015-12-25 19:28:31 -06001232 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/data"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001233 return false;
1234 }
1235 return false;
1236}
1237
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001238int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1239 std::vector<TWPartition*>::iterator iter;
1240 int ret = false;
1241 bool found = false;
1242 string Local_Path = TWFunc::Get_Root_Path(Path);
1243
1244 if (Local_Path == "/tmp" || Local_Path == "/")
1245 return true;
1246
1247 // Iterate through all partitions
1248 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1249 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1250 ret = (*iter)->Repair();
1251 found = true;
1252 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1253 (*iter)->Repair();
1254 }
1255 }
1256 if (found) {
1257 return ret;
1258 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001259 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 -05001260 } else {
1261 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1262 }
1263 return false;
1264}
1265
Ethan Yonkera2719152015-05-28 09:44:41 -05001266int TWPartitionManager::Resize_By_Path(string Path, bool Display_Error) {
1267 std::vector<TWPartition*>::iterator iter;
1268 int ret = false;
1269 bool found = false;
1270 string Local_Path = TWFunc::Get_Root_Path(Path);
1271
1272 if (Local_Path == "/tmp" || Local_Path == "/")
1273 return true;
1274
1275 // Iterate through all partitions
1276 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1277 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1278 ret = (*iter)->Resize();
1279 found = true;
1280 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1281 (*iter)->Resize();
1282 }
1283 }
1284 if (found) {
1285 return ret;
1286 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001287 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 -05001288 } else {
1289 LOGINFO("Resize: Unable to find partition for path '%s'\n", Local_Path.c_str());
1290 }
1291 return false;
1292}
1293
Dees_Troy51a0e822012-09-05 15:24:24 -04001294void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001295 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001296 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001297
Ethan Yonker74db1572015-10-28 12:44:49 -05001298 gui_msg("update_part_details=Updating partition details...");
Dees_Troy5bf43922012-09-07 16:07:55 -04001299 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001300 if ((*iter)->Can_Be_Mounted) {
1301 (*iter)->Update_Size(true);
1302 if ((*iter)->Mount_Point == "/system") {
1303 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1304 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1305 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1306 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1307 } else if ((*iter)->Mount_Point == "/cache") {
1308 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1309 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1310 } else if ((*iter)->Mount_Point == "/sd-ext") {
1311 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1312 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1313 if ((*iter)->Backup_Size == 0) {
1314 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1315 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1316 } else
1317 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001318 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001319 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001320 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001321 if ((*iter)->Backup_Size == 0) {
1322 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1323 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1324 } else
1325 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001326 } else if ((*iter)->Mount_Point == "/boot") {
1327 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1328 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1329 if ((*iter)->Backup_Size == 0) {
1330 DataManager::SetValue("tw_has_boot_partition", 0);
1331 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1332 } else
1333 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001334 }
Dees_Troyaa9cc402012-10-13 12:14:05 -04001335 } else {
1336 // Handle unmountable partitions in case we reset defaults
1337 if ((*iter)->Mount_Point == "/boot") {
1338 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1339 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1340 if ((*iter)->Backup_Size == 0) {
1341 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1342 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1343 } else
1344 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1345 } else if ((*iter)->Mount_Point == "/recovery") {
1346 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1347 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1348 if ((*iter)->Backup_Size == 0) {
1349 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1350 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1351 } else
1352 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001353 } else if ((*iter)->Mount_Point == "/data") {
1354 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001355 }
Dees_Troy51127312012-09-08 13:08:49 -04001356 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001357 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001358 gui_msg("update_part_details_done=...done");
Dees_Troy51127312012-09-08 13:08:49 -04001359 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1360 string current_storage_path = DataManager::GetCurrentStoragePath();
1361 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001362 if (FreeStorage != NULL) {
1363 // Attempt to mount storage
1364 if (!FreeStorage->Mount(false)) {
Matt Mower2d50cad2015-12-03 22:26:55 -06001365 gui_msg(Msg(msg::kError, "unable_to_mount_storage=Unable to mount storage"));
1366 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
Dees_Troy8170a922012-09-18 15:40:25 -04001367 } else {
1368 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1369 }
1370 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001371 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001372 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001373 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001374 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001375 return;
1376}
1377
1378int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001379#ifdef TW_INCLUDE_CRYPTO
1380 int ret_val, password_len;
Ethan Yonkera1de1492015-11-04 11:58:27 -06001381 char crypto_state[PROPERTY_VALUE_MAX], crypto_blkdev[PROPERTY_VALUE_MAX], cPassword[255];
Dees_Troy5bf43922012-09-07 16:07:55 -04001382 size_t result;
Ethan Yonker253368a2014-11-25 15:00:52 -06001383 std::vector<TWPartition*>::iterator iter;
Dees_Troy5bf43922012-09-07 16:07:55 -04001384
Ethan Yonker253368a2014-11-25 15:00:52 -06001385 // Mount any partitions that need to be mounted for decrypt
1386 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1387 if ((*iter)->Mount_To_Decrypt) {
1388 (*iter)->Mount(true);
1389 }
a39552696ff55ce2013-01-08 16:14:56 +00001390 }
a39552696ff55ce2013-01-08 16:14:56 +00001391
Ethan Yonkera1de1492015-11-04 11:58:27 -06001392 property_get("ro.crypto.state", crypto_state, "error");
1393 if (strcmp(crypto_state, "error") == 0) {
1394 property_set("ro.crypto.state", "encrypted");
1395 // Sleep for a bit so that services can start if needed
1396 sleep(1);
1397 }
1398
Dees_Troy5bf43922012-09-07 16:07:55 -04001399 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001400 int pwret = cryptfs_check_passwd(cPassword);
1401
Ethan Yonker253368a2014-11-25 15:00:52 -06001402 // Unmount any partitions that were needed for decrypt
1403 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1404 if ((*iter)->Mount_To_Decrypt) {
1405 (*iter)->UnMount(false);
1406 }
1407 }
1408
a39552696ff55ce2013-01-08 16:14:56 +00001409 if (pwret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001410 gui_err("fail_decrypt=Failed to decrypt data.");
Dees_Troy5bf43922012-09-07 16:07:55 -04001411 return -1;
1412 }
a39552696ff55ce2013-01-08 16:14:56 +00001413
Dees_Troy5bf43922012-09-07 16:07:55 -04001414 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1415 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001416 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001417 } else {
1418 TWPartition* dat = Find_Partition_By_Path("/data");
1419 if (dat != NULL) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001420 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1421 dat->Is_Decrypted = true;
1422 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001423 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001424 dat->Current_File_System = dat->Fstab_File_System; // Needed if we're ignoring blkid because encrypted devices start out as emmc
Matt Mower3c366972015-12-25 19:28:31 -06001425 gui_msg(Msg("decrypt_success_dev=Data successfully decrypted, new block device: '{1}'")(crypto_blkdev));
a39552696ff55ce2013-01-08 16:14:56 +00001426
Dees_Troy5bf43922012-09-07 16:07:55 -04001427 // Sleep for a bit so that the device will be ready
1428 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001429 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001430 dat->Storage_Path = "/data/media/0";
1431 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001432 DataManager::SetValue("tw_storage_path", "/data/media/0");
Ethan Yonkerec0fa4a2014-11-20 09:51:03 -06001433 DataManager::SetValue("tw_settings_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001434 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001435 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001436 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001437 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001438 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001439 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001440 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001441 }
1442 return 0;
1443#else
Ethan Yonker74db1572015-10-28 12:44:49 -05001444 gui_err("no_crypto_support=No crypto support was compiled into this build.");
Dees_Troy5bf43922012-09-07 16:07:55 -04001445 return -1;
1446#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001447 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001448}
1449
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001450int TWPartitionManager::Fix_Contexts(void) {
thata3d31fb2014-12-21 22:27:40 +01001451#ifdef HAVE_SELINUX
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001452 std::vector<TWPartition*>::iterator iter;
1453 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1454 if ((*iter)->Has_Data_Media) {
1455 if ((*iter)->Mount(true)) {
1456 if (fixContexts::fixDataMediaContexts((*iter)->Mount_Point) != 0)
1457 return -1;
1458 }
1459 }
1460 }
Dees_Troy1a650e62012-10-19 20:54:32 -04001461 UnMount_Main_Partitions();
Ethan Yonker74db1572015-10-28 12:44:49 -05001462 gui_msg("done=Done.");
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001463 return 0;
1464#else
1465 LOGERR("Cannot fix contexts, no selinux support present.\n");
1466 return -1;
1467#endif
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001468}
1469
Ethan Yonker66a19492015-12-10 10:19:45 -06001470TWPartition* TWPartitionManager::Find_Next_Storage(string Path, bool Exclude_Data_Media) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001471 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1472
1473 if (!Path.empty()) {
1474 string Search_Path = TWFunc::Get_Root_Path(Path);
1475 for (; iter != Partitions.end(); iter++) {
Matt Mower9a561dd2016-02-22 21:25:51 -06001476 if ((*iter)->Mount_Point == Search_Path) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001477 iter++;
1478 break;
1479 }
1480 }
1481 }
1482
1483 for (; iter != Partitions.end(); iter++) {
Ethan Yonker66a19492015-12-10 10:19:45 -06001484 if (Exclude_Data_Media && (*iter)->Has_Data_Media) {
1485 // do nothing, do not return this type of partition
1486 } else if ((*iter)->Is_Storage && (*iter)->Is_Present) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001487 return (*iter);
1488 }
1489 }
1490
1491 return NULL;
1492}
1493
Dees_Troyd21618c2012-10-14 18:48:49 -04001494int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001495 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1496
1497 if (Part == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001498 LOGINFO("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
1499 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 -04001500 return false;
1501 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001502 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1503 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001504 return false;
1505
Dees_Troy2673cec2013-04-02 20:22:16 +00001506 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1507 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001508 return false;
1509 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001510 return true;
1511}
1512
Dees_Troy8170a922012-09-18 15:40:25 -04001513int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001514 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001515 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001516 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001517
Dees_Troy8170a922012-09-18 15:40:25 -04001518 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001519 string Lun_File_str = CUSTOM_LUN_FILE;
1520 size_t found = Lun_File_str.find("%");
1521 if (found != string::npos) {
1522 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1523 if (TWFunc::Path_Exists(lun_file))
1524 has_multiple_lun = true;
1525 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001526 mtp_was_enabled = TWFunc::Toggle_MTP(false); // Must disable MTP for USB Storage
Ethan Yonker47360be2014-04-01 10:34:34 -05001527 if (!has_multiple_lun) {
1528 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1529 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1530 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
Ethan Yonker66a19492015-12-10 10:19:45 -06001531 TWPartition* Mount = Find_Next_Storage("", true);
Ethan Yonker47360be2014-04-01 10:34:34 -05001532 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001533 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1534 goto error_handle;
1535 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001536 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001537 gui_err("unable_locate_storage=Unable to locate storage device.");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001538 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001539 }
1540 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001541 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001542 }
Dees_Troy8170a922012-09-18 15:40:25 -04001543 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001544 LOGINFO("Device has multiple lun files\n");
1545 TWPartition* Mount1;
1546 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001547 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker66a19492015-12-10 10:19:45 -06001548 Mount1 = Find_Next_Storage("", true);
Ethan Yonker47360be2014-04-01 10:34:34 -05001549 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001550 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1551 goto error_handle;
1552 }
Matt Moweree717062014-04-26 01:46:46 -05001553 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker66a19492015-12-10 10:19:45 -06001554 Mount2 = Find_Next_Storage(Mount1->Mount_Point, true);
Matt Mower1fdcdb72016-01-25 20:53:47 -06001555 if (Mount2 && Mount2->Mount_Point != Mount1->Mount_Point) {
Matt Moweree717062014-04-26 01:46:46 -05001556 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001557 }
1558 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001559 gui_err("unable_locate_storage=Unable to locate storage device.");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001560 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001561 }
Dees_Troy8170a922012-09-18 15:40:25 -04001562 }
Matt Mowerb6ef4782014-11-06 02:24:36 -06001563 property_set("sys.storage.ums_enabled", "1");
HandyMenny37d42992014-10-26 22:15:59 +01001564 property_set("sys.usb.config", "mass_storage,adb");
Dees_Troy8170a922012-09-18 15:40:25 -04001565 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001566error_handle:
1567 if (mtp_was_enabled)
1568 if (!Enable_MTP())
1569 Disable_MTP();
1570 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001571}
1572
1573int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001574 int index, ret;
1575 char lun_file[255], ch[2] = {0, 0};
1576 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001577
1578 for (index=0; index<2; index++) {
1579 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001580 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001581 if (ret < 0) {
1582 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001583 }
Dees_Troy8170a922012-09-18 15:40:25 -04001584 }
Dees_Troye58d5262012-09-21 12:27:57 -04001585 Mount_All_Storage();
1586 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001587 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001588 property_set("sys.storage.ums_enabled", "0");
HandyMenny37d42992014-10-26 22:15:59 +01001589 property_set("sys.usb.config", "adb");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001590 if (mtp_was_enabled)
1591 if (!Enable_MTP())
1592 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001593 if (ret < 0 && index == 0) {
1594 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1595 return false;
1596 } else {
1597 return true;
1598 }
Dees_Troy8170a922012-09-18 15:40:25 -04001599 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001600}
1601
1602void TWPartitionManager::Mount_All_Storage(void) {
1603 std::vector<TWPartition*>::iterator iter;
1604
1605 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1606 if ((*iter)->Is_Storage)
1607 (*iter)->Mount(false);
1608 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001609}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001610
Dees_Troyd0384ef2012-10-12 12:15:42 -04001611void TWPartitionManager::UnMount_Main_Partitions(void) {
1612 // Unmounts system and data if data is not data/media
1613 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001614 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001615
1616 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1617
1618 UnMount_By_Path("/system", true);
Ethan Yonker6277c792014-09-15 14:54:30 -05001619 if (!datamedia)
1620 UnMount_By_Path("/data", true);
1621
Dees_Troyd0384ef2012-10-12 12:15:42 -04001622 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1623 Boot_Partition->UnMount(true);
1624}
1625
Dees_Troy9350b8d2012-09-27 12:38:38 -04001626int TWPartitionManager::Partition_SDCard(void) {
1627 char mkdir_path[255], temp[255], line[512];
Ethan Yonker483e9f42016-01-11 22:21:18 -06001628 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 -04001629 int ext, swap, total_size = 0, fat_size;
Dees_Troy9350b8d2012-09-27 12:38:38 -04001630
Ethan Yonker74db1572015-10-28 12:44:49 -05001631 gui_msg("start_partition_sd=Partitioning SD Card...");
Ethan Yonker483e9f42016-01-11 22:21:18 -06001632
1633 // Locate and validate device to partition
1634 TWPartition* SDCard = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
1635
Ethan Yonker66a19492015-12-10 10:19:45 -06001636 if (SDCard->Is_Adopted_Storage)
1637 SDCard->Revert_Adopted();
1638
Ethan Yonker1eff6cd2014-09-15 13:30:42 -05001639 if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001640 gui_err("partition_sd_locate=Unable to locate device to partition.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001641 return false;
1642 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001643
1644 // Unmount everything
Dees_Troy9350b8d2012-09-27 12:38:38 -04001645 if (!SDCard->UnMount(true))
1646 return false;
1647 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1648 if (SDext != NULL) {
1649 if (!SDext->UnMount(true))
1650 return false;
1651 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001652 char* swappath = getenv("SWAPPATH");
1653 if (swappath != NULL) {
1654 LOGINFO("Unmounting swap at '%s'\n", swappath);
1655 umount(swappath);
1656 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001657
Ethan Yonker483e9f42016-01-11 22:21:18 -06001658 // Determine block device
1659 if (SDCard->Alternate_Block_Device.empty()) {
1660 SDCard->Find_Actual_Block_Device();
1661 Device = SDCard->Actual_Block_Device;
1662 // Just use the root block device
1663 Device.resize(strlen("/dev/block/mmcblkX"));
1664 } else {
1665 Device = SDCard->Alternate_Block_Device;
1666 }
Dees_Troy9350b8d2012-09-27 12:38:38 -04001667
1668 // Find the size of the block device:
Ethan Yonker483e9f42016-01-11 22:21:18 -06001669 total_size = (int)(TWFunc::IOCTL_Get_Block_Size(Device.c_str()) / (1048576));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001670
1671 DataManager::GetValue("tw_sdext_size", ext);
1672 DataManager::GetValue("tw_swap_size", swap);
1673 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1674 fat_size = total_size - ext - swap;
Ethan Yonker483e9f42016-01-11 22:21:18 -06001675 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);
1676
1677 // Determine partition sizes
1678 if (swap == 0 && ext == 0) {
1679 fat_str = "-0";
1680 } else {
1681 memset(temp, 0, sizeof(temp));
1682 sprintf(temp, "%i", fat_size);
1683 fat_str = temp;
1684 fat_str += "MB";
1685 }
1686 if (swap == 0) {
1687 ext_str = "-0";
1688 } else {
1689 memset(temp, 0, sizeof(temp));
1690 sprintf(temp, "%i", ext);
1691 ext_str = "+";
1692 ext_str += temp;
1693 ext_str += "MB";
1694 }
1695
Dees_Troy9350b8d2012-09-27 12:38:38 -04001696 if (ext + swap > total_size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001697 gui_err("ext_swap_size=EXT + Swap size is larger than sdcard size.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001698 return false;
1699 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001700
Ethan Yonker74db1572015-10-28 12:44:49 -05001701 gui_msg("remove_part_table=Removing partition table...");
Ethan Yonker483e9f42016-01-11 22:21:18 -06001702 Command = "sgdisk --zap-all " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001703 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001704 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001705 gui_err("unable_rm_part=Unable to remove partition table.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001706 Update_System_Details();
1707 return false;
1708 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001709 gui_msg(Msg("create_part=Creating {1} partition...")("FAT32"));
Ethan Yonker483e9f42016-01-11 22:21:18 -06001710 Command = "sgdisk --new=0:0:" + fat_str + " --change-name=0:\"Microsoft basic data\" " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001711 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001712 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001713 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("FAT32"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001714 return false;
1715 }
1716 if (ext > 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001717 gui_msg(Msg("create_part=Creating {1} partition...")("EXT"));
Ethan Yonker483e9f42016-01-11 22:21:18 -06001718 Command = "sgdisk --new=0:0:" + ext_str + " --change-name=0:\"Linux filesystem\" " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001719 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001720 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001721 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("EXT"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001722 Update_System_Details();
1723 return false;
1724 }
1725 }
1726 if (swap > 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001727 gui_msg(Msg("create_part=Creating {1} partition...")("swap"));
Ethan Yonker483e9f42016-01-11 22:21:18 -06001728 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 +00001729 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001730 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001731 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("swap"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001732 Update_System_Details();
1733 return false;
1734 }
1735 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001736
1737 // Convert GPT to MBR
1738 Command = "sgdisk --gpttombr " + Device;
1739 if (TWFunc::Exec_Cmd(Command) != 0)
1740 LOGINFO("Failed to covert partition GPT to MBR\n");
1741
1742 // Tell the kernel to rescan the partition table
1743 int fd = open(Device.c_str(), O_RDONLY);
1744 ioctl(fd, BLKRRPART, 0);
1745 close(fd);
1746
1747 string format_device = Device;
1748 if (Device.substr(0, 17) == "/dev/block/mmcblk")
1749 format_device += "p";
1750
1751 // Format new partitions to proper file system
1752 if (fat_size > 0) {
1753 Command = "mkfs.fat " + format_device + "1";
1754 TWFunc::Exec_Cmd(Command);
1755 }
Dees_Troy9350b8d2012-09-27 12:38:38 -04001756 if (ext > 0) {
1757 if (SDext == NULL) {
Ethan Yonker483e9f42016-01-11 22:21:18 -06001758 Command = "mke2fs -t " + ext_format + " -m 0 " + format_device + "2";
1759 gui_msg(Msg("format_sdext_as=Formatting sd-ext as {1}...")(ext_format));
1760 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
1761 TWFunc::Exec_Cmd(Command);
1762 } else {
1763 SDext->Wipe(ext_format);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001764 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001765 }
1766 if (swap > 0) {
1767 Command = "mkswap " + format_device;
1768 if (ext > 0)
1769 Command += "3";
1770 else
1771 Command += "2";
Vojtech Bocek05534202013-09-11 08:11:56 +02001772 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001773 }
1774
Ethan Yonker483e9f42016-01-11 22:21:18 -06001775 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1776 if (SDCard->Mount(true)) {
1777 string TWRP_Folder = SDCard->Mount_Point + "/TWRP";
1778 mkdir(TWRP_Folder.c_str(), 0777);
1779 DataManager::Flush();
1780 }
1781
Dees_Troy9350b8d2012-09-27 12:38:38 -04001782 Update_System_Details();
Ethan Yonker74db1572015-10-28 12:44:49 -05001783 gui_msg("part_complete=Partitioning complete.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001784 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001785}
Dees_Troya13d74f2013-03-24 08:54:55 -05001786
1787void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1788 std::vector<TWPartition*>::iterator iter;
1789 if (ListType == "mount") {
1790 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1791 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1792 struct PartitionList part;
1793 part.Display_Name = (*iter)->Display_Name;
1794 part.Mount_Point = (*iter)->Mount_Point;
1795 part.selected = (*iter)->Is_Mounted();
1796 Partition_List->push_back(part);
1797 }
1798 }
1799 } else if (ListType == "storage") {
1800 char free_space[255];
1801 string Current_Storage = DataManager::GetCurrentStoragePath();
1802 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1803 if ((*iter)->Is_Storage) {
1804 struct PartitionList part;
1805 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1806 part.Display_Name = (*iter)->Storage_Name + " (";
1807 part.Display_Name += free_space;
1808 part.Display_Name += "MB)";
1809 part.Mount_Point = (*iter)->Storage_Path;
1810 if ((*iter)->Storage_Path == Current_Storage)
1811 part.selected = 1;
1812 else
1813 part.selected = 0;
1814 Partition_List->push_back(part);
1815 }
1816 }
1817 } else if (ListType == "backup") {
1818 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001819 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001820 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001821 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001822 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001823 Backup_Size = (*iter)->Backup_Size;
1824 if ((*iter)->Has_SubPartition) {
1825 std::vector<TWPartition*>::iterator subpart;
1826
1827 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1828 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1829 Backup_Size += (*subpart)->Backup_Size;
1830 }
1831 }
1832 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001833 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1834 part.Display_Name += backup_size;
1835 part.Display_Name += "MB)";
1836 part.Mount_Point = (*iter)->Backup_Path;
1837 part.selected = 0;
1838 Partition_List->push_back(part);
1839 }
1840 }
1841 } else if (ListType == "restore") {
1842 string Restore_List, restore_path;
1843 TWPartition* restore_part = NULL;
1844
1845 DataManager::GetValue("tw_restore_list", Restore_List);
1846 if (!Restore_List.empty()) {
1847 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1848 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1849 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05001850 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00001851 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001852 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05001853 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05001854 } else {
1855 struct PartitionList part;
1856 part.Display_Name = restore_part->Backup_Display_Name;
1857 part.Mount_Point = restore_part->Backup_Path;
1858 part.selected = 1;
1859 Partition_List->push_back(part);
1860 }
1861 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001862 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -05001863 }
1864 start_pos = end_pos + 1;
1865 end_pos = Restore_List.find(";", start_pos);
1866 }
1867 }
1868 } else if (ListType == "wipe") {
1869 struct PartitionList dalvik;
Ethan Yonker74db1572015-10-28 12:44:49 -05001870 dalvik.Display_Name = gui_parse_text("{@dalvik}");
Dees_Troya13d74f2013-03-24 08:54:55 -05001871 dalvik.Mount_Point = "DALVIK";
1872 dalvik.selected = 0;
1873 Partition_List->push_back(dalvik);
1874 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1875 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1876 struct PartitionList part;
1877 part.Display_Name = (*iter)->Display_Name;
1878 part.Mount_Point = (*iter)->Mount_Point;
1879 part.selected = 0;
1880 Partition_List->push_back(part);
1881 }
1882 if ((*iter)->Has_Android_Secure) {
1883 struct PartitionList part;
1884 part.Display_Name = (*iter)->Backup_Display_Name;
1885 part.Mount_Point = (*iter)->Backup_Path;
1886 part.selected = 0;
1887 Partition_List->push_back(part);
1888 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001889 if ((*iter)->Has_Data_Media) {
1890 struct PartitionList datamedia;
1891 datamedia.Display_Name = (*iter)->Storage_Name;
1892 datamedia.Mount_Point = "INTERNAL";
1893 datamedia.selected = 0;
1894 Partition_List->push_back(datamedia);
1895 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001896 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06001897 } else if (ListType == "flashimg") {
1898 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1899 if ((*iter)->Can_Flash_Img && (*iter)->Is_Present) {
1900 struct PartitionList part;
1901 part.Display_Name = (*iter)->Backup_Display_Name;
1902 part.Mount_Point = (*iter)->Backup_Path;
1903 part.selected = 0;
1904 Partition_List->push_back(part);
1905 }
1906 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001907 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001908 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001909 }
1910}
1911
1912int TWPartitionManager::Fstab_Processed(void) {
1913 return Partitions.size();
1914}
Dees_Troyd93bda52013-07-03 19:55:19 +00001915
1916void TWPartitionManager::Output_Storage_Fstab(void) {
1917 std::vector<TWPartition*>::iterator iter;
1918 char storage_partition[255];
1919 string Temp;
1920 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
1921
1922 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001923 gui_msg(Msg(msg::kError, "unable_to_open=Unable to open '{1}'.")("/cache/recovery/storage.fstab"));
Dees_Troyd93bda52013-07-03 19:55:19 +00001924 return;
1925 }
1926
1927 // Iterate through all partitions
1928 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1929 if ((*iter)->Is_Storage) {
1930 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
1931 strcpy(storage_partition, Temp.c_str());
1932 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
1933 }
1934 }
1935 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001936}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02001937
1938TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
1939{
1940 TWPartition *res = NULL;
1941 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
1942 if(!(*iter)->Is_Storage)
1943 continue;
1944
1945 if((*iter)->Is_Settings_Storage)
1946 return *iter;
1947
1948 if(!res)
1949 res = *iter;
1950 }
1951 return res;
1952}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001953
1954bool TWPartitionManager::Enable_MTP(void) {
1955#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001956 if (mtppid) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001957 gui_err("mtp_already_enabled=MTP already enabled");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001958 return true;
1959 }
1960 //Launch MTP Responder
1961 LOGINFO("Starting MTP\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06001962
1963 int mtppipe[2];
1964
1965 if (pipe(mtppipe) < 0) {
1966 LOGERR("Error creating MTP pipe\n");
1967 return false;
1968 }
1969
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06001970 char old_value[PROPERTY_VALUE_MAX];
1971 property_get("sys.usb.config", old_value, "error");
1972 if (strcmp(old_value, "error") != 0 && strcmp(old_value, "mtp,adb") != 0) {
1973 char vendor[PROPERTY_VALUE_MAX];
1974 char product[PROPERTY_VALUE_MAX];
1975 property_set("sys.usb.config", "none");
1976 property_get("usb.vendor", vendor, "18D1");
1977 property_get("usb.product.mtpadb", product, "4EE2");
1978 string vendorstr = vendor;
1979 string productstr = product;
1980 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
1981 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
1982 property_set("sys.usb.config", "mtp,adb");
1983 }
Ethan Yonker6d154c42014-09-03 14:19:43 -05001984 /* To enable MTP debug, use the twrp command line feature to
1985 * twrp set tw_mtp_debug 1
1986 */
1987 twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug"));
Ethan Yonker1b039202015-01-30 10:08:48 -06001988 mtppid = mtp->forkserver(mtppipe);
1989 if (mtppid) {
1990 close(mtppipe[0]); // Host closes read side
1991 mtp_write_fd = mtppipe[1];
1992 DataManager::SetValue("tw_mtp_enabled", 1);
1993 Add_All_MTP_Storage();
1994 return true;
Ethan Yonker726a0202014-12-16 20:01:38 -06001995 } else {
1996 close(mtppipe[0]);
1997 close(mtppipe[1]);
Ethan Yonker74db1572015-10-28 12:44:49 -05001998 gui_err("mtp_fail=Failed to enable MTP");
Ethan Yonker1b039202015-01-30 10:08:48 -06001999 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002000 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002001#else
Ethan Yonker74db1572015-10-28 12:44:49 -05002002 gui_err("no_mtp=MTP support not included");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002003#endif
2004 DataManager::SetValue("tw_mtp_enabled", 0);
2005 return false;
2006}
2007
Ethan Yonker1b039202015-01-30 10:08:48 -06002008void TWPartitionManager::Add_All_MTP_Storage(void) {
2009#ifdef TW_HAS_MTP
2010 std::vector<TWPartition*>::iterator iter;
2011
2012 if (!mtppid)
2013 return; // MTP is not enabled
2014
2015 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2016 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false))
2017 Add_Remove_MTP_Storage((*iter), MTP_MESSAGE_ADD_STORAGE);
2018 }
2019#else
2020 return;
2021#endif
2022}
2023
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002024bool TWPartitionManager::Disable_MTP(void) {
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002025 char old_value[PROPERTY_VALUE_MAX];
2026 property_get("sys.usb.config", old_value, "error");
2027 if (strcmp(old_value, "adb") != 0) {
2028 char vendor[PROPERTY_VALUE_MAX];
2029 char product[PROPERTY_VALUE_MAX];
2030 property_set("sys.usb.config", "none");
2031 property_get("usb.vendor", vendor, "18D1");
2032 property_get("usb.product.adb", product, "D002");
2033 string vendorstr = vendor;
2034 string productstr = product;
2035 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2036 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
2037 usleep(2000);
2038 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002039#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002040 if (mtppid) {
Ethan Yonker8613dc02014-09-11 09:28:20 -05002041 LOGINFO("Disabling MTP\n");
2042 int status;
2043 kill(mtppid, SIGKILL);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002044 mtppid = 0;
Ethan Yonker8613dc02014-09-11 09:28:20 -05002045 // We don't care about the exit value, but this prevents a zombie process
2046 waitpid(mtppid, &status, 0);
Ethan Yonker726a0202014-12-16 20:01:38 -06002047 close(mtp_write_fd);
2048 mtp_write_fd = -1;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002049 }
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002050#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002051 property_set("sys.usb.config", "adb");
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002052#ifdef TW_HAS_MTP
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002053 DataManager::SetValue("tw_mtp_enabled", 0);
2054 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002055#endif
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002056 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002057}
Ethan Yonker726a0202014-12-16 20:01:38 -06002058
2059TWPartition* TWPartitionManager::Find_Partition_By_MTP_Storage_ID(unsigned int Storage_ID) {
2060 std::vector<TWPartition*>::iterator iter;
2061
2062 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2063 if ((*iter)->MTP_Storage_ID == Storage_ID)
2064 return (*iter);
2065 }
2066 return NULL;
2067}
2068
2069bool TWPartitionManager::Add_Remove_MTP_Storage(TWPartition* Part, int message_type) {
2070#ifdef TW_HAS_MTP
2071 struct mtpmsg mtp_message;
2072
2073 if (!mtppid)
2074 return false; // MTP is disabled
2075
2076 if (mtp_write_fd < 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002077 LOGINFO("MTP: mtp_write_fd is not set\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06002078 return false;
2079 }
2080
2081 if (Part) {
Ethan Yonker4bfabab2014-12-29 09:15:37 -06002082 if (Part->MTP_Storage_ID == 0)
2083 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002084 if (message_type == MTP_MESSAGE_REMOVE_STORAGE) {
2085 mtp_message.message_type = MTP_MESSAGE_REMOVE_STORAGE; // Remove
2086 LOGINFO("sending message to remove %i\n", Part->MTP_Storage_ID);
2087 mtp_message.storage_id = Part->MTP_Storage_ID;
2088 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002089 LOGINFO("error sending message to remove storage %i\n", Part->MTP_Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002090 return false;
2091 } else {
2092 LOGINFO("Message sent, remove storage ID: %i\n", Part->MTP_Storage_ID);
2093 return true;
2094 }
2095 } else if (message_type == MTP_MESSAGE_ADD_STORAGE && Part->Is_Mounted()) {
2096 mtp_message.message_type = MTP_MESSAGE_ADD_STORAGE; // Add
2097 mtp_message.storage_id = Part->MTP_Storage_ID;
2098 mtp_message.path = Part->Storage_Path.c_str();
2099 mtp_message.display = Part->Storage_Name.c_str();
2100 mtp_message.maxFileSize = Part->Get_Max_FileSize();
Ethan Yonker1b039202015-01-30 10:08:48 -06002101 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 -06002102 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002103 LOGINFO("error sending message to add storage %i\n", Part->MTP_Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002104 return false;
2105 } else {
2106 LOGINFO("Message sent, add storage ID: %i\n", Part->MTP_Storage_ID);
2107 return true;
2108 }
2109 } else {
2110 LOGERR("Unknown MTP message type: %i\n", message_type);
2111 }
2112 } else {
2113 // This hopefully never happens as the error handling should
2114 // occur in the calling function.
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002115 LOGINFO("TWPartitionManager::Add_Remove_MTP_Storage NULL partition given\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06002116 }
2117 return true;
2118#else
Ethan Yonker74db1572015-10-28 12:44:49 -05002119 gui_err("no_mtp=MTP support not included");
Ethan Yonker726a0202014-12-16 20:01:38 -06002120 DataManager::SetValue("tw_mtp_enabled", 0);
2121 return false;
2122#endif
2123}
2124
2125bool TWPartitionManager::Add_MTP_Storage(string Mount_Point) {
2126#ifdef TW_HAS_MTP
2127 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2128 if (Part) {
2129 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2130 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002131 LOGINFO("TWFunc::Add_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -06002132 }
2133#endif
2134 return false;
2135}
2136
2137bool TWPartitionManager::Add_MTP_Storage(unsigned int Storage_ID) {
2138#ifdef TW_HAS_MTP
2139 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2140 if (Part) {
2141 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2142 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002143 LOGINFO("TWFunc::Add_MTP_Storage unable to locate partition for %i\n", Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002144 }
2145#endif
2146 return false;
2147}
2148
2149bool TWPartitionManager::Remove_MTP_Storage(string Mount_Point) {
2150#ifdef TW_HAS_MTP
2151 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2152 if (Part) {
2153 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2154 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002155 LOGINFO("TWFunc::Remove_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -06002156 }
2157#endif
2158 return false;
2159}
2160
2161bool TWPartitionManager::Remove_MTP_Storage(unsigned int Storage_ID) {
2162#ifdef TW_HAS_MTP
2163 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2164 if (Part) {
2165 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2166 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002167 LOGINFO("TWFunc::Remove_MTP_Storage unable to locate partition for %i\n", Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002168 }
2169#endif
2170 return false;
2171}
Ethan Yonker96af84a2015-01-05 14:58:36 -06002172
2173bool TWPartitionManager::Flash_Image(string Filename) {
2174 int check, partition_count = 0;
2175 TWPartition* flash_part = NULL;
2176 string Flash_List, flash_path;
2177 size_t start_pos = 0, end_pos = 0;
2178
Ethan Yonker74db1572015-10-28 12:44:49 -05002179 gui_msg("image_flash_start=[IMAGE FLASH STARTED]");
2180 gui_msg(Msg("img_to_flash=Image to flash: '{1}'")(Filename));
Ethan Yonker96af84a2015-01-05 14:58:36 -06002181
Ethan Yonkerb78fbdf2016-01-15 16:46:35 -06002182 if (!TWFunc::Path_Exists(Filename)) {
2183 if (!Mount_By_Path(Filename, true)) {
2184 return false;
2185 }
2186 if (!TWFunc::Path_Exists(Filename)) {
2187 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(Filename));
2188 return false;
2189 }
2190 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002191
Ethan Yonker74db1572015-10-28 12:44:49 -05002192 gui_msg("calc_restore=Calculating restore details...");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002193 DataManager::GetValue("tw_flash_partition", Flash_List);
2194 if (!Flash_List.empty()) {
2195 end_pos = Flash_List.find(";", start_pos);
2196 while (end_pos != string::npos && start_pos < Flash_List.size()) {
2197 flash_path = Flash_List.substr(start_pos, end_pos - start_pos);
2198 flash_part = Find_Partition_By_Path(flash_path);
2199 if (flash_part != NULL) {
2200 partition_count++;
2201 if (partition_count > 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002202 gui_err("too_many_flash=Too many partitions selected for flashing.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002203 return false;
2204 }
2205 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002206 gui_msg(Msg(msg::kError, "flash_unable_locate=Unable to locate '{1}' partition for flashing.")(flash_path));
Ethan Yonker96af84a2015-01-05 14:58:36 -06002207 return false;
2208 }
2209 start_pos = end_pos + 1;
2210 end_pos = Flash_List.find(";", start_pos);
2211 }
2212 }
2213
2214 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002215 gui_err("no_part_flash=No partitions selected for flashing.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002216 return false;
2217 }
2218
2219 DataManager::SetProgress(0.0);
2220 if (flash_part) {
2221 if (!flash_part->Flash_Image(Filename))
2222 return false;
2223 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002224 gui_err("invalid_flash=Invalid flash partition specified.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002225 return false;
2226 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002227 gui_highlight("flash_done=IMAGE FLASH COMPLETED]");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002228 return true;
2229}
Ethan Yonker74db1572015-10-28 12:44:49 -05002230
2231void TWPartitionManager::Translate_Partition(const char* path, const char* resource_name, const char* default_value) {
2232 TWPartition* part = PartitionManager.Find_Partition_By_Path(path);
2233 if (part) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002234 if (part->Is_Adopted_Storage) {
2235 part->Display_Name = part->Display_Name + " - " + gui_lookup("data", "Data");
2236 part->Backup_Display_Name = part->Display_Name;
2237 part->Storage_Name = part->Storage_Name + " - " + gui_lookup("adopted_storage", "Adopted Storage");
2238 } else {
2239 part->Display_Name = gui_lookup(resource_name, default_value);
2240 part->Backup_Display_Name = part->Display_Name;
2241 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002242 }
2243}
2244
2245void TWPartitionManager::Translate_Partition(const char* path, const char* resource_name, const char* default_value, const char* storage_resource_name, const char* storage_default_value) {
2246 TWPartition* part = PartitionManager.Find_Partition_By_Path(path);
2247 if (part) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002248 if (part->Is_Adopted_Storage) {
2249 part->Display_Name = part->Display_Name + " - " + gui_lookup("data", "Data");
2250 part->Backup_Display_Name = part->Display_Name;
2251 part->Storage_Name = part->Storage_Name + " - " + gui_lookup("adopted_storage", "Adopted Storage");
2252 } else {
2253 part->Display_Name = gui_lookup(resource_name, default_value);
2254 part->Backup_Display_Name = part->Display_Name;
2255 if (part->Is_Storage)
2256 part->Storage_Name = gui_lookup(storage_resource_name, storage_default_value);
2257 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002258 }
2259}
2260
2261void TWPartitionManager::Translate_Partition_Display_Names() {
Ethan Yonker66a19492015-12-10 10:19:45 -06002262 LOGINFO("Translating partition display names\n");
Ethan Yonker74db1572015-10-28 12:44:49 -05002263 Translate_Partition("/system", "system", "System");
2264 Translate_Partition("/system_image", "system_image", "System Image");
2265 Translate_Partition("/vendor", "vendor", "Vendor");
2266 Translate_Partition("/vendor_image", "vendor_image", "Vendor Image");
2267 Translate_Partition("/cache", "cache", "Cache");
2268 Translate_Partition("/data", "data", "Data", "internal", "Internal Storage");
2269 Translate_Partition("/boot", "boot", "Boot");
2270 Translate_Partition("/recovery", "recovery", "Recovery");
2271 if (!datamedia) {
2272 Translate_Partition("/sdcard", "sdcard", "SDCard", "sdcard", "SDCard");
2273 Translate_Partition("/internal_sd", "sdcard", "SDCard", "sdcard", "SDCard");
2274 Translate_Partition("/internal_sdcard", "sdcard", "SDCard", "sdcard", "SDCard");
2275 Translate_Partition("/emmc", "sdcard", "SDCard", "sdcard", "SDCard");
2276 }
2277 Translate_Partition("/external_sd", "microsd", "Micro SDCard", "microsd", "Micro SDCard");
2278 Translate_Partition("/external_sdcard", "microsd", "Micro SDCard", "microsd", "Micro SDCard");
2279 Translate_Partition("/usb-otg", "usbotg", "USB OTG", "usbotg", "USB OTG");
2280 Translate_Partition("/sd-ext", "sdext", "SD-EXT");
2281
2282 // Android secure is a special case
2283 TWPartition* part = PartitionManager.Find_Partition_By_Path("/and-sec");
2284 if (part)
2285 part->Backup_Display_Name = gui_lookup("android_secure", "Android Secure");
2286
2287 // This updates the text on all of the storage selection buttons in the GUI
2288 DataManager::SetBackupFolder();
2289}
Ethan Yonker66a19492015-12-10 10:19:45 -06002290
2291void TWPartitionManager::Decrypt_Adopted() {
2292#ifdef TW_INCLUDE_CRYPTO
2293 if (!Mount_By_Path("/data", false)) {
2294 LOGERR("Cannot decrypt adopted storage because /data will not mount\n");
2295 return;
2296 }
2297 LOGINFO("Decrypt adopted storage starting\n");
2298 char* xmlFile = PageManager::LoadFileToBuffer("/data/system/storage.xml", NULL);
2299 xml_document<> *doc = NULL;
2300 xml_node<>* volumes = NULL;
2301 xml_node<>* volume = NULL;
2302 string Primary_Storage_UUID = "";
2303 if (xmlFile != NULL) {
2304 LOGINFO("successfully loaded storage.xml\n");
2305 doc = new xml_document<>();
2306 doc->parse<0>(xmlFile);
2307 volumes = doc->first_node("volumes");
2308 if (volumes) {
2309 xml_attribute<>* psuuid = volumes->first_attribute("primaryStorageUuid");
2310 if (psuuid) {
2311 Primary_Storage_UUID = psuuid->value();
2312 }
2313 }
2314 }
2315 std::vector<TWPartition*>::iterator adopt;
2316 for (adopt = Partitions.begin(); adopt != Partitions.end(); adopt++) {
2317 if ((*adopt)->Removable && (*adopt)->Is_Present) {
2318 if ((*adopt)->Decrypt_Adopted() == 0) {
2319 if (volumes) {
2320 xml_node<>* volume = volumes->first_node("volume");
2321 while (volume) {
2322 xml_attribute<>* guid = volume->first_attribute("partGuid");
2323 if (guid) {
2324 string GUID = (*adopt)->Adopted_GUID.c_str();
2325 GUID.insert(8, "-");
2326 GUID.insert(13, "-");
2327 GUID.insert(18, "-");
2328 GUID.insert(23, "-");
2329
2330 if (strcasecmp(GUID.c_str(), guid->value()) == 0) {
2331 xml_attribute<>* attr = volume->first_attribute("nickname");
2332 if (attr) {
2333 (*adopt)->Storage_Name = attr->value();
2334 (*adopt)->Display_Name = (*adopt)->Storage_Name;
2335 (*adopt)->Backup_Display_Name = (*adopt)->Storage_Name;
2336 LOGINFO("storage name from storage.xml is '%s'\n", attr->value());
2337 }
2338 attr = volume->first_attribute("fsUuid");
2339 if (attr && !Primary_Storage_UUID.empty() && strcmp(Primary_Storage_UUID.c_str(), attr->value()) == 0) {
2340 TWPartition* Dat = Find_Partition_By_Path("/data");
2341 if (Dat) {
2342 LOGINFO("Internal storage is found on adopted storage '%s'\n", (*adopt)->Display_Name.c_str());
2343 LOGINFO("Changing '%s' to point to '%s'\n", Dat->Symlink_Mount_Point.c_str(), (*adopt)->Storage_Path.c_str());
2344 (*adopt)->Symlink_Mount_Point = Dat->Symlink_Mount_Point;
2345 Dat->Symlink_Mount_Point = "";
2346 // Toggle mounts to ensure that the symlink mount point (probably /sdcard) is mounted to the right location
2347 Dat->UnMount(false);
2348 Dat->Mount(false);
2349 (*adopt)->UnMount(false);
2350 (*adopt)->Mount(false);
2351 Output_Partition((*adopt));
2352 }
2353 }
2354 break;
2355 }
2356 }
2357 volume = volume->next_sibling("volume");
2358 }
2359 }
2360 }
2361 }
2362 }
2363 if (xmlFile) {
2364 doc->clear();
2365 delete doc;
2366 free(xmlFile);
2367 }
2368#else
2369 LOGINFO("Decrypt_Adopted: no crypto support\n");
2370 return;
2371#endif
2372}
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002373
2374void TWPartitionManager::Remove_Partition_By_Path(string Path) {
2375 std::vector<TWPartition*>::iterator iter;
2376 string Local_Path = TWFunc::Get_Root_Path(Path);
2377
2378 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2379 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
2380 LOGINFO("Found and erasing '%s' from partition list\n", Local_Path.c_str());
2381 Partitions.erase(iter);
2382 return;
2383 }
2384 }
2385}