blob: f75fcbc512125b79057801ca3f2d0be49894e765 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
24#include <unistd.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040025#include <vector>
Dees_Troy63c8df72012-09-10 14:02:05 -040026#include <dirent.h>
27#include <time.h>
Dees_Troy8170a922012-09-18 15:40:25 -040028#include <errno.h>
29#include <fcntl.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050030#include <iostream>
31#include <iomanip>
Ethan Yonker8613dc02014-09-11 09:28:20 -050032#include <sys/wait.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040033#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000034#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040036#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040037#include "twrp-functions.hpp"
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040038#include "fixPermissions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050039#include "twrpDigest.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050040#include "twrpDU.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040041
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040042#ifdef TW_HAS_MTP
43#include "mtp/mtp_MtpServer.hpp"
44#include "mtp/twrpMtp.hpp"
45#endif
46
Dees Troy6f6441d2014-01-23 02:07:03 +000047extern "C" {
48 #include "cutils/properties.h"
49}
50
Dees_Troy5bf43922012-09-07 16:07:55 -040051#ifdef TW_INCLUDE_CRYPTO
52 #ifdef TW_INCLUDE_JB_CRYPTO
53 #include "crypto/jb/cryptfs.h"
Ethan Yonker4eca40d2014-11-11 14:52:28 -060054 #elif defined(TW_INCLUDE_L_CRYPTO)
55 #include "crypto/lollipop/cryptfs.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040056 #else
57 #include "crypto/ics/cryptfs.h"
58 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -040059#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040060
Ethan Yonker6277c792014-09-15 14:54:30 -050061extern bool datamedia;
62
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050063TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040064 mtp_was_enabled = false;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050065}
66
Dees_Troy51a0e822012-09-05 15:24:24 -040067int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040068 FILE *fstabFile;
69 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +000070 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050071 TWPartition* andsec_partition = NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -040072
73 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
74 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000075 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040076 return false;
77 }
78
79 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
80 if (fstab_line[0] != '/')
81 continue;
82
Dees_Troy2a923582012-09-20 12:13:34 -040083 if (fstab_line[strlen(fstab_line) - 1] != '\n')
84 fstab_line[strlen(fstab_line)] = '\n';
that9e0593e2014-10-08 00:01:24 +020085 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040086 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040087 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040088
Dees_Troy5bf43922012-09-07 16:07:55 -040089 if (partition->Process_Fstab_Line(line, Display_Error)) {
Matt Mowered71fa32014-04-16 13:21:47 -050090 if (!settings_partition && partition->Is_Settings_Storage && partition->Is_Present) {
Dees Troy02a64532014-03-19 15:23:32 +000091 settings_partition = partition;
Dees_Troya13d74f2013-03-24 08:54:55 -050092 } else {
93 partition->Is_Settings_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050094 }
Matt Mowered71fa32014-04-16 13:21:47 -050095 if (!andsec_partition && partition->Has_Android_Secure && partition->Is_Present) {
Matt Mowerbf4efa32014-04-14 23:25:26 -050096 andsec_partition = partition;
97 } else {
98 partition->Has_Android_Secure = false;
99 }
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500100 Partitions.push_back(partition);
Dees_Troy5bf43922012-09-07 16:07:55 -0400101 } else {
102 delete partition;
103 }
104 }
105 fclose(fstabFile);
Ethan Yonker6277c792014-09-15 14:54:30 -0500106 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) {
107 // Attempt to automatically identify /data/media emulated storage devices
108 TWPartition* Dat = Find_Partition_By_Path("/data");
109 if (Dat) {
110 LOGINFO("Using automatic handling for /data/media emulated storage device.\n");
111 datamedia = true;
that9e0593e2014-10-08 00:01:24 +0200112 Dat->Setup_Data_Media();
Ethan Yonker6277c792014-09-15 14:54:30 -0500113 settings_partition = Dat;
114 }
115 }
Dees Troy02a64532014-03-19 15:23:32 +0000116 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500117 std::vector<TWPartition*>::iterator iter;
118 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
119 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000120 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500121 break;
122 }
123 }
Dees Troy02a64532014-03-19 15:23:32 +0000124 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000125 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500126 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400127 if (!Write_Fstab()) {
128 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000129 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400130 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000131 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400132 }
Matt Mowered71fa32014-04-16 13:21:47 -0500133
Matt Mowerbf4efa32014-04-14 23:25:26 -0500134 if (andsec_partition) {
135 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500136 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500137 Setup_Android_Secure_Location(settings_partition);
138 }
Matt Mowered71fa32014-04-16 13:21:47 -0500139 if (settings_partition) {
140 Setup_Settings_Storage_Partition(settings_partition);
141 }
Ethan Yonkercceebb82014-11-18 10:17:59 -0600142#ifdef TW_INCLUDE_L_CRYPTO
143 TWPartition* Decrypt_Data = Find_Partition_By_Path("/data");
144 if (Decrypt_Data && Decrypt_Data->Is_Encrypted && !Decrypt_Data->Is_Decrypted) {
145 int password_type = cryptfs_get_password_type();
146 if (password_type == CRYPT_TYPE_DEFAULT) {
147 LOGINFO("Device is encrypted with the default password, attempting to decrypt.\n");
148 if (Decrypt_Device("default_password") == 0) {
149 gui_print("Successfully decrypted with default password.\n");
150 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
151 } else {
152 LOGERR("Unable to decrypt with default password.");
153 LOGERR("You may need to perform a Format Data.\n");
154 }
155 } else {
156 DataManager::SetValue("TW_CRYPTO_TYPE", password_type);
157 }
158 }
159#endif
Dees_Troy51127312012-09-08 13:08:49 -0400160 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400161 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400162 return true;
163}
164
165int TWPartitionManager::Write_Fstab(void) {
166 FILE *fp;
167 std::vector<TWPartition*>::iterator iter;
168 string Line;
169
170 fp = fopen("/etc/fstab", "w");
171 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000172 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400173 return false;
174 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400175 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400176 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400177 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400178 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000179 }
180 // Handle subpartition tracking
181 if ((*iter)->Is_SubPartition) {
182 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
183 if (ParentPartition)
184 ParentPartition->Has_SubPartition = true;
185 else
186 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 -0400187 }
188 }
189 fclose(fp);
190 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400191}
192
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500193void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500194 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
195 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
196 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
197}
198
Matt Mowerbf4efa32014-04-14 23:25:26 -0500199void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
200 if (Part->Has_Android_Secure)
201 Part->Setup_AndSec();
Ethan Yonker6277c792014-09-15 14:54:30 -0500202 else if (!datamedia)
Matt Mowerbf4efa32014-04-14 23:25:26 -0500203 Part->Setup_AndSec();
Matt Mowerbf4efa32014-04-14 23:25:26 -0500204}
205
Dees_Troy8170a922012-09-18 15:40:25 -0400206void TWPartitionManager::Output_Partition_Logging(void) {
207 std::vector<TWPartition*>::iterator iter;
208
209 printf("\n\nPartition Logs:\n");
210 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
211 Output_Partition((*iter));
212}
213
214void TWPartitionManager::Output_Partition(TWPartition* Part) {
215 unsigned long long mb = 1048576;
216
Gary Peck004d48b2012-11-21 16:28:18 -0800217 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 -0400218 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800219 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 -0400220 }
Gary Peck004d48b2012-11-21 16:28:18 -0800221 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500222 if (Part->Can_Be_Mounted)
223 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800224 if (Part->Can_Be_Wiped)
225 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700226 if (Part->Use_Rm_Rf)
227 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500228 if (Part->Can_Be_Backed_Up)
229 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800230 if (Part->Wipe_During_Factory_Reset)
231 printf("Wipe_During_Factory_Reset ");
232 if (Part->Wipe_Available_in_GUI)
233 printf("Wipe_Available_in_GUI ");
234 if (Part->Is_SubPartition)
235 printf("Is_SubPartition ");
236 if (Part->Has_SubPartition)
237 printf("Has_SubPartition ");
238 if (Part->Removable)
239 printf("Removable ");
240 if (Part->Is_Present)
241 printf("IsPresent ");
242 if (Part->Can_Be_Encrypted)
243 printf("Can_Be_Encrypted ");
244 if (Part->Is_Encrypted)
245 printf("Is_Encrypted ");
246 if (Part->Is_Decrypted)
247 printf("Is_Decrypted ");
248 if (Part->Has_Data_Media)
249 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000250 if (Part->Can_Encrypt_Backup)
251 printf("Can_Encrypt_Backup ");
252 if (Part->Use_Userdata_Encryption)
253 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800254 if (Part->Has_Android_Secure)
255 printf("Has_Android_Secure ");
256 if (Part->Is_Storage)
257 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500258 if (Part->Is_Settings_Storage)
259 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000260 if (Part->Ignore_Blkid)
261 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000262 if (Part->Retain_Layout_Version)
263 printf("Retain_Layout_Version ");
Gary Peck004d48b2012-11-21 16:28:18 -0800264 printf("\n");
265 if (!Part->SubPartition_Of.empty())
266 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
267 if (!Part->Symlink_Path.empty())
268 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
269 if (!Part->Symlink_Mount_Point.empty())
270 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
271 if (!Part->Primary_Block_Device.empty())
272 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
273 if (!Part->Alternate_Block_Device.empty())
274 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
275 if (!Part->Decrypted_Block_Device.empty())
276 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
277 if (Part->Length != 0)
278 printf(" Length: %i\n", Part->Length);
279 if (!Part->Display_Name.empty())
280 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500281 if (!Part->Storage_Name.empty())
282 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800283 if (!Part->Backup_Path.empty())
284 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
285 if (!Part->Backup_Name.empty())
286 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500287 if (!Part->Backup_Display_Name.empty())
288 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800289 if (!Part->Backup_FileName.empty())
290 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
291 if (!Part->Storage_Path.empty())
292 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
293 if (!Part->Current_File_System.empty())
294 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
295 if (!Part->Fstab_File_System.empty())
296 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
297 if (Part->Format_Block_Size != 0)
298 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400299 if (!Part->MTD_Name.empty())
300 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400301 string back_meth = Part->Backup_Method_By_Name();
Ethan Yonker6277c792014-09-15 14:54:30 -0500302 printf(" Backup_Method: %s\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800303 if (Part->Mount_Flags || !Part->Mount_Options.empty())
304 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Ethan Yonker6277c792014-09-15 14:54:30 -0500305 printf("\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400306}
307
Dees_Troy51a0e822012-09-05 15:24:24 -0400308int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400309 std::vector<TWPartition*>::iterator iter;
310 int ret = false;
311 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400312 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400313
Dees_Troyd93bda52013-07-03 19:55:19 +0000314 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400315 return true;
316
Dees_Troy5bf43922012-09-07 16:07:55 -0400317 // Iterate through all partitions
318 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400319 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400320 ret = (*iter)->Mount(Display_Error);
321 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400322 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400323 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400324 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400325 }
326 if (found) {
327 return ret;
328 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000329 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400330 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000331 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400332 }
333 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400334}
335
Dees_Troy51a0e822012-09-05 15:24:24 -0400336int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400337 std::vector<TWPartition*>::iterator iter;
338 int ret = false;
339 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400340 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400341
342 // Iterate through all partitions
343 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400344 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400345 ret = (*iter)->UnMount(Display_Error);
346 found = true;
347 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
348 (*iter)->UnMount(Display_Error);
349 }
350 }
351 if (found) {
352 return ret;
353 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000354 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400355 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000356 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400357 }
358 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400359}
360
Dees_Troy51a0e822012-09-05 15:24:24 -0400361int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400362 TWPartition* Part = Find_Partition_By_Path(Path);
363
364 if (Part)
365 return Part->Is_Mounted();
366 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000367 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400368 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400369}
370
Dees_Troy5bf43922012-09-07 16:07:55 -0400371int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400372 string current_storage_path = DataManager::GetCurrentStoragePath();
373
374 if (Mount_By_Path(current_storage_path, Display_Error)) {
375 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
376 if (FreeStorage)
377 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
378 return true;
379 }
380 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400381}
382
Dees_Troy5bf43922012-09-07 16:07:55 -0400383int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
384 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
385}
386
387TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
388 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400389 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400390
391 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400392 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400393 return (*iter);
394 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400395 return NULL;
396}
397
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400398int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
399 // Check the backup name to ensure that it is the correct size and contains only valid characters
400 // and that a backup with that name doesn't already exist
401 char backup_name[MAX_BACKUP_NAME_LEN];
402 char backup_loc[255], tw_image_dir[255];
403 int copy_size;
404 int index, cur_char;
405 string Backup_Name, Backup_Loc;
406
407 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
408 copy_size = Backup_Name.size();
409 // Check size
410 if (copy_size > MAX_BACKUP_NAME_LEN) {
411 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000412 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400413 return -2;
414 }
415
416 // Check each character
417 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500418 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400419 return 0; // A "0" (zero) means to use the current timestamp for the backup name
420 for (index=0; index<copy_size; index++) {
421 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500422 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 -0400423 // These are valid characters
424 // Numbers
425 // Upper case letters
426 // Lower case letters
427 // Space
428 // and -_.{}[]
429 } else {
430 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000431 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400432 return -3;
433 }
434 }
435
436 // Check to make sure that a backup with this name doesn't already exist
437 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
438 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500439 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500440 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400441 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000442 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400443 return -4;
444 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400445 // No problems found, return 0
446 return 0;
447}
448
Dees_Troy43d8b002012-09-17 16:00:01 -0400449bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
450{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500451 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400452 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500453 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500454 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400455
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500456 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400457 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400458
Dees_Troyb46a6842012-09-25 11:06:46 -0400459 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000460 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400461
462 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500463 md5sum.setfn(Backup_Folder + Backup_Filename);
464 if (md5sum.computeMD5() == 0)
465 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000466 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500467 else
468 return -1;
469 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000470 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400471 } else {
472 char filename[512];
473 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500474 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400475 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500476 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000477 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400478 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000479 if (TWFunc::Path_Exists(filename)) {
480 if (md5sum.computeMD5() == 0) {
481 if (md5sum.write_md5digest() != 0)
482 {
483 gui_print(" * MD5 Error.\n");
484 return false;
485 }
486 } else {
487 gui_print(" * Error computing MD5.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500488 return false;
489 }
490 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400491 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400492 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500493 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400494 }
495 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000496 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400497 return false;
498 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000499 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400500 }
501 return true;
502}
503
Dees_Troy093b7642012-09-21 15:59:38 -0400504bool TWPartitionManager::Backup_Partition(TWPartition* Part, string Backup_Folder, bool generate_md5, unsigned long long* img_bytes_remaining, unsigned long long* file_bytes_remaining, unsigned long *img_time, unsigned long *file_time, unsigned long long *img_bytes, unsigned long long *file_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400505 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500506 int img_bps;
507 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400508 unsigned long total_time, remain_time, section_time;
509 int use_compression, backup_time;
510 float pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500511 unsigned long long total_size, current_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400512
513 if (Part == NULL)
514 return true;
515
Dees_Troy093b7642012-09-21 15:59:38 -0400516 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
517
518 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
519 if (use_compression)
520 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
521 else
522 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
523
524 // We know the speed for both, how far into the whole backup are we, based on time
525 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
526 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
527
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500528 //pos = (total_time - remain_time) / (float) total_time;
529 total_size = *file_bytes + *img_bytes;
530 current_size = *file_bytes + *img_bytes - *file_bytes_remaining - *img_bytes_remaining;
531 pos = ((float)(current_size) / (float)(total_size));
Dees_Troy2673cec2013-04-02 20:22:16 +0000532 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400533
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500534 LOGINFO("Estimated total time: %lu\nEstimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400535
536 // And get the time
537 if (Part->Backup_Method == 1)
538 section_time = Part->Backup_Size / file_bps;
539 else
540 section_time = Part->Backup_Size / img_bps;
541
542 // Set the position
543 pos = section_time / (float) total_time;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500544 //DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400545
Tom Hite5a926722014-09-15 01:31:03 +0000546 TWFunc::SetPerformanceMode(true);
Dees_Troy43d8b002012-09-17 16:00:01 -0400547 time(&start);
548
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500549 if (Part->Backup(Backup_Folder, &total_size, &current_size)) {
Tom Hite5a926722014-09-15 01:31:03 +0000550 bool md5Success = false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500551 current_size += Part->Backup_Size;
552 pos = (float)((float)(current_size) / (float)(total_size));
553 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400554 if (Part->Has_SubPartition) {
555 std::vector<TWPartition*>::iterator subpart;
556
557 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500558 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Tom Hite5a926722014-09-15 01:31:03 +0000559 if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size)) {
560 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400561 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000562 }
Dees_Troy2727b992013-08-14 20:09:30 +0000563 sync();
564 sync();
Tom Hite5a926722014-09-15 01:31:03 +0000565 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName)) {
566 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400567 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000568 }
Dees_Troy093b7642012-09-21 15:59:38 -0400569 if (Part->Backup_Method == 1) {
570 *file_bytes_remaining -= (*subpart)->Backup_Size;
571 } else {
572 *img_bytes_remaining -= (*subpart)->Backup_Size;
573 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500574 current_size += Part->Backup_Size;
575 pos = (float)(current_size / total_size);
576 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400577 }
578 }
579 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400580 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400581 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000582 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400583 if (Part->Backup_Method == 1) {
584 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400585 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400586 } else {
587 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400588 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400589 }
Tom Hite5a926722014-09-15 01:31:03 +0000590
591 md5Success = Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
592 TWFunc::SetPerformanceMode(false);
593 return md5Success;
Dees_Troy43d8b002012-09-17 16:00:01 -0400594 } else {
Tom Hite5a926722014-09-15 01:31:03 +0000595 TWFunc::SetPerformanceMode(false);
Dees_Troy43d8b002012-09-17 16:00:01 -0400596 return false;
597 }
598}
599
600int TWPartitionManager::Run_Backup(void) {
601 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500602 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400603 unsigned long long total_bytes = 0, file_bytes = 0, img_bytes = 0, free_space = 0, img_bytes_remaining, file_bytes_remaining, subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400604 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500605 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400606 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400607 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400608 struct tm *t;
609 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500610 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400611 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500612 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400613
614 time(&total_start);
615
616 Update_System_Details();
617
618 if (!Mount_Current_Storage(true))
619 return false;
620
621 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400622 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400623 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400624 else
625 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400626
Dees_Troy43d8b002012-09-17 16:00:01 -0400627 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
628 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000629 if (Backup_Name == "(Current Date)") {
630 Backup_Name = TWFunc::Get_Current_Date();
631 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
632 TWFunc::Auto_Generate_Backup_Name();
633 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400634 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000635 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400636 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000637 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400638
Dees_Troy2673cec2013-04-02 20:22:16 +0000639 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500640 DataManager::GetValue("tw_backup_list", Backup_List);
641 if (!Backup_List.empty()) {
642 end_pos = Backup_List.find(";", start_pos);
643 while (end_pos != string::npos && start_pos < Backup_List.size()) {
644 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
645 backup_part = Find_Partition_By_Path(backup_path);
646 if (backup_part != NULL) {
647 partition_count++;
648 if (backup_part->Backup_Method == 1)
649 file_bytes += backup_part->Backup_Size;
650 else
651 img_bytes += backup_part->Backup_Size;
652 if (backup_part->Has_SubPartition) {
653 std::vector<TWPartition*>::iterator subpart;
654
655 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000656 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == backup_part->Mount_Point) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500657 partition_count++;
658 if ((*subpart)->Backup_Method == 1)
659 file_bytes += (*subpart)->Backup_Size;
660 else
661 img_bytes += (*subpart)->Backup_Size;
662 }
663 }
Dees_Troy8170a922012-09-18 15:40:25 -0400664 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500665 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000666 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400667 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500668 start_pos = end_pos + 1;
669 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400670 }
671 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400672
673 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000674 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400675 return false;
676 }
677 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000678 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500679 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400680 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
681 if (storage != NULL) {
682 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000683 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400684 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000685 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400686 return false;
687 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000688 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400689 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000690 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400691 return false;
692 }
693 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500694 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400695
Dees_Troy2673cec2013-04-02 20:22:16 +0000696 gui_print("\n[BACKUP STARTED]\n");
697 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000698 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000699 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000700 return false;
701 }
702
Dees_Troy2673cec2013-04-02 20:22:16 +0000703 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400704
Dees_Troya13d74f2013-03-24 08:54:55 -0500705 start_pos = 0;
706 end_pos = Backup_List.find(";", start_pos);
707 while (end_pos != string::npos && start_pos < Backup_List.size()) {
708 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
709 backup_part = Find_Partition_By_Path(backup_path);
710 if (backup_part != NULL) {
711 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
712 return false;
713 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000714 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500715 }
716 start_pos = end_pos + 1;
717 end_pos = Backup_List.find(";", start_pos);
718 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400719
720 // Average BPS
721 if (img_time == 0)
722 img_time = 1;
723 if (file_time == 0)
724 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400725 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500726 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400727
Dees_Troy2673cec2013-04-02 20:22:16 +0000728 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
729 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400730
731 time(&total_stop);
732 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500733 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500734 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400735
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500736 int prev_img_bps, use_compression;
737 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400738 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
739 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500740 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400741
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500742 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400743 if (use_compression)
744 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500745 else
Dees_Troy093b7642012-09-21 15:59:38 -0400746 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
747 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500748 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400749
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500750 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400751 if (use_compression)
752 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
753 else
754 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
755
Dees_Troy2673cec2013-04-02 20:22:16 +0000756 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400757 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400758 UnMount_Main_Partitions();
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500759 gui_print_color("highlight", "[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000760 string backup_log = Full_Backup_Path + "recovery.log";
761 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
762 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400763}
764
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500765bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400766 time_t Start, Stop;
Tom Hite5a926722014-09-15 01:31:03 +0000767 TWFunc::SetPerformanceMode(true);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400768 time(&Start);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500769 //DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Tom Hite5a926722014-09-15 01:31:03 +0000770 if (!Part->Restore(Restore_Name, total_restore_size, already_restored_size)) {
771 TWFunc::SetPerformanceMode(false);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400772 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000773 }
Dees_Troy8170a922012-09-18 15:40:25 -0400774 if (Part->Has_SubPartition) {
775 std::vector<TWPartition*>::iterator subpart;
776
777 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
778 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Tom Hite5a926722014-09-15 01:31:03 +0000779 if (!(*subpart)->Restore(Restore_Name, total_restore_size, already_restored_size)) {
780 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400781 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000782 }
Dees_Troy8170a922012-09-18 15:40:25 -0400783 }
784 }
785 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400786 time(&Stop);
Tom Hite5a926722014-09-15 01:31:03 +0000787 TWFunc::SetPerformanceMode(false);
Dees_Troy2673cec2013-04-02 20:22:16 +0000788 gui_print("[%s done (%d seconds)]\n\n", Part->Backup_Display_Name.c_str(), (int)difftime(Stop, Start));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400789 return true;
790}
791
Dees_Troy51a0e822012-09-05 15:24:24 -0400792int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400793 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500794 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400795 time_t rStart, rStop;
796 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500797 string Restore_List, restore_path;
798 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500799 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400800
Dees_Troy2673cec2013-04-02 20:22:16 +0000801 gui_print("\n[RESTORE STARTED]\n\n");
802 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400803
Dees_Troy4a2a1262012-09-18 09:33:47 -0400804 if (!Mount_Current_Storage(true))
805 return false;
806
807 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500808 if (check_md5 > 0) {
809 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
810 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000811 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500812 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000813 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500814 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500815 gui_print("Calculating restore details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500816 DataManager::GetValue("tw_restore_selected", Restore_List);
817 if (!Restore_List.empty()) {
818 end_pos = Restore_List.find(";", start_pos);
819 while (end_pos != string::npos && start_pos < Restore_List.size()) {
820 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
821 restore_part = Find_Partition_By_Path(restore_path);
822 if (restore_part != NULL) {
823 partition_count++;
824 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
825 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500826 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500827 if (restore_part->Has_SubPartition) {
828 std::vector<TWPartition*>::iterator subpart;
829
830 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
831 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400832 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500833 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500834 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500835 }
836 }
837 }
838 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500839 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500840 }
841 start_pos = end_pos + 1;
842 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400843 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400844 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400845
846 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000847 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400848 return false;
849 }
850
Dees_Troy2673cec2013-04-02 20:22:16 +0000851 gui_print("Restoring %i partitions...\n", partition_count);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500852 gui_print("Total restore size is %lluMB\n", total_restore_size / 1048576);
Dees_Troy2673cec2013-04-02 20:22:16 +0000853 DataManager::SetProgress(0.0);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500854
Dees_Troya13d74f2013-03-24 08:54:55 -0500855 start_pos = 0;
856 if (!Restore_List.empty()) {
857 end_pos = Restore_List.find(";", start_pos);
858 while (end_pos != string::npos && start_pos < Restore_List.size()) {
859 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
860 restore_part = Find_Partition_By_Path(restore_path);
861 if (restore_part != NULL) {
862 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500863 if (!Restore_Partition(restore_part, Restore_Name, partition_count, &total_restore_size, &already_restored_size))
Dees_Troya13d74f2013-03-24 08:54:55 -0500864 return false;
865 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000866 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500867 }
868 start_pos = end_pos + 1;
869 end_pos = Restore_List.find(";", start_pos);
870 }
871 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400872 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400873 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400874 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400875 time(&rStop);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500876 gui_print_color("highlight", "[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500877 DataManager::SetValue("tw_file_progress", "");
Dees_Troy63c8df72012-09-10 14:02:05 -0400878 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400879}
880
881void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400882 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500883 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000884 bool get_date = true, check_encryption = true;
885
886 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400887
888 DIR* d;
889 d = opendir(Restore_Name.c_str());
890 if (d == NULL)
891 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000892 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400893 return;
894 }
895
896 struct dirent* de;
897 while ((de = readdir(d)) != NULL)
898 {
899 // Strip off three components
900 char str[256];
901 char* label;
902 char* fstype = NULL;
903 char* extn = NULL;
904 char* ptr;
905
906 strcpy(str, de->d_name);
907 if (strlen(str) <= 2)
908 continue;
909
910 if (get_date) {
911 char file_path[255];
912 struct stat st;
913
914 strcpy(file_path, Restore_Name.c_str());
915 strcat(file_path, "/");
916 strcat(file_path, str);
917 stat(file_path, &st);
918 string backup_date = ctime((const time_t*)(&st.st_mtime));
919 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
920 get_date = false;
921 }
922
923 label = str;
924 ptr = label;
925 while (*ptr && *ptr != '.') ptr++;
926 if (*ptr == '.')
927 {
928 *ptr = 0x00;
929 ptr++;
930 fstype = ptr;
931 }
932 while (*ptr && *ptr != '.') ptr++;
933 if (*ptr == '.')
934 {
935 *ptr = 0x00;
936 ptr++;
937 extn = ptr;
938 }
939
Dees_Troy83bd4832013-05-04 12:39:56 +0000940 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -0500941 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +0000942 if (extnlength != 3 && extnlength != 6) continue;
943 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
944 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
945
946 if (check_encryption) {
947 string filename = Restore_Name + "/";
948 filename += de->d_name;
949 if (TWFunc::Get_File_Type(filename) == 2) {
950 LOGINFO("'%s' is encrypted\n", filename.c_str());
951 DataManager::SetValue("tw_restore_encrypted", 1);
952 }
953 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500954 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -0400955
956 TWPartition* Part = Find_Partition_By_Path(label);
957 if (Part == NULL)
958 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000959 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -0400960 continue;
961 }
962
963 Part->Backup_FileName = de->d_name;
964 if (strlen(extn) > 3) {
965 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
966 }
967
Dees_Troya13d74f2013-03-24 08:54:55 -0500968 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -0400969 }
970 closedir(d);
971
Dees_Troya13d74f2013-03-24 08:54:55 -0500972 // Set the final value
973 DataManager::SetValue("tw_restore_list", Restore_List);
974 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -0400975 return;
976}
977
978int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400979 std::vector<TWPartition*>::iterator iter;
980 int ret = false;
981 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400982 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -0400983
984 // Iterate through all partitions
985 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400986 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -0400987 if (Path == "/and-sec")
988 ret = (*iter)->Wipe_AndSec();
989 else
990 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -0400991 found = true;
992 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
993 (*iter)->Wipe();
994 }
995 }
996 if (found) {
997 return ret;
998 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000999 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001000 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001001}
1002
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001003int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1004 std::vector<TWPartition*>::iterator iter;
1005 int ret = false;
1006 bool found = false;
1007 string Local_Path = TWFunc::Get_Root_Path(Path);
1008
1009 // Iterate through all partitions
1010 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1011 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1012 if (Path == "/and-sec")
1013 ret = (*iter)->Wipe_AndSec();
1014 else
1015 ret = (*iter)->Wipe(New_File_System);
1016 found = true;
1017 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1018 (*iter)->Wipe(New_File_System);
1019 }
1020 }
1021 if (found) {
1022 return ret;
1023 } else
1024 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1025 return false;
1026}
1027
Dees_Troy51a0e822012-09-05 15:24:24 -04001028int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001029 std::vector<TWPartition*>::iterator iter;
1030 int ret = true;
1031
1032 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001033 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001034 if (!(*iter)->Wipe())
1035 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001036 } else if ((*iter)->Has_Android_Secure) {
1037 if (!(*iter)->Wipe_AndSec())
1038 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001039 }
1040 }
1041 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001042}
1043
Dees_Troy38bd7602012-09-14 13:33:53 -04001044int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1045 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001046 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001047
1048 if (!Mount_By_Path("/data", true))
1049 return false;
1050
1051 if (!Mount_By_Path("/cache", true))
1052 return false;
1053
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001054 dir.push_back("/data/dalvik-cache");
1055 dir.push_back("/cache/dalvik-cache");
1056 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001057 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001058 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001059 if (stat(dir.at(i).c_str(), &st) == 0) {
1060 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001061 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001062 }
1063 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001064 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001065 if (sdext && sdext->Is_Present && sdext->Mount(false))
1066 {
1067 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1068 {
1069 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001070 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001071 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001072 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001073 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001074 return true;
1075}
1076
1077int TWPartitionManager::Wipe_Rotate_Data(void) {
1078 if (!Mount_By_Path("/data", true))
1079 return false;
1080
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001081 unlink("/data/misc/akmd*");
1082 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001083 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001084 return true;
1085}
1086
1087int TWPartitionManager::Wipe_Battery_Stats(void) {
1088 struct stat st;
1089
1090 if (!Mount_By_Path("/data", true))
1091 return false;
1092
1093 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001094 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001095 } else {
1096 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001097 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001098 }
1099 return true;
1100}
1101
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001102int TWPartitionManager::Wipe_Android_Secure(void) {
1103 std::vector<TWPartition*>::iterator iter;
1104 int ret = false;
1105 bool found = false;
1106
1107 // Iterate through all partitions
1108 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1109 if ((*iter)->Has_Android_Secure) {
1110 ret = (*iter)->Wipe_AndSec();
1111 found = true;
1112 }
1113 }
1114 if (found) {
1115 return ret;
1116 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001117 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001118 }
1119 return false;
1120}
1121
Dees_Troy38bd7602012-09-14 13:33:53 -04001122int TWPartitionManager::Format_Data(void) {
1123 TWPartition* dat = Find_Partition_By_Path("/data");
1124
1125 if (dat != NULL) {
1126 if (!dat->UnMount(true))
1127 return false;
1128
1129 return dat->Wipe_Encryption();
1130 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001131 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001132 return false;
1133 }
1134 return false;
1135}
1136
1137int TWPartitionManager::Wipe_Media_From_Data(void) {
1138 TWPartition* dat = Find_Partition_By_Path("/data");
1139
1140 if (dat != NULL) {
1141 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001142 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001143 return false;
1144 }
1145 if (!dat->Mount(true))
1146 return false;
1147
Dees_Troy2673cec2013-04-02 20:22:16 +00001148 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001149 mtp_was_enabled = TWFunc::Toggle_MTP(false);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001150 TWFunc::removeDir("/data/media", false);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001151 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
1152 if (mtp_was_enabled) {
1153 if (!Enable_MTP())
1154 Disable_MTP();
1155 }
1156 return false;
1157 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001158 if (dat->Has_Data_Media) {
1159 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001160 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1161 dat->UnMount(false);
1162 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001163 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001164 if (mtp_was_enabled) {
1165 if (!Enable_MTP())
1166 Disable_MTP();
1167 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001168 return true;
1169 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001170 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001171 return false;
1172 }
1173 return false;
1174}
1175
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001176int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1177 std::vector<TWPartition*>::iterator iter;
1178 int ret = false;
1179 bool found = false;
1180 string Local_Path = TWFunc::Get_Root_Path(Path);
1181
1182 if (Local_Path == "/tmp" || Local_Path == "/")
1183 return true;
1184
1185 // Iterate through all partitions
1186 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1187 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1188 ret = (*iter)->Repair();
1189 found = true;
1190 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1191 (*iter)->Repair();
1192 }
1193 }
1194 if (found) {
1195 return ret;
1196 } else if (Display_Error) {
1197 LOGERR("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1198 } else {
1199 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1200 }
1201 return false;
1202}
1203
Dees_Troy51a0e822012-09-05 15:24:24 -04001204void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001205 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001206 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001207
Dees_Troy2673cec2013-04-02 20:22:16 +00001208 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001209 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001210 if ((*iter)->Can_Be_Mounted) {
1211 (*iter)->Update_Size(true);
1212 if ((*iter)->Mount_Point == "/system") {
1213 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1214 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1215 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1216 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1217 } else if ((*iter)->Mount_Point == "/cache") {
1218 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1219 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1220 } else if ((*iter)->Mount_Point == "/sd-ext") {
1221 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1222 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1223 if ((*iter)->Backup_Size == 0) {
1224 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1225 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1226 } else
1227 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001228 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001229 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001230 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001231 if ((*iter)->Backup_Size == 0) {
1232 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1233 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1234 } else
1235 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001236 } else if ((*iter)->Mount_Point == "/boot") {
1237 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1238 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1239 if ((*iter)->Backup_Size == 0) {
1240 DataManager::SetValue("tw_has_boot_partition", 0);
1241 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1242 } else
1243 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001244 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001245#ifdef SP1_NAME
1246 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1247 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1248 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1249 }
1250#endif
1251#ifdef SP2_NAME
1252 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1253 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1254 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1255 }
1256#endif
1257#ifdef SP3_NAME
1258 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1259 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1260 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1261 }
1262#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001263 } else {
1264 // Handle unmountable partitions in case we reset defaults
1265 if ((*iter)->Mount_Point == "/boot") {
1266 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1267 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1268 if ((*iter)->Backup_Size == 0) {
1269 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1270 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1271 } else
1272 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1273 } else if ((*iter)->Mount_Point == "/recovery") {
1274 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1275 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1276 if ((*iter)->Backup_Size == 0) {
1277 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1278 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1279 } else
1280 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001281 } else if ((*iter)->Mount_Point == "/data") {
1282 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001283 }
1284#ifdef SP1_NAME
1285 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1286 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1287 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1288 }
1289#endif
1290#ifdef SP2_NAME
1291 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1292 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1293 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1294 }
1295#endif
1296#ifdef SP3_NAME
1297 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1298 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1299 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1300 }
1301#endif
Dees_Troy51127312012-09-08 13:08:49 -04001302 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001303 }
that39b7c162013-11-22 22:07:33 +01001304 gui_print("...done\n");
Dees_Troy51127312012-09-08 13:08:49 -04001305 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1306 string current_storage_path = DataManager::GetCurrentStoragePath();
1307 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001308 if (FreeStorage != NULL) {
1309 // Attempt to mount storage
1310 if (!FreeStorage->Mount(false)) {
1311 // We couldn't mount storage... check to see if we have dual storage
1312 int has_dual_storage;
1313 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1314 if (has_dual_storage == 1) {
1315 // We have dual storage, see if we're using the internal storage that should always be present
1316 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001317 if (!FreeStorage->Is_Encrypted) {
1318 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001319 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001320 }
Dees_Troy8170a922012-09-18 15:40:25 -04001321 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1322 } else {
1323 // We were using external, flip to internal
1324 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1325 current_storage_path = DataManager::GetCurrentStoragePath();
1326 FreeStorage = Find_Partition_By_Path(current_storage_path);
1327 if (FreeStorage != NULL) {
1328 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1329 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001330 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001331 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1332 }
1333 }
1334 } else {
1335 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001336 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001337 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1338 }
1339 } else {
1340 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1341 }
1342 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001343 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001344 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001345 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001346 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001347 return;
1348}
1349
1350int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001351#ifdef TW_INCLUDE_CRYPTO
1352 int ret_val, password_len;
1353 char crypto_blkdev[255], cPassword[255];
1354 size_t result;
1355
1356 property_set("ro.crypto.state", "encrypted");
Ethan Yonker4eca40d2014-11-11 14:52:28 -06001357#if defined(TW_INCLUDE_JB_CRYPTO) || defined(TW_INCLUDE_L_CRYPTO)
Dees_Troy5bf43922012-09-07 16:07:55 -04001358 // No extra flags needed
1359#else
1360 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1361 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1362 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1363 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1364 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1365 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001366
1367#ifdef CRYPTO_SD_FS_TYPE
Ethan Yonker71413f42014-02-26 13:36:08 -06001368 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1369 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1370 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001371#endif
a39552696ff55ce2013-01-08 16:14:56 +00001372
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001373 property_set("rw.km_fips_status", "ready");
a39552696ff55ce2013-01-08 16:14:56 +00001374
1375#endif
1376
1377 // some samsung devices store "footer" on efs partition
1378 TWPartition *efs = Find_Partition_By_Path("/efs");
1379 if(efs && !efs->Is_Mounted())
1380 efs->Mount(false);
1381 else
1382 efs = 0;
1383#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001384#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001385 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001386 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001387 property_set("ro.crypto.external_encrypted", "1");
1388 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1389 } else {
1390 property_set("ro.crypto.external_encrypted", "0");
1391 }
1392#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001393#endif
a39552696ff55ce2013-01-08 16:14:56 +00001394
Dees_Troy5bf43922012-09-07 16:07:55 -04001395 strcpy(cPassword, Password.c_str());
Ethan Yonkercceebb82014-11-18 10:17:59 -06001396#ifdef TW_INCLUDE_L_CRYPTO
1397 Mount_By_Path("/vendor", false); // if exists, mount vendor partition as we may need some proprietary files
1398#endif
a39552696ff55ce2013-01-08 16:14:56 +00001399 int pwret = cryptfs_check_passwd(cPassword);
1400
1401 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001402 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001403 return -1;
1404 }
a39552696ff55ce2013-01-08 16:14:56 +00001405
1406 if(efs)
1407 efs->UnMount(false);
1408
Dees_Troy5bf43922012-09-07 16:07:55 -04001409 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1410 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001411 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001412 } else {
1413 TWPartition* dat = Find_Partition_By_Path("/data");
1414 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001415 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001416 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1417 dat->Is_Decrypted = true;
1418 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001419 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001420 dat->Current_File_System = dat->Fstab_File_System; // Needed if we're ignoring blkid because encrypted devices start out as emmc
Dees_Troy2673cec2013-04-02 20:22:16 +00001421 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001422
1423#ifdef CRYPTO_SD_FS_TYPE
1424 char crypto_blkdev_sd[255];
1425 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1426 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001427 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001428 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001429 emmc->Is_Decrypted = true;
1430 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1431 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001432 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001433 }
a39552696ff55ce2013-01-08 16:14:56 +00001434#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001435#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001436#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001437 char is_external_decrypted[255];
1438 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1439 if (strcmp(is_external_decrypted, "1") == 0) {
1440 sdcard->Is_Decrypted = true;
1441 sdcard->EcryptFS_Password = Password;
1442 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001443 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1444 MetaEcfsFile += "/.MetaEcfsFile";
1445 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1446 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1447 sdcard->UnMount(false);
1448 sdcard->Mount(false);
1449 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001450 } else {
Dees_Troy066eb302013-08-23 17:20:32 +00001451 LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001452 sdcard->Is_Decrypted = false;
1453 sdcard->Decrypted_Block_Device = "";
1454 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001455#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001456#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001457
Dees_Troy5bf43922012-09-07 16:07:55 -04001458 // Sleep for a bit so that the device will be ready
1459 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001460 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001461 dat->Storage_Path = "/data/media/0";
1462 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001463 DataManager::SetValue("tw_storage_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001464 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001465 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001466 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001467 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001468 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001469 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001470 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001471 }
1472 return 0;
1473#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001474 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001475 return -1;
1476#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001477 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001478}
1479
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001480int TWPartitionManager::Fix_Permissions(void) {
1481 int result = 0;
1482 if (!Mount_By_Path("/data", true))
1483 return false;
1484
1485 if (!Mount_By_Path("/system", true))
1486 return false;
1487
1488 Mount_By_Path("/sd-ext", false);
1489
1490 fixPermissions perms;
1491 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001492 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001493 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001494 return result;
1495}
1496
Ethan Yonker47360be2014-04-01 10:34:34 -05001497TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1498 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1499
1500 if (!Path.empty()) {
1501 string Search_Path = TWFunc::Get_Root_Path(Path);
1502 for (; iter != Partitions.end(); iter++) {
1503 if ((*iter)->Mount_Point == Search_Path) {
1504 iter++;
1505 break;
1506 }
1507 }
1508 }
1509
1510 for (; iter != Partitions.end(); iter++) {
1511 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) {
1512 return (*iter);
1513 }
1514 }
1515
1516 return NULL;
1517}
1518
Dees_Troyd21618c2012-10-14 18:48:49 -04001519int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001520 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1521
1522 if (Part == NULL) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001523 LOGERR("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
Dees_Troyd21618c2012-10-14 18:48:49 -04001524 return false;
1525 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001526 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1527 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001528 return false;
1529
Dees_Troy2673cec2013-04-02 20:22:16 +00001530 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1531 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001532 return false;
1533 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001534 return true;
1535}
1536
Dees_Troy8170a922012-09-18 15:40:25 -04001537int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001538 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001539 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001540 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001541
Ethan Yonker9eb1cd42014-11-03 22:14:08 -06001542 property_set("sys.storage.ums_enabled", "1");
1543 sleep(1);
Dees_Troy8170a922012-09-18 15:40:25 -04001544 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001545 string Lun_File_str = CUSTOM_LUN_FILE;
1546 size_t found = Lun_File_str.find("%");
1547 if (found != string::npos) {
1548 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1549 if (TWFunc::Path_Exists(lun_file))
1550 has_multiple_lun = true;
1551 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001552 mtp_was_enabled = TWFunc::Toggle_MTP(false);
Ethan Yonker47360be2014-04-01 10:34:34 -05001553 if (!has_multiple_lun) {
1554 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1555 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1556 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
1557 TWPartition* Mount = Find_Next_Storage("", "/data");
1558 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001559 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1560 goto error_handle;
1561 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001562 } else {
1563 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001564 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001565 }
1566 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001567 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001568 }
Dees_Troy8170a922012-09-18 15:40:25 -04001569 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001570 LOGINFO("Device has multiple lun files\n");
1571 TWPartition* Mount1;
1572 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001573 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker47360be2014-04-01 10:34:34 -05001574 Mount1 = Find_Next_Storage("", "/data");
1575 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001576 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1577 goto error_handle;
1578 }
Matt Moweree717062014-04-26 01:46:46 -05001579 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker47360be2014-04-01 10:34:34 -05001580 Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data");
1581 if (Mount2) {
Matt Moweree717062014-04-26 01:46:46 -05001582 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001583 }
1584 } else {
1585 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001586 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001587 }
Dees_Troy8170a922012-09-18 15:40:25 -04001588 }
1589 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001590error_handle:
1591 if (mtp_was_enabled)
1592 if (!Enable_MTP())
1593 Disable_MTP();
Ethan Yonker9eb1cd42014-11-03 22:14:08 -06001594 property_set("sys.storage.ums_enabled", "0");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001595 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001596}
1597
1598int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001599 int index, ret;
1600 char lun_file[255], ch[2] = {0, 0};
1601 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001602
1603 for (index=0; index<2; index++) {
1604 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001605 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001606 if (ret < 0) {
1607 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001608 }
Dees_Troy8170a922012-09-18 15:40:25 -04001609 }
Dees_Troye58d5262012-09-21 12:27:57 -04001610 Mount_All_Storage();
1611 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001612 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001613 property_set("sys.storage.ums_enabled", "0");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001614 if (mtp_was_enabled)
1615 if (!Enable_MTP())
1616 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001617 if (ret < 0 && index == 0) {
1618 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1619 return false;
1620 } else {
1621 return true;
1622 }
Dees_Troy8170a922012-09-18 15:40:25 -04001623 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001624}
1625
1626void TWPartitionManager::Mount_All_Storage(void) {
1627 std::vector<TWPartition*>::iterator iter;
1628
1629 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1630 if ((*iter)->Is_Storage)
1631 (*iter)->Mount(false);
1632 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001633}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001634
Dees_Troyd0384ef2012-10-12 12:15:42 -04001635void TWPartitionManager::UnMount_Main_Partitions(void) {
1636 // Unmounts system and data if data is not data/media
1637 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001638 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001639
1640 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1641
1642 UnMount_By_Path("/system", true);
Ethan Yonker6277c792014-09-15 14:54:30 -05001643 if (!datamedia)
1644 UnMount_By_Path("/data", true);
1645
Dees_Troyd0384ef2012-10-12 12:15:42 -04001646 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1647 Boot_Partition->UnMount(true);
1648}
1649
Dees_Troy9350b8d2012-09-27 12:38:38 -04001650int TWPartitionManager::Partition_SDCard(void) {
1651 char mkdir_path[255], temp[255], line[512];
1652 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1653 int ext, swap, total_size = 0, fat_size;
1654 FILE* fp;
1655
Dees_Troy2673cec2013-04-02 20:22:16 +00001656 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001657#ifdef TW_EXTERNAL_STORAGE_PATH
1658 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1659#else
1660 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1661#endif
Ethan Yonker1eff6cd2014-09-15 13:30:42 -05001662 if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001663 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001664 return false;
1665 }
1666 if (!SDCard->UnMount(true))
1667 return false;
1668 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1669 if (SDext != NULL) {
1670 if (!SDext->UnMount(true))
1671 return false;
1672 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001673
1674 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001675 Device = SDCard->Actual_Block_Device;
1676 // Just use the root block device
1677 Device.resize(strlen("/dev/block/mmcblkX"));
1678
1679 // Find the size of the block device:
1680 fp = fopen("/proc/partitions", "rt");
1681 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001682 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001683 return false;
1684 }
1685
1686 while (fgets(line, sizeof(line), fp) != NULL)
1687 {
1688 unsigned long major, minor, blocks;
1689 char device[512];
1690 char tmpString[64];
1691
1692 if (strlen(line) < 7 || line[0] == 'm') continue;
1693 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1694
1695 tmpdevice = "/dev/block/";
1696 tmpdevice += device;
1697 if (tmpdevice == Device) {
1698 // Adjust block size to byte size
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001699 total_size = (int)(blocks * 1024ULL / 1000000LLU);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001700 break;
1701 }
1702 }
1703 fclose(fp);
1704
1705 DataManager::GetValue("tw_sdext_size", ext);
1706 DataManager::GetValue("tw_swap_size", swap);
1707 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1708 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001709 LOGINFO("sd card block device is '%s', sdcard size is: %iMB, fat size: %iMB, ext size: %iMB, ext system: '%s', swap size: %iMB\n", Device.c_str(), total_size, fat_size, ext, ext_format.c_str(), swap);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001710 memset(temp, 0, sizeof(temp));
1711 sprintf(temp, "%i", fat_size);
1712 fat_str = temp;
1713 memset(temp, 0, sizeof(temp));
1714 sprintf(temp, "%i", fat_size + ext);
1715 ext_str = temp;
1716 memset(temp, 0, sizeof(temp));
1717 sprintf(temp, "%i", fat_size + ext + swap);
1718 swap_str = temp;
1719 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001720 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001721 return false;
1722 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001723 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001724 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001725 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001726 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001727 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001728 Update_System_Details();
1729 return false;
1730 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001731 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001732 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001733 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001734 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001735 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001736 return false;
1737 }
1738 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001739 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001740 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001741 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001742 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001743 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001744 Update_System_Details();
1745 return false;
1746 }
1747 }
1748 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001749 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001750 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001751 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001752 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001753 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001754 Update_System_Details();
1755 return false;
1756 }
1757 }
1758 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1759#ifdef TW_EXTERNAL_STORAGE_PATH
1760 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1761 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1762 memset(mkdir_path, 0, sizeof(mkdir_path));
1763 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1764#else
1765 Mount_By_Path("/sdcard", 1);
1766 strcpy(mkdir_path, "/sdcard/TWRP");
1767#endif
1768 mkdir(mkdir_path, 0777);
1769 DataManager::Flush();
1770#ifdef TW_EXTERNAL_STORAGE_PATH
1771 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1772 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1773 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1774#else
1775 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1776 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1777 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1778#endif
1779 if (ext > 0) {
1780 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001781 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001782 return false;
1783 }
1784 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001785 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1786 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001787 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001788 }
1789
1790 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00001791 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001792 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001793}
Dees_Troya13d74f2013-03-24 08:54:55 -05001794
1795void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1796 std::vector<TWPartition*>::iterator iter;
1797 if (ListType == "mount") {
1798 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1799 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1800 struct PartitionList part;
1801 part.Display_Name = (*iter)->Display_Name;
1802 part.Mount_Point = (*iter)->Mount_Point;
1803 part.selected = (*iter)->Is_Mounted();
1804 Partition_List->push_back(part);
1805 }
1806 }
1807 } else if (ListType == "storage") {
1808 char free_space[255];
1809 string Current_Storage = DataManager::GetCurrentStoragePath();
1810 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1811 if ((*iter)->Is_Storage) {
1812 struct PartitionList part;
1813 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1814 part.Display_Name = (*iter)->Storage_Name + " (";
1815 part.Display_Name += free_space;
1816 part.Display_Name += "MB)";
1817 part.Mount_Point = (*iter)->Storage_Path;
1818 if ((*iter)->Storage_Path == Current_Storage)
1819 part.selected = 1;
1820 else
1821 part.selected = 0;
1822 Partition_List->push_back(part);
1823 }
1824 }
1825 } else if (ListType == "backup") {
1826 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001827 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001828 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001829 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001830 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001831 Backup_Size = (*iter)->Backup_Size;
1832 if ((*iter)->Has_SubPartition) {
1833 std::vector<TWPartition*>::iterator subpart;
1834
1835 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1836 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1837 Backup_Size += (*subpart)->Backup_Size;
1838 }
1839 }
1840 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001841 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1842 part.Display_Name += backup_size;
1843 part.Display_Name += "MB)";
1844 part.Mount_Point = (*iter)->Backup_Path;
1845 part.selected = 0;
1846 Partition_List->push_back(part);
1847 }
1848 }
1849 } else if (ListType == "restore") {
1850 string Restore_List, restore_path;
1851 TWPartition* restore_part = NULL;
1852
1853 DataManager::GetValue("tw_restore_list", Restore_List);
1854 if (!Restore_List.empty()) {
1855 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1856 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1857 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05001858 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00001859 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001860 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05001861 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05001862 } else {
1863 struct PartitionList part;
1864 part.Display_Name = restore_part->Backup_Display_Name;
1865 part.Mount_Point = restore_part->Backup_Path;
1866 part.selected = 1;
1867 Partition_List->push_back(part);
1868 }
1869 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001870 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001871 }
1872 start_pos = end_pos + 1;
1873 end_pos = Restore_List.find(";", start_pos);
1874 }
1875 }
1876 } else if (ListType == "wipe") {
1877 struct PartitionList dalvik;
1878 dalvik.Display_Name = "Dalvik Cache";
1879 dalvik.Mount_Point = "DALVIK";
1880 dalvik.selected = 0;
1881 Partition_List->push_back(dalvik);
1882 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1883 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1884 struct PartitionList part;
1885 part.Display_Name = (*iter)->Display_Name;
1886 part.Mount_Point = (*iter)->Mount_Point;
1887 part.selected = 0;
1888 Partition_List->push_back(part);
1889 }
1890 if ((*iter)->Has_Android_Secure) {
1891 struct PartitionList part;
1892 part.Display_Name = (*iter)->Backup_Display_Name;
1893 part.Mount_Point = (*iter)->Backup_Path;
1894 part.selected = 0;
1895 Partition_List->push_back(part);
1896 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001897 if ((*iter)->Has_Data_Media) {
1898 struct PartitionList datamedia;
1899 datamedia.Display_Name = (*iter)->Storage_Name;
1900 datamedia.Mount_Point = "INTERNAL";
1901 datamedia.selected = 0;
1902 Partition_List->push_back(datamedia);
1903 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001904 }
1905 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001906 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001907 }
1908}
1909
1910int TWPartitionManager::Fstab_Processed(void) {
1911 return Partitions.size();
1912}
Dees_Troyd93bda52013-07-03 19:55:19 +00001913
1914void TWPartitionManager::Output_Storage_Fstab(void) {
1915 std::vector<TWPartition*>::iterator iter;
1916 char storage_partition[255];
1917 string Temp;
1918 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
1919
1920 if (fp == NULL) {
1921 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
1922 return;
1923 }
1924
1925 // Iterate through all partitions
1926 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1927 if ((*iter)->Is_Storage) {
1928 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
1929 strcpy(storage_partition, Temp.c_str());
1930 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
1931 }
1932 }
1933 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001934}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02001935
1936TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
1937{
1938 TWPartition *res = NULL;
1939 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
1940 if(!(*iter)->Is_Storage)
1941 continue;
1942
1943 if((*iter)->Is_Settings_Storage)
1944 return *iter;
1945
1946 if(!res)
1947 res = *iter;
1948 }
1949 return res;
1950}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001951
1952bool TWPartitionManager::Enable_MTP(void) {
1953#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001954 if (mtppid) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001955 LOGERR("MTP already enabled\n");
1956 return true;
1957 }
1958 //Launch MTP Responder
1959 LOGINFO("Starting MTP\n");
1960 char vendor[PROPERTY_VALUE_MAX];
1961 char product[PROPERTY_VALUE_MAX];
1962 int count = 0;
1963 property_set("sys.usb.config", "none");
1964 property_get("usb.vendor", vendor, "18D1");
1965 property_get("usb.product.mtpadb", product, "4EE2");
1966 string vendorstr = vendor;
1967 string productstr = product;
1968 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
1969 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
1970 property_set("sys.usb.config", "mtp,adb");
1971 std::vector<TWPartition*>::iterator iter;
Ethan Yonker6d154c42014-09-03 14:19:43 -05001972 /* To enable MTP debug, use the twrp command line feature to
1973 * twrp set tw_mtp_debug 1
1974 */
1975 twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug"));
that9e0593e2014-10-08 00:01:24 +02001976 unsigned int storageid = 1 << 16; // upper 16 bits are for physical storage device, we pretend to have only one
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001977 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1978 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false)) {
that9e0593e2014-10-08 00:01:24 +02001979 ++storageid;
1980 printf("twrp addStorage %s, mtpstorageid: %u\n", (*iter)->Storage_Path.c_str(), storageid);
1981 mtp->addStorage((*iter)->Storage_Name, (*iter)->Storage_Path, storageid);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001982 count++;
1983 }
1984 }
1985 if (count) {
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001986 mtppid = mtp->forkserver();
1987 if (mtppid) {
1988 DataManager::SetValue("tw_mtp_enabled", 1);
1989 return true;
1990 } else {
1991 LOGERR("Failed to enable MTP\n");
1992 return false;
1993 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001994 }
1995 LOGERR("No valid storage partitions found for MTP.\n");
1996#else
1997 LOGERR("MTP support not included\n");
1998#endif
1999 DataManager::SetValue("tw_mtp_enabled", 0);
2000 return false;
2001}
2002
2003bool TWPartitionManager::Disable_MTP(void) {
2004#ifdef TW_HAS_MTP
2005 char vendor[PROPERTY_VALUE_MAX];
2006 char product[PROPERTY_VALUE_MAX];
2007 property_set("sys.usb.config", "none");
2008 property_get("usb.vendor", vendor, "18D1");
2009 property_get("usb.product.adb", product, "D002");
2010 string vendorstr = vendor;
2011 string productstr = product;
2012 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2013 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002014 if (mtppid) {
Ethan Yonker8613dc02014-09-11 09:28:20 -05002015 LOGINFO("Disabling MTP\n");
2016 int status;
2017 kill(mtppid, SIGKILL);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002018 mtppid = 0;
Ethan Yonker8613dc02014-09-11 09:28:20 -05002019 // We don't care about the exit value, but this prevents a zombie process
2020 waitpid(mtppid, &status, 0);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002021 }
2022 property_set("sys.usb.config", "adb");
2023 DataManager::SetValue("tw_mtp_enabled", 0);
2024 return true;
2025#else
2026 LOGERR("MTP support not included\n");
2027 DataManager::SetValue("tw_mtp_enabled", 0);
2028 return false;
2029#endif
2030}