blob: 96266a3e882c22d3ae655661dac6d5c514e82b3c [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"
54 #else
55 #include "crypto/ics/cryptfs.h"
56 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -040057#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040058
Ethan Yonker6277c792014-09-15 14:54:30 -050059extern bool datamedia;
60
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050061TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040062 mtp_was_enabled = false;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050063}
64
Dees_Troy51a0e822012-09-05 15:24:24 -040065int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040066 FILE *fstabFile;
67 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +000068 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050069 TWPartition* andsec_partition = NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -040070
71 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
72 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000073 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040074 return false;
75 }
76
77 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
78 if (fstab_line[0] != '/')
79 continue;
80
Dees_Troy2a923582012-09-20 12:13:34 -040081 if (fstab_line[strlen(fstab_line) - 1] != '\n')
82 fstab_line[strlen(fstab_line)] = '\n';
that9e0593e2014-10-08 00:01:24 +020083 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040084 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040085 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040086
Dees_Troy5bf43922012-09-07 16:07:55 -040087 if (partition->Process_Fstab_Line(line, Display_Error)) {
Matt Mowered71fa32014-04-16 13:21:47 -050088 if (!settings_partition && partition->Is_Settings_Storage && partition->Is_Present) {
Dees Troy02a64532014-03-19 15:23:32 +000089 settings_partition = partition;
Dees_Troya13d74f2013-03-24 08:54:55 -050090 } else {
91 partition->Is_Settings_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050092 }
Matt Mowered71fa32014-04-16 13:21:47 -050093 if (!andsec_partition && partition->Has_Android_Secure && partition->Is_Present) {
Matt Mowerbf4efa32014-04-14 23:25:26 -050094 andsec_partition = partition;
95 } else {
96 partition->Has_Android_Secure = false;
97 }
Ethan Yonkerc05c5982014-03-13 09:19:56 -050098 Partitions.push_back(partition);
Dees_Troy5bf43922012-09-07 16:07:55 -040099 } else {
100 delete partition;
101 }
102 }
103 fclose(fstabFile);
Ethan Yonker6277c792014-09-15 14:54:30 -0500104 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) {
105 // Attempt to automatically identify /data/media emulated storage devices
106 TWPartition* Dat = Find_Partition_By_Path("/data");
107 if (Dat) {
108 LOGINFO("Using automatic handling for /data/media emulated storage device.\n");
109 datamedia = true;
that9e0593e2014-10-08 00:01:24 +0200110 Dat->Setup_Data_Media();
Ethan Yonker6277c792014-09-15 14:54:30 -0500111 settings_partition = Dat;
112 }
113 }
Dees Troy02a64532014-03-19 15:23:32 +0000114 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500115 std::vector<TWPartition*>::iterator iter;
116 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
117 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000118 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500119 break;
120 }
121 }
Dees Troy02a64532014-03-19 15:23:32 +0000122 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000123 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500124 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400125 if (!Write_Fstab()) {
126 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000127 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400128 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000129 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400130 }
Matt Mowered71fa32014-04-16 13:21:47 -0500131
Matt Mowerbf4efa32014-04-14 23:25:26 -0500132 if (andsec_partition) {
133 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500134 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500135 Setup_Android_Secure_Location(settings_partition);
136 }
Matt Mowered71fa32014-04-16 13:21:47 -0500137 if (settings_partition) {
138 Setup_Settings_Storage_Partition(settings_partition);
139 }
Dees_Troy51127312012-09-08 13:08:49 -0400140 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400141 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400142 return true;
143}
144
145int TWPartitionManager::Write_Fstab(void) {
146 FILE *fp;
147 std::vector<TWPartition*>::iterator iter;
148 string Line;
149
150 fp = fopen("/etc/fstab", "w");
151 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000152 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400153 return false;
154 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400155 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400156 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400157 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400158 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000159 }
160 // Handle subpartition tracking
161 if ((*iter)->Is_SubPartition) {
162 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
163 if (ParentPartition)
164 ParentPartition->Has_SubPartition = true;
165 else
166 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 -0400167 }
168 }
169 fclose(fp);
170 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400171}
172
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500173void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500174 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
175 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
176 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
177}
178
Matt Mowerbf4efa32014-04-14 23:25:26 -0500179void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
180 if (Part->Has_Android_Secure)
181 Part->Setup_AndSec();
Ethan Yonker6277c792014-09-15 14:54:30 -0500182 else if (!datamedia)
Matt Mowerbf4efa32014-04-14 23:25:26 -0500183 Part->Setup_AndSec();
Matt Mowerbf4efa32014-04-14 23:25:26 -0500184}
185
Dees_Troy8170a922012-09-18 15:40:25 -0400186void TWPartitionManager::Output_Partition_Logging(void) {
187 std::vector<TWPartition*>::iterator iter;
188
189 printf("\n\nPartition Logs:\n");
190 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
191 Output_Partition((*iter));
192}
193
194void TWPartitionManager::Output_Partition(TWPartition* Part) {
195 unsigned long long mb = 1048576;
196
Gary Peck004d48b2012-11-21 16:28:18 -0800197 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 -0400198 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800199 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 -0400200 }
Gary Peck004d48b2012-11-21 16:28:18 -0800201 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500202 if (Part->Can_Be_Mounted)
203 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800204 if (Part->Can_Be_Wiped)
205 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700206 if (Part->Use_Rm_Rf)
207 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500208 if (Part->Can_Be_Backed_Up)
209 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800210 if (Part->Wipe_During_Factory_Reset)
211 printf("Wipe_During_Factory_Reset ");
212 if (Part->Wipe_Available_in_GUI)
213 printf("Wipe_Available_in_GUI ");
214 if (Part->Is_SubPartition)
215 printf("Is_SubPartition ");
216 if (Part->Has_SubPartition)
217 printf("Has_SubPartition ");
218 if (Part->Removable)
219 printf("Removable ");
220 if (Part->Is_Present)
221 printf("IsPresent ");
222 if (Part->Can_Be_Encrypted)
223 printf("Can_Be_Encrypted ");
224 if (Part->Is_Encrypted)
225 printf("Is_Encrypted ");
226 if (Part->Is_Decrypted)
227 printf("Is_Decrypted ");
228 if (Part->Has_Data_Media)
229 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000230 if (Part->Can_Encrypt_Backup)
231 printf("Can_Encrypt_Backup ");
232 if (Part->Use_Userdata_Encryption)
233 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800234 if (Part->Has_Android_Secure)
235 printf("Has_Android_Secure ");
236 if (Part->Is_Storage)
237 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500238 if (Part->Is_Settings_Storage)
239 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000240 if (Part->Ignore_Blkid)
241 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000242 if (Part->Retain_Layout_Version)
243 printf("Retain_Layout_Version ");
Gary Peck004d48b2012-11-21 16:28:18 -0800244 printf("\n");
245 if (!Part->SubPartition_Of.empty())
246 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
247 if (!Part->Symlink_Path.empty())
248 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
249 if (!Part->Symlink_Mount_Point.empty())
250 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
251 if (!Part->Primary_Block_Device.empty())
252 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
253 if (!Part->Alternate_Block_Device.empty())
254 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
255 if (!Part->Decrypted_Block_Device.empty())
256 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
257 if (Part->Length != 0)
258 printf(" Length: %i\n", Part->Length);
259 if (!Part->Display_Name.empty())
260 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500261 if (!Part->Storage_Name.empty())
262 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800263 if (!Part->Backup_Path.empty())
264 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
265 if (!Part->Backup_Name.empty())
266 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500267 if (!Part->Backup_Display_Name.empty())
268 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800269 if (!Part->Backup_FileName.empty())
270 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
271 if (!Part->Storage_Path.empty())
272 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
273 if (!Part->Current_File_System.empty())
274 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
275 if (!Part->Fstab_File_System.empty())
276 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
277 if (Part->Format_Block_Size != 0)
278 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400279 if (!Part->MTD_Name.empty())
280 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400281 string back_meth = Part->Backup_Method_By_Name();
Ethan Yonker6277c792014-09-15 14:54:30 -0500282 printf(" Backup_Method: %s\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800283 if (Part->Mount_Flags || !Part->Mount_Options.empty())
284 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Ethan Yonker6277c792014-09-15 14:54:30 -0500285 printf("\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400286}
287
Dees_Troy51a0e822012-09-05 15:24:24 -0400288int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400289 std::vector<TWPartition*>::iterator iter;
290 int ret = false;
291 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400292 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400293
Dees_Troyd93bda52013-07-03 19:55:19 +0000294 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400295 return true;
296
Dees_Troy5bf43922012-09-07 16:07:55 -0400297 // Iterate through all partitions
298 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400299 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400300 ret = (*iter)->Mount(Display_Error);
301 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400302 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400303 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400304 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400305 }
306 if (found) {
307 return ret;
308 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000309 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400310 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000311 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400312 }
313 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400314}
315
Dees_Troy51a0e822012-09-05 15:24:24 -0400316int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400317 std::vector<TWPartition*>::iterator iter;
318 int ret = false;
319 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400320 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400321
322 // Iterate through all partitions
323 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400324 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400325 ret = (*iter)->UnMount(Display_Error);
326 found = true;
327 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
328 (*iter)->UnMount(Display_Error);
329 }
330 }
331 if (found) {
332 return ret;
333 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000334 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400335 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000336 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400337 }
338 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400339}
340
Dees_Troy51a0e822012-09-05 15:24:24 -0400341int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400342 TWPartition* Part = Find_Partition_By_Path(Path);
343
344 if (Part)
345 return Part->Is_Mounted();
346 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000347 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400348 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400349}
350
Dees_Troy5bf43922012-09-07 16:07:55 -0400351int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400352 string current_storage_path = DataManager::GetCurrentStoragePath();
353
354 if (Mount_By_Path(current_storage_path, Display_Error)) {
355 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
356 if (FreeStorage)
357 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
358 return true;
359 }
360 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400361}
362
Dees_Troy5bf43922012-09-07 16:07:55 -0400363int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
364 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
365}
366
367TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
368 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400369 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400370
371 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400372 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400373 return (*iter);
374 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400375 return NULL;
376}
377
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400378int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
379 // Check the backup name to ensure that it is the correct size and contains only valid characters
380 // and that a backup with that name doesn't already exist
381 char backup_name[MAX_BACKUP_NAME_LEN];
382 char backup_loc[255], tw_image_dir[255];
383 int copy_size;
384 int index, cur_char;
385 string Backup_Name, Backup_Loc;
386
387 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
388 copy_size = Backup_Name.size();
389 // Check size
390 if (copy_size > MAX_BACKUP_NAME_LEN) {
391 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000392 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400393 return -2;
394 }
395
396 // Check each character
397 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500398 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400399 return 0; // A "0" (zero) means to use the current timestamp for the backup name
400 for (index=0; index<copy_size; index++) {
401 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500402 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 -0400403 // These are valid characters
404 // Numbers
405 // Upper case letters
406 // Lower case letters
407 // Space
408 // and -_.{}[]
409 } else {
410 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000411 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400412 return -3;
413 }
414 }
415
416 // Check to make sure that a backup with this name doesn't already exist
417 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
418 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500419 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500420 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400421 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000422 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400423 return -4;
424 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400425 // No problems found, return 0
426 return 0;
427}
428
Dees_Troy43d8b002012-09-17 16:00:01 -0400429bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
430{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500431 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400432 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500433 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500434 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400435
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500436 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400437 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400438
Dees_Troyb46a6842012-09-25 11:06:46 -0400439 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000440 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400441
442 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500443 md5sum.setfn(Backup_Folder + Backup_Filename);
444 if (md5sum.computeMD5() == 0)
445 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000446 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500447 else
448 return -1;
449 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000450 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400451 } else {
452 char filename[512];
453 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500454 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400455 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500456 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000457 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400458 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000459 if (TWFunc::Path_Exists(filename)) {
460 if (md5sum.computeMD5() == 0) {
461 if (md5sum.write_md5digest() != 0)
462 {
463 gui_print(" * MD5 Error.\n");
464 return false;
465 }
466 } else {
467 gui_print(" * Error computing MD5.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500468 return false;
469 }
470 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400471 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400472 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500473 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400474 }
475 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000476 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400477 return false;
478 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000479 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400480 }
481 return true;
482}
483
Dees_Troy093b7642012-09-21 15:59:38 -0400484bool 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 -0400485 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500486 int img_bps;
487 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400488 unsigned long total_time, remain_time, section_time;
489 int use_compression, backup_time;
490 float pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500491 unsigned long long total_size, current_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400492
493 if (Part == NULL)
494 return true;
495
Dees_Troy093b7642012-09-21 15:59:38 -0400496 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
497
498 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
499 if (use_compression)
500 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
501 else
502 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
503
504 // We know the speed for both, how far into the whole backup are we, based on time
505 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
506 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
507
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500508 //pos = (total_time - remain_time) / (float) total_time;
509 total_size = *file_bytes + *img_bytes;
510 current_size = *file_bytes + *img_bytes - *file_bytes_remaining - *img_bytes_remaining;
511 pos = ((float)(current_size) / (float)(total_size));
Dees_Troy2673cec2013-04-02 20:22:16 +0000512 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400513
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500514 LOGINFO("Estimated total time: %lu\nEstimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400515
516 // And get the time
517 if (Part->Backup_Method == 1)
518 section_time = Part->Backup_Size / file_bps;
519 else
520 section_time = Part->Backup_Size / img_bps;
521
522 // Set the position
523 pos = section_time / (float) total_time;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500524 //DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400525
Tom Hite5a926722014-09-15 01:31:03 +0000526 TWFunc::SetPerformanceMode(true);
Dees_Troy43d8b002012-09-17 16:00:01 -0400527 time(&start);
528
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500529 if (Part->Backup(Backup_Folder, &total_size, &current_size)) {
Tom Hite5a926722014-09-15 01:31:03 +0000530 bool md5Success = false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500531 current_size += Part->Backup_Size;
532 pos = (float)((float)(current_size) / (float)(total_size));
533 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400534 if (Part->Has_SubPartition) {
535 std::vector<TWPartition*>::iterator subpart;
536
537 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500538 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Tom Hite5a926722014-09-15 01:31:03 +0000539 if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size)) {
540 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400541 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000542 }
Dees_Troy2727b992013-08-14 20:09:30 +0000543 sync();
544 sync();
Tom Hite5a926722014-09-15 01:31:03 +0000545 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName)) {
546 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400547 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000548 }
Dees_Troy093b7642012-09-21 15:59:38 -0400549 if (Part->Backup_Method == 1) {
550 *file_bytes_remaining -= (*subpart)->Backup_Size;
551 } else {
552 *img_bytes_remaining -= (*subpart)->Backup_Size;
553 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500554 current_size += Part->Backup_Size;
555 pos = (float)(current_size / total_size);
556 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400557 }
558 }
559 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400560 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400561 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000562 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400563 if (Part->Backup_Method == 1) {
564 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400565 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400566 } else {
567 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400568 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400569 }
Tom Hite5a926722014-09-15 01:31:03 +0000570
571 md5Success = Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
572 TWFunc::SetPerformanceMode(false);
573 return md5Success;
Dees_Troy43d8b002012-09-17 16:00:01 -0400574 } else {
Tom Hite5a926722014-09-15 01:31:03 +0000575 TWFunc::SetPerformanceMode(false);
Dees_Troy43d8b002012-09-17 16:00:01 -0400576 return false;
577 }
578}
579
580int TWPartitionManager::Run_Backup(void) {
581 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500582 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400583 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 -0400584 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500585 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400586 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400587 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400588 struct tm *t;
589 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500590 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400591 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500592 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400593
594 time(&total_start);
595
596 Update_System_Details();
597
598 if (!Mount_Current_Storage(true))
599 return false;
600
601 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400602 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400603 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400604 else
605 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400606
Dees_Troy43d8b002012-09-17 16:00:01 -0400607 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
608 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000609 if (Backup_Name == "(Current Date)") {
610 Backup_Name = TWFunc::Get_Current_Date();
611 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
612 TWFunc::Auto_Generate_Backup_Name();
613 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400614 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000615 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400616 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000617 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400618
Dees_Troy2673cec2013-04-02 20:22:16 +0000619 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500620 DataManager::GetValue("tw_backup_list", Backup_List);
621 if (!Backup_List.empty()) {
622 end_pos = Backup_List.find(";", start_pos);
623 while (end_pos != string::npos && start_pos < Backup_List.size()) {
624 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
625 backup_part = Find_Partition_By_Path(backup_path);
626 if (backup_part != NULL) {
627 partition_count++;
628 if (backup_part->Backup_Method == 1)
629 file_bytes += backup_part->Backup_Size;
630 else
631 img_bytes += backup_part->Backup_Size;
632 if (backup_part->Has_SubPartition) {
633 std::vector<TWPartition*>::iterator subpart;
634
635 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000636 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 -0500637 partition_count++;
638 if ((*subpart)->Backup_Method == 1)
639 file_bytes += (*subpart)->Backup_Size;
640 else
641 img_bytes += (*subpart)->Backup_Size;
642 }
643 }
Dees_Troy8170a922012-09-18 15:40:25 -0400644 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500645 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000646 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400647 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500648 start_pos = end_pos + 1;
649 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400650 }
651 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400652
653 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000654 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400655 return false;
656 }
657 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000658 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500659 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400660 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
661 if (storage != NULL) {
662 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000663 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400664 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000665 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400666 return false;
667 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000668 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400669 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000670 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400671 return false;
672 }
673 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500674 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400675
Dees_Troy2673cec2013-04-02 20:22:16 +0000676 gui_print("\n[BACKUP STARTED]\n");
677 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000678 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000679 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000680 return false;
681 }
682
Dees_Troy2673cec2013-04-02 20:22:16 +0000683 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400684
Dees_Troya13d74f2013-03-24 08:54:55 -0500685 start_pos = 0;
686 end_pos = Backup_List.find(";", start_pos);
687 while (end_pos != string::npos && start_pos < Backup_List.size()) {
688 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
689 backup_part = Find_Partition_By_Path(backup_path);
690 if (backup_part != NULL) {
691 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
692 return false;
693 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000694 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500695 }
696 start_pos = end_pos + 1;
697 end_pos = Backup_List.find(";", start_pos);
698 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400699
700 // Average BPS
701 if (img_time == 0)
702 img_time = 1;
703 if (file_time == 0)
704 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400705 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500706 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400707
Dees_Troy2673cec2013-04-02 20:22:16 +0000708 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
709 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400710
711 time(&total_stop);
712 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500713 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500714 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400715
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500716 int prev_img_bps, use_compression;
717 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400718 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
719 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500720 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400721
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500722 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400723 if (use_compression)
724 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500725 else
Dees_Troy093b7642012-09-21 15:59:38 -0400726 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
727 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500728 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400729
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500730 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400731 if (use_compression)
732 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
733 else
734 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
735
Dees_Troy2673cec2013-04-02 20:22:16 +0000736 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400737 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400738 UnMount_Main_Partitions();
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500739 gui_print_color("highlight", "[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000740 string backup_log = Full_Backup_Path + "recovery.log";
741 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
742 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400743}
744
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500745bool 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 -0400746 time_t Start, Stop;
Tom Hite5a926722014-09-15 01:31:03 +0000747 TWFunc::SetPerformanceMode(true);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400748 time(&Start);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500749 //DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Tom Hite5a926722014-09-15 01:31:03 +0000750 if (!Part->Restore(Restore_Name, total_restore_size, already_restored_size)) {
751 TWFunc::SetPerformanceMode(false);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400752 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000753 }
Dees_Troy8170a922012-09-18 15:40:25 -0400754 if (Part->Has_SubPartition) {
755 std::vector<TWPartition*>::iterator subpart;
756
757 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
758 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Tom Hite5a926722014-09-15 01:31:03 +0000759 if (!(*subpart)->Restore(Restore_Name, total_restore_size, already_restored_size)) {
760 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400761 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000762 }
Dees_Troy8170a922012-09-18 15:40:25 -0400763 }
764 }
765 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400766 time(&Stop);
Tom Hite5a926722014-09-15 01:31:03 +0000767 TWFunc::SetPerformanceMode(false);
Dees_Troy2673cec2013-04-02 20:22:16 +0000768 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 -0400769 return true;
770}
771
Dees_Troy51a0e822012-09-05 15:24:24 -0400772int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400773 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500774 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400775 time_t rStart, rStop;
776 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500777 string Restore_List, restore_path;
778 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500779 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400780
Dees_Troy2673cec2013-04-02 20:22:16 +0000781 gui_print("\n[RESTORE STARTED]\n\n");
782 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400783
Dees_Troy4a2a1262012-09-18 09:33:47 -0400784 if (!Mount_Current_Storage(true))
785 return false;
786
787 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500788 if (check_md5 > 0) {
789 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
790 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000791 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500792 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000793 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500794 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500795 gui_print("Calculating restore details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500796 DataManager::GetValue("tw_restore_selected", Restore_List);
797 if (!Restore_List.empty()) {
798 end_pos = Restore_List.find(";", start_pos);
799 while (end_pos != string::npos && start_pos < Restore_List.size()) {
800 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
801 restore_part = Find_Partition_By_Path(restore_path);
802 if (restore_part != NULL) {
803 partition_count++;
804 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
805 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500806 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500807 if (restore_part->Has_SubPartition) {
808 std::vector<TWPartition*>::iterator subpart;
809
810 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
811 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400812 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500813 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500814 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500815 }
816 }
817 }
818 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500819 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500820 }
821 start_pos = end_pos + 1;
822 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400823 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400824 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400825
826 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000827 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400828 return false;
829 }
830
Dees_Troy2673cec2013-04-02 20:22:16 +0000831 gui_print("Restoring %i partitions...\n", partition_count);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500832 gui_print("Total restore size is %lluMB\n", total_restore_size / 1048576);
Dees_Troy2673cec2013-04-02 20:22:16 +0000833 DataManager::SetProgress(0.0);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500834
Dees_Troya13d74f2013-03-24 08:54:55 -0500835 start_pos = 0;
836 if (!Restore_List.empty()) {
837 end_pos = Restore_List.find(";", start_pos);
838 while (end_pos != string::npos && start_pos < Restore_List.size()) {
839 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
840 restore_part = Find_Partition_By_Path(restore_path);
841 if (restore_part != NULL) {
842 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500843 if (!Restore_Partition(restore_part, Restore_Name, partition_count, &total_restore_size, &already_restored_size))
Dees_Troya13d74f2013-03-24 08:54:55 -0500844 return false;
845 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000846 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500847 }
848 start_pos = end_pos + 1;
849 end_pos = Restore_List.find(";", start_pos);
850 }
851 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400852 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400853 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400854 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400855 time(&rStop);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500856 gui_print_color("highlight", "[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500857 DataManager::SetValue("tw_file_progress", "");
Dees_Troy63c8df72012-09-10 14:02:05 -0400858 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400859}
860
861void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400862 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500863 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000864 bool get_date = true, check_encryption = true;
865
866 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400867
868 DIR* d;
869 d = opendir(Restore_Name.c_str());
870 if (d == NULL)
871 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000872 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400873 return;
874 }
875
876 struct dirent* de;
877 while ((de = readdir(d)) != NULL)
878 {
879 // Strip off three components
880 char str[256];
881 char* label;
882 char* fstype = NULL;
883 char* extn = NULL;
884 char* ptr;
885
886 strcpy(str, de->d_name);
887 if (strlen(str) <= 2)
888 continue;
889
890 if (get_date) {
891 char file_path[255];
892 struct stat st;
893
894 strcpy(file_path, Restore_Name.c_str());
895 strcat(file_path, "/");
896 strcat(file_path, str);
897 stat(file_path, &st);
898 string backup_date = ctime((const time_t*)(&st.st_mtime));
899 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
900 get_date = false;
901 }
902
903 label = str;
904 ptr = label;
905 while (*ptr && *ptr != '.') ptr++;
906 if (*ptr == '.')
907 {
908 *ptr = 0x00;
909 ptr++;
910 fstype = ptr;
911 }
912 while (*ptr && *ptr != '.') ptr++;
913 if (*ptr == '.')
914 {
915 *ptr = 0x00;
916 ptr++;
917 extn = ptr;
918 }
919
Dees_Troy83bd4832013-05-04 12:39:56 +0000920 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -0500921 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +0000922 if (extnlength != 3 && extnlength != 6) continue;
923 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
924 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
925
926 if (check_encryption) {
927 string filename = Restore_Name + "/";
928 filename += de->d_name;
929 if (TWFunc::Get_File_Type(filename) == 2) {
930 LOGINFO("'%s' is encrypted\n", filename.c_str());
931 DataManager::SetValue("tw_restore_encrypted", 1);
932 }
933 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500934 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -0400935
936 TWPartition* Part = Find_Partition_By_Path(label);
937 if (Part == NULL)
938 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000939 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -0400940 continue;
941 }
942
943 Part->Backup_FileName = de->d_name;
944 if (strlen(extn) > 3) {
945 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
946 }
947
Dees_Troya13d74f2013-03-24 08:54:55 -0500948 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -0400949 }
950 closedir(d);
951
Dees_Troya13d74f2013-03-24 08:54:55 -0500952 // Set the final value
953 DataManager::SetValue("tw_restore_list", Restore_List);
954 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -0400955 return;
956}
957
958int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400959 std::vector<TWPartition*>::iterator iter;
960 int ret = false;
961 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400962 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -0400963
964 // Iterate through all partitions
965 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400966 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -0400967 if (Path == "/and-sec")
968 ret = (*iter)->Wipe_AndSec();
969 else
970 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -0400971 found = true;
972 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
973 (*iter)->Wipe();
974 }
975 }
976 if (found) {
977 return ret;
978 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000979 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400980 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400981}
982
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500983int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
984 std::vector<TWPartition*>::iterator iter;
985 int ret = false;
986 bool found = false;
987 string Local_Path = TWFunc::Get_Root_Path(Path);
988
989 // Iterate through all partitions
990 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
991 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
992 if (Path == "/and-sec")
993 ret = (*iter)->Wipe_AndSec();
994 else
995 ret = (*iter)->Wipe(New_File_System);
996 found = true;
997 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
998 (*iter)->Wipe(New_File_System);
999 }
1000 }
1001 if (found) {
1002 return ret;
1003 } else
1004 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1005 return false;
1006}
1007
Dees_Troy51a0e822012-09-05 15:24:24 -04001008int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001009 std::vector<TWPartition*>::iterator iter;
1010 int ret = true;
1011
1012 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001013 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001014 if (!(*iter)->Wipe())
1015 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001016 } else if ((*iter)->Has_Android_Secure) {
1017 if (!(*iter)->Wipe_AndSec())
1018 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001019 }
1020 }
1021 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001022}
1023
Dees_Troy38bd7602012-09-14 13:33:53 -04001024int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1025 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001026 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001027
1028 if (!Mount_By_Path("/data", true))
1029 return false;
1030
1031 if (!Mount_By_Path("/cache", true))
1032 return false;
1033
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001034 dir.push_back("/data/dalvik-cache");
1035 dir.push_back("/cache/dalvik-cache");
1036 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001037 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001038 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001039 if (stat(dir.at(i).c_str(), &st) == 0) {
1040 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001041 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001042 }
1043 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001044 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001045 if (sdext && sdext->Is_Present && sdext->Mount(false))
1046 {
1047 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1048 {
1049 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001050 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001051 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001052 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001053 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001054 return true;
1055}
1056
1057int TWPartitionManager::Wipe_Rotate_Data(void) {
1058 if (!Mount_By_Path("/data", true))
1059 return false;
1060
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001061 unlink("/data/misc/akmd*");
1062 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001063 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001064 return true;
1065}
1066
1067int TWPartitionManager::Wipe_Battery_Stats(void) {
1068 struct stat st;
1069
1070 if (!Mount_By_Path("/data", true))
1071 return false;
1072
1073 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001074 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001075 } else {
1076 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001077 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001078 }
1079 return true;
1080}
1081
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001082int TWPartitionManager::Wipe_Android_Secure(void) {
1083 std::vector<TWPartition*>::iterator iter;
1084 int ret = false;
1085 bool found = false;
1086
1087 // Iterate through all partitions
1088 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1089 if ((*iter)->Has_Android_Secure) {
1090 ret = (*iter)->Wipe_AndSec();
1091 found = true;
1092 }
1093 }
1094 if (found) {
1095 return ret;
1096 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001097 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001098 }
1099 return false;
1100}
1101
Dees_Troy38bd7602012-09-14 13:33:53 -04001102int TWPartitionManager::Format_Data(void) {
1103 TWPartition* dat = Find_Partition_By_Path("/data");
1104
1105 if (dat != NULL) {
1106 if (!dat->UnMount(true))
1107 return false;
1108
1109 return dat->Wipe_Encryption();
1110 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001111 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001112 return false;
1113 }
1114 return false;
1115}
1116
1117int TWPartitionManager::Wipe_Media_From_Data(void) {
1118 TWPartition* dat = Find_Partition_By_Path("/data");
1119
1120 if (dat != NULL) {
1121 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001122 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001123 return false;
1124 }
1125 if (!dat->Mount(true))
1126 return false;
1127
Dees_Troy2673cec2013-04-02 20:22:16 +00001128 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001129 mtp_was_enabled = TWFunc::Toggle_MTP(false);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001130 TWFunc::removeDir("/data/media", false);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001131 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
1132 if (mtp_was_enabled) {
1133 if (!Enable_MTP())
1134 Disable_MTP();
1135 }
1136 return false;
1137 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001138 if (dat->Has_Data_Media) {
1139 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001140 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1141 dat->UnMount(false);
1142 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001143 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001144 if (mtp_was_enabled) {
1145 if (!Enable_MTP())
1146 Disable_MTP();
1147 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001148 return true;
1149 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001150 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001151 return false;
1152 }
1153 return false;
1154}
1155
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001156int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1157 std::vector<TWPartition*>::iterator iter;
1158 int ret = false;
1159 bool found = false;
1160 string Local_Path = TWFunc::Get_Root_Path(Path);
1161
1162 if (Local_Path == "/tmp" || Local_Path == "/")
1163 return true;
1164
1165 // Iterate through all partitions
1166 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1167 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1168 ret = (*iter)->Repair();
1169 found = true;
1170 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1171 (*iter)->Repair();
1172 }
1173 }
1174 if (found) {
1175 return ret;
1176 } else if (Display_Error) {
1177 LOGERR("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1178 } else {
1179 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1180 }
1181 return false;
1182}
1183
Dees_Troy51a0e822012-09-05 15:24:24 -04001184void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001185 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001186 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001187
Dees_Troy2673cec2013-04-02 20:22:16 +00001188 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001189 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001190 if ((*iter)->Can_Be_Mounted) {
1191 (*iter)->Update_Size(true);
1192 if ((*iter)->Mount_Point == "/system") {
1193 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1194 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1195 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1196 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1197 } else if ((*iter)->Mount_Point == "/cache") {
1198 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1199 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1200 } else if ((*iter)->Mount_Point == "/sd-ext") {
1201 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1202 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1203 if ((*iter)->Backup_Size == 0) {
1204 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1205 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1206 } else
1207 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001208 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001209 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001210 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001211 if ((*iter)->Backup_Size == 0) {
1212 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1213 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1214 } else
1215 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001216 } else if ((*iter)->Mount_Point == "/boot") {
1217 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1218 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1219 if ((*iter)->Backup_Size == 0) {
1220 DataManager::SetValue("tw_has_boot_partition", 0);
1221 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1222 } else
1223 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001224 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001225#ifdef SP1_NAME
1226 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1227 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1228 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1229 }
1230#endif
1231#ifdef SP2_NAME
1232 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1233 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1234 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1235 }
1236#endif
1237#ifdef SP3_NAME
1238 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1239 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1240 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1241 }
1242#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001243 } else {
1244 // Handle unmountable partitions in case we reset defaults
1245 if ((*iter)->Mount_Point == "/boot") {
1246 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1247 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1248 if ((*iter)->Backup_Size == 0) {
1249 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1250 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1251 } else
1252 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1253 } else if ((*iter)->Mount_Point == "/recovery") {
1254 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1255 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1256 if ((*iter)->Backup_Size == 0) {
1257 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1258 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1259 } else
1260 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001261 } else if ((*iter)->Mount_Point == "/data") {
1262 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001263 }
1264#ifdef SP1_NAME
1265 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1266 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1267 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1268 }
1269#endif
1270#ifdef SP2_NAME
1271 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1272 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1273 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1274 }
1275#endif
1276#ifdef SP3_NAME
1277 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1278 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1279 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1280 }
1281#endif
Dees_Troy51127312012-09-08 13:08:49 -04001282 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001283 }
that39b7c162013-11-22 22:07:33 +01001284 gui_print("...done\n");
Dees_Troy51127312012-09-08 13:08:49 -04001285 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1286 string current_storage_path = DataManager::GetCurrentStoragePath();
1287 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001288 if (FreeStorage != NULL) {
1289 // Attempt to mount storage
1290 if (!FreeStorage->Mount(false)) {
1291 // We couldn't mount storage... check to see if we have dual storage
1292 int has_dual_storage;
1293 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1294 if (has_dual_storage == 1) {
1295 // We have dual storage, see if we're using the internal storage that should always be present
1296 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001297 if (!FreeStorage->Is_Encrypted) {
1298 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001299 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001300 }
Dees_Troy8170a922012-09-18 15:40:25 -04001301 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1302 } else {
1303 // We were using external, flip to internal
1304 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1305 current_storage_path = DataManager::GetCurrentStoragePath();
1306 FreeStorage = Find_Partition_By_Path(current_storage_path);
1307 if (FreeStorage != NULL) {
1308 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1309 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001310 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001311 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1312 }
1313 }
1314 } else {
1315 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001316 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001317 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1318 }
1319 } else {
1320 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1321 }
1322 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001323 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001324 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001325 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001326 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001327 return;
1328}
1329
1330int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001331#ifdef TW_INCLUDE_CRYPTO
1332 int ret_val, password_len;
1333 char crypto_blkdev[255], cPassword[255];
1334 size_t result;
1335
1336 property_set("ro.crypto.state", "encrypted");
1337#ifdef TW_INCLUDE_JB_CRYPTO
1338 // No extra flags needed
1339#else
1340 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1341 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1342 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1343 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1344 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1345 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001346
1347#ifdef CRYPTO_SD_FS_TYPE
Ethan Yonker71413f42014-02-26 13:36:08 -06001348 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1349 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1350 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001351#endif
a39552696ff55ce2013-01-08 16:14:56 +00001352
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001353 property_set("rw.km_fips_status", "ready");
a39552696ff55ce2013-01-08 16:14:56 +00001354
1355#endif
1356
1357 // some samsung devices store "footer" on efs partition
1358 TWPartition *efs = Find_Partition_By_Path("/efs");
1359 if(efs && !efs->Is_Mounted())
1360 efs->Mount(false);
1361 else
1362 efs = 0;
1363#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001364#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001365 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001366 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001367 property_set("ro.crypto.external_encrypted", "1");
1368 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1369 } else {
1370 property_set("ro.crypto.external_encrypted", "0");
1371 }
1372#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001373#endif
a39552696ff55ce2013-01-08 16:14:56 +00001374
Dees_Troy5bf43922012-09-07 16:07:55 -04001375 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001376 int pwret = cryptfs_check_passwd(cPassword);
1377
1378 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001379 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001380 return -1;
1381 }
a39552696ff55ce2013-01-08 16:14:56 +00001382
1383 if(efs)
1384 efs->UnMount(false);
1385
Dees_Troy5bf43922012-09-07 16:07:55 -04001386 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1387 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001388 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001389 } else {
1390 TWPartition* dat = Find_Partition_By_Path("/data");
1391 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001392 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001393 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1394 dat->Is_Decrypted = true;
1395 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001396 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001397 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 +00001398 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001399
1400#ifdef CRYPTO_SD_FS_TYPE
1401 char crypto_blkdev_sd[255];
1402 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1403 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001404 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001405 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001406 emmc->Is_Decrypted = true;
1407 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1408 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001409 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001410 }
a39552696ff55ce2013-01-08 16:14:56 +00001411#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001412#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001413#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001414 char is_external_decrypted[255];
1415 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1416 if (strcmp(is_external_decrypted, "1") == 0) {
1417 sdcard->Is_Decrypted = true;
1418 sdcard->EcryptFS_Password = Password;
1419 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001420 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1421 MetaEcfsFile += "/.MetaEcfsFile";
1422 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1423 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1424 sdcard->UnMount(false);
1425 sdcard->Mount(false);
1426 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001427 } else {
Dees_Troy066eb302013-08-23 17:20:32 +00001428 LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001429 sdcard->Is_Decrypted = false;
1430 sdcard->Decrypted_Block_Device = "";
1431 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001432#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001433#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001434
Dees_Troy5bf43922012-09-07 16:07:55 -04001435 // Sleep for a bit so that the device will be ready
1436 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001437 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001438 dat->Storage_Path = "/data/media/0";
1439 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001440 DataManager::SetValue("tw_storage_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001441 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001442 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001443 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001444 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001445 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001446 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001447 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001448 }
1449 return 0;
1450#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001451 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001452 return -1;
1453#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001454 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001455}
1456
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001457int TWPartitionManager::Fix_Permissions(void) {
1458 int result = 0;
1459 if (!Mount_By_Path("/data", true))
1460 return false;
1461
1462 if (!Mount_By_Path("/system", true))
1463 return false;
1464
1465 Mount_By_Path("/sd-ext", false);
1466
1467 fixPermissions perms;
1468 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001469 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001470 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001471 return result;
1472}
1473
Ethan Yonker47360be2014-04-01 10:34:34 -05001474TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1475 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1476
1477 if (!Path.empty()) {
1478 string Search_Path = TWFunc::Get_Root_Path(Path);
1479 for (; iter != Partitions.end(); iter++) {
1480 if ((*iter)->Mount_Point == Search_Path) {
1481 iter++;
1482 break;
1483 }
1484 }
1485 }
1486
1487 for (; iter != Partitions.end(); iter++) {
1488 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) {
1489 return (*iter);
1490 }
1491 }
1492
1493 return NULL;
1494}
1495
Dees_Troyd21618c2012-10-14 18:48:49 -04001496int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001497 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1498
1499 if (Part == NULL) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001500 LOGERR("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
Dees_Troyd21618c2012-10-14 18:48:49 -04001501 return false;
1502 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001503 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1504 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001505 return false;
1506
Dees_Troy2673cec2013-04-02 20:22:16 +00001507 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1508 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001509 return false;
1510 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001511 return true;
1512}
1513
Dees_Troy8170a922012-09-18 15:40:25 -04001514int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001515 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001516 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001517 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001518
Ethan Yonker9eb1cd42014-11-03 22:14:08 -06001519 property_set("sys.storage.ums_enabled", "1");
1520 sleep(1);
Dees_Troy8170a922012-09-18 15:40:25 -04001521 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001522 string Lun_File_str = CUSTOM_LUN_FILE;
1523 size_t found = Lun_File_str.find("%");
1524 if (found != string::npos) {
1525 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1526 if (TWFunc::Path_Exists(lun_file))
1527 has_multiple_lun = true;
1528 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001529 mtp_was_enabled = TWFunc::Toggle_MTP(false);
Ethan Yonker47360be2014-04-01 10:34:34 -05001530 if (!has_multiple_lun) {
1531 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1532 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1533 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
1534 TWPartition* Mount = Find_Next_Storage("", "/data");
1535 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001536 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1537 goto error_handle;
1538 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001539 } else {
1540 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001541 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001542 }
1543 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001544 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001545 }
Dees_Troy8170a922012-09-18 15:40:25 -04001546 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001547 LOGINFO("Device has multiple lun files\n");
1548 TWPartition* Mount1;
1549 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001550 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker47360be2014-04-01 10:34:34 -05001551 Mount1 = Find_Next_Storage("", "/data");
1552 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001553 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1554 goto error_handle;
1555 }
Matt Moweree717062014-04-26 01:46:46 -05001556 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker47360be2014-04-01 10:34:34 -05001557 Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data");
1558 if (Mount2) {
Matt Moweree717062014-04-26 01:46:46 -05001559 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001560 }
1561 } else {
1562 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001563 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001564 }
Dees_Troy8170a922012-09-18 15:40:25 -04001565 }
1566 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001567error_handle:
1568 if (mtp_was_enabled)
1569 if (!Enable_MTP())
1570 Disable_MTP();
Ethan Yonker9eb1cd42014-11-03 22:14:08 -06001571 property_set("sys.storage.ums_enabled", "0");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001572 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001573}
1574
1575int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001576 int index, ret;
1577 char lun_file[255], ch[2] = {0, 0};
1578 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001579
1580 for (index=0; index<2; index++) {
1581 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001582 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001583 if (ret < 0) {
1584 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001585 }
Dees_Troy8170a922012-09-18 15:40:25 -04001586 }
Dees_Troye58d5262012-09-21 12:27:57 -04001587 Mount_All_Storage();
1588 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001589 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001590 property_set("sys.storage.ums_enabled", "0");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001591 if (mtp_was_enabled)
1592 if (!Enable_MTP())
1593 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001594 if (ret < 0 && index == 0) {
1595 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1596 return false;
1597 } else {
1598 return true;
1599 }
Dees_Troy8170a922012-09-18 15:40:25 -04001600 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001601}
1602
1603void TWPartitionManager::Mount_All_Storage(void) {
1604 std::vector<TWPartition*>::iterator iter;
1605
1606 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1607 if ((*iter)->Is_Storage)
1608 (*iter)->Mount(false);
1609 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001610}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001611
Dees_Troyd0384ef2012-10-12 12:15:42 -04001612void TWPartitionManager::UnMount_Main_Partitions(void) {
1613 // Unmounts system and data if data is not data/media
1614 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001615 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001616
1617 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1618
1619 UnMount_By_Path("/system", true);
Ethan Yonker6277c792014-09-15 14:54:30 -05001620 if (!datamedia)
1621 UnMount_By_Path("/data", true);
1622
Dees_Troyd0384ef2012-10-12 12:15:42 -04001623 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1624 Boot_Partition->UnMount(true);
1625}
1626
Dees_Troy9350b8d2012-09-27 12:38:38 -04001627int TWPartitionManager::Partition_SDCard(void) {
1628 char mkdir_path[255], temp[255], line[512];
1629 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1630 int ext, swap, total_size = 0, fat_size;
1631 FILE* fp;
1632
Dees_Troy2673cec2013-04-02 20:22:16 +00001633 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001634#ifdef TW_EXTERNAL_STORAGE_PATH
1635 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1636#else
1637 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1638#endif
Ethan Yonker1eff6cd2014-09-15 13:30:42 -05001639 if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001640 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001641 return false;
1642 }
1643 if (!SDCard->UnMount(true))
1644 return false;
1645 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1646 if (SDext != NULL) {
1647 if (!SDext->UnMount(true))
1648 return false;
1649 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001650
1651 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001652 Device = SDCard->Actual_Block_Device;
1653 // Just use the root block device
1654 Device.resize(strlen("/dev/block/mmcblkX"));
1655
1656 // Find the size of the block device:
1657 fp = fopen("/proc/partitions", "rt");
1658 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001659 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001660 return false;
1661 }
1662
1663 while (fgets(line, sizeof(line), fp) != NULL)
1664 {
1665 unsigned long major, minor, blocks;
1666 char device[512];
1667 char tmpString[64];
1668
1669 if (strlen(line) < 7 || line[0] == 'm') continue;
1670 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1671
1672 tmpdevice = "/dev/block/";
1673 tmpdevice += device;
1674 if (tmpdevice == Device) {
1675 // Adjust block size to byte size
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001676 total_size = (int)(blocks * 1024ULL / 1000000LLU);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001677 break;
1678 }
1679 }
1680 fclose(fp);
1681
1682 DataManager::GetValue("tw_sdext_size", ext);
1683 DataManager::GetValue("tw_swap_size", swap);
1684 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1685 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001686 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 -04001687 memset(temp, 0, sizeof(temp));
1688 sprintf(temp, "%i", fat_size);
1689 fat_str = temp;
1690 memset(temp, 0, sizeof(temp));
1691 sprintf(temp, "%i", fat_size + ext);
1692 ext_str = temp;
1693 memset(temp, 0, sizeof(temp));
1694 sprintf(temp, "%i", fat_size + ext + swap);
1695 swap_str = temp;
1696 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001697 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001698 return false;
1699 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001700 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001701 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001702 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001703 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001704 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001705 Update_System_Details();
1706 return false;
1707 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001708 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001709 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001710 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001711 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001712 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001713 return false;
1714 }
1715 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001716 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001717 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001718 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001719 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001720 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001721 Update_System_Details();
1722 return false;
1723 }
1724 }
1725 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001726 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001727 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001728 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001729 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001730 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001731 Update_System_Details();
1732 return false;
1733 }
1734 }
1735 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1736#ifdef TW_EXTERNAL_STORAGE_PATH
1737 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1738 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1739 memset(mkdir_path, 0, sizeof(mkdir_path));
1740 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1741#else
1742 Mount_By_Path("/sdcard", 1);
1743 strcpy(mkdir_path, "/sdcard/TWRP");
1744#endif
1745 mkdir(mkdir_path, 0777);
1746 DataManager::Flush();
1747#ifdef TW_EXTERNAL_STORAGE_PATH
1748 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1749 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1750 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1751#else
1752 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1753 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1754 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1755#endif
1756 if (ext > 0) {
1757 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001758 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001759 return false;
1760 }
1761 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001762 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1763 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001764 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001765 }
1766
1767 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00001768 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001769 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001770}
Dees_Troya13d74f2013-03-24 08:54:55 -05001771
1772void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1773 std::vector<TWPartition*>::iterator iter;
1774 if (ListType == "mount") {
1775 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1776 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1777 struct PartitionList part;
1778 part.Display_Name = (*iter)->Display_Name;
1779 part.Mount_Point = (*iter)->Mount_Point;
1780 part.selected = (*iter)->Is_Mounted();
1781 Partition_List->push_back(part);
1782 }
1783 }
1784 } else if (ListType == "storage") {
1785 char free_space[255];
1786 string Current_Storage = DataManager::GetCurrentStoragePath();
1787 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1788 if ((*iter)->Is_Storage) {
1789 struct PartitionList part;
1790 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1791 part.Display_Name = (*iter)->Storage_Name + " (";
1792 part.Display_Name += free_space;
1793 part.Display_Name += "MB)";
1794 part.Mount_Point = (*iter)->Storage_Path;
1795 if ((*iter)->Storage_Path == Current_Storage)
1796 part.selected = 1;
1797 else
1798 part.selected = 0;
1799 Partition_List->push_back(part);
1800 }
1801 }
1802 } else if (ListType == "backup") {
1803 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001804 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001805 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001806 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001807 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001808 Backup_Size = (*iter)->Backup_Size;
1809 if ((*iter)->Has_SubPartition) {
1810 std::vector<TWPartition*>::iterator subpart;
1811
1812 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1813 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1814 Backup_Size += (*subpart)->Backup_Size;
1815 }
1816 }
1817 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001818 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1819 part.Display_Name += backup_size;
1820 part.Display_Name += "MB)";
1821 part.Mount_Point = (*iter)->Backup_Path;
1822 part.selected = 0;
1823 Partition_List->push_back(part);
1824 }
1825 }
1826 } else if (ListType == "restore") {
1827 string Restore_List, restore_path;
1828 TWPartition* restore_part = NULL;
1829
1830 DataManager::GetValue("tw_restore_list", Restore_List);
1831 if (!Restore_List.empty()) {
1832 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1833 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1834 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05001835 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00001836 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001837 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05001838 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05001839 } else {
1840 struct PartitionList part;
1841 part.Display_Name = restore_part->Backup_Display_Name;
1842 part.Mount_Point = restore_part->Backup_Path;
1843 part.selected = 1;
1844 Partition_List->push_back(part);
1845 }
1846 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001847 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001848 }
1849 start_pos = end_pos + 1;
1850 end_pos = Restore_List.find(";", start_pos);
1851 }
1852 }
1853 } else if (ListType == "wipe") {
1854 struct PartitionList dalvik;
1855 dalvik.Display_Name = "Dalvik Cache";
1856 dalvik.Mount_Point = "DALVIK";
1857 dalvik.selected = 0;
1858 Partition_List->push_back(dalvik);
1859 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1860 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1861 struct PartitionList part;
1862 part.Display_Name = (*iter)->Display_Name;
1863 part.Mount_Point = (*iter)->Mount_Point;
1864 part.selected = 0;
1865 Partition_List->push_back(part);
1866 }
1867 if ((*iter)->Has_Android_Secure) {
1868 struct PartitionList part;
1869 part.Display_Name = (*iter)->Backup_Display_Name;
1870 part.Mount_Point = (*iter)->Backup_Path;
1871 part.selected = 0;
1872 Partition_List->push_back(part);
1873 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001874 if ((*iter)->Has_Data_Media) {
1875 struct PartitionList datamedia;
1876 datamedia.Display_Name = (*iter)->Storage_Name;
1877 datamedia.Mount_Point = "INTERNAL";
1878 datamedia.selected = 0;
1879 Partition_List->push_back(datamedia);
1880 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001881 }
1882 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001883 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001884 }
1885}
1886
1887int TWPartitionManager::Fstab_Processed(void) {
1888 return Partitions.size();
1889}
Dees_Troyd93bda52013-07-03 19:55:19 +00001890
1891void TWPartitionManager::Output_Storage_Fstab(void) {
1892 std::vector<TWPartition*>::iterator iter;
1893 char storage_partition[255];
1894 string Temp;
1895 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
1896
1897 if (fp == NULL) {
1898 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
1899 return;
1900 }
1901
1902 // Iterate through all partitions
1903 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1904 if ((*iter)->Is_Storage) {
1905 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
1906 strcpy(storage_partition, Temp.c_str());
1907 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
1908 }
1909 }
1910 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001911}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02001912
1913TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
1914{
1915 TWPartition *res = NULL;
1916 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
1917 if(!(*iter)->Is_Storage)
1918 continue;
1919
1920 if((*iter)->Is_Settings_Storage)
1921 return *iter;
1922
1923 if(!res)
1924 res = *iter;
1925 }
1926 return res;
1927}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001928
1929bool TWPartitionManager::Enable_MTP(void) {
1930#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001931 if (mtppid) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001932 LOGERR("MTP already enabled\n");
1933 return true;
1934 }
1935 //Launch MTP Responder
1936 LOGINFO("Starting MTP\n");
1937 char vendor[PROPERTY_VALUE_MAX];
1938 char product[PROPERTY_VALUE_MAX];
1939 int count = 0;
1940 property_set("sys.usb.config", "none");
1941 property_get("usb.vendor", vendor, "18D1");
1942 property_get("usb.product.mtpadb", product, "4EE2");
1943 string vendorstr = vendor;
1944 string productstr = product;
1945 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
1946 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
1947 property_set("sys.usb.config", "mtp,adb");
1948 std::vector<TWPartition*>::iterator iter;
Ethan Yonker6d154c42014-09-03 14:19:43 -05001949 /* To enable MTP debug, use the twrp command line feature to
1950 * twrp set tw_mtp_debug 1
1951 */
1952 twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug"));
that9e0593e2014-10-08 00:01:24 +02001953 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 -04001954 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1955 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false)) {
that9e0593e2014-10-08 00:01:24 +02001956 ++storageid;
1957 printf("twrp addStorage %s, mtpstorageid: %u\n", (*iter)->Storage_Path.c_str(), storageid);
1958 mtp->addStorage((*iter)->Storage_Name, (*iter)->Storage_Path, storageid);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001959 count++;
1960 }
1961 }
1962 if (count) {
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001963 mtppid = mtp->forkserver();
1964 if (mtppid) {
1965 DataManager::SetValue("tw_mtp_enabled", 1);
1966 return true;
1967 } else {
1968 LOGERR("Failed to enable MTP\n");
1969 return false;
1970 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001971 }
1972 LOGERR("No valid storage partitions found for MTP.\n");
1973#else
1974 LOGERR("MTP support not included\n");
1975#endif
1976 DataManager::SetValue("tw_mtp_enabled", 0);
1977 return false;
1978}
1979
1980bool TWPartitionManager::Disable_MTP(void) {
1981#ifdef TW_HAS_MTP
1982 char vendor[PROPERTY_VALUE_MAX];
1983 char product[PROPERTY_VALUE_MAX];
1984 property_set("sys.usb.config", "none");
1985 property_get("usb.vendor", vendor, "18D1");
1986 property_get("usb.product.adb", product, "D002");
1987 string vendorstr = vendor;
1988 string productstr = product;
1989 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
1990 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001991 if (mtppid) {
Ethan Yonker8613dc02014-09-11 09:28:20 -05001992 LOGINFO("Disabling MTP\n");
1993 int status;
1994 kill(mtppid, SIGKILL);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001995 mtppid = 0;
Ethan Yonker8613dc02014-09-11 09:28:20 -05001996 // We don't care about the exit value, but this prevents a zombie process
1997 waitpid(mtppid, &status, 0);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001998 }
1999 property_set("sys.usb.config", "adb");
2000 DataManager::SetValue("tw_mtp_enabled", 0);
2001 return true;
2002#else
2003 LOGERR("MTP support not included\n");
2004 DataManager::SetValue("tw_mtp_enabled", 0);
2005 return false;
2006#endif
2007}