blob: 9e23ccb9425163c6dd0074b9b0943b309a5aebc1 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
Ethan Yonker472f5062016-02-25 13:47:30 -06002 Copyright 2014 to 2016 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
24#include <unistd.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040025#include <vector>
Dees_Troy63c8df72012-09-10 14:02:05 -040026#include <dirent.h>
27#include <time.h>
Dees_Troy8170a922012-09-18 15:40:25 -040028#include <errno.h>
29#include <fcntl.h>
bigbiffce8f83c2015-12-12 18:30:21 -050030#include <zlib.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050031#include <iostream>
32#include <iomanip>
Ethan Yonker8613dc02014-09-11 09:28:20 -050033#include <sys/wait.h>
Ethan Yonker483e9f42016-01-11 22:21:18 -060034#include <linux/fs.h>
35#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040036#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040039#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "twrp-functions.hpp"
Ethan Yonkerb5fab762016-01-28 14:03:33 -060041#include "fixContexts.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050042#include "twrpDigest.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060043#include "exclude.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060044#include "set_metadata.h"
bigbiff7abc5fe2015-01-17 16:53:12 -050045#include "tw_atomic.hpp"
Ethan Yonker74db1572015-10-28 12:44:49 -050046#include "gui/gui.hpp"
Ethan Yonker472f5062016-02-25 13:47:30 -060047#include "progresstracking.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050048#include "adbbu/libtwadbbu.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040049
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040050#ifdef TW_HAS_MTP
51#include "mtp/mtp_MtpServer.hpp"
52#include "mtp/twrpMtp.hpp"
Ethan Yonker726a0202014-12-16 20:01:38 -060053#include "mtp/MtpMessage.hpp"
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040054#endif
55
Dees Troy6f6441d2014-01-23 02:07:03 +000056extern "C" {
57 #include "cutils/properties.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050058 #include "gui/gui.h"
Dees Troy6f6441d2014-01-23 02:07:03 +000059}
60
Dees_Troy5bf43922012-09-07 16:07:55 -040061#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060062 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060063 #include "gui/rapidxml.hpp"
64 #include "gui/pages.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040065#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040066
Ethan Yonker1b190162016-12-05 15:25:19 -060067#ifdef AB_OTA_UPDATER
68#include <hardware/hardware.h>
69#include <hardware/boot_control.h>
70#endif
71
Ethan Yonker6277c792014-09-15 14:54:30 -050072extern bool datamedia;
73
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050074TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040075 mtp_was_enabled = false;
Ethan Yonker726a0202014-12-16 20:01:38 -060076 mtp_write_fd = -1;
bigbiff7abc5fe2015-01-17 16:53:12 -050077 stop_backup.set_value(0);
Ethan Yonker1b190162016-12-05 15:25:19 -060078#ifdef AB_OTA_UPDATER
79 char slot_suffix[PROPERTY_VALUE_MAX];
80 property_get("ro.boot.slot_suffix", slot_suffix, "_a");
81 Active_Slot_Display = "";
82 if (strcmp(slot_suffix, "_a") == 0)
83 Set_Active_Slot("A");
84 else
85 Set_Active_Slot("B");
86#endif
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050087}
88
Dees_Troy51a0e822012-09-05 15:24:24 -040089int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040090 FILE *fstabFile;
91 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +000092 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050093 TWPartition* andsec_partition = NULL;
Ethan Yonker726a0202014-12-16 20:01:38 -060094 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 -040095
96 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
97 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000098 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040099 return false;
100 }
101
102 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
103 if (fstab_line[0] != '/')
104 continue;
105
Dees_Troy2a923582012-09-20 12:13:34 -0400106 if (fstab_line[strlen(fstab_line) - 1] != '\n')
107 fstab_line[strlen(fstab_line)] = '\n';
Dees_Troy2a923582012-09-20 12:13:34 -0400108
Matt Mower2b2dd152016-04-26 11:24:08 -0500109 TWPartition* partition = new TWPartition();
Matt Mower72c87ce2016-04-26 14:34:56 -0500110 if (partition->Process_Fstab_Line(fstab_line, Display_Error))
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500111 Partitions.push_back(partition);
Matt Mower72c87ce2016-04-26 14:34:56 -0500112 else
Dees_Troy5bf43922012-09-07 16:07:55 -0400113 delete partition;
Matt Mower2b2dd152016-04-26 11:24:08 -0500114
115 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy5bf43922012-09-07 16:07:55 -0400116 }
117 fclose(fstabFile);
Matt Mower72c87ce2016-04-26 14:34:56 -0500118
119 std::vector<TWPartition*>::iterator iter;
120 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
121 (*iter)->Partition_Post_Processing(Display_Error);
122
123 if ((*iter)->Is_Storage) {
124 ++storageid;
125 (*iter)->MTP_Storage_ID = storageid;
126 }
127
128 if (!settings_partition && (*iter)->Is_Settings_Storage && (*iter)->Is_Present)
129 settings_partition = (*iter);
130 else
131 (*iter)->Is_Settings_Storage = false;
132
133 if (!andsec_partition && (*iter)->Has_Android_Secure && (*iter)->Is_Present)
134 andsec_partition = (*iter);
135 else
136 (*iter)->Has_Android_Secure = false;
137 }
138
Ethan Yonker6277c792014-09-15 14:54:30 -0500139 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) {
140 // Attempt to automatically identify /data/media emulated storage devices
141 TWPartition* Dat = Find_Partition_By_Path("/data");
142 if (Dat) {
143 LOGINFO("Using automatic handling for /data/media emulated storage device.\n");
144 datamedia = true;
that9e0593e2014-10-08 00:01:24 +0200145 Dat->Setup_Data_Media();
Ethan Yonker6277c792014-09-15 14:54:30 -0500146 settings_partition = Dat;
Ethan Yonker726a0202014-12-16 20:01:38 -0600147 // Since /data was not considered a storage partition earlier, we still need to assign an MTP ID
148 ++storageid;
149 Dat->MTP_Storage_ID = storageid;
Ethan Yonker6277c792014-09-15 14:54:30 -0500150 }
151 }
Dees Troy02a64532014-03-19 15:23:32 +0000152 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500153 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
154 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000155 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500156 break;
157 }
158 }
Dees Troy02a64532014-03-19 15:23:32 +0000159 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000160 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500161 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400162 if (!Write_Fstab()) {
163 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000164 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400165 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000166 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400167 }
Matt Mowered71fa32014-04-16 13:21:47 -0500168
Matt Mowerbf4efa32014-04-14 23:25:26 -0500169 if (andsec_partition) {
170 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500171 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500172 Setup_Android_Secure_Location(settings_partition);
173 }
Matt Mowered71fa32014-04-16 13:21:47 -0500174 if (settings_partition) {
175 Setup_Settings_Storage_Partition(settings_partition);
176 }
Ethan Yonker253368a2014-11-25 15:00:52 -0600177#ifdef TW_INCLUDE_CRYPTO
Ethan Yonkercceebb82014-11-18 10:17:59 -0600178 TWPartition* Decrypt_Data = Find_Partition_By_Path("/data");
179 if (Decrypt_Data && Decrypt_Data->Is_Encrypted && !Decrypt_Data->Is_Decrypted) {
180 int password_type = cryptfs_get_password_type();
181 if (password_type == CRYPT_TYPE_DEFAULT) {
182 LOGINFO("Device is encrypted with the default password, attempting to decrypt.\n");
183 if (Decrypt_Device("default_password") == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500184 gui_msg("decrypt_success=Successfully decrypted with default password.");
Ethan Yonkercceebb82014-11-18 10:17:59 -0600185 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
186 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500187 gui_err("unable_to_decrypt=Unable to decrypt with default password.");
Ethan Yonkercceebb82014-11-18 10:17:59 -0600188 }
189 } else {
190 DataManager::SetValue("TW_CRYPTO_TYPE", password_type);
191 }
192 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600193 if (Decrypt_Data && (!Decrypt_Data->Is_Encrypted || Decrypt_Data->Is_Decrypted) && Decrypt_Data->Mount(false)) {
194 Decrypt_Adopted();
195 }
Ethan Yonkercceebb82014-11-18 10:17:59 -0600196#endif
Dees_Troy51127312012-09-08 13:08:49 -0400197 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400198 UnMount_Main_Partitions();
Ethan Yonker1b190162016-12-05 15:25:19 -0600199#ifdef AB_OTA_UPDATER
200 DataManager::SetValue("tw_active_slot", Get_Active_Slot_Display());
201#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400202 return true;
203}
204
205int TWPartitionManager::Write_Fstab(void) {
206 FILE *fp;
207 std::vector<TWPartition*>::iterator iter;
208 string Line;
209
210 fp = fopen("/etc/fstab", "w");
211 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000212 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400213 return false;
214 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400215 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400216 if ((*iter)->Can_Be_Mounted) {
dianlujitao7d304c72016-01-02 12:15:50 +0800217 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw 0 0\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400218 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000219 }
220 // Handle subpartition tracking
221 if ((*iter)->Is_SubPartition) {
222 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
223 if (ParentPartition)
224 ParentPartition->Has_SubPartition = true;
225 else
226 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 -0400227 }
228 }
229 fclose(fp);
230 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400231}
232
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500233void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500234 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
235 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
236 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
237}
238
Matt Mowerbf4efa32014-04-14 23:25:26 -0500239void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
240 if (Part->Has_Android_Secure)
241 Part->Setup_AndSec();
Ethan Yonker6277c792014-09-15 14:54:30 -0500242 else if (!datamedia)
Matt Mowerbf4efa32014-04-14 23:25:26 -0500243 Part->Setup_AndSec();
Matt Mowerbf4efa32014-04-14 23:25:26 -0500244}
245
Dees_Troy8170a922012-09-18 15:40:25 -0400246void TWPartitionManager::Output_Partition_Logging(void) {
247 std::vector<TWPartition*>::iterator iter;
248
249 printf("\n\nPartition Logs:\n");
250 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
251 Output_Partition((*iter));
252}
253
254void TWPartitionManager::Output_Partition(TWPartition* Part) {
255 unsigned long long mb = 1048576;
256
Gary Peck004d48b2012-11-21 16:28:18 -0800257 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 -0400258 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800259 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 -0400260 }
Gary Peck004d48b2012-11-21 16:28:18 -0800261 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500262 if (Part->Can_Be_Mounted)
263 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800264 if (Part->Can_Be_Wiped)
265 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700266 if (Part->Use_Rm_Rf)
267 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500268 if (Part->Can_Be_Backed_Up)
269 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800270 if (Part->Wipe_During_Factory_Reset)
271 printf("Wipe_During_Factory_Reset ");
272 if (Part->Wipe_Available_in_GUI)
273 printf("Wipe_Available_in_GUI ");
274 if (Part->Is_SubPartition)
275 printf("Is_SubPartition ");
276 if (Part->Has_SubPartition)
277 printf("Has_SubPartition ");
278 if (Part->Removable)
279 printf("Removable ");
280 if (Part->Is_Present)
281 printf("IsPresent ");
282 if (Part->Can_Be_Encrypted)
283 printf("Can_Be_Encrypted ");
284 if (Part->Is_Encrypted)
285 printf("Is_Encrypted ");
286 if (Part->Is_Decrypted)
287 printf("Is_Decrypted ");
288 if (Part->Has_Data_Media)
289 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000290 if (Part->Can_Encrypt_Backup)
291 printf("Can_Encrypt_Backup ");
292 if (Part->Use_Userdata_Encryption)
293 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800294 if (Part->Has_Android_Secure)
295 printf("Has_Android_Secure ");
296 if (Part->Is_Storage)
297 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500298 if (Part->Is_Settings_Storage)
299 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000300 if (Part->Ignore_Blkid)
301 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000302 if (Part->Retain_Layout_Version)
303 printf("Retain_Layout_Version ");
Ethan Yonker253368a2014-11-25 15:00:52 -0600304 if (Part->Mount_To_Decrypt)
305 printf("Mount_To_Decrypt ");
Ethan Yonker96af84a2015-01-05 14:58:36 -0600306 if (Part->Can_Flash_Img)
307 printf("Can_Flash_Img ");
Ethan Yonker66a19492015-12-10 10:19:45 -0600308 if (Part->Is_Adopted_Storage)
309 printf("Is_Adopted_Storage ");
Ethan Yonker1b190162016-12-05 15:25:19 -0600310 if (Part->SlotSelect)
311 printf("SlotSelect ");
Gary Peck004d48b2012-11-21 16:28:18 -0800312 printf("\n");
313 if (!Part->SubPartition_Of.empty())
314 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
315 if (!Part->Symlink_Path.empty())
316 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
317 if (!Part->Symlink_Mount_Point.empty())
318 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
319 if (!Part->Primary_Block_Device.empty())
320 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
321 if (!Part->Alternate_Block_Device.empty())
322 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
323 if (!Part->Decrypted_Block_Device.empty())
324 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
Ethan Yonker253368a2014-11-25 15:00:52 -0600325 if (!Part->Crypto_Key_Location.empty() && Part->Crypto_Key_Location != "footer")
326 printf(" Crypto_Key_Location: %s\n", Part->Crypto_Key_Location.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800327 if (Part->Length != 0)
328 printf(" Length: %i\n", Part->Length);
329 if (!Part->Display_Name.empty())
330 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500331 if (!Part->Storage_Name.empty())
332 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800333 if (!Part->Backup_Path.empty())
334 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
335 if (!Part->Backup_Name.empty())
336 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500337 if (!Part->Backup_Display_Name.empty())
338 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800339 if (!Part->Backup_FileName.empty())
340 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
341 if (!Part->Storage_Path.empty())
342 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
343 if (!Part->Current_File_System.empty())
344 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
345 if (!Part->Fstab_File_System.empty())
346 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
347 if (Part->Format_Block_Size != 0)
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500348 printf(" Format_Block_Size: %lu\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400349 if (!Part->MTD_Name.empty())
350 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400351 string back_meth = Part->Backup_Method_By_Name();
Ethan Yonker6277c792014-09-15 14:54:30 -0500352 printf(" Backup_Method: %s\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800353 if (Part->Mount_Flags || !Part->Mount_Options.empty())
Matt Mower4ab42b12016-04-21 13:52:18 -0500354 printf(" Mount_Options: %s\n", Part->Mount_Options.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -0600355 if (Part->MTP_Storage_ID)
356 printf(" MTP_Storage_ID: %i\n", Part->MTP_Storage_ID);
Ethan Yonker6277c792014-09-15 14:54:30 -0500357 printf("\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400358}
359
Dees_Troy51a0e822012-09-05 15:24:24 -0400360int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400361 std::vector<TWPartition*>::iterator iter;
362 int ret = false;
363 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400364 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400365
Dees_Troyd93bda52013-07-03 19:55:19 +0000366 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400367 return true;
368
Dees_Troy5bf43922012-09-07 16:07:55 -0400369 // Iterate through all partitions
370 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400371 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400372 ret = (*iter)->Mount(Display_Error);
373 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400374 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400375 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400376 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400377 }
378 if (found) {
379 return ret;
380 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500381 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 -0400382 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000383 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400384 }
385 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400386}
387
Dees_Troy51a0e822012-09-05 15:24:24 -0400388int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400389 std::vector<TWPartition*>::iterator iter;
390 int ret = false;
391 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400392 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400393
394 // Iterate through all partitions
395 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400396 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400397 ret = (*iter)->UnMount(Display_Error);
398 found = true;
399 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
400 (*iter)->UnMount(Display_Error);
401 }
402 }
403 if (found) {
404 return ret;
405 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500406 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 -0400407 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000408 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400409 }
410 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400411}
412
Dees_Troy51a0e822012-09-05 15:24:24 -0400413int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400414 TWPartition* Part = Find_Partition_By_Path(Path);
415
416 if (Part)
417 return Part->Is_Mounted();
418 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000419 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400420 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400421}
422
Dees_Troy5bf43922012-09-07 16:07:55 -0400423int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400424 string current_storage_path = DataManager::GetCurrentStoragePath();
425
426 if (Mount_By_Path(current_storage_path, Display_Error)) {
427 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
428 if (FreeStorage)
429 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
430 return true;
431 }
432 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400433}
434
Dees_Troy5bf43922012-09-07 16:07:55 -0400435int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
436 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
437}
438
439TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
440 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400441 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400442
443 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400444 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400445 return (*iter);
446 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400447 return NULL;
448}
449
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400450int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
451 // Check the backup name to ensure that it is the correct size and contains only valid characters
452 // and that a backup with that name doesn't already exist
453 char backup_name[MAX_BACKUP_NAME_LEN];
454 char backup_loc[255], tw_image_dir[255];
455 int copy_size;
456 int index, cur_char;
457 string Backup_Name, Backup_Loc;
458
459 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
460 copy_size = Backup_Name.size();
461 // Check size
462 if (copy_size > MAX_BACKUP_NAME_LEN) {
463 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500464 gui_err("backup_name_len=Backup name is too long.");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400465 return -2;
466 }
467
468 // Check each character
469 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500470 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400471 return 0; // A "0" (zero) means to use the current timestamp for the backup name
472 for (index=0; index<copy_size; index++) {
473 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500474 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 -0400475 // These are valid characters
476 // Numbers
477 // Upper case letters
478 // Lower case letters
479 // Space
480 // and -_.{}[]
481 } else {
482 if (Display_Error)
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600483 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 -0400484 return -3;
485 }
486 }
487
488 // Check to make sure that a backup with this name doesn't already exist
489 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
490 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500491 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500492 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400493 if (Display_Error)
Matt Mower3c366972015-12-25 19:28:31 -0600494 gui_err("backup_name_exists=A backup with that name already exists!");
bigbiffce8f83c2015-12-12 18:30:21 -0500495
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400496 return -4;
497 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400498 // No problems found, return 0
499 return 0;
500}
501
bigbiff bigbiffa481d362016-08-08 20:36:10 -0400502bool TWPartitionManager::Make_MD5(PartitionSettings *part_settings)
Dees_Troy43d8b002012-09-17 16:00:01 -0400503{
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500504 string command, result;
505
506 if (part_settings->Part == NULL)
507 return false;
Ethan Yonkere080c1f2016-09-19 13:50:25 -0500508 string Full_File = part_settings->Backup_Folder + "/" + part_settings->Part->Backup_FileName;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500509 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400510
bigbiff bigbiffa481d362016-08-08 20:36:10 -0400511 if (!part_settings->generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400512 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400513
Ethan Yonker74db1572015-10-28 12:44:49 -0500514 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, gui_parse_text("{@generating_md51}"));
515 gui_msg("generating_md52= * Generating md5...");
Dees_Troy43d8b002012-09-17 16:00:01 -0400516 if (TWFunc::Path_Exists(Full_File)) {
bigbiffce8f83c2015-12-12 18:30:21 -0500517 md5sum.setfn(Full_File);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500518 if (md5sum.computeMD5() == 0)
519 if (md5sum.write_md5digest() == 0)
Ethan Yonker74db1572015-10-28 12:44:49 -0500520 gui_msg("md5_created= * MD5 Created.");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500521 else
522 return -1;
523 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500524 gui_err("md5_error= * MD5 Error!");
Dees_Troy43d8b002012-09-17 16:00:01 -0400525 } else {
526 char filename[512];
527 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500528 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400529 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500530 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000531 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400532 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000533 if (TWFunc::Path_Exists(filename)) {
534 if (md5sum.computeMD5() == 0) {
535 if (md5sum.write_md5digest() != 0)
536 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500537 gui_err("md5_error= * MD5 Error!");
Dees_Troy83bd4832013-05-04 12:39:56 +0000538 return false;
539 }
540 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500541 gui_err("md5_compute_error= * Error computing MD5.");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500542 return false;
543 }
544 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400545 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400546 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500547 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400548 }
549 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000550 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400551 return false;
552 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500553 gui_msg("md5_created= * MD5 Created.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400554 }
555 return true;
556}
557
bigbiffce8f83c2015-12-12 18:30:21 -0500558
559bool TWPartitionManager::Backup_Partition(PartitionSettings *part_settings) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400560 time_t start, stop;
bigbiffce8f83c2015-12-12 18:30:21 -0500561 int use_compression, adb_control_bu_fd;
562 string backup_log = part_settings->Backup_Folder + "/recovery.log";
Dees_Troy43d8b002012-09-17 16:00:01 -0400563
bigbiffce8f83c2015-12-12 18:30:21 -0500564 if (part_settings->Part == NULL)
Dees_Troy43d8b002012-09-17 16:00:01 -0400565 return true;
566
Dees_Troy093b7642012-09-21 15:59:38 -0400567 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400568
Tom Hite5a926722014-09-15 01:31:03 +0000569 TWFunc::SetPerformanceMode(true);
Dees_Troy43d8b002012-09-17 16:00:01 -0400570 time(&start);
571
bigbiffce8f83c2015-12-12 18:30:21 -0500572 if (part_settings->Part->Backup(part_settings, &tar_fork_pid)) {
Tom Hite5a926722014-09-15 01:31:03 +0000573 bool md5Success = false;
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400574 if (part_settings->adbbackup) {
575 md5Success = true;
576 }
577 else
578 md5Success = Make_MD5(part_settings);
579
580 TWFunc::SetPerformanceMode(false);
bigbiffce8f83c2015-12-12 18:30:21 -0500581 if (part_settings->Part->Has_SubPartition) {
Dees_Troy8170a922012-09-18 15:40:25 -0400582 std::vector<TWPartition*>::iterator subpart;
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400583 TWPartition *parentPart = part_settings->Part;
Dees_Troy8170a922012-09-18 15:40:25 -0400584
585 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400586 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == parentPart->Mount_Point) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500587 part_settings->Part = *subpart;
bigbiffce8f83c2015-12-12 18:30:21 -0500588 if (!(*subpart)->Backup(part_settings, &tar_fork_pid)) {
Tom Hite5a926722014-09-15 01:31:03 +0000589 TWFunc::SetPerformanceMode(false);
bigbiffce8f83c2015-12-12 18:30:21 -0500590 Clean_Backup_Folder(part_settings->Backup_Folder);
bigbiffbf1d6722015-02-14 16:25:59 -0500591 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
592 tw_set_default_metadata(backup_log.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400593 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000594 }
Dees_Troy2727b992013-08-14 20:09:30 +0000595 sync();
596 sync();
bigbiffce8f83c2015-12-12 18:30:21 -0500597 if (!part_settings->adbbackup) {
bigbiff bigbiffa481d362016-08-08 20:36:10 -0400598 if (!Make_MD5(part_settings)) {
bigbiffce8f83c2015-12-12 18:30:21 -0500599 TWFunc::SetPerformanceMode(false);
600 return false;
601 }
Tom Hite5a926722014-09-15 01:31:03 +0000602 }
Dees_Troy8170a922012-09-18 15:40:25 -0400603 }
604 }
605 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400606 time(&stop);
that203bcc92015-05-09 17:27:33 +0200607 int backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000608 LOGINFO("Partition Backup time: %d\n", backup_time);
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400609
bigbiffce8f83c2015-12-12 18:30:21 -0500610 if (part_settings->Part->Backup_Method == BM_FILES) {
611 part_settings->file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400612 } else {
bigbiffce8f83c2015-12-12 18:30:21 -0500613 part_settings->img_time += backup_time;
614
Dees_Troy43d8b002012-09-17 16:00:01 -0400615 }
Tom Hite5a926722014-09-15 01:31:03 +0000616
Tom Hite5a926722014-09-15 01:31:03 +0000617 return md5Success;
Dees_Troy43d8b002012-09-17 16:00:01 -0400618 } else {
bigbiffce8f83c2015-12-12 18:30:21 -0500619 Clean_Backup_Folder(part_settings->Backup_Folder);
bigbiffbf1d6722015-02-14 16:25:59 -0500620 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
621 tw_set_default_metadata(backup_log.c_str());
Tom Hite5a926722014-09-15 01:31:03 +0000622 TWFunc::SetPerformanceMode(false);
Dees_Troy43d8b002012-09-17 16:00:01 -0400623 return false;
624 }
bigbiff7abc5fe2015-01-17 16:53:12 -0500625 return 0;
626}
627
bigbiffbf1d6722015-02-14 16:25:59 -0500628void TWPartitionManager::Clean_Backup_Folder(string Backup_Folder) {
629 DIR *d = opendir(Backup_Folder.c_str());
630 struct dirent *p;
631 int r;
632
Ethan Yonker74db1572015-10-28 12:44:49 -0500633 gui_msg("backup_clean=Backup Failed. Cleaning Backup Folder.");
bigbiffbf1d6722015-02-14 16:25:59 -0500634
635 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500636 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Backup_Folder)(strerror(errno)));
bigbiffbf1d6722015-02-14 16:25:59 -0500637 return;
638 }
639
Matt Mower2b18a532015-02-20 16:58:05 -0600640 while ((p = readdir(d))) {
bigbiffbf1d6722015-02-14 16:25:59 -0500641 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
642 continue;
643
Ethan Yonkere080c1f2016-09-19 13:50:25 -0500644 string path = Backup_Folder + "/" + p->d_name;
bigbiffbf1d6722015-02-14 16:25:59 -0500645
646 size_t dot = path.find_last_of(".") + 1;
647 if (path.substr(dot) == "win" || path.substr(dot) == "md5" || path.substr(dot) == "info") {
648 r = unlink(path.c_str());
649 if (r != 0) {
650 LOGINFO("Unable to unlink '%s: %s'\n", path.c_str(), strerror(errno));
651 }
652 }
653 }
654 closedir(d);
655}
656
Ethan Yonker472f5062016-02-25 13:47:30 -0600657int TWPartitionManager::Check_Backup_Cancel() {
658 return stop_backup.get_value();
659}
660
bigbiff7abc5fe2015-01-17 16:53:12 -0500661int TWPartitionManager::Cancel_Backup() {
662 string Backup_Folder, Backup_Name, Full_Backup_Path;
663
664 stop_backup.set_value(1);
665
666 if (tar_fork_pid != 0) {
667 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
668 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
Ethan Yonkere080c1f2016-09-19 13:50:25 -0500669 Full_Backup_Path = Backup_Folder + "/" + Backup_Name;
bigbiff7abc5fe2015-01-17 16:53:12 -0500670 LOGINFO("Killing pid: %d\n", tar_fork_pid);
671 kill(tar_fork_pid, SIGUSR2);
672 while (kill(tar_fork_pid, 0) == 0) {
673 usleep(1000);
674 }
675 LOGINFO("Backup_Run stopped and returning false, backup cancelled.\n");
676 LOGINFO("Removing directory %s\n", Full_Backup_Path.c_str());
677 TWFunc::removeDir(Full_Backup_Path, false);
678 tar_fork_pid = 0;
679 }
680
681 return 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400682}
683
bigbiffce8f83c2015-12-12 18:30:21 -0500684int TWPartitionManager::Run_Backup(bool adbbackup) {
685 PartitionSettings part_settings;
686 int check, partition_count = 0, disable_free_space_check = 0, do_md5 = 0;
687 int gui_adb_backup;
688 string Backup_Name, Backup_List, backup_path;
689 unsigned long long total_bytes = 0, free_space = 0, subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400690 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400691 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400692 struct tm *t;
693 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500694 size_t start_pos = 0, end_pos = 0;
bigbiff7abc5fe2015-01-17 16:53:12 -0500695 stop_backup.set_value(0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400696 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500697 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400698
bigbiffce8f83c2015-12-12 18:30:21 -0500699 part_settings.img_bytes_remaining = 0;
700 part_settings.file_bytes_remaining = 0;
701 part_settings.img_time = 0;
702 part_settings.file_time = 0;
703 part_settings.img_bytes = 0;
704 part_settings.file_bytes = 0;
705 part_settings.PM_Method = PM_BACKUP;
706
707 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
708 if (gui_adb_backup == true)
709 adbbackup = true;
710
711 part_settings.adbbackup = adbbackup;
Dees_Troy43d8b002012-09-17 16:00:01 -0400712 time(&total_start);
713
714 Update_System_Details();
715
716 if (!Mount_Current_Storage(true))
717 return false;
718
bigbiffce8f83c2015-12-12 18:30:21 -0500719
Dees_Troy43d8b002012-09-17 16:00:01 -0400720 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400721 if (do_md5 == 0)
bigbiffce8f83c2015-12-12 18:30:21 -0500722 part_settings.generate_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400723 else
bigbiffce8f83c2015-12-12 18:30:21 -0500724 part_settings.generate_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400725
bigbiffce8f83c2015-12-12 18:30:21 -0500726 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, part_settings.Backup_Folder);
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500727 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
728 if (Backup_Name == gui_lookup("curr_date", "(Current Date)")) {
729 Backup_Name = TWFunc::Get_Current_Date();
730 } else if (Backup_Name == gui_lookup("auto_generate", "(Auto Generate)") || Backup_Name == "0" || Backup_Name.empty()) {
Dees Troyb21cc642013-09-10 17:36:41 +0000731 TWFunc::Auto_Generate_Backup_Name();
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500732 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400733 }
bigbiffce8f83c2015-12-12 18:30:21 -0500734
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500735 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Ethan Yonkere080c1f2016-09-19 13:50:25 -0500736 part_settings.Backup_Folder = part_settings.Backup_Folder + "/" + Backup_Name;
bigbiffce8f83c2015-12-12 18:30:21 -0500737
Ethan Yonkere080c1f2016-09-19 13:50:25 -0500738 LOGINFO("Backup_Folder is: '%s'\n", part_settings.Backup_Folder.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400739
Dees_Troy2673cec2013-04-02 20:22:16 +0000740 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500741 DataManager::GetValue("tw_backup_list", Backup_List);
742 if (!Backup_List.empty()) {
743 end_pos = Backup_List.find(";", start_pos);
744 while (end_pos != string::npos && start_pos < Backup_List.size()) {
745 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
bigbiffce8f83c2015-12-12 18:30:21 -0500746 part_settings.Part = Find_Partition_By_Path(backup_path);
747 if (part_settings.Part != NULL) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500748 partition_count++;
bigbiffce8f83c2015-12-12 18:30:21 -0500749 if (part_settings.Part->Backup_Method == BM_FILES)
750 part_settings.file_bytes += part_settings.Part->Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -0500751 else
bigbiffce8f83c2015-12-12 18:30:21 -0500752 part_settings.img_bytes += part_settings.Part->Backup_Size;
753 if (part_settings.Part->Has_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500754 std::vector<TWPartition*>::iterator subpart;
755
756 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
bigbiffce8f83c2015-12-12 18:30:21 -0500757 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == part_settings.Part->Mount_Point) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500758 partition_count++;
bigbiffce8f83c2015-12-12 18:30:21 -0500759 if ((*subpart)->Backup_Method == BM_FILES)
760 part_settings.file_bytes += (*subpart)->Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -0500761 else
bigbiffce8f83c2015-12-12 18:30:21 -0500762 part_settings.img_bytes += (*subpart)->Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -0500763 }
764 }
Dees_Troy8170a922012-09-18 15:40:25 -0400765 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500766 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500767 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 -0400768 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500769 start_pos = end_pos + 1;
770 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400771 }
772 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400773
774 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500775 gui_msg("no_partition_selected=No partitions selected for backup.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400776 return false;
777 }
bigbiffce8f83c2015-12-12 18:30:21 -0500778 if (adbbackup) {
779 if (twadbbu::Write_ADB_Stream_Header(partition_count) == false) {
780 return false;
781 }
782 }
783 total_bytes = part_settings.file_bytes + part_settings.img_bytes;
Ethan Yonker472f5062016-02-25 13:47:30 -0600784 ProgressTracking progress(total_bytes);
bigbiffce8f83c2015-12-12 18:30:21 -0500785 part_settings.progress = &progress;
786
Ethan Yonker74db1572015-10-28 12:44:49 -0500787 gui_msg(Msg("total_partitions_backup= * Total number of partitions to back up: {1}")(partition_count));
788 gui_msg(Msg("total_backup_size= * Total size of all data: {1}MB")(total_bytes / 1024 / 1024));
Dees_Troy43d8b002012-09-17 16:00:01 -0400789 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
790 if (storage != NULL) {
791 free_space = storage->Free;
Ethan Yonker74db1572015-10-28 12:44:49 -0500792 gui_msg(Msg("available_space= * Available space: {1}MB")(free_space / 1024 / 1024));
Dees_Troy43d8b002012-09-17 16:00:01 -0400793 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500794 gui_err("unable_locate_storage=Unable to locate storage device.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400795 return false;
796 }
bigbiffbf1d6722015-02-14 16:25:59 -0500797
Matt Mowerbfccfb82016-04-25 23:22:31 -0500798 DataManager::GetValue(TW_DISABLE_FREE_SPACE_VAR, disable_free_space_check);
bigbiffce8f83c2015-12-12 18:30:21 -0500799
800 if (adbbackup)
801 disable_free_space_check = true;
802
bigbiffbf1d6722015-02-14 16:25:59 -0500803 if (!disable_free_space_check) {
804 if (free_space - (32 * 1024 * 1024) < total_bytes) {
805 // We require an extra 32MB just in case
Ethan Yonker74db1572015-10-28 12:44:49 -0500806 gui_err("no_space=Not enough free space on storage.");
bigbiffbf1d6722015-02-14 16:25:59 -0500807 return false;
808 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400809 }
bigbiffce8f83c2015-12-12 18:30:21 -0500810 part_settings.img_bytes_remaining = part_settings.img_bytes;
811 part_settings.file_bytes_remaining = part_settings.file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400812
Ethan Yonker74db1572015-10-28 12:44:49 -0500813 gui_msg("backup_started=[BACKUP STARTED]");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500814 gui_msg(Msg("backup_folder= * Backup Folder: {1}")(part_settings.Backup_Folder));
815 if (!TWFunc::Recursive_Mkdir(part_settings.Backup_Folder)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500816 gui_err("fail_backup_folder=Failed to make backup folder.");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000817 return false;
818 }
819
Dees_Troy2673cec2013-04-02 20:22:16 +0000820 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400821
Dees_Troya13d74f2013-03-24 08:54:55 -0500822 start_pos = 0;
823 end_pos = Backup_List.find(";", start_pos);
824 while (end_pos != string::npos && start_pos < Backup_List.size()) {
bigbiff7abc5fe2015-01-17 16:53:12 -0500825 if (stop_backup.get_value() != 0)
826 return -1;
Dees_Troya13d74f2013-03-24 08:54:55 -0500827 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
bigbiffce8f83c2015-12-12 18:30:21 -0500828 part_settings.Part = Find_Partition_By_Path(backup_path);
829 if (part_settings.Part != NULL) {
830 if (!Backup_Partition(&part_settings))
831
Dees_Troya13d74f2013-03-24 08:54:55 -0500832 return false;
833 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500834 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 -0500835 }
836 start_pos = end_pos + 1;
837 end_pos = Backup_List.find(";", start_pos);
838 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400839
840 // Average BPS
bigbiffce8f83c2015-12-12 18:30:21 -0500841 if (part_settings.img_time == 0)
842 part_settings.img_time = 1;
843 if (part_settings.file_time == 0)
844 part_settings.file_time = 1;
845 int img_bps = (int)part_settings.img_bytes / (int)part_settings.img_time;
846 unsigned long long file_bps = part_settings.file_bytes / (int)part_settings.file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400847
bigbiffce8f83c2015-12-12 18:30:21 -0500848 if (part_settings.file_bytes != 0)
Ethan Yonker472f5062016-02-25 13:47:30 -0600849 gui_msg(Msg("avg_backup_fs=Average backup rate for file systems: {1} MB/sec")(file_bps / (1024 * 1024)));
bigbiffce8f83c2015-12-12 18:30:21 -0500850 if (part_settings.img_bytes != 0)
Ethan Yonker472f5062016-02-25 13:47:30 -0600851 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 -0400852
853 time(&total_stop);
854 int total_time = (int) difftime(total_stop, total_start);
bigbiffce8f83c2015-12-12 18:30:21 -0500855
856 uint64_t actual_backup_size;
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600857 if (!adbbackup) {
858 TWExclude twe;
859 actual_backup_size = twe.Get_Folder_Size(part_settings.Backup_Folder);
860 } else
bigbiffce8f83c2015-12-12 18:30:21 -0500861 actual_backup_size = part_settings.file_bytes + part_settings.img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500862 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400863
bigbiffce8f83c2015-12-12 18:30:21 -0500864 int prev_img_bps = 0, use_compression = 0;
865 unsigned long long prev_file_bps = 0;
Dees_Troy093b7642012-09-21 15:59:38 -0400866 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500867 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400868
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500869 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400870 if (use_compression)
871 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500872 else
Dees_Troy093b7642012-09-21 15:59:38 -0400873 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
874 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500875 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400876
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500877 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400878 if (use_compression)
879 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
880 else
881 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
882
Ethan Yonker74db1572015-10-28 12:44:49 -0500883 gui_msg(Msg("total_backed_size=[{1} MB TOTAL BACKED UP]")(actual_backup_size));
Dees_Troy43d8b002012-09-17 16:00:01 -0400884 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400885 UnMount_Main_Partitions();
Ethan Yonker56db1c42015-12-20 22:49:00 -0600886 gui_msg(Msg(msg::kHighlight, "backup_completed=[BACKUP COMPLETED IN {1} SECONDS]")(total_time)); // the end
Ethan Yonkere080c1f2016-09-19 13:50:25 -0500887 string backup_log = part_settings.Backup_Folder + "/recovery.log";
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000888 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600889 tw_set_default_metadata(backup_log.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -0500890
891 if (part_settings.adbbackup) {
892 if (twadbbu::Write_ADB_Stream_Trailer() == false) {
893 return false;
894 }
895 }
896 part_settings.adbbackup = false;
897 DataManager::SetValue("tw_enable_adb_backup", 0);
898
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000899 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400900}
901
bigbiffce8f83c2015-12-12 18:30:21 -0500902bool TWPartitionManager::Restore_Partition(PartitionSettings *part_settings) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400903 time_t Start, Stop;
bigbiffce8f83c2015-12-12 18:30:21 -0500904
Tom Hite5a926722014-09-15 01:31:03 +0000905 TWFunc::SetPerformanceMode(true);
bigbiffce8f83c2015-12-12 18:30:21 -0500906
Dees_Troy4a2a1262012-09-18 09:33:47 -0400907 time(&Start);
Ethan Yonker472f5062016-02-25 13:47:30 -0600908
bigbiffce8f83c2015-12-12 18:30:21 -0500909
910 if (!part_settings->Part->Restore(part_settings)) {
Tom Hite5a926722014-09-15 01:31:03 +0000911 TWFunc::SetPerformanceMode(false);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400912 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000913 }
bigbiffce8f83c2015-12-12 18:30:21 -0500914 if (part_settings->Part->Has_SubPartition) {
Dees_Troy8170a922012-09-18 15:40:25 -0400915 std::vector<TWPartition*>::iterator subpart;
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400916 TWPartition *parentPart = part_settings->Part;
Dees_Troy8170a922012-09-18 15:40:25 -0400917
918 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400919 part_settings->Part = *subpart;
920 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == parentPart->Mount_Point) {
921 part_settings->Part = (*subpart);
bigbiffce8f83c2015-12-12 18:30:21 -0500922 if (!(*subpart)->Restore(part_settings)) {
Tom Hite5a926722014-09-15 01:31:03 +0000923 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400924 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000925 }
Dees_Troy8170a922012-09-18 15:40:25 -0400926 }
927 }
928 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400929 time(&Stop);
Tom Hite5a926722014-09-15 01:31:03 +0000930 TWFunc::SetPerformanceMode(false);
bigbiffce8f83c2015-12-12 18:30:21 -0500931 gui_msg(Msg("restore_part_done=[{1} done ({2} seconds)]")(part_settings->Part->Backup_Display_Name)((int)difftime(Stop, Start)));
932
Dees_Troy4a2a1262012-09-18 09:33:47 -0400933 return true;
934}
935
Ethan Yonker472f5062016-02-25 13:47:30 -0600936int TWPartitionManager::Run_Restore(const string& Restore_Name) {
bigbiffce8f83c2015-12-12 18:30:21 -0500937 PartitionSettings part_settings;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400938 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500939 TWPartition* restore_part = NULL;
bigbiffce8f83c2015-12-12 18:30:21 -0500940
Dees_Troy4a2a1262012-09-18 09:33:47 -0400941 time_t rStart, rStop;
942 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500943 string Restore_List, restore_path;
944 size_t start_pos = 0, end_pos;
bigbiffce8f83c2015-12-12 18:30:21 -0500945
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500946 part_settings.Backup_Folder = Restore_Name;
bigbiffce8f83c2015-12-12 18:30:21 -0500947 part_settings.Part = NULL;
948 part_settings.partition_count = 0;
949 part_settings.total_restore_size = 0;
950 part_settings.adbbackup = false;
951 part_settings.PM_Method = PM_RESTORE;
Dees_Troy43d8b002012-09-17 16:00:01 -0400952
Ethan Yonker74db1572015-10-28 12:44:49 -0500953 gui_msg("restore_started=[RESTORE STARTED]");
954 gui_msg(Msg("restore_folder=Restore folder: '{1}'")(Restore_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -0400955
Dees_Troy4a2a1262012-09-18 09:33:47 -0400956 if (!Mount_Current_Storage(true))
957 return false;
958
959 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500960 if (check_md5 > 0) {
961 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
Ethan Yonker74db1572015-10-28 12:44:49 -0500962 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, gui_parse_text("{@verifying_md5}"));
963 gui_msg("verifying_md5=Verifying MD5");
Dees_Troya13d74f2013-03-24 08:54:55 -0500964 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500965 gui_msg("skip_md5=Skipping MD5 check based on user setting.");
Dees_Troya13d74f2013-03-24 08:54:55 -0500966 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500967 gui_msg("calc_restore=Calculating restore details...");
Dees_Troya13d74f2013-03-24 08:54:55 -0500968 DataManager::GetValue("tw_restore_selected", Restore_List);
bigbiffce8f83c2015-12-12 18:30:21 -0500969
Dees_Troya13d74f2013-03-24 08:54:55 -0500970 if (!Restore_List.empty()) {
971 end_pos = Restore_List.find(";", start_pos);
972 while (end_pos != string::npos && start_pos < Restore_List.size()) {
973 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
bigbiffce8f83c2015-12-12 18:30:21 -0500974 part_settings.Part = Find_Partition_By_Path(restore_path);
975 if (part_settings.Part != NULL) {
bigbiffce8f83c2015-12-12 18:30:21 -0500976 if (part_settings.Part->Mount_Read_Only) {
977 gui_msg(Msg(msg::kError, "restore_read_only=Cannot restore {1} -- mounted read only.")(part_settings.Part->Backup_Display_Name));
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500978 return false;
979 }
bigbiffce8f83c2015-12-12 18:30:21 -0500980
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400981 if (check_md5 > 0 && !part_settings.Part->Check_MD5(&part_settings))
Dees_Troya13d74f2013-03-24 08:54:55 -0500982 return false;
bigbiffce8f83c2015-12-12 18:30:21 -0500983 part_settings.partition_count++;
984 part_settings.total_restore_size += part_settings.Part->Get_Restore_Size(&part_settings);
985 if (part_settings.Part->Has_SubPartition) {
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400986 TWPartition *parentPart = part_settings.Part;
Dees_Troya13d74f2013-03-24 08:54:55 -0500987 std::vector<TWPartition*>::iterator subpart;
988
989 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400990 part_settings.Part = *subpart;
991 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == parentPart->Mount_Point) {
992 if (check_md5 > 0 && !(*subpart)->Check_MD5(&part_settings))
Dees_Troya13d74f2013-03-24 08:54:55 -0500993 return false;
bigbiffce8f83c2015-12-12 18:30:21 -0500994 part_settings.total_restore_size += (*subpart)->Get_Restore_Size(&part_settings);
Dees_Troya13d74f2013-03-24 08:54:55 -0500995 }
996 }
997 }
998 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500999 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -05001000 }
1001 start_pos = end_pos + 1;
1002 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -04001003 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001004 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001005
bigbiffce8f83c2015-12-12 18:30:21 -05001006 if (part_settings.partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001007 gui_err("no_part_restore=No partitions selected for restore.");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001008 return false;
1009 }
1010
bigbiffce8f83c2015-12-12 18:30:21 -05001011 gui_msg(Msg("restore_part_count=Restoring {1} partitions...")(part_settings.partition_count));
1012 gui_msg(Msg("total_restore_size=Total restore size is {1}MB")(part_settings.total_restore_size / 1048576));
Dees_Troy2673cec2013-04-02 20:22:16 +00001013 DataManager::SetProgress(0.0);
bigbiffce8f83c2015-12-12 18:30:21 -05001014 ProgressTracking progress(part_settings.total_restore_size);
1015 part_settings.progress = &progress;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001016
Dees_Troya13d74f2013-03-24 08:54:55 -05001017 start_pos = 0;
1018 if (!Restore_List.empty()) {
1019 end_pos = Restore_List.find(";", start_pos);
1020 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1021 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
bigbiffce8f83c2015-12-12 18:30:21 -05001022
1023 part_settings.Part = Find_Partition_By_Path(restore_path);
1024 if (part_settings.Part != NULL) {
1025 part_settings.partition_count++;
bigbiffce8f83c2015-12-12 18:30:21 -05001026 if (!Restore_Partition(&part_settings))
Dees_Troya13d74f2013-03-24 08:54:55 -05001027 return false;
1028 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001029 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -05001030 }
1031 start_pos = end_pos + 1;
1032 end_pos = Restore_List.find(";", start_pos);
1033 }
1034 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001035 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, gui_parse_text("{@updating_system_details}"));
Dees_Troy43d8b002012-09-17 16:00:01 -04001036 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001037 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -04001038 time(&rStop);
Matt Mower3c366972015-12-25 19:28:31 -06001039 gui_msg(Msg(msg::kHighlight, "restore_completed=[RESTORE COMPLETED IN {1} SECONDS]")((int)difftime(rStop,rStart)));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001040 DataManager::SetValue("tw_file_progress", "");
bigbiffce8f83c2015-12-12 18:30:21 -05001041
Dees_Troy63c8df72012-09-10 14:02:05 -04001042 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001043}
1044
1045void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001046 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -05001047 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +00001048 bool get_date = true, check_encryption = true;
1049
1050 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -04001051
1052 DIR* d;
1053 d = opendir(Restore_Name.c_str());
1054 if (d == NULL)
1055 {
Ethan Yonker74db1572015-10-28 12:44:49 -05001056 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Restore_Name)(strerror(errno)));
Dees_Troy63c8df72012-09-10 14:02:05 -04001057 return;
1058 }
1059
1060 struct dirent* de;
1061 while ((de = readdir(d)) != NULL)
1062 {
1063 // Strip off three components
1064 char str[256];
1065 char* label;
1066 char* fstype = NULL;
1067 char* extn = NULL;
1068 char* ptr;
1069
1070 strcpy(str, de->d_name);
1071 if (strlen(str) <= 2)
1072 continue;
1073
1074 if (get_date) {
1075 char file_path[255];
1076 struct stat st;
1077
1078 strcpy(file_path, Restore_Name.c_str());
1079 strcat(file_path, "/");
1080 strcat(file_path, str);
1081 stat(file_path, &st);
1082 string backup_date = ctime((const time_t*)(&st.st_mtime));
1083 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
1084 get_date = false;
1085 }
1086
1087 label = str;
1088 ptr = label;
1089 while (*ptr && *ptr != '.') ptr++;
1090 if (*ptr == '.')
1091 {
1092 *ptr = 0x00;
1093 ptr++;
1094 fstype = ptr;
1095 }
1096 while (*ptr && *ptr != '.') ptr++;
1097 if (*ptr == '.')
1098 {
1099 *ptr = 0x00;
1100 ptr++;
1101 extn = ptr;
1102 }
1103
Dees_Troy83bd4832013-05-04 12:39:56 +00001104 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -05001105 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +00001106 if (extnlength != 3 && extnlength != 6) continue;
1107 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
1108 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
1109
1110 if (check_encryption) {
1111 string filename = Restore_Name + "/";
1112 filename += de->d_name;
1113 if (TWFunc::Get_File_Type(filename) == 2) {
1114 LOGINFO("'%s' is encrypted\n", filename.c_str());
1115 DataManager::SetValue("tw_restore_encrypted", 1);
1116 }
1117 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001118 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -04001119
1120 TWPartition* Part = Find_Partition_By_Path(label);
1121 if (Part == NULL)
1122 {
Ethan Yonker74db1572015-10-28 12:44:49 -05001123 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 -04001124 continue;
1125 }
1126
1127 Part->Backup_FileName = de->d_name;
1128 if (strlen(extn) > 3) {
1129 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1130 }
1131
James Christopher Adduonofddbdfa2016-02-29 15:08:11 -05001132 if (!Part->Is_SubPartition)
1133 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001134 }
1135 closedir(d);
1136
Dees_Troya13d74f2013-03-24 08:54:55 -05001137 // Set the final value
1138 DataManager::SetValue("tw_restore_list", Restore_List);
1139 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001140 return;
1141}
1142
1143int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001144 std::vector<TWPartition*>::iterator iter;
1145 int ret = false;
1146 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001147 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001148
1149 // Iterate through all partitions
1150 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001151 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001152 if (Path == "/and-sec")
1153 ret = (*iter)->Wipe_AndSec();
1154 else
1155 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001156 found = true;
1157 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1158 (*iter)->Wipe();
1159 }
1160 }
1161 if (found) {
1162 return ret;
1163 } else
Ethan Yonker74db1572015-10-28 12:44:49 -05001164 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 -04001165 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001166}
1167
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001168int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1169 std::vector<TWPartition*>::iterator iter;
1170 int ret = false;
1171 bool found = false;
1172 string Local_Path = TWFunc::Get_Root_Path(Path);
1173
1174 // Iterate through all partitions
1175 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1176 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1177 if (Path == "/and-sec")
1178 ret = (*iter)->Wipe_AndSec();
1179 else
1180 ret = (*iter)->Wipe(New_File_System);
1181 found = true;
1182 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1183 (*iter)->Wipe(New_File_System);
1184 }
1185 }
1186 if (found) {
1187 return ret;
1188 } else
Ethan Yonker74db1572015-10-28 12:44:49 -05001189 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 -05001190 return false;
1191}
1192
Dees_Troy51a0e822012-09-05 15:24:24 -04001193int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001194 std::vector<TWPartition*>::iterator iter;
1195 int ret = true;
1196
1197 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001198 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Ethan Yonker89583ef2015-08-26 09:01:59 -05001199#ifdef TW_OEM_BUILD
1200 if ((*iter)->Mount_Point == "/data") {
1201 if (!(*iter)->Wipe_Encryption())
1202 ret = false;
1203 } else {
1204#endif
1205 if (!(*iter)->Wipe())
1206 ret = false;
1207#ifdef TW_OEM_BUILD
1208 }
1209#endif
Dees_Troy094207a2012-09-26 12:00:39 -04001210 } else if ((*iter)->Has_Android_Secure) {
1211 if (!(*iter)->Wipe_AndSec())
1212 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001213 }
1214 }
Ethan Yonker9fa76ba2016-06-30 14:05:43 -05001215 TWFunc::check_and_run_script("/sbin/factoryreset.sh", "Factory Reset Script");
Dees_Troy63c8df72012-09-10 14:02:05 -04001216 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001217}
1218
Dees_Troy38bd7602012-09-14 13:33:53 -04001219int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1220 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001221 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001222
1223 if (!Mount_By_Path("/data", true))
1224 return false;
1225
1226 if (!Mount_By_Path("/cache", true))
1227 return false;
1228
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001229 dir.push_back("/data/dalvik-cache");
1230 dir.push_back("/cache/dalvik-cache");
1231 dir.push_back("/cache/dc");
Ethan Yonker74db1572015-10-28 12:44:49 -05001232 gui_msg("wiping_dalvik=Wiping Dalvik Cache Directories...");
Dees_Troya13d74f2013-03-24 08:54:55 -05001233 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001234 if (stat(dir.at(i).c_str(), &st) == 0) {
1235 TWFunc::removeDir(dir.at(i), false);
Ethan Yonker74db1572015-10-28 12:44:49 -05001236 gui_msg(Msg("cleaned=Cleaned: {1}...")(dir.at(i)));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001237 }
1238 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001239 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001240 if (sdext && sdext->Is_Present && sdext->Mount(false))
1241 {
1242 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1243 {
1244 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Ethan Yonker74db1572015-10-28 12:44:49 -05001245 gui_msg(Msg("cleaned=Cleaned: {1}...")("/sd-ext/dalvik-cache"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001246 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001247 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001248 gui_msg("dalvik_done=-- Dalvik Cache Directories Wipe Complete!");
Dees_Troy38bd7602012-09-14 13:33:53 -04001249 return true;
1250}
1251
1252int TWPartitionManager::Wipe_Rotate_Data(void) {
1253 if (!Mount_By_Path("/data", true))
1254 return false;
1255
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001256 unlink("/data/misc/akmd*");
1257 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001258 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001259 return true;
1260}
1261
1262int TWPartitionManager::Wipe_Battery_Stats(void) {
1263 struct stat st;
1264
1265 if (!Mount_By_Path("/data", true))
1266 return false;
1267
1268 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001269 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001270 } else {
1271 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001272 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001273 }
1274 return true;
1275}
1276
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001277int TWPartitionManager::Wipe_Android_Secure(void) {
1278 std::vector<TWPartition*>::iterator iter;
1279 int ret = false;
1280 bool found = false;
1281
1282 // Iterate through all partitions
1283 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1284 if ((*iter)->Has_Android_Secure) {
1285 ret = (*iter)->Wipe_AndSec();
1286 found = true;
1287 }
1288 }
1289 if (found) {
1290 return ret;
1291 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001292 gui_err("no_andsec=No android secure partitions found.");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001293 }
1294 return false;
1295}
1296
Dees_Troy38bd7602012-09-14 13:33:53 -04001297int TWPartitionManager::Format_Data(void) {
1298 TWPartition* dat = Find_Partition_By_Path("/data");
1299
1300 if (dat != NULL) {
1301 if (!dat->UnMount(true))
1302 return false;
1303
1304 return dat->Wipe_Encryption();
1305 } else {
Matt Mower3c366972015-12-25 19:28:31 -06001306 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/data"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001307 return false;
1308 }
1309 return false;
1310}
1311
1312int TWPartitionManager::Wipe_Media_From_Data(void) {
1313 TWPartition* dat = Find_Partition_By_Path("/data");
1314
1315 if (dat != NULL) {
1316 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001317 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001318 return false;
1319 }
1320 if (!dat->Mount(true))
1321 return false;
1322
Ethan Yonker74db1572015-10-28 12:44:49 -05001323 gui_msg("wiping_datamedia=Wiping internal storage -- /data/media...");
Ethan Yonker726a0202014-12-16 20:01:38 -06001324 Remove_MTP_Storage(dat->MTP_Storage_ID);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001325 TWFunc::removeDir("/data/media", false);
xiaolu9416f4f2015-06-04 08:22:23 +08001326 dat->Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001327 Add_MTP_Storage(dat->MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001328 return true;
1329 } else {
Matt Mower3c366972015-12-25 19:28:31 -06001330 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/data"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001331 return false;
1332 }
1333 return false;
1334}
1335
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001336int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1337 std::vector<TWPartition*>::iterator iter;
1338 int ret = false;
1339 bool found = false;
1340 string Local_Path = TWFunc::Get_Root_Path(Path);
1341
1342 if (Local_Path == "/tmp" || Local_Path == "/")
1343 return true;
1344
1345 // Iterate through all partitions
1346 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1347 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1348 ret = (*iter)->Repair();
1349 found = true;
1350 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1351 (*iter)->Repair();
1352 }
1353 }
1354 if (found) {
1355 return ret;
1356 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001357 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 -05001358 } else {
1359 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1360 }
1361 return false;
1362}
1363
Ethan Yonkera2719152015-05-28 09:44:41 -05001364int TWPartitionManager::Resize_By_Path(string Path, bool Display_Error) {
1365 std::vector<TWPartition*>::iterator iter;
1366 int ret = false;
1367 bool found = false;
1368 string Local_Path = TWFunc::Get_Root_Path(Path);
1369
1370 if (Local_Path == "/tmp" || Local_Path == "/")
1371 return true;
1372
1373 // Iterate through all partitions
1374 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1375 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1376 ret = (*iter)->Resize();
1377 found = true;
1378 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1379 (*iter)->Resize();
1380 }
1381 }
1382 if (found) {
1383 return ret;
1384 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001385 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 -05001386 } else {
1387 LOGINFO("Resize: Unable to find partition for path '%s'\n", Local_Path.c_str());
1388 }
1389 return false;
1390}
1391
Dees_Troy51a0e822012-09-05 15:24:24 -04001392void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001393 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001394 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001395
Ethan Yonker74db1572015-10-28 12:44:49 -05001396 gui_msg("update_part_details=Updating partition details...");
Dees_Troy5bf43922012-09-07 16:07:55 -04001397 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Ethan Yonker1b190162016-12-05 15:25:19 -06001398 (*iter)->Update_Size(true);
Dees_Troy51127312012-09-08 13:08:49 -04001399 if ((*iter)->Can_Be_Mounted) {
Dees_Troy51127312012-09-08 13:08:49 -04001400 if ((*iter)->Mount_Point == "/system") {
1401 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1402 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1403 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1404 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1405 } else if ((*iter)->Mount_Point == "/cache") {
1406 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1407 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1408 } else if ((*iter)->Mount_Point == "/sd-ext") {
1409 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1410 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1411 if ((*iter)->Backup_Size == 0) {
1412 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1413 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1414 } else
1415 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001416 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001417 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001418 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001419 if ((*iter)->Backup_Size == 0) {
1420 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1421 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1422 } else
1423 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001424 } else if ((*iter)->Mount_Point == "/boot") {
1425 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1426 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1427 if ((*iter)->Backup_Size == 0) {
1428 DataManager::SetValue("tw_has_boot_partition", 0);
1429 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1430 } else
1431 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001432 }
Dees_Troyaa9cc402012-10-13 12:14:05 -04001433 } else {
1434 // Handle unmountable partitions in case we reset defaults
1435 if ((*iter)->Mount_Point == "/boot") {
1436 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1437 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1438 if ((*iter)->Backup_Size == 0) {
1439 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1440 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1441 } else
1442 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1443 } else if ((*iter)->Mount_Point == "/recovery") {
1444 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1445 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1446 if ((*iter)->Backup_Size == 0) {
1447 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1448 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1449 } else
1450 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001451 } else if ((*iter)->Mount_Point == "/data") {
1452 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001453 }
Dees_Troy51127312012-09-08 13:08:49 -04001454 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001455 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001456 gui_msg("update_part_details_done=...done");
Dees_Troy51127312012-09-08 13:08:49 -04001457 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1458 string current_storage_path = DataManager::GetCurrentStoragePath();
1459 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001460 if (FreeStorage != NULL) {
1461 // Attempt to mount storage
1462 if (!FreeStorage->Mount(false)) {
Matt Mower2d50cad2015-12-03 22:26:55 -06001463 gui_msg(Msg(msg::kError, "unable_to_mount_storage=Unable to mount storage"));
1464 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
Dees_Troy8170a922012-09-18 15:40:25 -04001465 } else {
1466 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1467 }
1468 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001469 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001470 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001471 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001472 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001473 return;
1474}
1475
1476int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001477#ifdef TW_INCLUDE_CRYPTO
1478 int ret_val, password_len;
Ethan Yonkera1de1492015-11-04 11:58:27 -06001479 char crypto_state[PROPERTY_VALUE_MAX], crypto_blkdev[PROPERTY_VALUE_MAX], cPassword[255];
Dees_Troy5bf43922012-09-07 16:07:55 -04001480 size_t result;
Ethan Yonker253368a2014-11-25 15:00:52 -06001481 std::vector<TWPartition*>::iterator iter;
Dees_Troy5bf43922012-09-07 16:07:55 -04001482
Ethan Yonker253368a2014-11-25 15:00:52 -06001483 // Mount any partitions that need to be mounted for decrypt
1484 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1485 if ((*iter)->Mount_To_Decrypt) {
1486 (*iter)->Mount(true);
1487 }
a39552696ff55ce2013-01-08 16:14:56 +00001488 }
a39552696ff55ce2013-01-08 16:14:56 +00001489
Ethan Yonkera1de1492015-11-04 11:58:27 -06001490 property_get("ro.crypto.state", crypto_state, "error");
1491 if (strcmp(crypto_state, "error") == 0) {
1492 property_set("ro.crypto.state", "encrypted");
1493 // Sleep for a bit so that services can start if needed
1494 sleep(1);
1495 }
1496
Dees_Troy5bf43922012-09-07 16:07:55 -04001497 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001498 int pwret = cryptfs_check_passwd(cPassword);
1499
Ethan Yonker253368a2014-11-25 15:00:52 -06001500 // Unmount any partitions that were needed for decrypt
1501 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1502 if ((*iter)->Mount_To_Decrypt) {
1503 (*iter)->UnMount(false);
1504 }
1505 }
1506
a39552696ff55ce2013-01-08 16:14:56 +00001507 if (pwret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001508 gui_err("fail_decrypt=Failed to decrypt data.");
Dees_Troy5bf43922012-09-07 16:07:55 -04001509 return -1;
1510 }
a39552696ff55ce2013-01-08 16:14:56 +00001511
Dees_Troy5bf43922012-09-07 16:07:55 -04001512 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1513 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001514 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001515 } else {
1516 TWPartition* dat = Find_Partition_By_Path("/data");
1517 if (dat != NULL) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001518 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1519 dat->Is_Decrypted = true;
1520 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001521 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001522 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 -06001523 gui_msg(Msg("decrypt_success_dev=Data successfully decrypted, new block device: '{1}'")(crypto_blkdev));
a39552696ff55ce2013-01-08 16:14:56 +00001524
Dees_Troy5bf43922012-09-07 16:07:55 -04001525 // Sleep for a bit so that the device will be ready
1526 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001527 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001528 dat->Storage_Path = "/data/media/0";
1529 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001530 DataManager::SetValue("tw_storage_path", "/data/media/0");
Ethan Yonkerec0fa4a2014-11-20 09:51:03 -06001531 DataManager::SetValue("tw_settings_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001532 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001533 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001534 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001535 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001536 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001537 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001538 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001539 }
1540 return 0;
1541#else
Ethan Yonker74db1572015-10-28 12:44:49 -05001542 gui_err("no_crypto_support=No crypto support was compiled into this build.");
Dees_Troy5bf43922012-09-07 16:07:55 -04001543 return -1;
1544#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001545 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001546}
1547
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001548int TWPartitionManager::Fix_Contexts(void) {
thata3d31fb2014-12-21 22:27:40 +01001549#ifdef HAVE_SELINUX
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001550 std::vector<TWPartition*>::iterator iter;
1551 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1552 if ((*iter)->Has_Data_Media) {
1553 if ((*iter)->Mount(true)) {
1554 if (fixContexts::fixDataMediaContexts((*iter)->Mount_Point) != 0)
1555 return -1;
1556 }
1557 }
1558 }
Dees_Troy1a650e62012-10-19 20:54:32 -04001559 UnMount_Main_Partitions();
Ethan Yonker74db1572015-10-28 12:44:49 -05001560 gui_msg("done=Done.");
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001561 return 0;
1562#else
1563 LOGERR("Cannot fix contexts, no selinux support present.\n");
1564 return -1;
1565#endif
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001566}
1567
Ethan Yonker66a19492015-12-10 10:19:45 -06001568TWPartition* TWPartitionManager::Find_Next_Storage(string Path, bool Exclude_Data_Media) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001569 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1570
1571 if (!Path.empty()) {
1572 string Search_Path = TWFunc::Get_Root_Path(Path);
1573 for (; iter != Partitions.end(); iter++) {
Matt Mower9a561dd2016-02-22 21:25:51 -06001574 if ((*iter)->Mount_Point == Search_Path) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001575 iter++;
1576 break;
1577 }
1578 }
1579 }
1580
1581 for (; iter != Partitions.end(); iter++) {
Ethan Yonker66a19492015-12-10 10:19:45 -06001582 if (Exclude_Data_Media && (*iter)->Has_Data_Media) {
1583 // do nothing, do not return this type of partition
1584 } else if ((*iter)->Is_Storage && (*iter)->Is_Present) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001585 return (*iter);
1586 }
1587 }
1588
1589 return NULL;
1590}
1591
Dees_Troyd21618c2012-10-14 18:48:49 -04001592int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001593 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1594
1595 if (Part == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001596 LOGINFO("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
1597 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 -04001598 return false;
1599 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001600 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1601 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001602 return false;
1603
Dees_Troy2673cec2013-04-02 20:22:16 +00001604 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1605 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001606 return false;
1607 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001608 return true;
1609}
1610
Dees_Troy8170a922012-09-18 15:40:25 -04001611int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001612 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001613 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001614 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001615
Dees_Troy8170a922012-09-18 15:40:25 -04001616 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001617 string Lun_File_str = CUSTOM_LUN_FILE;
1618 size_t found = Lun_File_str.find("%");
1619 if (found != string::npos) {
1620 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1621 if (TWFunc::Path_Exists(lun_file))
1622 has_multiple_lun = true;
1623 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001624 mtp_was_enabled = TWFunc::Toggle_MTP(false); // Must disable MTP for USB Storage
Ethan Yonker47360be2014-04-01 10:34:34 -05001625 if (!has_multiple_lun) {
1626 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1627 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1628 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
Ethan Yonker66a19492015-12-10 10:19:45 -06001629 TWPartition* Mount = Find_Next_Storage("", true);
Ethan Yonker47360be2014-04-01 10:34:34 -05001630 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001631 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1632 goto error_handle;
1633 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001634 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001635 gui_err("unable_locate_storage=Unable to locate storage device.");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001636 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001637 }
1638 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001639 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001640 }
Dees_Troy8170a922012-09-18 15:40:25 -04001641 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001642 LOGINFO("Device has multiple lun files\n");
1643 TWPartition* Mount1;
1644 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001645 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker66a19492015-12-10 10:19:45 -06001646 Mount1 = Find_Next_Storage("", true);
Ethan Yonker47360be2014-04-01 10:34:34 -05001647 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001648 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1649 goto error_handle;
1650 }
Matt Moweree717062014-04-26 01:46:46 -05001651 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker66a19492015-12-10 10:19:45 -06001652 Mount2 = Find_Next_Storage(Mount1->Mount_Point, true);
Matt Mower1fdcdb72016-01-25 20:53:47 -06001653 if (Mount2 && Mount2->Mount_Point != Mount1->Mount_Point) {
Matt Moweree717062014-04-26 01:46:46 -05001654 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001655 }
1656 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001657 gui_err("unable_locate_storage=Unable to locate storage device.");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001658 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001659 }
Dees_Troy8170a922012-09-18 15:40:25 -04001660 }
Matt Mowerb6ef4782014-11-06 02:24:36 -06001661 property_set("sys.storage.ums_enabled", "1");
HandyMenny37d42992014-10-26 22:15:59 +01001662 property_set("sys.usb.config", "mass_storage,adb");
Dees_Troy8170a922012-09-18 15:40:25 -04001663 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001664error_handle:
1665 if (mtp_was_enabled)
1666 if (!Enable_MTP())
1667 Disable_MTP();
1668 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001669}
1670
1671int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001672 int index, ret;
1673 char lun_file[255], ch[2] = {0, 0};
1674 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001675
1676 for (index=0; index<2; index++) {
1677 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001678 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001679 if (ret < 0) {
1680 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001681 }
Dees_Troy8170a922012-09-18 15:40:25 -04001682 }
Dees_Troye58d5262012-09-21 12:27:57 -04001683 Mount_All_Storage();
1684 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001685 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001686 property_set("sys.storage.ums_enabled", "0");
HandyMenny37d42992014-10-26 22:15:59 +01001687 property_set("sys.usb.config", "adb");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001688 if (mtp_was_enabled)
1689 if (!Enable_MTP())
1690 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001691 if (ret < 0 && index == 0) {
1692 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1693 return false;
1694 } else {
1695 return true;
1696 }
Dees_Troy8170a922012-09-18 15:40:25 -04001697 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001698}
1699
1700void TWPartitionManager::Mount_All_Storage(void) {
1701 std::vector<TWPartition*>::iterator iter;
1702
1703 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1704 if ((*iter)->Is_Storage)
1705 (*iter)->Mount(false);
1706 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001707}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001708
Dees_Troyd0384ef2012-10-12 12:15:42 -04001709void TWPartitionManager::UnMount_Main_Partitions(void) {
1710 // Unmounts system and data if data is not data/media
1711 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001712 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001713
1714 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1715
1716 UnMount_By_Path("/system", true);
Ethan Yonker6277c792014-09-15 14:54:30 -05001717 if (!datamedia)
1718 UnMount_By_Path("/data", true);
1719
Dees_Troyd0384ef2012-10-12 12:15:42 -04001720 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1721 Boot_Partition->UnMount(true);
1722}
1723
Dees_Troy9350b8d2012-09-27 12:38:38 -04001724int TWPartitionManager::Partition_SDCard(void) {
1725 char mkdir_path[255], temp[255], line[512];
Ethan Yonker483e9f42016-01-11 22:21:18 -06001726 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 -04001727 int ext, swap, total_size = 0, fat_size;
Dees_Troy9350b8d2012-09-27 12:38:38 -04001728
Ethan Yonker74db1572015-10-28 12:44:49 -05001729 gui_msg("start_partition_sd=Partitioning SD Card...");
Ethan Yonker483e9f42016-01-11 22:21:18 -06001730
1731 // Locate and validate device to partition
1732 TWPartition* SDCard = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
1733
Ethan Yonker66a19492015-12-10 10:19:45 -06001734 if (SDCard->Is_Adopted_Storage)
1735 SDCard->Revert_Adopted();
1736
Ethan Yonker1eff6cd2014-09-15 13:30:42 -05001737 if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001738 gui_err("partition_sd_locate=Unable to locate device to partition.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001739 return false;
1740 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001741
1742 // Unmount everything
Dees_Troy9350b8d2012-09-27 12:38:38 -04001743 if (!SDCard->UnMount(true))
1744 return false;
1745 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1746 if (SDext != NULL) {
1747 if (!SDext->UnMount(true))
1748 return false;
1749 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001750 char* swappath = getenv("SWAPPATH");
1751 if (swappath != NULL) {
1752 LOGINFO("Unmounting swap at '%s'\n", swappath);
1753 umount(swappath);
1754 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001755
Ethan Yonker483e9f42016-01-11 22:21:18 -06001756 // Determine block device
1757 if (SDCard->Alternate_Block_Device.empty()) {
1758 SDCard->Find_Actual_Block_Device();
1759 Device = SDCard->Actual_Block_Device;
1760 // Just use the root block device
1761 Device.resize(strlen("/dev/block/mmcblkX"));
1762 } else {
1763 Device = SDCard->Alternate_Block_Device;
1764 }
Dees_Troy9350b8d2012-09-27 12:38:38 -04001765
1766 // Find the size of the block device:
Ethan Yonker483e9f42016-01-11 22:21:18 -06001767 total_size = (int)(TWFunc::IOCTL_Get_Block_Size(Device.c_str()) / (1048576));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001768
1769 DataManager::GetValue("tw_sdext_size", ext);
1770 DataManager::GetValue("tw_swap_size", swap);
1771 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1772 fat_size = total_size - ext - swap;
Ethan Yonker483e9f42016-01-11 22:21:18 -06001773 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);
1774
1775 // Determine partition sizes
1776 if (swap == 0 && ext == 0) {
1777 fat_str = "-0";
1778 } else {
1779 memset(temp, 0, sizeof(temp));
1780 sprintf(temp, "%i", fat_size);
1781 fat_str = temp;
1782 fat_str += "MB";
1783 }
1784 if (swap == 0) {
1785 ext_str = "-0";
1786 } else {
1787 memset(temp, 0, sizeof(temp));
1788 sprintf(temp, "%i", ext);
1789 ext_str = "+";
1790 ext_str += temp;
1791 ext_str += "MB";
1792 }
1793
Dees_Troy9350b8d2012-09-27 12:38:38 -04001794 if (ext + swap > total_size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001795 gui_err("ext_swap_size=EXT + Swap size is larger than sdcard size.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001796 return false;
1797 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001798
Ethan Yonker74db1572015-10-28 12:44:49 -05001799 gui_msg("remove_part_table=Removing partition table...");
Ethan Yonker483e9f42016-01-11 22:21:18 -06001800 Command = "sgdisk --zap-all " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001801 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001802 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001803 gui_err("unable_rm_part=Unable to remove partition table.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001804 Update_System_Details();
1805 return false;
1806 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001807 gui_msg(Msg("create_part=Creating {1} partition...")("FAT32"));
Captain Throwback9643a802016-05-27 11:44:51 -04001808 Command = "sgdisk --new=0:0:" + fat_str + " --change-name=0:\"Microsoft basic data\" --typecode=0:EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001809 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001810 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001811 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("FAT32"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001812 return false;
1813 }
1814 if (ext > 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001815 gui_msg(Msg("create_part=Creating {1} partition...")("EXT"));
Ethan Yonker483e9f42016-01-11 22:21:18 -06001816 Command = "sgdisk --new=0:0:" + ext_str + " --change-name=0:\"Linux filesystem\" " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001817 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001818 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001819 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("EXT"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001820 Update_System_Details();
1821 return false;
1822 }
1823 }
1824 if (swap > 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001825 gui_msg(Msg("create_part=Creating {1} partition...")("swap"));
Ethan Yonker483e9f42016-01-11 22:21:18 -06001826 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 +00001827 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001828 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001829 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("swap"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001830 Update_System_Details();
1831 return false;
1832 }
1833 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001834
1835 // Convert GPT to MBR
1836 Command = "sgdisk --gpttombr " + Device;
1837 if (TWFunc::Exec_Cmd(Command) != 0)
1838 LOGINFO("Failed to covert partition GPT to MBR\n");
1839
1840 // Tell the kernel to rescan the partition table
1841 int fd = open(Device.c_str(), O_RDONLY);
1842 ioctl(fd, BLKRRPART, 0);
1843 close(fd);
1844
1845 string format_device = Device;
1846 if (Device.substr(0, 17) == "/dev/block/mmcblk")
1847 format_device += "p";
1848
1849 // Format new partitions to proper file system
1850 if (fat_size > 0) {
1851 Command = "mkfs.fat " + format_device + "1";
1852 TWFunc::Exec_Cmd(Command);
1853 }
Dees_Troy9350b8d2012-09-27 12:38:38 -04001854 if (ext > 0) {
1855 if (SDext == NULL) {
Ethan Yonker483e9f42016-01-11 22:21:18 -06001856 Command = "mke2fs -t " + ext_format + " -m 0 " + format_device + "2";
1857 gui_msg(Msg("format_sdext_as=Formatting sd-ext as {1}...")(ext_format));
1858 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
1859 TWFunc::Exec_Cmd(Command);
1860 } else {
1861 SDext->Wipe(ext_format);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001862 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06001863 }
1864 if (swap > 0) {
1865 Command = "mkswap " + format_device;
1866 if (ext > 0)
1867 Command += "3";
1868 else
1869 Command += "2";
Vojtech Bocek05534202013-09-11 08:11:56 +02001870 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001871 }
1872
Ethan Yonker483e9f42016-01-11 22:21:18 -06001873 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1874 if (SDCard->Mount(true)) {
1875 string TWRP_Folder = SDCard->Mount_Point + "/TWRP";
1876 mkdir(TWRP_Folder.c_str(), 0777);
1877 DataManager::Flush();
1878 }
1879
Dees_Troy9350b8d2012-09-27 12:38:38 -04001880 Update_System_Details();
Ethan Yonker74db1572015-10-28 12:44:49 -05001881 gui_msg("part_complete=Partitioning complete.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001882 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001883}
Dees_Troya13d74f2013-03-24 08:54:55 -05001884
1885void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1886 std::vector<TWPartition*>::iterator iter;
1887 if (ListType == "mount") {
1888 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1889 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1890 struct PartitionList part;
1891 part.Display_Name = (*iter)->Display_Name;
1892 part.Mount_Point = (*iter)->Mount_Point;
1893 part.selected = (*iter)->Is_Mounted();
1894 Partition_List->push_back(part);
1895 }
1896 }
1897 } else if (ListType == "storage") {
1898 char free_space[255];
1899 string Current_Storage = DataManager::GetCurrentStoragePath();
1900 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1901 if ((*iter)->Is_Storage) {
1902 struct PartitionList part;
1903 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1904 part.Display_Name = (*iter)->Storage_Name + " (";
1905 part.Display_Name += free_space;
1906 part.Display_Name += "MB)";
1907 part.Mount_Point = (*iter)->Storage_Path;
1908 if ((*iter)->Storage_Path == Current_Storage)
1909 part.selected = 1;
1910 else
1911 part.selected = 0;
1912 Partition_List->push_back(part);
1913 }
1914 }
1915 } else if (ListType == "backup") {
1916 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001917 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001918 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001919 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001920 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001921 Backup_Size = (*iter)->Backup_Size;
1922 if ((*iter)->Has_SubPartition) {
1923 std::vector<TWPartition*>::iterator subpart;
1924
1925 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1926 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1927 Backup_Size += (*subpart)->Backup_Size;
1928 }
1929 }
1930 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001931 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1932 part.Display_Name += backup_size;
1933 part.Display_Name += "MB)";
1934 part.Mount_Point = (*iter)->Backup_Path;
1935 part.selected = 0;
1936 Partition_List->push_back(part);
1937 }
1938 }
1939 } else if (ListType == "restore") {
1940 string Restore_List, restore_path;
1941 TWPartition* restore_part = NULL;
1942
1943 DataManager::GetValue("tw_restore_list", Restore_List);
1944 if (!Restore_List.empty()) {
1945 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1946 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1947 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05001948 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00001949 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001950 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05001951 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05001952 } else {
1953 struct PartitionList part;
1954 part.Display_Name = restore_part->Backup_Display_Name;
1955 part.Mount_Point = restore_part->Backup_Path;
1956 part.selected = 1;
1957 Partition_List->push_back(part);
1958 }
1959 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001960 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -05001961 }
1962 start_pos = end_pos + 1;
1963 end_pos = Restore_List.find(";", start_pos);
1964 }
1965 }
1966 } else if (ListType == "wipe") {
1967 struct PartitionList dalvik;
Ethan Yonker74db1572015-10-28 12:44:49 -05001968 dalvik.Display_Name = gui_parse_text("{@dalvik}");
Dees_Troya13d74f2013-03-24 08:54:55 -05001969 dalvik.Mount_Point = "DALVIK";
1970 dalvik.selected = 0;
1971 Partition_List->push_back(dalvik);
1972 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1973 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1974 struct PartitionList part;
1975 part.Display_Name = (*iter)->Display_Name;
1976 part.Mount_Point = (*iter)->Mount_Point;
1977 part.selected = 0;
1978 Partition_List->push_back(part);
1979 }
1980 if ((*iter)->Has_Android_Secure) {
1981 struct PartitionList part;
1982 part.Display_Name = (*iter)->Backup_Display_Name;
1983 part.Mount_Point = (*iter)->Backup_Path;
1984 part.selected = 0;
1985 Partition_List->push_back(part);
1986 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001987 if ((*iter)->Has_Data_Media) {
1988 struct PartitionList datamedia;
1989 datamedia.Display_Name = (*iter)->Storage_Name;
1990 datamedia.Mount_Point = "INTERNAL";
1991 datamedia.selected = 0;
1992 Partition_List->push_back(datamedia);
1993 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001994 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06001995 } else if (ListType == "flashimg") {
1996 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1997 if ((*iter)->Can_Flash_Img && (*iter)->Is_Present) {
1998 struct PartitionList part;
1999 part.Display_Name = (*iter)->Backup_Display_Name;
2000 part.Mount_Point = (*iter)->Backup_Path;
2001 part.selected = 0;
2002 Partition_List->push_back(part);
2003 }
2004 }
Dees_Troya13d74f2013-03-24 08:54:55 -05002005 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00002006 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05002007 }
2008}
2009
2010int TWPartitionManager::Fstab_Processed(void) {
2011 return Partitions.size();
2012}
Dees_Troyd93bda52013-07-03 19:55:19 +00002013
2014void TWPartitionManager::Output_Storage_Fstab(void) {
2015 std::vector<TWPartition*>::iterator iter;
2016 char storage_partition[255];
2017 string Temp;
2018 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
2019
2020 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002021 gui_msg(Msg(msg::kError, "unable_to_open=Unable to open '{1}'.")("/cache/recovery/storage.fstab"));
Dees_Troyd93bda52013-07-03 19:55:19 +00002022 return;
2023 }
2024
2025 // Iterate through all partitions
2026 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2027 if ((*iter)->Is_Storage) {
2028 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
2029 strcpy(storage_partition, Temp.c_str());
2030 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
2031 }
2032 }
2033 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002034}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02002035
2036TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
2037{
2038 TWPartition *res = NULL;
2039 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
2040 if(!(*iter)->Is_Storage)
2041 continue;
2042
2043 if((*iter)->Is_Settings_Storage)
2044 return *iter;
2045
2046 if(!res)
2047 res = *iter;
2048 }
2049 return res;
2050}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002051
2052bool TWPartitionManager::Enable_MTP(void) {
2053#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002054 if (mtppid) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002055 gui_err("mtp_already_enabled=MTP already enabled");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002056 return true;
2057 }
2058 //Launch MTP Responder
2059 LOGINFO("Starting MTP\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06002060
2061 int mtppipe[2];
2062
2063 if (pipe(mtppipe) < 0) {
2064 LOGERR("Error creating MTP pipe\n");
2065 return false;
2066 }
2067
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, "error") != 0 && strcmp(old_value, "mtp,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.mtpadb", product, "4EE2");
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 property_set("sys.usb.config", "mtp,adb");
2081 }
Ethan Yonker6d154c42014-09-03 14:19:43 -05002082 /* To enable MTP debug, use the twrp command line feature to
2083 * twrp set tw_mtp_debug 1
2084 */
2085 twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug"));
Ethan Yonker1b039202015-01-30 10:08:48 -06002086 mtppid = mtp->forkserver(mtppipe);
2087 if (mtppid) {
2088 close(mtppipe[0]); // Host closes read side
2089 mtp_write_fd = mtppipe[1];
2090 DataManager::SetValue("tw_mtp_enabled", 1);
2091 Add_All_MTP_Storage();
2092 return true;
Ethan Yonker726a0202014-12-16 20:01:38 -06002093 } else {
2094 close(mtppipe[0]);
2095 close(mtppipe[1]);
Ethan Yonker74db1572015-10-28 12:44:49 -05002096 gui_err("mtp_fail=Failed to enable MTP");
Ethan Yonker1b039202015-01-30 10:08:48 -06002097 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002098 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002099#else
Ethan Yonker74db1572015-10-28 12:44:49 -05002100 gui_err("no_mtp=MTP support not included");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002101#endif
2102 DataManager::SetValue("tw_mtp_enabled", 0);
2103 return false;
2104}
2105
Ethan Yonker1b039202015-01-30 10:08:48 -06002106void TWPartitionManager::Add_All_MTP_Storage(void) {
2107#ifdef TW_HAS_MTP
2108 std::vector<TWPartition*>::iterator iter;
2109
2110 if (!mtppid)
2111 return; // MTP is not enabled
2112
2113 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2114 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false))
2115 Add_Remove_MTP_Storage((*iter), MTP_MESSAGE_ADD_STORAGE);
2116 }
2117#else
2118 return;
2119#endif
2120}
2121
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002122bool TWPartitionManager::Disable_MTP(void) {
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002123 char old_value[PROPERTY_VALUE_MAX];
2124 property_get("sys.usb.config", old_value, "error");
2125 if (strcmp(old_value, "adb") != 0) {
2126 char vendor[PROPERTY_VALUE_MAX];
2127 char product[PROPERTY_VALUE_MAX];
2128 property_set("sys.usb.config", "none");
2129 property_get("usb.vendor", vendor, "18D1");
2130 property_get("usb.product.adb", product, "D002");
2131 string vendorstr = vendor;
2132 string productstr = product;
2133 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2134 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
2135 usleep(2000);
2136 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002137#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002138 if (mtppid) {
Ethan Yonker8613dc02014-09-11 09:28:20 -05002139 LOGINFO("Disabling MTP\n");
2140 int status;
2141 kill(mtppid, SIGKILL);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002142 mtppid = 0;
Ethan Yonker8613dc02014-09-11 09:28:20 -05002143 // We don't care about the exit value, but this prevents a zombie process
2144 waitpid(mtppid, &status, 0);
Ethan Yonker726a0202014-12-16 20:01:38 -06002145 close(mtp_write_fd);
2146 mtp_write_fd = -1;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002147 }
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002148#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002149 property_set("sys.usb.config", "adb");
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002150#ifdef TW_HAS_MTP
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002151 DataManager::SetValue("tw_mtp_enabled", 0);
2152 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002153#endif
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002154 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002155}
Ethan Yonker726a0202014-12-16 20:01:38 -06002156
2157TWPartition* TWPartitionManager::Find_Partition_By_MTP_Storage_ID(unsigned int Storage_ID) {
2158 std::vector<TWPartition*>::iterator iter;
2159
2160 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2161 if ((*iter)->MTP_Storage_ID == Storage_ID)
2162 return (*iter);
2163 }
2164 return NULL;
2165}
2166
2167bool TWPartitionManager::Add_Remove_MTP_Storage(TWPartition* Part, int message_type) {
2168#ifdef TW_HAS_MTP
2169 struct mtpmsg mtp_message;
2170
2171 if (!mtppid)
2172 return false; // MTP is disabled
2173
2174 if (mtp_write_fd < 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002175 LOGINFO("MTP: mtp_write_fd is not set\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06002176 return false;
2177 }
2178
2179 if (Part) {
Ethan Yonker4bfabab2014-12-29 09:15:37 -06002180 if (Part->MTP_Storage_ID == 0)
2181 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002182 if (message_type == MTP_MESSAGE_REMOVE_STORAGE) {
2183 mtp_message.message_type = MTP_MESSAGE_REMOVE_STORAGE; // Remove
2184 LOGINFO("sending message to remove %i\n", Part->MTP_Storage_ID);
2185 mtp_message.storage_id = Part->MTP_Storage_ID;
2186 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002187 LOGINFO("error sending message to remove storage %i\n", Part->MTP_Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002188 return false;
2189 } else {
2190 LOGINFO("Message sent, remove storage ID: %i\n", Part->MTP_Storage_ID);
2191 return true;
2192 }
2193 } else if (message_type == MTP_MESSAGE_ADD_STORAGE && Part->Is_Mounted()) {
2194 mtp_message.message_type = MTP_MESSAGE_ADD_STORAGE; // Add
2195 mtp_message.storage_id = Part->MTP_Storage_ID;
2196 mtp_message.path = Part->Storage_Path.c_str();
2197 mtp_message.display = Part->Storage_Name.c_str();
2198 mtp_message.maxFileSize = Part->Get_Max_FileSize();
Ethan Yonker1b039202015-01-30 10:08:48 -06002199 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 -06002200 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002201 LOGINFO("error sending message to add storage %i\n", Part->MTP_Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002202 return false;
2203 } else {
2204 LOGINFO("Message sent, add storage ID: %i\n", Part->MTP_Storage_ID);
2205 return true;
2206 }
2207 } else {
2208 LOGERR("Unknown MTP message type: %i\n", message_type);
2209 }
2210 } else {
2211 // This hopefully never happens as the error handling should
2212 // occur in the calling function.
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002213 LOGINFO("TWPartitionManager::Add_Remove_MTP_Storage NULL partition given\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06002214 }
2215 return true;
2216#else
Ethan Yonker74db1572015-10-28 12:44:49 -05002217 gui_err("no_mtp=MTP support not included");
Ethan Yonker726a0202014-12-16 20:01:38 -06002218 DataManager::SetValue("tw_mtp_enabled", 0);
2219 return false;
2220#endif
2221}
2222
2223bool TWPartitionManager::Add_MTP_Storage(string Mount_Point) {
2224#ifdef TW_HAS_MTP
2225 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2226 if (Part) {
2227 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2228 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002229 LOGINFO("TWFunc::Add_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -06002230 }
2231#endif
2232 return false;
2233}
2234
2235bool TWPartitionManager::Add_MTP_Storage(unsigned int Storage_ID) {
2236#ifdef TW_HAS_MTP
2237 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2238 if (Part) {
2239 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2240 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002241 LOGINFO("TWFunc::Add_MTP_Storage unable to locate partition for %i\n", Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002242 }
2243#endif
2244 return false;
2245}
2246
2247bool TWPartitionManager::Remove_MTP_Storage(string Mount_Point) {
2248#ifdef TW_HAS_MTP
2249 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2250 if (Part) {
2251 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2252 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002253 LOGINFO("TWFunc::Remove_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -06002254 }
2255#endif
2256 return false;
2257}
2258
2259bool TWPartitionManager::Remove_MTP_Storage(unsigned int Storage_ID) {
2260#ifdef TW_HAS_MTP
2261 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2262 if (Part) {
2263 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2264 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002265 LOGINFO("TWFunc::Remove_MTP_Storage unable to locate partition for %i\n", Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002266 }
2267#endif
2268 return false;
2269}
Ethan Yonker96af84a2015-01-05 14:58:36 -06002270
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002271bool TWPartitionManager::Flash_Image(string& path, string& filename) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002272 int check, partition_count = 0;
2273 TWPartition* flash_part = NULL;
bigbiffce8f83c2015-12-12 18:30:21 -05002274 string Flash_List, flash_path, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002275 size_t start_pos = 0, end_pos = 0;
2276
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002277 full_filename = path + "/" + filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002278
bigbiffce8f83c2015-12-12 18:30:21 -05002279 gui_msg("image_flash_start=[IMAGE FLASH STARTED]");
2280 gui_msg(Msg("img_to_flash=Image to flash: '{1}'")(full_filename));
2281
2282 if (!TWFunc::Path_Exists(full_filename)) {
2283 if (!Mount_By_Path(full_filename, true)) {
Ethan Yonkerb78fbdf2016-01-15 16:46:35 -06002284 return false;
2285 }
bigbiffce8f83c2015-12-12 18:30:21 -05002286 if (!TWFunc::Path_Exists(full_filename)) {
2287 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(full_filename));
Ethan Yonkerb78fbdf2016-01-15 16:46:35 -06002288 return false;
2289 }
2290 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002291
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002292 PartitionSettings part_settings;
2293 part_settings.Backup_Folder = path;
2294 unsigned long long total_bytes = TWFunc::Get_File_Size(full_filename);
2295 ProgressTracking progress(total_bytes);
2296 part_settings.progress = &progress;
2297 part_settings.adbbackup = false;
2298 part_settings.PM_Method = PM_RESTORE;
2299
Ethan Yonker74db1572015-10-28 12:44:49 -05002300 gui_msg("calc_restore=Calculating restore details...");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002301 DataManager::GetValue("tw_flash_partition", Flash_List);
2302 if (!Flash_List.empty()) {
2303 end_pos = Flash_List.find(";", start_pos);
2304 while (end_pos != string::npos && start_pos < Flash_List.size()) {
2305 flash_path = Flash_List.substr(start_pos, end_pos - start_pos);
2306 flash_part = Find_Partition_By_Path(flash_path);
2307 if (flash_part != NULL) {
2308 partition_count++;
2309 if (partition_count > 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002310 gui_err("too_many_flash=Too many partitions selected for flashing.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002311 return false;
2312 }
2313 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002314 gui_msg(Msg(msg::kError, "flash_unable_locate=Unable to locate '{1}' partition for flashing.")(flash_path));
Ethan Yonker96af84a2015-01-05 14:58:36 -06002315 return false;
2316 }
2317 start_pos = end_pos + 1;
2318 end_pos = Flash_List.find(";", start_pos);
2319 }
2320 }
2321
2322 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002323 gui_err("no_part_flash=No partitions selected for flashing.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002324 return false;
2325 }
2326
2327 DataManager::SetProgress(0.0);
2328 if (flash_part) {
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002329 flash_part->Backup_FileName = filename;
2330 if (!flash_part->Flash_Image(&part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002331 return false;
2332 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002333 gui_err("invalid_flash=Invalid flash partition specified.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002334 return false;
2335 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002336 gui_highlight("flash_done=IMAGE FLASH COMPLETED]");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002337 return true;
2338}
Ethan Yonker74db1572015-10-28 12:44:49 -05002339
2340void TWPartitionManager::Translate_Partition(const char* path, const char* resource_name, const char* default_value) {
2341 TWPartition* part = PartitionManager.Find_Partition_By_Path(path);
2342 if (part) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002343 if (part->Is_Adopted_Storage) {
2344 part->Display_Name = part->Display_Name + " - " + gui_lookup("data", "Data");
2345 part->Backup_Display_Name = part->Display_Name;
2346 part->Storage_Name = part->Storage_Name + " - " + gui_lookup("adopted_storage", "Adopted Storage");
2347 } else {
2348 part->Display_Name = gui_lookup(resource_name, default_value);
2349 part->Backup_Display_Name = part->Display_Name;
2350 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002351 }
2352}
2353
2354void TWPartitionManager::Translate_Partition(const char* path, const char* resource_name, const char* default_value, const char* storage_resource_name, const char* storage_default_value) {
2355 TWPartition* part = PartitionManager.Find_Partition_By_Path(path);
2356 if (part) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002357 if (part->Is_Adopted_Storage) {
2358 part->Display_Name = part->Display_Name + " - " + gui_lookup("data", "Data");
2359 part->Backup_Display_Name = part->Display_Name;
2360 part->Storage_Name = part->Storage_Name + " - " + gui_lookup("adopted_storage", "Adopted Storage");
2361 } else {
2362 part->Display_Name = gui_lookup(resource_name, default_value);
2363 part->Backup_Display_Name = part->Display_Name;
2364 if (part->Is_Storage)
2365 part->Storage_Name = gui_lookup(storage_resource_name, storage_default_value);
2366 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002367 }
2368}
2369
2370void TWPartitionManager::Translate_Partition_Display_Names() {
Ethan Yonker66a19492015-12-10 10:19:45 -06002371 LOGINFO("Translating partition display names\n");
Ethan Yonker74db1572015-10-28 12:44:49 -05002372 Translate_Partition("/system", "system", "System");
2373 Translate_Partition("/system_image", "system_image", "System Image");
2374 Translate_Partition("/vendor", "vendor", "Vendor");
2375 Translate_Partition("/vendor_image", "vendor_image", "Vendor Image");
2376 Translate_Partition("/cache", "cache", "Cache");
2377 Translate_Partition("/data", "data", "Data", "internal", "Internal Storage");
2378 Translate_Partition("/boot", "boot", "Boot");
2379 Translate_Partition("/recovery", "recovery", "Recovery");
2380 if (!datamedia) {
2381 Translate_Partition("/sdcard", "sdcard", "SDCard", "sdcard", "SDCard");
2382 Translate_Partition("/internal_sd", "sdcard", "SDCard", "sdcard", "SDCard");
2383 Translate_Partition("/internal_sdcard", "sdcard", "SDCard", "sdcard", "SDCard");
2384 Translate_Partition("/emmc", "sdcard", "SDCard", "sdcard", "SDCard");
2385 }
2386 Translate_Partition("/external_sd", "microsd", "Micro SDCard", "microsd", "Micro SDCard");
2387 Translate_Partition("/external_sdcard", "microsd", "Micro SDCard", "microsd", "Micro SDCard");
2388 Translate_Partition("/usb-otg", "usbotg", "USB OTG", "usbotg", "USB OTG");
2389 Translate_Partition("/sd-ext", "sdext", "SD-EXT");
2390
2391 // Android secure is a special case
2392 TWPartition* part = PartitionManager.Find_Partition_By_Path("/and-sec");
2393 if (part)
2394 part->Backup_Display_Name = gui_lookup("android_secure", "Android Secure");
2395
2396 // This updates the text on all of the storage selection buttons in the GUI
2397 DataManager::SetBackupFolder();
2398}
Ethan Yonker66a19492015-12-10 10:19:45 -06002399
2400void TWPartitionManager::Decrypt_Adopted() {
2401#ifdef TW_INCLUDE_CRYPTO
2402 if (!Mount_By_Path("/data", false)) {
2403 LOGERR("Cannot decrypt adopted storage because /data will not mount\n");
2404 return;
2405 }
2406 LOGINFO("Decrypt adopted storage starting\n");
2407 char* xmlFile = PageManager::LoadFileToBuffer("/data/system/storage.xml", NULL);
2408 xml_document<> *doc = NULL;
2409 xml_node<>* volumes = NULL;
2410 xml_node<>* volume = NULL;
2411 string Primary_Storage_UUID = "";
2412 if (xmlFile != NULL) {
2413 LOGINFO("successfully loaded storage.xml\n");
2414 doc = new xml_document<>();
2415 doc->parse<0>(xmlFile);
2416 volumes = doc->first_node("volumes");
2417 if (volumes) {
2418 xml_attribute<>* psuuid = volumes->first_attribute("primaryStorageUuid");
2419 if (psuuid) {
2420 Primary_Storage_UUID = psuuid->value();
2421 }
2422 }
2423 }
2424 std::vector<TWPartition*>::iterator adopt;
2425 for (adopt = Partitions.begin(); adopt != Partitions.end(); adopt++) {
2426 if ((*adopt)->Removable && (*adopt)->Is_Present) {
2427 if ((*adopt)->Decrypt_Adopted() == 0) {
2428 if (volumes) {
2429 xml_node<>* volume = volumes->first_node("volume");
2430 while (volume) {
2431 xml_attribute<>* guid = volume->first_attribute("partGuid");
2432 if (guid) {
2433 string GUID = (*adopt)->Adopted_GUID.c_str();
2434 GUID.insert(8, "-");
2435 GUID.insert(13, "-");
2436 GUID.insert(18, "-");
2437 GUID.insert(23, "-");
2438
2439 if (strcasecmp(GUID.c_str(), guid->value()) == 0) {
2440 xml_attribute<>* attr = volume->first_attribute("nickname");
2441 if (attr) {
2442 (*adopt)->Storage_Name = attr->value();
2443 (*adopt)->Display_Name = (*adopt)->Storage_Name;
2444 (*adopt)->Backup_Display_Name = (*adopt)->Storage_Name;
2445 LOGINFO("storage name from storage.xml is '%s'\n", attr->value());
2446 }
2447 attr = volume->first_attribute("fsUuid");
2448 if (attr && !Primary_Storage_UUID.empty() && strcmp(Primary_Storage_UUID.c_str(), attr->value()) == 0) {
2449 TWPartition* Dat = Find_Partition_By_Path("/data");
2450 if (Dat) {
2451 LOGINFO("Internal storage is found on adopted storage '%s'\n", (*adopt)->Display_Name.c_str());
2452 LOGINFO("Changing '%s' to point to '%s'\n", Dat->Symlink_Mount_Point.c_str(), (*adopt)->Storage_Path.c_str());
2453 (*adopt)->Symlink_Mount_Point = Dat->Symlink_Mount_Point;
2454 Dat->Symlink_Mount_Point = "";
2455 // Toggle mounts to ensure that the symlink mount point (probably /sdcard) is mounted to the right location
2456 Dat->UnMount(false);
2457 Dat->Mount(false);
2458 (*adopt)->UnMount(false);
2459 (*adopt)->Mount(false);
2460 Output_Partition((*adopt));
2461 }
2462 }
2463 break;
2464 }
2465 }
2466 volume = volume->next_sibling("volume");
2467 }
2468 }
2469 }
2470 }
2471 }
2472 if (xmlFile) {
2473 doc->clear();
2474 delete doc;
2475 free(xmlFile);
2476 }
2477#else
2478 LOGINFO("Decrypt_Adopted: no crypto support\n");
2479 return;
2480#endif
2481}
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002482
2483void TWPartitionManager::Remove_Partition_By_Path(string Path) {
2484 std::vector<TWPartition*>::iterator iter;
2485 string Local_Path = TWFunc::Get_Root_Path(Path);
2486
2487 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2488 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
2489 LOGINFO("Found and erasing '%s' from partition list\n", Local_Path.c_str());
2490 Partitions.erase(iter);
2491 return;
2492 }
2493 }
2494}
Ethan Yonker1b190162016-12-05 15:25:19 -06002495
2496void TWPartitionManager::Set_Active_Slot(const string& Slot) {
2497 if (Slot != "A" && Slot != "B") {
2498 LOGERR("Set_Active_Slot invalid slot '%s'\n", Slot.c_str());
2499 return;
2500 }
2501 if (Active_Slot_Display == Slot)
2502 return;
2503 LOGINFO("Setting active slot %s\n", Slot.c_str());
2504#ifdef AB_OTA_UPDATER
2505 if (!Active_Slot_Display.empty()) {
2506 const hw_module_t *hw_module;
2507 boot_control_module_t *module;
2508 int ret;
2509 ret = hw_get_module("bootctrl", &hw_module);
2510 if (ret != 0) {
2511 LOGERR("Error getting bootctrl module.\n");
2512 } else {
2513 module = (boot_control_module_t*) hw_module;
2514 module->init(module);
2515 int slot_number = 0;
2516 if (Slot == "B")
2517 slot_number = 1;
2518 if (module->setActiveBootSlot(module, slot_number))
2519 gui_msg(Msg(msg::kError, "unable_set_boot_slot=Error changing bootloader boot slot to {1}")(Slot));
2520 }
2521 DataManager::SetValue("tw_active_slot", Slot); // Doing this outside of this if block may result in a seg fault because the DataManager may not be ready yet
2522 }
2523#else
2524 LOGERR("Boot slot feature not present\n");
2525#endif
2526 Active_Slot_Display = Slot;
2527 if (Fstab_Processed())
2528 Update_System_Details();
2529}
2530string TWPartitionManager::Get_Active_Slot_Suffix() {
2531 if (Active_Slot_Display == "A")
2532 return "_a";
2533 return "_b";
2534}
2535string TWPartitionManager::Get_Active_Slot_Display() {
2536 return Active_Slot_Display;
2537}