blob: 89580d06957ca910ea35fd86d6ae797df4164819 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff7abc5fe2015-01-17 16:53:12 -05002 Copyright 2014 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
24#include <unistd.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040025#include <vector>
Dees_Troy63c8df72012-09-10 14:02:05 -040026#include <dirent.h>
27#include <time.h>
Dees_Troy8170a922012-09-18 15:40:25 -040028#include <errno.h>
29#include <fcntl.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050030#include <iostream>
31#include <iomanip>
Ethan Yonker8613dc02014-09-11 09:28:20 -050032#include <sys/wait.h>
Ethan Yonker483e9f42016-01-11 22:21:18 -060033#include <linux/fs.h>
34#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000036#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040037#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040038#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040039#include "twrp-functions.hpp"
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"
Dees_Troy38bd7602012-09-14 13:33:53 -040046
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040047#ifdef TW_HAS_MTP
48#include "mtp/mtp_MtpServer.hpp"
49#include "mtp/twrpMtp.hpp"
Ethan Yonker726a0202014-12-16 20:01:38 -060050#include "mtp/MtpMessage.hpp"
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040051#endif
52
Dees Troy6f6441d2014-01-23 02:07:03 +000053extern "C" {
54 #include "cutils/properties.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050055 #include "gui/gui.h"
Dees Troy6f6441d2014-01-23 02:07:03 +000056}
57
Dees_Troy5bf43922012-09-07 16:07:55 -040058#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060059 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060060 #include "gui/rapidxml.hpp"
61 #include "gui/pages.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040062#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040063
Ethan Yonker6277c792014-09-15 14:54:30 -050064extern bool datamedia;
65
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050066TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040067 mtp_was_enabled = false;
Ethan Yonker726a0202014-12-16 20:01:38 -060068 mtp_write_fd = -1;
bigbiff7abc5fe2015-01-17 16:53:12 -050069 stop_backup.set_value(0);
70 tar_fork_pid = 0;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050071}
72
Dees_Troy51a0e822012-09-05 15:24:24 -040073int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040074 FILE *fstabFile;
75 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +000076 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050077 TWPartition* andsec_partition = NULL;
Ethan Yonker726a0202014-12-16 20:01:38 -060078 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 -040079
80 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
81 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000082 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040083 return false;
84 }
85
86 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
87 if (fstab_line[0] != '/')
88 continue;
89
Dees_Troy2a923582012-09-20 12:13:34 -040090 if (fstab_line[strlen(fstab_line) - 1] != '\n')
91 fstab_line[strlen(fstab_line)] = '\n';
that9e0593e2014-10-08 00:01:24 +020092 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040093 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040094 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040095
Dees_Troy5bf43922012-09-07 16:07:55 -040096 if (partition->Process_Fstab_Line(line, Display_Error)) {
Ethan Yonker726a0202014-12-16 20:01:38 -060097 if (partition->Is_Storage) {
98 ++storageid;
99 partition->MTP_Storage_ID = storageid;
100 }
Matt Mowered71fa32014-04-16 13:21:47 -0500101 if (!settings_partition && partition->Is_Settings_Storage && partition->Is_Present) {
Dees Troy02a64532014-03-19 15:23:32 +0000102 settings_partition = partition;
Dees_Troya13d74f2013-03-24 08:54:55 -0500103 } else {
104 partition->Is_Settings_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500105 }
Matt Mowered71fa32014-04-16 13:21:47 -0500106 if (!andsec_partition && partition->Has_Android_Secure && partition->Is_Present) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500107 andsec_partition = partition;
108 } else {
109 partition->Has_Android_Secure = false;
110 }
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500111 Partitions.push_back(partition);
Dees_Troy5bf43922012-09-07 16:07:55 -0400112 } else {
113 delete partition;
114 }
115 }
116 fclose(fstabFile);
Ethan Yonker6277c792014-09-15 14:54:30 -0500117 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) {
118 // Attempt to automatically identify /data/media emulated storage devices
119 TWPartition* Dat = Find_Partition_By_Path("/data");
120 if (Dat) {
121 LOGINFO("Using automatic handling for /data/media emulated storage device.\n");
122 datamedia = true;
that9e0593e2014-10-08 00:01:24 +0200123 Dat->Setup_Data_Media();
Ethan Yonker6277c792014-09-15 14:54:30 -0500124 settings_partition = Dat;
Ethan Yonker726a0202014-12-16 20:01:38 -0600125 // Since /data was not considered a storage partition earlier, we still need to assign an MTP ID
126 ++storageid;
127 Dat->MTP_Storage_ID = storageid;
Ethan Yonker6277c792014-09-15 14:54:30 -0500128 }
129 }
Dees Troy02a64532014-03-19 15:23:32 +0000130 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500131 std::vector<TWPartition*>::iterator iter;
132 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
133 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000134 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500135 break;
136 }
137 }
Dees Troy02a64532014-03-19 15:23:32 +0000138 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000139 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500140 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400141 if (!Write_Fstab()) {
142 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000143 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400144 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000145 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400146 }
Matt Mowered71fa32014-04-16 13:21:47 -0500147
Matt Mowerbf4efa32014-04-14 23:25:26 -0500148 if (andsec_partition) {
149 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500150 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500151 Setup_Android_Secure_Location(settings_partition);
152 }
Matt Mowered71fa32014-04-16 13:21:47 -0500153 if (settings_partition) {
154 Setup_Settings_Storage_Partition(settings_partition);
155 }
Ethan Yonker253368a2014-11-25 15:00:52 -0600156#ifdef TW_INCLUDE_CRYPTO
Ethan Yonkercceebb82014-11-18 10:17:59 -0600157 TWPartition* Decrypt_Data = Find_Partition_By_Path("/data");
158 if (Decrypt_Data && Decrypt_Data->Is_Encrypted && !Decrypt_Data->Is_Decrypted) {
159 int password_type = cryptfs_get_password_type();
160 if (password_type == CRYPT_TYPE_DEFAULT) {
161 LOGINFO("Device is encrypted with the default password, attempting to decrypt.\n");
162 if (Decrypt_Device("default_password") == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500163 gui_msg("decrypt_success=Successfully decrypted with default password.");
Ethan Yonkercceebb82014-11-18 10:17:59 -0600164 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
165 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500166 gui_err("unable_to_decrypt=Unable to decrypt with default password.");
Ethan Yonkercceebb82014-11-18 10:17:59 -0600167 }
168 } else {
169 DataManager::SetValue("TW_CRYPTO_TYPE", password_type);
170 }
171 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600172 if (Decrypt_Data && (!Decrypt_Data->Is_Encrypted || Decrypt_Data->Is_Decrypted) && Decrypt_Data->Mount(false)) {
173 Decrypt_Adopted();
174 }
Ethan Yonkercceebb82014-11-18 10:17:59 -0600175#endif
Dees_Troy51127312012-09-08 13:08:49 -0400176 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400177 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400178 return true;
179}
180
181int TWPartitionManager::Write_Fstab(void) {
182 FILE *fp;
183 std::vector<TWPartition*>::iterator iter;
184 string Line;
185
186 fp = fopen("/etc/fstab", "w");
187 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000188 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400189 return false;
190 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400191 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400192 if ((*iter)->Can_Be_Mounted) {
dianlujitao7d304c72016-01-02 12:15:50 +0800193 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw 0 0\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400194 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000195 }
196 // Handle subpartition tracking
197 if ((*iter)->Is_SubPartition) {
198 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
199 if (ParentPartition)
200 ParentPartition->Has_SubPartition = true;
201 else
202 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 -0400203 }
204 }
205 fclose(fp);
206 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400207}
208
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500209void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500210 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
211 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
212 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
213}
214
Matt Mowerbf4efa32014-04-14 23:25:26 -0500215void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
216 if (Part->Has_Android_Secure)
217 Part->Setup_AndSec();
Ethan Yonker6277c792014-09-15 14:54:30 -0500218 else if (!datamedia)
Matt Mowerbf4efa32014-04-14 23:25:26 -0500219 Part->Setup_AndSec();
Matt Mowerbf4efa32014-04-14 23:25:26 -0500220}
221
Dees_Troy8170a922012-09-18 15:40:25 -0400222void TWPartitionManager::Output_Partition_Logging(void) {
223 std::vector<TWPartition*>::iterator iter;
224
225 printf("\n\nPartition Logs:\n");
226 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
227 Output_Partition((*iter));
228}
229
230void TWPartitionManager::Output_Partition(TWPartition* Part) {
231 unsigned long long mb = 1048576;
232
Gary Peck004d48b2012-11-21 16:28:18 -0800233 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 -0400234 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800235 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 -0400236 }
Gary Peck004d48b2012-11-21 16:28:18 -0800237 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500238 if (Part->Can_Be_Mounted)
239 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800240 if (Part->Can_Be_Wiped)
241 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700242 if (Part->Use_Rm_Rf)
243 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500244 if (Part->Can_Be_Backed_Up)
245 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800246 if (Part->Wipe_During_Factory_Reset)
247 printf("Wipe_During_Factory_Reset ");
248 if (Part->Wipe_Available_in_GUI)
249 printf("Wipe_Available_in_GUI ");
250 if (Part->Is_SubPartition)
251 printf("Is_SubPartition ");
252 if (Part->Has_SubPartition)
253 printf("Has_SubPartition ");
254 if (Part->Removable)
255 printf("Removable ");
256 if (Part->Is_Present)
257 printf("IsPresent ");
258 if (Part->Can_Be_Encrypted)
259 printf("Can_Be_Encrypted ");
260 if (Part->Is_Encrypted)
261 printf("Is_Encrypted ");
262 if (Part->Is_Decrypted)
263 printf("Is_Decrypted ");
264 if (Part->Has_Data_Media)
265 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000266 if (Part->Can_Encrypt_Backup)
267 printf("Can_Encrypt_Backup ");
268 if (Part->Use_Userdata_Encryption)
269 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800270 if (Part->Has_Android_Secure)
271 printf("Has_Android_Secure ");
272 if (Part->Is_Storage)
273 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500274 if (Part->Is_Settings_Storage)
275 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000276 if (Part->Ignore_Blkid)
277 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000278 if (Part->Retain_Layout_Version)
279 printf("Retain_Layout_Version ");
Ethan Yonker253368a2014-11-25 15:00:52 -0600280 if (Part->Mount_To_Decrypt)
281 printf("Mount_To_Decrypt ");
Ethan Yonker96af84a2015-01-05 14:58:36 -0600282 if (Part->Can_Flash_Img)
283 printf("Can_Flash_Img ");
Ethan Yonker66a19492015-12-10 10:19:45 -0600284 if (Part->Is_Adopted_Storage)
285 printf("Is_Adopted_Storage ");
Gary Peck004d48b2012-11-21 16:28:18 -0800286 printf("\n");
287 if (!Part->SubPartition_Of.empty())
288 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
289 if (!Part->Symlink_Path.empty())
290 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
291 if (!Part->Symlink_Mount_Point.empty())
292 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
293 if (!Part->Primary_Block_Device.empty())
294 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
295 if (!Part->Alternate_Block_Device.empty())
296 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
297 if (!Part->Decrypted_Block_Device.empty())
298 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
Ethan Yonker253368a2014-11-25 15:00:52 -0600299 if (!Part->Crypto_Key_Location.empty() && Part->Crypto_Key_Location != "footer")
300 printf(" Crypto_Key_Location: %s\n", Part->Crypto_Key_Location.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800301 if (Part->Length != 0)
302 printf(" Length: %i\n", Part->Length);
303 if (!Part->Display_Name.empty())
304 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500305 if (!Part->Storage_Name.empty())
306 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800307 if (!Part->Backup_Path.empty())
308 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
309 if (!Part->Backup_Name.empty())
310 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500311 if (!Part->Backup_Display_Name.empty())
312 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800313 if (!Part->Backup_FileName.empty())
314 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
315 if (!Part->Storage_Path.empty())
316 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
317 if (!Part->Current_File_System.empty())
318 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
319 if (!Part->Fstab_File_System.empty())
320 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
321 if (Part->Format_Block_Size != 0)
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500322 printf(" Format_Block_Size: %lu\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400323 if (!Part->MTD_Name.empty())
324 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400325 string back_meth = Part->Backup_Method_By_Name();
Ethan Yonker6277c792014-09-15 14:54:30 -0500326 printf(" Backup_Method: %s\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800327 if (Part->Mount_Flags || !Part->Mount_Options.empty())
328 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -0600329 if (Part->MTP_Storage_ID)
330 printf(" MTP_Storage_ID: %i\n", Part->MTP_Storage_ID);
Ethan Yonker6277c792014-09-15 14:54:30 -0500331 printf("\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400332}
333
Dees_Troy51a0e822012-09-05 15:24:24 -0400334int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400335 std::vector<TWPartition*>::iterator iter;
336 int ret = false;
337 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400338 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400339
Dees_Troyd93bda52013-07-03 19:55:19 +0000340 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400341 return true;
342
Dees_Troy5bf43922012-09-07 16:07:55 -0400343 // Iterate through all partitions
344 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400345 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400346 ret = (*iter)->Mount(Display_Error);
347 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400348 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400349 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400350 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400351 }
352 if (found) {
353 return ret;
354 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500355 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 -0400356 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000357 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400358 }
359 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400360}
361
Dees_Troy51a0e822012-09-05 15:24:24 -0400362int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400363 std::vector<TWPartition*>::iterator iter;
364 int ret = false;
365 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400366 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400367
368 // Iterate through all partitions
369 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400370 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400371 ret = (*iter)->UnMount(Display_Error);
372 found = true;
373 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
374 (*iter)->UnMount(Display_Error);
375 }
376 }
377 if (found) {
378 return ret;
379 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500380 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 -0400381 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000382 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400383 }
384 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400385}
386
Dees_Troy51a0e822012-09-05 15:24:24 -0400387int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400388 TWPartition* Part = Find_Partition_By_Path(Path);
389
390 if (Part)
391 return Part->Is_Mounted();
392 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000393 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400394 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400395}
396
Dees_Troy5bf43922012-09-07 16:07:55 -0400397int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400398 string current_storage_path = DataManager::GetCurrentStoragePath();
399
400 if (Mount_By_Path(current_storage_path, Display_Error)) {
401 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
402 if (FreeStorage)
403 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
404 return true;
405 }
406 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400407}
408
Dees_Troy5bf43922012-09-07 16:07:55 -0400409int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
410 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
411}
412
413TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
414 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400415 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400416
417 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400418 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400419 return (*iter);
420 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400421 return NULL;
422}
423
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400424int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
425 // Check the backup name to ensure that it is the correct size and contains only valid characters
426 // and that a backup with that name doesn't already exist
427 char backup_name[MAX_BACKUP_NAME_LEN];
428 char backup_loc[255], tw_image_dir[255];
429 int copy_size;
430 int index, cur_char;
431 string Backup_Name, Backup_Loc;
432
433 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
434 copy_size = Backup_Name.size();
435 // Check size
436 if (copy_size > MAX_BACKUP_NAME_LEN) {
437 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500438 gui_err("backup_name_len=Backup name is too long.");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400439 return -2;
440 }
441
442 // Check each character
443 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500444 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400445 return 0; // A "0" (zero) means to use the current timestamp for the backup name
446 for (index=0; index<copy_size; index++) {
447 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500448 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 -0400449 // These are valid characters
450 // Numbers
451 // Upper case letters
452 // Lower case letters
453 // Space
454 // and -_.{}[]
455 } else {
456 if (Display_Error)
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600457 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 -0400458 return -3;
459 }
460 }
461
462 // Check to make sure that a backup with this name doesn't already exist
463 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
464 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500465 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500466 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400467 if (Display_Error)
Matt Mower3c366972015-12-25 19:28:31 -0600468 gui_err("backup_name_exists=A backup with that name already exists!");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400469 return -4;
470 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400471 // No problems found, return 0
472 return 0;
473}
474
Dees_Troy43d8b002012-09-17 16:00:01 -0400475bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
476{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500477 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400478 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500479 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500480 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400481
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500482 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400483 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400484
Ethan Yonker74db1572015-10-28 12:44:49 -0500485 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, gui_parse_text("{@generating_md51}"));
486 gui_msg("generating_md52= * Generating md5...");
Dees_Troy43d8b002012-09-17 16:00:01 -0400487
488 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500489 md5sum.setfn(Backup_Folder + Backup_Filename);
490 if (md5sum.computeMD5() == 0)
491 if (md5sum.write_md5digest() == 0)
Ethan Yonker74db1572015-10-28 12:44:49 -0500492 gui_msg("md5_created= * MD5 Created.");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500493 else
494 return -1;
495 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500496 gui_err("md5_error= * MD5 Error!");
Dees_Troy43d8b002012-09-17 16:00:01 -0400497 } else {
498 char filename[512];
499 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500500 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400501 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500502 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000503 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400504 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000505 if (TWFunc::Path_Exists(filename)) {
506 if (md5sum.computeMD5() == 0) {
507 if (md5sum.write_md5digest() != 0)
508 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500509 gui_err("md5_error= * MD5 Error!");
Dees_Troy83bd4832013-05-04 12:39:56 +0000510 return false;
511 }
512 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500513 gui_err("md5_compute_error= * Error computing MD5.");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500514 return false;
515 }
516 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400517 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400518 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500519 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400520 }
521 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000522 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400523 return false;
524 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500525 gui_msg("md5_created= * MD5 Created.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400526 }
527 return true;
528}
529
Dees_Troy093b7642012-09-21 15:59:38 -0400530bool TWPartitionManager::Backup_Partition(TWPartition* Part, string Backup_Folder, bool generate_md5, unsigned long long* img_bytes_remaining, unsigned long long* file_bytes_remaining, unsigned long *img_time, unsigned long *file_time, unsigned long long *img_bytes, unsigned long long *file_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400531 time_t start, stop;
that203bcc92015-05-09 17:27:33 +0200532 int use_compression;
Dees_Troy093b7642012-09-21 15:59:38 -0400533 float pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500534 unsigned long long total_size, current_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400535
bigbiffbf1d6722015-02-14 16:25:59 -0500536 string backup_log = Backup_Folder + "recovery.log";
537
Dees_Troy43d8b002012-09-17 16:00:01 -0400538 if (Part == NULL)
539 return true;
540
Dees_Troy093b7642012-09-21 15:59:38 -0400541 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400542
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500543 total_size = *file_bytes + *img_bytes;
544 current_size = *file_bytes + *img_bytes - *file_bytes_remaining - *img_bytes_remaining;
that203bcc92015-05-09 17:27:33 +0200545 // Set the position
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500546 pos = ((float)(current_size) / (float)(total_size));
Dees_Troy2673cec2013-04-02 20:22:16 +0000547 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400548
Tom Hite5a926722014-09-15 01:31:03 +0000549 TWFunc::SetPerformanceMode(true);
Dees_Troy43d8b002012-09-17 16:00:01 -0400550 time(&start);
551
bigbiff7abc5fe2015-01-17 16:53:12 -0500552 if (Part->Backup(Backup_Folder, &total_size, &current_size, tar_fork_pid)) {
Tom Hite5a926722014-09-15 01:31:03 +0000553 bool md5Success = false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500554 current_size += Part->Backup_Size;
555 pos = (float)((float)(current_size) / (float)(total_size));
556 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400557 if (Part->Has_SubPartition) {
558 std::vector<TWPartition*>::iterator subpart;
559
560 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500561 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
bigbiff7abc5fe2015-01-17 16:53:12 -0500562 if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size, tar_fork_pid)) {
Tom Hite5a926722014-09-15 01:31:03 +0000563 TWFunc::SetPerformanceMode(false);
bigbiffbf1d6722015-02-14 16:25:59 -0500564 Clean_Backup_Folder(Backup_Folder);
565 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
566 tw_set_default_metadata(backup_log.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400567 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000568 }
Dees_Troy2727b992013-08-14 20:09:30 +0000569 sync();
570 sync();
Tom Hite5a926722014-09-15 01:31:03 +0000571 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName)) {
572 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400573 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000574 }
Dees_Troy093b7642012-09-21 15:59:38 -0400575 if (Part->Backup_Method == 1) {
576 *file_bytes_remaining -= (*subpart)->Backup_Size;
577 } else {
578 *img_bytes_remaining -= (*subpart)->Backup_Size;
579 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500580 current_size += Part->Backup_Size;
581 pos = (float)(current_size / total_size);
582 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400583 }
584 }
585 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400586 time(&stop);
that203bcc92015-05-09 17:27:33 +0200587 int backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000588 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400589 if (Part->Backup_Method == 1) {
590 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400591 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400592 } else {
593 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400594 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400595 }
Tom Hite5a926722014-09-15 01:31:03 +0000596
597 md5Success = Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
598 TWFunc::SetPerformanceMode(false);
599 return md5Success;
Dees_Troy43d8b002012-09-17 16:00:01 -0400600 } else {
bigbiffbf1d6722015-02-14 16:25:59 -0500601 Clean_Backup_Folder(Backup_Folder);
602 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
603 tw_set_default_metadata(backup_log.c_str());
Tom Hite5a926722014-09-15 01:31:03 +0000604 TWFunc::SetPerformanceMode(false);
Dees_Troy43d8b002012-09-17 16:00:01 -0400605 return false;
606 }
bigbiff7abc5fe2015-01-17 16:53:12 -0500607 return 0;
608}
609
bigbiffbf1d6722015-02-14 16:25:59 -0500610void TWPartitionManager::Clean_Backup_Folder(string Backup_Folder) {
611 DIR *d = opendir(Backup_Folder.c_str());
612 struct dirent *p;
613 int r;
614
Ethan Yonker74db1572015-10-28 12:44:49 -0500615 gui_msg("backup_clean=Backup Failed. Cleaning Backup Folder.");
bigbiffbf1d6722015-02-14 16:25:59 -0500616
617 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500618 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Backup_Folder)(strerror(errno)));
bigbiffbf1d6722015-02-14 16:25:59 -0500619 return;
620 }
621
Matt Mower2b18a532015-02-20 16:58:05 -0600622 while ((p = readdir(d))) {
bigbiffbf1d6722015-02-14 16:25:59 -0500623 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
624 continue;
625
626 string path = Backup_Folder + p->d_name;
627
628 size_t dot = path.find_last_of(".") + 1;
629 if (path.substr(dot) == "win" || path.substr(dot) == "md5" || path.substr(dot) == "info") {
630 r = unlink(path.c_str());
631 if (r != 0) {
632 LOGINFO("Unable to unlink '%s: %s'\n", path.c_str(), strerror(errno));
633 }
634 }
635 }
636 closedir(d);
637}
638
bigbiff7abc5fe2015-01-17 16:53:12 -0500639int TWPartitionManager::Cancel_Backup() {
640 string Backup_Folder, Backup_Name, Full_Backup_Path;
641
642 stop_backup.set_value(1);
643
644 if (tar_fork_pid != 0) {
645 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
646 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
647 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
648 LOGINFO("Killing pid: %d\n", tar_fork_pid);
649 kill(tar_fork_pid, SIGUSR2);
650 while (kill(tar_fork_pid, 0) == 0) {
651 usleep(1000);
652 }
653 LOGINFO("Backup_Run stopped and returning false, backup cancelled.\n");
654 LOGINFO("Removing directory %s\n", Full_Backup_Path.c_str());
655 TWFunc::removeDir(Full_Backup_Path, false);
656 tar_fork_pid = 0;
657 }
658
659 return 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400660}
661
662int TWPartitionManager::Run_Backup(void) {
Matt Mower2b18a532015-02-20 16:58:05 -0600663 int check, do_md5, partition_count = 0, disable_free_space_check = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500664 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400665 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 -0400666 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500667 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400668 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400669 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400670 struct tm *t;
671 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500672 size_t start_pos = 0, end_pos = 0;
bigbiff7abc5fe2015-01-17 16:53:12 -0500673 stop_backup.set_value(0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400674 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500675 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400676
677 time(&total_start);
678
679 Update_System_Details();
680
681 if (!Mount_Current_Storage(true))
682 return false;
683
684 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400685 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400686 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400687 else
688 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400689
Dees_Troy43d8b002012-09-17 16:00:01 -0400690 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
691 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600692 if (Backup_Name == gui_lookup("curr_date", "(Current Date)")) {
Dees Troyb21cc642013-09-10 17:36:41 +0000693 Backup_Name = TWFunc::Get_Current_Date();
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600694 } else if (Backup_Name == gui_lookup("auto_generate", "(Auto Generate)") || Backup_Name == "0" || Backup_Name.empty()) {
Dees Troyb21cc642013-09-10 17:36:41 +0000695 TWFunc::Auto_Generate_Backup_Name();
696 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400697 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000698 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400699 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000700 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400701
Dees_Troy2673cec2013-04-02 20:22:16 +0000702 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500703 DataManager::GetValue("tw_backup_list", Backup_List);
704 if (!Backup_List.empty()) {
705 end_pos = Backup_List.find(";", start_pos);
706 while (end_pos != string::npos && start_pos < Backup_List.size()) {
707 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
708 backup_part = Find_Partition_By_Path(backup_path);
709 if (backup_part != NULL) {
710 partition_count++;
711 if (backup_part->Backup_Method == 1)
712 file_bytes += backup_part->Backup_Size;
713 else
714 img_bytes += backup_part->Backup_Size;
715 if (backup_part->Has_SubPartition) {
716 std::vector<TWPartition*>::iterator subpart;
717
718 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000719 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 -0500720 partition_count++;
721 if ((*subpart)->Backup_Method == 1)
722 file_bytes += (*subpart)->Backup_Size;
723 else
724 img_bytes += (*subpart)->Backup_Size;
725 }
726 }
Dees_Troy8170a922012-09-18 15:40:25 -0400727 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500728 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500729 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 -0400730 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500731 start_pos = end_pos + 1;
732 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400733 }
734 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400735
736 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500737 gui_msg("no_partition_selected=No partitions selected for backup.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400738 return false;
739 }
740 total_bytes = file_bytes + img_bytes;
Ethan Yonker74db1572015-10-28 12:44:49 -0500741 gui_msg(Msg("total_partitions_backup= * Total number of partitions to back up: {1}")(partition_count));
742 gui_msg(Msg("total_backup_size= * Total size of all data: {1}MB")(total_bytes / 1024 / 1024));
Dees_Troy43d8b002012-09-17 16:00:01 -0400743 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
744 if (storage != NULL) {
745 free_space = storage->Free;
Ethan Yonker74db1572015-10-28 12:44:49 -0500746 gui_msg(Msg("available_space= * Available space: {1}MB")(free_space / 1024 / 1024));
Dees_Troy43d8b002012-09-17 16:00:01 -0400747 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500748 gui_err("unable_locate_storage=Unable to locate storage device.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400749 return false;
750 }
bigbiffbf1d6722015-02-14 16:25:59 -0500751
752 DataManager::GetValue("tw_disable_free_space", disable_free_space_check);
753 if (!disable_free_space_check) {
754 if (free_space - (32 * 1024 * 1024) < total_bytes) {
755 // We require an extra 32MB just in case
Ethan Yonker74db1572015-10-28 12:44:49 -0500756 gui_err("no_space=Not enough free space on storage.");
bigbiffbf1d6722015-02-14 16:25:59 -0500757 return false;
758 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400759 }
760 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500761 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400762
Ethan Yonker74db1572015-10-28 12:44:49 -0500763 gui_msg("backup_started=[BACKUP STARTED]");
764 gui_msg(Msg("backup_folder= * Backup Folder: {1}")(Full_Backup_Path));
Dees_Troyd4b22b02013-01-18 17:17:58 +0000765 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500766 gui_err("fail_backup_folder=Failed to make backup folder.");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000767 return false;
768 }
769
Dees_Troy2673cec2013-04-02 20:22:16 +0000770 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400771
Dees_Troya13d74f2013-03-24 08:54:55 -0500772 start_pos = 0;
773 end_pos = Backup_List.find(";", start_pos);
774 while (end_pos != string::npos && start_pos < Backup_List.size()) {
bigbiff7abc5fe2015-01-17 16:53:12 -0500775 if (stop_backup.get_value() != 0)
776 return -1;
Dees_Troya13d74f2013-03-24 08:54:55 -0500777 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
778 backup_part = Find_Partition_By_Path(backup_path);
779 if (backup_part != NULL) {
780 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
781 return false;
782 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500783 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 -0500784 }
785 start_pos = end_pos + 1;
786 end_pos = Backup_List.find(";", start_pos);
787 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400788
789 // Average BPS
790 if (img_time == 0)
791 img_time = 1;
792 if (file_time == 0)
793 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400794 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500795 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400796
Ethan Yonker74db1572015-10-28 12:44:49 -0500797 gui_msg(Msg("avg_backup_fs=Average backup rate for file systems: {1} MB/sec")(file_bps / (1024 * 1024)));
798 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 -0400799
800 time(&total_stop);
801 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500802 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500803 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400804
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500805 int prev_img_bps, use_compression;
806 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400807 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
808 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500809 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400810
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500811 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400812 if (use_compression)
813 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500814 else
Dees_Troy093b7642012-09-21 15:59:38 -0400815 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
816 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500817 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400818
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500819 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400820 if (use_compression)
821 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
822 else
823 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
824
Ethan Yonker74db1572015-10-28 12:44:49 -0500825 gui_msg(Msg("total_backed_size=[{1} MB TOTAL BACKED UP]")(actual_backup_size));
Dees_Troy43d8b002012-09-17 16:00:01 -0400826 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400827 UnMount_Main_Partitions();
Ethan Yonker56db1c42015-12-20 22:49:00 -0600828 gui_msg(Msg(msg::kHighlight, "backup_completed=[BACKUP COMPLETED IN {1} SECONDS]")(total_time)); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000829 string backup_log = Full_Backup_Path + "recovery.log";
830 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600831 tw_set_default_metadata(backup_log.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000832 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400833}
834
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500835bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400836 time_t Start, Stop;
Tom Hite5a926722014-09-15 01:31:03 +0000837 TWFunc::SetPerformanceMode(true);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400838 time(&Start);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500839 //DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Tom Hite5a926722014-09-15 01:31:03 +0000840 if (!Part->Restore(Restore_Name, total_restore_size, already_restored_size)) {
841 TWFunc::SetPerformanceMode(false);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400842 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000843 }
Dees_Troy8170a922012-09-18 15:40:25 -0400844 if (Part->Has_SubPartition) {
845 std::vector<TWPartition*>::iterator subpart;
846
847 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
848 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Tom Hite5a926722014-09-15 01:31:03 +0000849 if (!(*subpart)->Restore(Restore_Name, total_restore_size, already_restored_size)) {
850 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400851 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000852 }
Dees_Troy8170a922012-09-18 15:40:25 -0400853 }
854 }
855 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400856 time(&Stop);
Tom Hite5a926722014-09-15 01:31:03 +0000857 TWFunc::SetPerformanceMode(false);
Ethan Yonker74db1572015-10-28 12:44:49 -0500858 gui_msg(Msg("restort_part_done=[{1} done ({2} seconds)]")(Part->Backup_Display_Name)((int)difftime(Stop, Start)));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400859 return true;
860}
861
Dees_Troy51a0e822012-09-05 15:24:24 -0400862int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400863 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500864 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400865 time_t rStart, rStop;
866 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500867 string Restore_List, restore_path;
868 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500869 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400870
Ethan Yonker74db1572015-10-28 12:44:49 -0500871 gui_msg("restore_started=[RESTORE STARTED]");
872 gui_msg(Msg("restore_folder=Restore folder: '{1}'")(Restore_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -0400873
Dees_Troy4a2a1262012-09-18 09:33:47 -0400874 if (!Mount_Current_Storage(true))
875 return false;
876
877 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500878 if (check_md5 > 0) {
879 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
Ethan Yonker74db1572015-10-28 12:44:49 -0500880 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, gui_parse_text("{@verifying_md5}"));
881 gui_msg("verifying_md5=Verifying MD5");
Dees_Troya13d74f2013-03-24 08:54:55 -0500882 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500883 gui_msg("skip_md5=Skipping MD5 check based on user setting.");
Dees_Troya13d74f2013-03-24 08:54:55 -0500884 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500885 gui_msg("calc_restore=Calculating restore details...");
Dees_Troya13d74f2013-03-24 08:54:55 -0500886 DataManager::GetValue("tw_restore_selected", Restore_List);
887 if (!Restore_List.empty()) {
888 end_pos = Restore_List.find(";", start_pos);
889 while (end_pos != string::npos && start_pos < Restore_List.size()) {
890 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
891 restore_part = Find_Partition_By_Path(restore_path);
892 if (restore_part != NULL) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500893 if (restore_part->Mount_Read_Only) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500894 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 -0500895 return false;
896 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500897 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
898 return false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500899 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500900 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500901 if (restore_part->Has_SubPartition) {
902 std::vector<TWPartition*>::iterator subpart;
903
904 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
905 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400906 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500907 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500908 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500909 }
910 }
911 }
912 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500913 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -0500914 }
915 start_pos = end_pos + 1;
916 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400917 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400918 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400919
920 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500921 gui_err("no_part_restore=No partitions selected for restore.");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400922 return false;
923 }
924
Ethan Yonker74db1572015-10-28 12:44:49 -0500925 gui_msg(Msg("restore_part_count=Restoring {1} partitions...")(partition_count));
926 gui_msg(Msg("total_restore_size=Total restore size is {1}MB")(total_restore_size / 1048576));
Dees_Troy2673cec2013-04-02 20:22:16 +0000927 DataManager::SetProgress(0.0);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500928
Dees_Troya13d74f2013-03-24 08:54:55 -0500929 start_pos = 0;
930 if (!Restore_List.empty()) {
931 end_pos = Restore_List.find(";", start_pos);
932 while (end_pos != string::npos && start_pos < Restore_List.size()) {
933 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
934 restore_part = Find_Partition_By_Path(restore_path);
935 if (restore_part != NULL) {
936 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500937 if (!Restore_Partition(restore_part, Restore_Name, partition_count, &total_restore_size, &already_restored_size))
Dees_Troya13d74f2013-03-24 08:54:55 -0500938 return false;
939 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500940 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -0500941 }
942 start_pos = end_pos + 1;
943 end_pos = Restore_List.find(";", start_pos);
944 }
945 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500946 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, gui_parse_text("{@updating_system_details}"));
Dees_Troy43d8b002012-09-17 16:00:01 -0400947 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400948 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400949 time(&rStop);
Matt Mower3c366972015-12-25 19:28:31 -0600950 gui_msg(Msg(msg::kHighlight, "restore_completed=[RESTORE COMPLETED IN {1} SECONDS]")((int)difftime(rStop,rStart)));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500951 DataManager::SetValue("tw_file_progress", "");
Dees_Troy63c8df72012-09-10 14:02:05 -0400952 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400953}
954
955void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400956 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500957 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000958 bool get_date = true, check_encryption = true;
959
960 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400961
962 DIR* d;
963 d = opendir(Restore_Name.c_str());
964 if (d == NULL)
965 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500966 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Restore_Name)(strerror(errno)));
Dees_Troy63c8df72012-09-10 14:02:05 -0400967 return;
968 }
969
970 struct dirent* de;
971 while ((de = readdir(d)) != NULL)
972 {
973 // Strip off three components
974 char str[256];
975 char* label;
976 char* fstype = NULL;
977 char* extn = NULL;
978 char* ptr;
979
980 strcpy(str, de->d_name);
981 if (strlen(str) <= 2)
982 continue;
983
984 if (get_date) {
985 char file_path[255];
986 struct stat st;
987
988 strcpy(file_path, Restore_Name.c_str());
989 strcat(file_path, "/");
990 strcat(file_path, str);
991 stat(file_path, &st);
992 string backup_date = ctime((const time_t*)(&st.st_mtime));
993 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
994 get_date = false;
995 }
996
997 label = str;
998 ptr = label;
999 while (*ptr && *ptr != '.') ptr++;
1000 if (*ptr == '.')
1001 {
1002 *ptr = 0x00;
1003 ptr++;
1004 fstype = ptr;
1005 }
1006 while (*ptr && *ptr != '.') ptr++;
1007 if (*ptr == '.')
1008 {
1009 *ptr = 0x00;
1010 ptr++;
1011 extn = ptr;
1012 }
1013
Dees_Troy83bd4832013-05-04 12:39:56 +00001014 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -05001015 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +00001016 if (extnlength != 3 && extnlength != 6) continue;
1017 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
1018 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
1019
1020 if (check_encryption) {
1021 string filename = Restore_Name + "/";
1022 filename += de->d_name;
1023 if (TWFunc::Get_File_Type(filename) == 2) {
1024 LOGINFO("'%s' is encrypted\n", filename.c_str());
1025 DataManager::SetValue("tw_restore_encrypted", 1);
1026 }
1027 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001028 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -04001029
1030 TWPartition* Part = Find_Partition_By_Path(label);
1031 if (Part == NULL)
1032 {
Ethan Yonker74db1572015-10-28 12:44:49 -05001033 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 -04001034 continue;
1035 }
1036
1037 Part->Backup_FileName = de->d_name;
1038 if (strlen(extn) > 3) {
1039 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1040 }
1041
Dees_Troya13d74f2013-03-24 08:54:55 -05001042 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001043 }
1044 closedir(d);
1045
Dees_Troya13d74f2013-03-24 08:54:55 -05001046 // Set the final value
1047 DataManager::SetValue("tw_restore_list", Restore_List);
1048 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001049 return;
1050}
1051
1052int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001053 std::vector<TWPartition*>::iterator iter;
1054 int ret = false;
1055 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001056 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001057
1058 // Iterate through all partitions
1059 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001060 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001061 if (Path == "/and-sec")
1062 ret = (*iter)->Wipe_AndSec();
1063 else
1064 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001065 found = true;
1066 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1067 (*iter)->Wipe();
1068 }
1069 }
1070 if (found) {
1071 return ret;
1072 } else
Ethan Yonker74db1572015-10-28 12:44:49 -05001073 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 -04001074 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001075}
1076
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001077int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1078 std::vector<TWPartition*>::iterator iter;
1079 int ret = false;
1080 bool found = false;
1081 string Local_Path = TWFunc::Get_Root_Path(Path);
1082
1083 // Iterate through all partitions
1084 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1085 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1086 if (Path == "/and-sec")
1087 ret = (*iter)->Wipe_AndSec();
1088 else
1089 ret = (*iter)->Wipe(New_File_System);
1090 found = true;
1091 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1092 (*iter)->Wipe(New_File_System);
1093 }
1094 }
1095 if (found) {
1096 return ret;
1097 } else
Ethan Yonker74db1572015-10-28 12:44:49 -05001098 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 -05001099 return false;
1100}
1101
Dees_Troy51a0e822012-09-05 15:24:24 -04001102int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001103 std::vector<TWPartition*>::iterator iter;
1104 int ret = true;
1105
1106 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001107 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Ethan Yonker89583ef2015-08-26 09:01:59 -05001108#ifdef TW_OEM_BUILD
1109 if ((*iter)->Mount_Point == "/data") {
1110 if (!(*iter)->Wipe_Encryption())
1111 ret = false;
1112 } else {
1113#endif
1114 if (!(*iter)->Wipe())
1115 ret = false;
1116#ifdef TW_OEM_BUILD
1117 }
1118#endif
Dees_Troy094207a2012-09-26 12:00:39 -04001119 } else if ((*iter)->Has_Android_Secure) {
1120 if (!(*iter)->Wipe_AndSec())
1121 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001122 }
1123 }
1124 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001125}
1126
Dees_Troy38bd7602012-09-14 13:33:53 -04001127int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1128 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001129 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001130
1131 if (!Mount_By_Path("/data", true))
1132 return false;
1133
1134 if (!Mount_By_Path("/cache", true))
1135 return false;
1136
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001137 dir.push_back("/data/dalvik-cache");
1138 dir.push_back("/cache/dalvik-cache");
1139 dir.push_back("/cache/dc");
Ethan Yonker74db1572015-10-28 12:44:49 -05001140 gui_msg("wiping_dalvik=Wiping Dalvik Cache Directories...");
Dees_Troya13d74f2013-03-24 08:54:55 -05001141 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001142 if (stat(dir.at(i).c_str(), &st) == 0) {
1143 TWFunc::removeDir(dir.at(i), false);
Ethan Yonker74db1572015-10-28 12:44:49 -05001144 gui_msg(Msg("cleaned=Cleaned: {1}...")(dir.at(i)));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001145 }
1146 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001147 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001148 if (sdext && sdext->Is_Present && sdext->Mount(false))
1149 {
1150 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1151 {
1152 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Ethan Yonker74db1572015-10-28 12:44:49 -05001153 gui_msg(Msg("cleaned=Cleaned: {1}...")("/sd-ext/dalvik-cache"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001154 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001155 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001156 gui_msg("dalvik_done=-- Dalvik Cache Directories Wipe Complete!");
Dees_Troy38bd7602012-09-14 13:33:53 -04001157 return true;
1158}
1159
1160int TWPartitionManager::Wipe_Rotate_Data(void) {
1161 if (!Mount_By_Path("/data", true))
1162 return false;
1163
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001164 unlink("/data/misc/akmd*");
1165 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001166 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001167 return true;
1168}
1169
1170int TWPartitionManager::Wipe_Battery_Stats(void) {
1171 struct stat st;
1172
1173 if (!Mount_By_Path("/data", true))
1174 return false;
1175
1176 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001177 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001178 } else {
1179 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001180 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001181 }
1182 return true;
1183}
1184
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001185int TWPartitionManager::Wipe_Android_Secure(void) {
1186 std::vector<TWPartition*>::iterator iter;
1187 int ret = false;
1188 bool found = false;
1189
1190 // Iterate through all partitions
1191 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1192 if ((*iter)->Has_Android_Secure) {
1193 ret = (*iter)->Wipe_AndSec();
1194 found = true;
1195 }
1196 }
1197 if (found) {
1198 return ret;
1199 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001200 gui_err("no_andsec=No android secure partitions found.");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001201 }
1202 return false;
1203}
1204
Dees_Troy38bd7602012-09-14 13:33:53 -04001205int TWPartitionManager::Format_Data(void) {
1206 TWPartition* dat = Find_Partition_By_Path("/data");
1207
1208 if (dat != NULL) {
1209 if (!dat->UnMount(true))
1210 return false;
1211
1212 return dat->Wipe_Encryption();
1213 } else {
Matt Mower3c366972015-12-25 19:28:31 -06001214 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/data"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001215 return false;
1216 }
1217 return false;
1218}
1219
1220int TWPartitionManager::Wipe_Media_From_Data(void) {
1221 TWPartition* dat = Find_Partition_By_Path("/data");
1222
1223 if (dat != NULL) {
1224 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001225 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001226 return false;
1227 }
1228 if (!dat->Mount(true))
1229 return false;
1230
Ethan Yonker74db1572015-10-28 12:44:49 -05001231 gui_msg("wiping_datamedia=Wiping internal storage -- /data/media...");
Ethan Yonker726a0202014-12-16 20:01:38 -06001232 Remove_MTP_Storage(dat->MTP_Storage_ID);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001233 TWFunc::removeDir("/data/media", false);
xiaolu9416f4f2015-06-04 08:22:23 +08001234 dat->Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001235 Add_MTP_Storage(dat->MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001236 return true;
1237 } else {
Matt Mower3c366972015-12-25 19:28:31 -06001238 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/data"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001239 return false;
1240 }
1241 return false;
1242}
1243
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001244int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1245 std::vector<TWPartition*>::iterator iter;
1246 int ret = false;
1247 bool found = false;
1248 string Local_Path = TWFunc::Get_Root_Path(Path);
1249
1250 if (Local_Path == "/tmp" || Local_Path == "/")
1251 return true;
1252
1253 // Iterate through all partitions
1254 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1255 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1256 ret = (*iter)->Repair();
1257 found = true;
1258 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1259 (*iter)->Repair();
1260 }
1261 }
1262 if (found) {
1263 return ret;
1264 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001265 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 -05001266 } else {
1267 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1268 }
1269 return false;
1270}
1271
Ethan Yonkera2719152015-05-28 09:44:41 -05001272int TWPartitionManager::Resize_By_Path(string Path, bool Display_Error) {
1273 std::vector<TWPartition*>::iterator iter;
1274 int ret = false;
1275 bool found = false;
1276 string Local_Path = TWFunc::Get_Root_Path(Path);
1277
1278 if (Local_Path == "/tmp" || Local_Path == "/")
1279 return true;
1280
1281 // Iterate through all partitions
1282 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1283 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1284 ret = (*iter)->Resize();
1285 found = true;
1286 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1287 (*iter)->Resize();
1288 }
1289 }
1290 if (found) {
1291 return ret;
1292 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001293 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 -05001294 } else {
1295 LOGINFO("Resize: Unable to find partition for path '%s'\n", Local_Path.c_str());
1296 }
1297 return false;
1298}
1299
Dees_Troy51a0e822012-09-05 15:24:24 -04001300void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001301 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001302 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001303
Ethan Yonker74db1572015-10-28 12:44:49 -05001304 gui_msg("update_part_details=Updating partition details...");
Dees_Troy5bf43922012-09-07 16:07:55 -04001305 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001306 if ((*iter)->Can_Be_Mounted) {
1307 (*iter)->Update_Size(true);
1308 if ((*iter)->Mount_Point == "/system") {
1309 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1310 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1311 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1312 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1313 } else if ((*iter)->Mount_Point == "/cache") {
1314 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1315 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1316 } else if ((*iter)->Mount_Point == "/sd-ext") {
1317 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1318 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1319 if ((*iter)->Backup_Size == 0) {
1320 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1321 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1322 } else
1323 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001324 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001325 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001326 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001327 if ((*iter)->Backup_Size == 0) {
1328 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1329 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1330 } else
1331 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001332 } else if ((*iter)->Mount_Point == "/boot") {
1333 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1334 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1335 if ((*iter)->Backup_Size == 0) {
1336 DataManager::SetValue("tw_has_boot_partition", 0);
1337 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1338 } else
1339 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001340 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001341#ifdef SP1_NAME
1342 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1343 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1344 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1345 }
1346#endif
1347#ifdef SP2_NAME
1348 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1349 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1350 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1351 }
1352#endif
1353#ifdef SP3_NAME
1354 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1355 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1356 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1357 }
1358#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001359 } else {
1360 // Handle unmountable partitions in case we reset defaults
1361 if ((*iter)->Mount_Point == "/boot") {
1362 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1363 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1364 if ((*iter)->Backup_Size == 0) {
1365 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1366 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1367 } else
1368 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1369 } else if ((*iter)->Mount_Point == "/recovery") {
1370 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1371 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1372 if ((*iter)->Backup_Size == 0) {
1373 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1374 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1375 } else
1376 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001377 } else if ((*iter)->Mount_Point == "/data") {
1378 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001379 }
1380#ifdef SP1_NAME
1381 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1382 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1383 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1384 }
1385#endif
1386#ifdef SP2_NAME
1387 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1388 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1389 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1390 }
1391#endif
1392#ifdef SP3_NAME
1393 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1394 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1395 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1396 }
1397#endif
Dees_Troy51127312012-09-08 13:08:49 -04001398 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001399 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001400 gui_msg("update_part_details_done=...done");
Dees_Troy51127312012-09-08 13:08:49 -04001401 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1402 string current_storage_path = DataManager::GetCurrentStoragePath();
1403 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001404 if (FreeStorage != NULL) {
1405 // Attempt to mount storage
1406 if (!FreeStorage->Mount(false)) {
Matt Mower2d50cad2015-12-03 22:26:55 -06001407 gui_msg(Msg(msg::kError, "unable_to_mount_storage=Unable to mount storage"));
1408 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
Dees_Troy8170a922012-09-18 15:40:25 -04001409 } else {
1410 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1411 }
1412 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001413 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001414 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001415 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001416 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001417 return;
1418}
1419
1420int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001421#ifdef TW_INCLUDE_CRYPTO
1422 int ret_val, password_len;
Ethan Yonkera1de1492015-11-04 11:58:27 -06001423 char crypto_state[PROPERTY_VALUE_MAX], crypto_blkdev[PROPERTY_VALUE_MAX], cPassword[255];
Dees_Troy5bf43922012-09-07 16:07:55 -04001424 size_t result;
Ethan Yonker253368a2014-11-25 15:00:52 -06001425 std::vector<TWPartition*>::iterator iter;
Dees_Troy5bf43922012-09-07 16:07:55 -04001426
Ethan Yonker253368a2014-11-25 15:00:52 -06001427 // Mount any partitions that need to be mounted for decrypt
1428 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1429 if ((*iter)->Mount_To_Decrypt) {
1430 (*iter)->Mount(true);
1431 }
a39552696ff55ce2013-01-08 16:14:56 +00001432 }
a39552696ff55ce2013-01-08 16:14:56 +00001433
Ethan Yonkera1de1492015-11-04 11:58:27 -06001434 property_get("ro.crypto.state", crypto_state, "error");
1435 if (strcmp(crypto_state, "error") == 0) {
1436 property_set("ro.crypto.state", "encrypted");
1437 // Sleep for a bit so that services can start if needed
1438 sleep(1);
1439 }
1440
Dees_Troy5bf43922012-09-07 16:07:55 -04001441 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001442 int pwret = cryptfs_check_passwd(cPassword);
1443
Ethan Yonker253368a2014-11-25 15:00:52 -06001444 // Unmount any partitions that were needed for decrypt
1445 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1446 if ((*iter)->Mount_To_Decrypt) {
1447 (*iter)->UnMount(false);
1448 }
1449 }
1450
a39552696ff55ce2013-01-08 16:14:56 +00001451 if (pwret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001452 gui_err("fail_decrypt=Failed to decrypt data.");
Dees_Troy5bf43922012-09-07 16:07:55 -04001453 return -1;
1454 }
a39552696ff55ce2013-01-08 16:14:56 +00001455
Dees_Troy5bf43922012-09-07 16:07:55 -04001456 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1457 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001458 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001459 } else {
1460 TWPartition* dat = Find_Partition_By_Path("/data");
1461 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001462 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001463 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1464 dat->Is_Decrypted = true;
1465 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001466 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001467 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 -06001468 gui_msg(Msg("decrypt_success_dev=Data successfully decrypted, new block device: '{1}'")(crypto_blkdev));
a39552696ff55ce2013-01-08 16:14:56 +00001469
Dees_Troy5bf43922012-09-07 16:07:55 -04001470 // Sleep for a bit so that the device will be ready
1471 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001472 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001473 dat->Storage_Path = "/data/media/0";
1474 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001475 DataManager::SetValue("tw_storage_path", "/data/media/0");
Ethan Yonkerec0fa4a2014-11-20 09:51:03 -06001476 DataManager::SetValue("tw_settings_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001477 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001478 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001479 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001480 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001481 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001482 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001483 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001484 }
1485 return 0;
1486#else
Ethan Yonker74db1572015-10-28 12:44:49 -05001487 gui_err("no_crypto_support=No crypto support was compiled into this build.");
Dees_Troy5bf43922012-09-07 16:07:55 -04001488 return -1;
1489#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001490 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001491}
1492
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001493int TWPartitionManager::Fix_Contexts(void) {
thata3d31fb2014-12-21 22:27:40 +01001494#ifdef HAVE_SELINUX
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001495 std::vector<TWPartition*>::iterator iter;
1496 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1497 if ((*iter)->Has_Data_Media) {
1498 if ((*iter)->Mount(true)) {
1499 if (fixContexts::fixDataMediaContexts((*iter)->Mount_Point) != 0)
1500 return -1;
1501 }
1502 }
1503 }
Dees_Troy1a650e62012-10-19 20:54:32 -04001504 UnMount_Main_Partitions();
Ethan Yonker74db1572015-10-28 12:44:49 -05001505 gui_msg("done=Done.");
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001506 return 0;
1507#else
1508 LOGERR("Cannot fix contexts, no selinux support present.\n");
1509 return -1;
1510#endif
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001511}
1512
Ethan Yonker66a19492015-12-10 10:19:45 -06001513TWPartition* TWPartitionManager::Find_Next_Storage(string Path, bool Exclude_Data_Media) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001514 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1515
1516 if (!Path.empty()) {
1517 string Search_Path = TWFunc::Get_Root_Path(Path);
1518 for (; iter != Partitions.end(); iter++) {
Ethan Yonker66a19492015-12-10 10:19:45 -06001519 if (Exclude_Data_Media && (*iter)->Has_Data_Media) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001520 iter++;
1521 break;
1522 }
1523 }
1524 }
1525
1526 for (; iter != Partitions.end(); iter++) {
Ethan Yonker66a19492015-12-10 10:19:45 -06001527 if (Exclude_Data_Media && (*iter)->Has_Data_Media) {
1528 // do nothing, do not return this type of partition
1529 } else if ((*iter)->Is_Storage && (*iter)->Is_Present) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001530 return (*iter);
1531 }
1532 }
1533
1534 return NULL;
1535}
1536
Dees_Troyd21618c2012-10-14 18:48:49 -04001537int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001538 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1539
1540 if (Part == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001541 LOGINFO("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
1542 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 -04001543 return false;
1544 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001545 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1546 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001547 return false;
1548
Dees_Troy2673cec2013-04-02 20:22:16 +00001549 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1550 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001551 return false;
1552 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001553 return true;
1554}
1555
Dees_Troy8170a922012-09-18 15:40:25 -04001556int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001557 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001558 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001559 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001560
Dees_Troy8170a922012-09-18 15:40:25 -04001561 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001562 string Lun_File_str = CUSTOM_LUN_FILE;
1563 size_t found = Lun_File_str.find("%");
1564 if (found != string::npos) {
1565 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1566 if (TWFunc::Path_Exists(lun_file))
1567 has_multiple_lun = true;
1568 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001569 mtp_was_enabled = TWFunc::Toggle_MTP(false); // Must disable MTP for USB Storage
Ethan Yonker47360be2014-04-01 10:34:34 -05001570 if (!has_multiple_lun) {
1571 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1572 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1573 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
Ethan Yonker66a19492015-12-10 10:19:45 -06001574 TWPartition* Mount = Find_Next_Storage("", true);
Ethan Yonker47360be2014-04-01 10:34:34 -05001575 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001576 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1577 goto error_handle;
1578 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001579 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001580 gui_err("unable_locate_storage=Unable to locate storage device.");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001581 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001582 }
1583 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001584 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001585 }
Dees_Troy8170a922012-09-18 15:40:25 -04001586 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001587 LOGINFO("Device has multiple lun files\n");
1588 TWPartition* Mount1;
1589 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001590 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker66a19492015-12-10 10:19:45 -06001591 Mount1 = Find_Next_Storage("", true);
Ethan Yonker47360be2014-04-01 10:34:34 -05001592 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001593 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1594 goto error_handle;
1595 }
Matt Moweree717062014-04-26 01:46:46 -05001596 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker66a19492015-12-10 10:19:45 -06001597 Mount2 = Find_Next_Storage(Mount1->Mount_Point, true);
Ethan Yonker47360be2014-04-01 10:34:34 -05001598 if (Mount2) {
Matt Moweree717062014-04-26 01:46:46 -05001599 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001600 }
1601 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001602 gui_err("unable_locate_storage=Unable to locate storage device.");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001603 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001604 }
Dees_Troy8170a922012-09-18 15:40:25 -04001605 }
Matt Mowerb6ef4782014-11-06 02:24:36 -06001606 property_set("sys.storage.ums_enabled", "1");
HandyMenny37d42992014-10-26 22:15:59 +01001607 property_set("sys.usb.config", "mass_storage,adb");
Dees_Troy8170a922012-09-18 15:40:25 -04001608 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001609error_handle:
1610 if (mtp_was_enabled)
1611 if (!Enable_MTP())
1612 Disable_MTP();
1613 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001614}
1615
1616int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001617 int index, ret;
1618 char lun_file[255], ch[2] = {0, 0};
1619 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001620
1621 for (index=0; index<2; index++) {
1622 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001623 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001624 if (ret < 0) {
1625 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001626 }
Dees_Troy8170a922012-09-18 15:40:25 -04001627 }
Dees_Troye58d5262012-09-21 12:27:57 -04001628 Mount_All_Storage();
1629 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001630 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001631 property_set("sys.storage.ums_enabled", "0");
HandyMenny37d42992014-10-26 22:15:59 +01001632 property_set("sys.usb.config", "adb");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001633 if (mtp_was_enabled)
1634 if (!Enable_MTP())
1635 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001636 if (ret < 0 && index == 0) {
1637 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1638 return false;
1639 } else {
1640 return true;
1641 }
Dees_Troy8170a922012-09-18 15:40:25 -04001642 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001643}
1644
1645void TWPartitionManager::Mount_All_Storage(void) {
1646 std::vector<TWPartition*>::iterator iter;
1647
1648 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1649 if ((*iter)->Is_Storage)
1650 (*iter)->Mount(false);
1651 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001652}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001653
Dees_Troyd0384ef2012-10-12 12:15:42 -04001654void TWPartitionManager::UnMount_Main_Partitions(void) {
1655 // Unmounts system and data if data is not data/media
1656 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001657 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001658
1659 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1660
1661 UnMount_By_Path("/system", true);
Ethan Yonker6277c792014-09-15 14:54:30 -05001662 if (!datamedia)
1663 UnMount_By_Path("/data", true);
1664
Dees_Troyd0384ef2012-10-12 12:15:42 -04001665 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1666 Boot_Partition->UnMount(true);
1667}
1668
Dees_Troy9350b8d2012-09-27 12:38:38 -04001669int TWPartitionManager::Partition_SDCard(void) {
1670 char mkdir_path[255], temp[255], line[512];
Ethan Yonker483e9f42016-01-11 22:21:18 -06001671 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 -04001672 int ext, swap, total_size = 0, fat_size;
Dees_Troy9350b8d2012-09-27 12:38:38 -04001673
Ethan Yonker74db1572015-10-28 12:44:49 -05001674 gui_msg("start_partition_sd=Partitioning SD Card...");
Ethan Yonker483e9f42016-01-11 22:21:18 -06001675
1676 // Locate and validate device to partition
1677 TWPartition* SDCard = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
1678
Ethan Yonker66a19492015-12-10 10:19:45 -06001679 if (SDCard->Is_Adopted_Storage)
1680 SDCard->Revert_Adopted();
1681
Ethan Yonker1eff6cd2014-09-15 13:30:42 -05001682 if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001683 gui_err("partition_sd_locate=Unable to locate device to partition.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001684 return false;
1685 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001686
1687 // Unmount everything
Dees_Troy9350b8d2012-09-27 12:38:38 -04001688 if (!SDCard->UnMount(true))
1689 return false;
1690 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1691 if (SDext != NULL) {
1692 if (!SDext->UnMount(true))
1693 return false;
1694 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001695 char* swappath = getenv("SWAPPATH");
1696 if (swappath != NULL) {
1697 LOGINFO("Unmounting swap at '%s'\n", swappath);
1698 umount(swappath);
1699 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001700
Ethan Yonker483e9f42016-01-11 22:21:18 -06001701 // Determine block device
1702 if (SDCard->Alternate_Block_Device.empty()) {
1703 SDCard->Find_Actual_Block_Device();
1704 Device = SDCard->Actual_Block_Device;
1705 // Just use the root block device
1706 Device.resize(strlen("/dev/block/mmcblkX"));
1707 } else {
1708 Device = SDCard->Alternate_Block_Device;
1709 }
Dees_Troy9350b8d2012-09-27 12:38:38 -04001710
1711 // Find the size of the block device:
Ethan Yonker483e9f42016-01-11 22:21:18 -06001712 total_size = (int)(TWFunc::IOCTL_Get_Block_Size(Device.c_str()) / (1048576));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001713
1714 DataManager::GetValue("tw_sdext_size", ext);
1715 DataManager::GetValue("tw_swap_size", swap);
1716 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1717 fat_size = total_size - ext - swap;
Ethan Yonker483e9f42016-01-11 22:21:18 -06001718 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);
1719
1720 // Determine partition sizes
1721 if (swap == 0 && ext == 0) {
1722 fat_str = "-0";
1723 } else {
1724 memset(temp, 0, sizeof(temp));
1725 sprintf(temp, "%i", fat_size);
1726 fat_str = temp;
1727 fat_str += "MB";
1728 }
1729 if (swap == 0) {
1730 ext_str = "-0";
1731 } else {
1732 memset(temp, 0, sizeof(temp));
1733 sprintf(temp, "%i", ext);
1734 ext_str = "+";
1735 ext_str += temp;
1736 ext_str += "MB";
1737 }
1738
Dees_Troy9350b8d2012-09-27 12:38:38 -04001739 if (ext + swap > total_size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001740 gui_err("ext_swap_size=EXT + Swap size is larger than sdcard size.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001741 return false;
1742 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001743
Ethan Yonker74db1572015-10-28 12:44:49 -05001744 gui_msg("remove_part_table=Removing partition table...");
Ethan Yonker483e9f42016-01-11 22:21:18 -06001745 Command = "sgdisk --zap-all " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001746 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001747 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001748 gui_err("unable_rm_part=Unable to remove partition table.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001749 Update_System_Details();
1750 return false;
1751 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001752 gui_msg(Msg("create_part=Creating {1} partition...")("FAT32"));
Ethan Yonker483e9f42016-01-11 22:21:18 -06001753 Command = "sgdisk --new=0:0:" + fat_str + " --change-name=0:\"Microsoft basic data\" " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001754 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001755 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001756 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("FAT32"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001757 return false;
1758 }
1759 if (ext > 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001760 gui_msg(Msg("create_part=Creating {1} partition...")("EXT"));
Ethan Yonker483e9f42016-01-11 22:21:18 -06001761 Command = "sgdisk --new=0:0:" + ext_str + " --change-name=0:\"Linux filesystem\" " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001762 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001763 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001764 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("EXT"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001765 Update_System_Details();
1766 return false;
1767 }
1768 }
1769 if (swap > 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001770 gui_msg(Msg("create_part=Creating {1} partition...")("swap"));
Ethan Yonker483e9f42016-01-11 22:21:18 -06001771 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 +00001772 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001773 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001774 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("swap"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001775 Update_System_Details();
1776 return false;
1777 }
1778 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001779
1780 // Convert GPT to MBR
1781 Command = "sgdisk --gpttombr " + Device;
1782 if (TWFunc::Exec_Cmd(Command) != 0)
1783 LOGINFO("Failed to covert partition GPT to MBR\n");
1784
1785 // Tell the kernel to rescan the partition table
1786 int fd = open(Device.c_str(), O_RDONLY);
1787 ioctl(fd, BLKRRPART, 0);
1788 close(fd);
1789
1790 string format_device = Device;
1791 if (Device.substr(0, 17) == "/dev/block/mmcblk")
1792 format_device += "p";
1793
1794 // Format new partitions to proper file system
1795 if (fat_size > 0) {
1796 Command = "mkfs.fat " + format_device + "1";
1797 TWFunc::Exec_Cmd(Command);
1798 }
Dees_Troy9350b8d2012-09-27 12:38:38 -04001799 if (ext > 0) {
1800 if (SDext == NULL) {
Ethan Yonker483e9f42016-01-11 22:21:18 -06001801 Command = "mke2fs -t " + ext_format + " -m 0 " + format_device + "2";
1802 gui_msg(Msg("format_sdext_as=Formatting sd-ext as {1}...")(ext_format));
1803 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
1804 TWFunc::Exec_Cmd(Command);
1805 } else {
1806 SDext->Wipe(ext_format);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001807 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001808 }
1809 if (swap > 0) {
1810 Command = "mkswap " + format_device;
1811 if (ext > 0)
1812 Command += "3";
1813 else
1814 Command += "2";
Vojtech Bocek05534202013-09-11 08:11:56 +02001815 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001816 }
1817
Ethan Yonker483e9f42016-01-11 22:21:18 -06001818 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1819 if (SDCard->Mount(true)) {
1820 string TWRP_Folder = SDCard->Mount_Point + "/TWRP";
1821 mkdir(TWRP_Folder.c_str(), 0777);
1822 DataManager::Flush();
1823 }
1824
Dees_Troy9350b8d2012-09-27 12:38:38 -04001825 Update_System_Details();
Ethan Yonker74db1572015-10-28 12:44:49 -05001826 gui_msg("part_complete=Partitioning complete.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001827 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001828}
Dees_Troya13d74f2013-03-24 08:54:55 -05001829
1830void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1831 std::vector<TWPartition*>::iterator iter;
1832 if (ListType == "mount") {
1833 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1834 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1835 struct PartitionList part;
1836 part.Display_Name = (*iter)->Display_Name;
1837 part.Mount_Point = (*iter)->Mount_Point;
1838 part.selected = (*iter)->Is_Mounted();
1839 Partition_List->push_back(part);
1840 }
1841 }
1842 } else if (ListType == "storage") {
1843 char free_space[255];
1844 string Current_Storage = DataManager::GetCurrentStoragePath();
1845 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1846 if ((*iter)->Is_Storage) {
1847 struct PartitionList part;
1848 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1849 part.Display_Name = (*iter)->Storage_Name + " (";
1850 part.Display_Name += free_space;
1851 part.Display_Name += "MB)";
1852 part.Mount_Point = (*iter)->Storage_Path;
1853 if ((*iter)->Storage_Path == Current_Storage)
1854 part.selected = 1;
1855 else
1856 part.selected = 0;
1857 Partition_List->push_back(part);
1858 }
1859 }
1860 } else if (ListType == "backup") {
1861 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001862 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001863 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001864 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001865 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001866 Backup_Size = (*iter)->Backup_Size;
1867 if ((*iter)->Has_SubPartition) {
1868 std::vector<TWPartition*>::iterator subpart;
1869
1870 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1871 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1872 Backup_Size += (*subpart)->Backup_Size;
1873 }
1874 }
1875 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001876 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1877 part.Display_Name += backup_size;
1878 part.Display_Name += "MB)";
1879 part.Mount_Point = (*iter)->Backup_Path;
1880 part.selected = 0;
1881 Partition_List->push_back(part);
1882 }
1883 }
1884 } else if (ListType == "restore") {
1885 string Restore_List, restore_path;
1886 TWPartition* restore_part = NULL;
1887
1888 DataManager::GetValue("tw_restore_list", Restore_List);
1889 if (!Restore_List.empty()) {
1890 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1891 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1892 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05001893 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00001894 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001895 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05001896 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05001897 } else {
1898 struct PartitionList part;
1899 part.Display_Name = restore_part->Backup_Display_Name;
1900 part.Mount_Point = restore_part->Backup_Path;
1901 part.selected = 1;
1902 Partition_List->push_back(part);
1903 }
1904 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001905 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -05001906 }
1907 start_pos = end_pos + 1;
1908 end_pos = Restore_List.find(";", start_pos);
1909 }
1910 }
1911 } else if (ListType == "wipe") {
1912 struct PartitionList dalvik;
Ethan Yonker74db1572015-10-28 12:44:49 -05001913 dalvik.Display_Name = gui_parse_text("{@dalvik}");
Dees_Troya13d74f2013-03-24 08:54:55 -05001914 dalvik.Mount_Point = "DALVIK";
1915 dalvik.selected = 0;
1916 Partition_List->push_back(dalvik);
1917 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1918 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1919 struct PartitionList part;
1920 part.Display_Name = (*iter)->Display_Name;
1921 part.Mount_Point = (*iter)->Mount_Point;
1922 part.selected = 0;
1923 Partition_List->push_back(part);
1924 }
1925 if ((*iter)->Has_Android_Secure) {
1926 struct PartitionList part;
1927 part.Display_Name = (*iter)->Backup_Display_Name;
1928 part.Mount_Point = (*iter)->Backup_Path;
1929 part.selected = 0;
1930 Partition_List->push_back(part);
1931 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001932 if ((*iter)->Has_Data_Media) {
1933 struct PartitionList datamedia;
1934 datamedia.Display_Name = (*iter)->Storage_Name;
1935 datamedia.Mount_Point = "INTERNAL";
1936 datamedia.selected = 0;
1937 Partition_List->push_back(datamedia);
1938 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001939 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06001940 } else if (ListType == "flashimg") {
1941 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1942 if ((*iter)->Can_Flash_Img && (*iter)->Is_Present) {
1943 struct PartitionList part;
1944 part.Display_Name = (*iter)->Backup_Display_Name;
1945 part.Mount_Point = (*iter)->Backup_Path;
1946 part.selected = 0;
1947 Partition_List->push_back(part);
1948 }
1949 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001950 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001951 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001952 }
1953}
1954
1955int TWPartitionManager::Fstab_Processed(void) {
1956 return Partitions.size();
1957}
Dees_Troyd93bda52013-07-03 19:55:19 +00001958
1959void TWPartitionManager::Output_Storage_Fstab(void) {
1960 std::vector<TWPartition*>::iterator iter;
1961 char storage_partition[255];
1962 string Temp;
1963 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
1964
1965 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001966 gui_msg(Msg(msg::kError, "unable_to_open=Unable to open '{1}'.")("/cache/recovery/storage.fstab"));
Dees_Troyd93bda52013-07-03 19:55:19 +00001967 return;
1968 }
1969
1970 // Iterate through all partitions
1971 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1972 if ((*iter)->Is_Storage) {
1973 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
1974 strcpy(storage_partition, Temp.c_str());
1975 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
1976 }
1977 }
1978 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001979}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02001980
1981TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
1982{
1983 TWPartition *res = NULL;
1984 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
1985 if(!(*iter)->Is_Storage)
1986 continue;
1987
1988 if((*iter)->Is_Settings_Storage)
1989 return *iter;
1990
1991 if(!res)
1992 res = *iter;
1993 }
1994 return res;
1995}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001996
1997bool TWPartitionManager::Enable_MTP(void) {
1998#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001999 if (mtppid) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002000 gui_err("mtp_already_enabled=MTP already enabled");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002001 return true;
2002 }
2003 //Launch MTP Responder
2004 LOGINFO("Starting MTP\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06002005
2006 int mtppipe[2];
2007
2008 if (pipe(mtppipe) < 0) {
2009 LOGERR("Error creating MTP pipe\n");
2010 return false;
2011 }
2012
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002013 char old_value[PROPERTY_VALUE_MAX];
2014 property_get("sys.usb.config", old_value, "error");
2015 if (strcmp(old_value, "error") != 0 && strcmp(old_value, "mtp,adb") != 0) {
2016 char vendor[PROPERTY_VALUE_MAX];
2017 char product[PROPERTY_VALUE_MAX];
2018 property_set("sys.usb.config", "none");
2019 property_get("usb.vendor", vendor, "18D1");
2020 property_get("usb.product.mtpadb", product, "4EE2");
2021 string vendorstr = vendor;
2022 string productstr = product;
2023 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2024 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
2025 property_set("sys.usb.config", "mtp,adb");
2026 }
Ethan Yonker6d154c42014-09-03 14:19:43 -05002027 /* To enable MTP debug, use the twrp command line feature to
2028 * twrp set tw_mtp_debug 1
2029 */
2030 twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug"));
Ethan Yonker1b039202015-01-30 10:08:48 -06002031 mtppid = mtp->forkserver(mtppipe);
2032 if (mtppid) {
2033 close(mtppipe[0]); // Host closes read side
2034 mtp_write_fd = mtppipe[1];
2035 DataManager::SetValue("tw_mtp_enabled", 1);
2036 Add_All_MTP_Storage();
2037 return true;
Ethan Yonker726a0202014-12-16 20:01:38 -06002038 } else {
2039 close(mtppipe[0]);
2040 close(mtppipe[1]);
Ethan Yonker74db1572015-10-28 12:44:49 -05002041 gui_err("mtp_fail=Failed to enable MTP");
Ethan Yonker1b039202015-01-30 10:08:48 -06002042 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002043 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002044#else
Ethan Yonker74db1572015-10-28 12:44:49 -05002045 gui_err("no_mtp=MTP support not included");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002046#endif
2047 DataManager::SetValue("tw_mtp_enabled", 0);
2048 return false;
2049}
2050
Ethan Yonker1b039202015-01-30 10:08:48 -06002051void TWPartitionManager::Add_All_MTP_Storage(void) {
2052#ifdef TW_HAS_MTP
2053 std::vector<TWPartition*>::iterator iter;
2054
2055 if (!mtppid)
2056 return; // MTP is not enabled
2057
2058 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2059 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false))
2060 Add_Remove_MTP_Storage((*iter), MTP_MESSAGE_ADD_STORAGE);
2061 }
2062#else
2063 return;
2064#endif
2065}
2066
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002067bool TWPartitionManager::Disable_MTP(void) {
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002068 char old_value[PROPERTY_VALUE_MAX];
2069 property_get("sys.usb.config", old_value, "error");
2070 if (strcmp(old_value, "adb") != 0) {
2071 char vendor[PROPERTY_VALUE_MAX];
2072 char product[PROPERTY_VALUE_MAX];
2073 property_set("sys.usb.config", "none");
2074 property_get("usb.vendor", vendor, "18D1");
2075 property_get("usb.product.adb", product, "D002");
2076 string vendorstr = vendor;
2077 string productstr = product;
2078 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2079 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
2080 usleep(2000);
2081 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002082#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002083 if (mtppid) {
Ethan Yonker8613dc02014-09-11 09:28:20 -05002084 LOGINFO("Disabling MTP\n");
2085 int status;
2086 kill(mtppid, SIGKILL);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002087 mtppid = 0;
Ethan Yonker8613dc02014-09-11 09:28:20 -05002088 // We don't care about the exit value, but this prevents a zombie process
2089 waitpid(mtppid, &status, 0);
Ethan Yonker726a0202014-12-16 20:01:38 -06002090 close(mtp_write_fd);
2091 mtp_write_fd = -1;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002092 }
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002093#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002094 property_set("sys.usb.config", "adb");
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002095#ifdef TW_HAS_MTP
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002096 DataManager::SetValue("tw_mtp_enabled", 0);
2097 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002098#endif
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002099 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002100}
Ethan Yonker726a0202014-12-16 20:01:38 -06002101
2102TWPartition* TWPartitionManager::Find_Partition_By_MTP_Storage_ID(unsigned int Storage_ID) {
2103 std::vector<TWPartition*>::iterator iter;
2104
2105 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2106 if ((*iter)->MTP_Storage_ID == Storage_ID)
2107 return (*iter);
2108 }
2109 return NULL;
2110}
2111
2112bool TWPartitionManager::Add_Remove_MTP_Storage(TWPartition* Part, int message_type) {
2113#ifdef TW_HAS_MTP
2114 struct mtpmsg mtp_message;
2115
2116 if (!mtppid)
2117 return false; // MTP is disabled
2118
2119 if (mtp_write_fd < 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002120 LOGINFO("MTP: mtp_write_fd is not set\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06002121 return false;
2122 }
2123
2124 if (Part) {
Ethan Yonker4bfabab2014-12-29 09:15:37 -06002125 if (Part->MTP_Storage_ID == 0)
2126 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002127 if (message_type == MTP_MESSAGE_REMOVE_STORAGE) {
2128 mtp_message.message_type = MTP_MESSAGE_REMOVE_STORAGE; // Remove
2129 LOGINFO("sending message to remove %i\n", Part->MTP_Storage_ID);
2130 mtp_message.storage_id = Part->MTP_Storage_ID;
2131 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002132 LOGINFO("error sending message to remove storage %i\n", Part->MTP_Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002133 return false;
2134 } else {
2135 LOGINFO("Message sent, remove storage ID: %i\n", Part->MTP_Storage_ID);
2136 return true;
2137 }
2138 } else if (message_type == MTP_MESSAGE_ADD_STORAGE && Part->Is_Mounted()) {
2139 mtp_message.message_type = MTP_MESSAGE_ADD_STORAGE; // Add
2140 mtp_message.storage_id = Part->MTP_Storage_ID;
2141 mtp_message.path = Part->Storage_Path.c_str();
2142 mtp_message.display = Part->Storage_Name.c_str();
2143 mtp_message.maxFileSize = Part->Get_Max_FileSize();
Ethan Yonker1b039202015-01-30 10:08:48 -06002144 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 -06002145 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002146 LOGINFO("error sending message to add storage %i\n", Part->MTP_Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002147 return false;
2148 } else {
2149 LOGINFO("Message sent, add storage ID: %i\n", Part->MTP_Storage_ID);
2150 return true;
2151 }
2152 } else {
2153 LOGERR("Unknown MTP message type: %i\n", message_type);
2154 }
2155 } else {
2156 // This hopefully never happens as the error handling should
2157 // occur in the calling function.
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002158 LOGINFO("TWPartitionManager::Add_Remove_MTP_Storage NULL partition given\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06002159 }
2160 return true;
2161#else
Ethan Yonker74db1572015-10-28 12:44:49 -05002162 gui_err("no_mtp=MTP support not included");
Ethan Yonker726a0202014-12-16 20:01:38 -06002163 DataManager::SetValue("tw_mtp_enabled", 0);
2164 return false;
2165#endif
2166}
2167
2168bool TWPartitionManager::Add_MTP_Storage(string Mount_Point) {
2169#ifdef TW_HAS_MTP
2170 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2171 if (Part) {
2172 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2173 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002174 LOGINFO("TWFunc::Add_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -06002175 }
2176#endif
2177 return false;
2178}
2179
2180bool TWPartitionManager::Add_MTP_Storage(unsigned int Storage_ID) {
2181#ifdef TW_HAS_MTP
2182 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2183 if (Part) {
2184 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2185 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002186 LOGINFO("TWFunc::Add_MTP_Storage unable to locate partition for %i\n", Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002187 }
2188#endif
2189 return false;
2190}
2191
2192bool TWPartitionManager::Remove_MTP_Storage(string Mount_Point) {
2193#ifdef TW_HAS_MTP
2194 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2195 if (Part) {
2196 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2197 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002198 LOGINFO("TWFunc::Remove_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -06002199 }
2200#endif
2201 return false;
2202}
2203
2204bool TWPartitionManager::Remove_MTP_Storage(unsigned int Storage_ID) {
2205#ifdef TW_HAS_MTP
2206 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2207 if (Part) {
2208 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2209 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002210 LOGINFO("TWFunc::Remove_MTP_Storage unable to locate partition for %i\n", Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002211 }
2212#endif
2213 return false;
2214}
Ethan Yonker96af84a2015-01-05 14:58:36 -06002215
2216bool TWPartitionManager::Flash_Image(string Filename) {
2217 int check, partition_count = 0;
2218 TWPartition* flash_part = NULL;
2219 string Flash_List, flash_path;
2220 size_t start_pos = 0, end_pos = 0;
2221
Ethan Yonker74db1572015-10-28 12:44:49 -05002222 gui_msg("image_flash_start=[IMAGE FLASH STARTED]");
2223 gui_msg(Msg("img_to_flash=Image to flash: '{1}'")(Filename));
Ethan Yonker96af84a2015-01-05 14:58:36 -06002224
Ethan Yonkerb78fbdf2016-01-15 16:46:35 -06002225 if (!TWFunc::Path_Exists(Filename)) {
2226 if (!Mount_By_Path(Filename, true)) {
2227 return false;
2228 }
2229 if (!TWFunc::Path_Exists(Filename)) {
2230 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(Filename));
2231 return false;
2232 }
2233 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002234
Ethan Yonker74db1572015-10-28 12:44:49 -05002235 gui_msg("calc_restore=Calculating restore details...");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002236 DataManager::GetValue("tw_flash_partition", Flash_List);
2237 if (!Flash_List.empty()) {
2238 end_pos = Flash_List.find(";", start_pos);
2239 while (end_pos != string::npos && start_pos < Flash_List.size()) {
2240 flash_path = Flash_List.substr(start_pos, end_pos - start_pos);
2241 flash_part = Find_Partition_By_Path(flash_path);
2242 if (flash_part != NULL) {
2243 partition_count++;
2244 if (partition_count > 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002245 gui_err("too_many_flash=Too many partitions selected for flashing.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002246 return false;
2247 }
2248 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002249 gui_msg(Msg(msg::kError, "flash_unable_locate=Unable to locate '{1}' partition for flashing.")(flash_path));
Ethan Yonker96af84a2015-01-05 14:58:36 -06002250 return false;
2251 }
2252 start_pos = end_pos + 1;
2253 end_pos = Flash_List.find(";", start_pos);
2254 }
2255 }
2256
2257 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002258 gui_err("no_part_flash=No partitions selected for flashing.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002259 return false;
2260 }
2261
2262 DataManager::SetProgress(0.0);
2263 if (flash_part) {
2264 if (!flash_part->Flash_Image(Filename))
2265 return false;
2266 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002267 gui_err("invalid_flash=Invalid flash partition specified.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002268 return false;
2269 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002270 gui_highlight("flash_done=IMAGE FLASH COMPLETED]");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002271 return true;
2272}
Ethan Yonker74db1572015-10-28 12:44:49 -05002273
2274void TWPartitionManager::Translate_Partition(const char* path, const char* resource_name, const char* default_value) {
2275 TWPartition* part = PartitionManager.Find_Partition_By_Path(path);
2276 if (part) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002277 if (part->Is_Adopted_Storage) {
2278 part->Display_Name = part->Display_Name + " - " + gui_lookup("data", "Data");
2279 part->Backup_Display_Name = part->Display_Name;
2280 part->Storage_Name = part->Storage_Name + " - " + gui_lookup("adopted_storage", "Adopted Storage");
2281 } else {
2282 part->Display_Name = gui_lookup(resource_name, default_value);
2283 part->Backup_Display_Name = part->Display_Name;
2284 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002285 }
2286}
2287
2288void TWPartitionManager::Translate_Partition(const char* path, const char* resource_name, const char* default_value, const char* storage_resource_name, const char* storage_default_value) {
2289 TWPartition* part = PartitionManager.Find_Partition_By_Path(path);
2290 if (part) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002291 if (part->Is_Adopted_Storage) {
2292 part->Display_Name = part->Display_Name + " - " + gui_lookup("data", "Data");
2293 part->Backup_Display_Name = part->Display_Name;
2294 part->Storage_Name = part->Storage_Name + " - " + gui_lookup("adopted_storage", "Adopted Storage");
2295 } else {
2296 part->Display_Name = gui_lookup(resource_name, default_value);
2297 part->Backup_Display_Name = part->Display_Name;
2298 if (part->Is_Storage)
2299 part->Storage_Name = gui_lookup(storage_resource_name, storage_default_value);
2300 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002301 }
2302}
2303
2304void TWPartitionManager::Translate_Partition_Display_Names() {
Ethan Yonker66a19492015-12-10 10:19:45 -06002305 LOGINFO("Translating partition display names\n");
Ethan Yonker74db1572015-10-28 12:44:49 -05002306 Translate_Partition("/system", "system", "System");
2307 Translate_Partition("/system_image", "system_image", "System Image");
2308 Translate_Partition("/vendor", "vendor", "Vendor");
2309 Translate_Partition("/vendor_image", "vendor_image", "Vendor Image");
2310 Translate_Partition("/cache", "cache", "Cache");
2311 Translate_Partition("/data", "data", "Data", "internal", "Internal Storage");
2312 Translate_Partition("/boot", "boot", "Boot");
2313 Translate_Partition("/recovery", "recovery", "Recovery");
2314 if (!datamedia) {
2315 Translate_Partition("/sdcard", "sdcard", "SDCard", "sdcard", "SDCard");
2316 Translate_Partition("/internal_sd", "sdcard", "SDCard", "sdcard", "SDCard");
2317 Translate_Partition("/internal_sdcard", "sdcard", "SDCard", "sdcard", "SDCard");
2318 Translate_Partition("/emmc", "sdcard", "SDCard", "sdcard", "SDCard");
2319 }
2320 Translate_Partition("/external_sd", "microsd", "Micro SDCard", "microsd", "Micro SDCard");
2321 Translate_Partition("/external_sdcard", "microsd", "Micro SDCard", "microsd", "Micro SDCard");
2322 Translate_Partition("/usb-otg", "usbotg", "USB OTG", "usbotg", "USB OTG");
2323 Translate_Partition("/sd-ext", "sdext", "SD-EXT");
2324
2325 // Android secure is a special case
2326 TWPartition* part = PartitionManager.Find_Partition_By_Path("/and-sec");
2327 if (part)
2328 part->Backup_Display_Name = gui_lookup("android_secure", "Android Secure");
2329
2330 // This updates the text on all of the storage selection buttons in the GUI
2331 DataManager::SetBackupFolder();
2332}
Ethan Yonker66a19492015-12-10 10:19:45 -06002333
2334void TWPartitionManager::Decrypt_Adopted() {
2335#ifdef TW_INCLUDE_CRYPTO
2336 if (!Mount_By_Path("/data", false)) {
2337 LOGERR("Cannot decrypt adopted storage because /data will not mount\n");
2338 return;
2339 }
2340 LOGINFO("Decrypt adopted storage starting\n");
2341 char* xmlFile = PageManager::LoadFileToBuffer("/data/system/storage.xml", NULL);
2342 xml_document<> *doc = NULL;
2343 xml_node<>* volumes = NULL;
2344 xml_node<>* volume = NULL;
2345 string Primary_Storage_UUID = "";
2346 if (xmlFile != NULL) {
2347 LOGINFO("successfully loaded storage.xml\n");
2348 doc = new xml_document<>();
2349 doc->parse<0>(xmlFile);
2350 volumes = doc->first_node("volumes");
2351 if (volumes) {
2352 xml_attribute<>* psuuid = volumes->first_attribute("primaryStorageUuid");
2353 if (psuuid) {
2354 Primary_Storage_UUID = psuuid->value();
2355 }
2356 }
2357 }
2358 std::vector<TWPartition*>::iterator adopt;
2359 for (adopt = Partitions.begin(); adopt != Partitions.end(); adopt++) {
2360 if ((*adopt)->Removable && (*adopt)->Is_Present) {
2361 if ((*adopt)->Decrypt_Adopted() == 0) {
2362 if (volumes) {
2363 xml_node<>* volume = volumes->first_node("volume");
2364 while (volume) {
2365 xml_attribute<>* guid = volume->first_attribute("partGuid");
2366 if (guid) {
2367 string GUID = (*adopt)->Adopted_GUID.c_str();
2368 GUID.insert(8, "-");
2369 GUID.insert(13, "-");
2370 GUID.insert(18, "-");
2371 GUID.insert(23, "-");
2372
2373 if (strcasecmp(GUID.c_str(), guid->value()) == 0) {
2374 xml_attribute<>* attr = volume->first_attribute("nickname");
2375 if (attr) {
2376 (*adopt)->Storage_Name = attr->value();
2377 (*adopt)->Display_Name = (*adopt)->Storage_Name;
2378 (*adopt)->Backup_Display_Name = (*adopt)->Storage_Name;
2379 LOGINFO("storage name from storage.xml is '%s'\n", attr->value());
2380 }
2381 attr = volume->first_attribute("fsUuid");
2382 if (attr && !Primary_Storage_UUID.empty() && strcmp(Primary_Storage_UUID.c_str(), attr->value()) == 0) {
2383 TWPartition* Dat = Find_Partition_By_Path("/data");
2384 if (Dat) {
2385 LOGINFO("Internal storage is found on adopted storage '%s'\n", (*adopt)->Display_Name.c_str());
2386 LOGINFO("Changing '%s' to point to '%s'\n", Dat->Symlink_Mount_Point.c_str(), (*adopt)->Storage_Path.c_str());
2387 (*adopt)->Symlink_Mount_Point = Dat->Symlink_Mount_Point;
2388 Dat->Symlink_Mount_Point = "";
2389 // Toggle mounts to ensure that the symlink mount point (probably /sdcard) is mounted to the right location
2390 Dat->UnMount(false);
2391 Dat->Mount(false);
2392 (*adopt)->UnMount(false);
2393 (*adopt)->Mount(false);
2394 Output_Partition((*adopt));
2395 }
2396 }
2397 break;
2398 }
2399 }
2400 volume = volume->next_sibling("volume");
2401 }
2402 }
2403 }
2404 }
2405 }
2406 if (xmlFile) {
2407 doc->clear();
2408 delete doc;
2409 free(xmlFile);
2410 }
2411#else
2412 LOGINFO("Decrypt_Adopted: no crypto support\n");
2413 return;
2414#endif
2415}