blob: 71c1fcbb9f94ac843f195d25387455f8289ead34 [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 }
Dees_Troy51127312012-09-08 13:08:49 -04001284 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1285 string current_storage_path = DataManager::GetCurrentStoragePath();
1286 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001287 if (FreeStorage != NULL) {
1288 // Attempt to mount storage
1289 if (!FreeStorage->Mount(false)) {
1290 // We couldn't mount storage... check to see if we have dual storage
1291 int has_dual_storage;
1292 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1293 if (has_dual_storage == 1) {
1294 // We have dual storage, see if we're using the internal storage that should always be present
1295 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001296 if (!FreeStorage->Is_Encrypted) {
1297 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001298 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001299 }
Dees_Troy8170a922012-09-18 15:40:25 -04001300 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1301 } else {
1302 // We were using external, flip to internal
1303 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1304 current_storage_path = DataManager::GetCurrentStoragePath();
1305 FreeStorage = Find_Partition_By_Path(current_storage_path);
1306 if (FreeStorage != NULL) {
1307 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1308 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001309 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001310 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1311 }
1312 }
1313 } else {
1314 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001315 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001316 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1317 }
1318 } else {
1319 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1320 }
1321 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001322 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001323 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001324 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001325 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001326 return;
1327}
1328
1329int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001330#ifdef TW_INCLUDE_CRYPTO
1331 int ret_val, password_len;
1332 char crypto_blkdev[255], cPassword[255];
1333 size_t result;
1334
1335 property_set("ro.crypto.state", "encrypted");
1336#ifdef TW_INCLUDE_JB_CRYPTO
1337 // No extra flags needed
1338#else
1339 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1340 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1341 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1342 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1343 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1344 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001345
1346#ifdef CRYPTO_SD_FS_TYPE
Ethan Yonker71413f42014-02-26 13:36:08 -06001347 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1348 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1349 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001350#endif
a39552696ff55ce2013-01-08 16:14:56 +00001351
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001352 property_set("rw.km_fips_status", "ready");
a39552696ff55ce2013-01-08 16:14:56 +00001353
1354#endif
1355
1356 // some samsung devices store "footer" on efs partition
1357 TWPartition *efs = Find_Partition_By_Path("/efs");
1358 if(efs && !efs->Is_Mounted())
1359 efs->Mount(false);
1360 else
1361 efs = 0;
1362#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001363#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001364 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001365 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001366 property_set("ro.crypto.external_encrypted", "1");
1367 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1368 } else {
1369 property_set("ro.crypto.external_encrypted", "0");
1370 }
1371#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001372#endif
a39552696ff55ce2013-01-08 16:14:56 +00001373
Dees_Troy5bf43922012-09-07 16:07:55 -04001374 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001375 int pwret = cryptfs_check_passwd(cPassword);
1376
1377 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001378 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001379 return -1;
1380 }
a39552696ff55ce2013-01-08 16:14:56 +00001381
1382 if(efs)
1383 efs->UnMount(false);
1384
Dees_Troy5bf43922012-09-07 16:07:55 -04001385 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1386 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001387 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001388 } else {
1389 TWPartition* dat = Find_Partition_By_Path("/data");
1390 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001391 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001392 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1393 dat->Is_Decrypted = true;
1394 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001395 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001396 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 +00001397 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001398
1399#ifdef CRYPTO_SD_FS_TYPE
1400 char crypto_blkdev_sd[255];
1401 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1402 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001403 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001404 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001405 emmc->Is_Decrypted = true;
1406 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1407 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001408 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001409 }
a39552696ff55ce2013-01-08 16:14:56 +00001410#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001411#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001412#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001413 char is_external_decrypted[255];
1414 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1415 if (strcmp(is_external_decrypted, "1") == 0) {
1416 sdcard->Is_Decrypted = true;
1417 sdcard->EcryptFS_Password = Password;
1418 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001419 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1420 MetaEcfsFile += "/.MetaEcfsFile";
1421 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1422 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1423 sdcard->UnMount(false);
1424 sdcard->Mount(false);
1425 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001426 } else {
Dees_Troy066eb302013-08-23 17:20:32 +00001427 LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001428 sdcard->Is_Decrypted = false;
1429 sdcard->Decrypted_Block_Device = "";
1430 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001431#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001432#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001433
Dees_Troy5bf43922012-09-07 16:07:55 -04001434 // Sleep for a bit so that the device will be ready
1435 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001436 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001437 dat->Storage_Path = "/data/media/0";
1438 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001439 DataManager::SetValue("tw_storage_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001440 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001441 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001442 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001443 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001444 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001445 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001446 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001447 }
1448 return 0;
1449#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001450 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001451 return -1;
1452#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001453 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001454}
1455
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001456int TWPartitionManager::Fix_Permissions(void) {
1457 int result = 0;
1458 if (!Mount_By_Path("/data", true))
1459 return false;
1460
1461 if (!Mount_By_Path("/system", true))
1462 return false;
1463
1464 Mount_By_Path("/sd-ext", false);
1465
1466 fixPermissions perms;
1467 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001468 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001469 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001470 return result;
1471}
1472
Ethan Yonker47360be2014-04-01 10:34:34 -05001473TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1474 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1475
1476 if (!Path.empty()) {
1477 string Search_Path = TWFunc::Get_Root_Path(Path);
1478 for (; iter != Partitions.end(); iter++) {
1479 if ((*iter)->Mount_Point == Search_Path) {
1480 iter++;
1481 break;
1482 }
1483 }
1484 }
1485
1486 for (; iter != Partitions.end(); iter++) {
1487 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) {
1488 return (*iter);
1489 }
1490 }
1491
1492 return NULL;
1493}
1494
Dees_Troyd21618c2012-10-14 18:48:49 -04001495int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001496 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1497
1498 if (Part == NULL) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001499 LOGERR("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
Dees_Troyd21618c2012-10-14 18:48:49 -04001500 return false;
1501 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001502 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1503 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001504 return false;
1505
Dees_Troy2673cec2013-04-02 20:22:16 +00001506 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1507 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001508 return false;
1509 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001510 return true;
1511}
1512
Dees_Troy8170a922012-09-18 15:40:25 -04001513int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001514 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001515 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001516 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001517
Dees_Troy8170a922012-09-18 15:40:25 -04001518 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001519 string Lun_File_str = CUSTOM_LUN_FILE;
1520 size_t found = Lun_File_str.find("%");
1521 if (found != string::npos) {
1522 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1523 if (TWFunc::Path_Exists(lun_file))
1524 has_multiple_lun = true;
1525 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001526 mtp_was_enabled = TWFunc::Toggle_MTP(false);
Ethan Yonker47360be2014-04-01 10:34:34 -05001527 if (!has_multiple_lun) {
1528 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1529 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1530 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
1531 TWPartition* Mount = Find_Next_Storage("", "/data");
1532 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001533 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1534 goto error_handle;
1535 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001536 } else {
1537 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001538 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001539 }
1540 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001541 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001542 }
Dees_Troy8170a922012-09-18 15:40:25 -04001543 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001544 LOGINFO("Device has multiple lun files\n");
1545 TWPartition* Mount1;
1546 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001547 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker47360be2014-04-01 10:34:34 -05001548 Mount1 = Find_Next_Storage("", "/data");
1549 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001550 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1551 goto error_handle;
1552 }
Matt Moweree717062014-04-26 01:46:46 -05001553 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker47360be2014-04-01 10:34:34 -05001554 Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data");
1555 if (Mount2) {
Matt Moweree717062014-04-26 01:46:46 -05001556 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001557 }
1558 } else {
1559 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001560 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001561 }
Dees_Troy8170a922012-09-18 15:40:25 -04001562 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001563 property_set("sys.storage.ums_enabled", "1");
Dees_Troy8170a922012-09-18 15:40:25 -04001564 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001565error_handle:
1566 if (mtp_was_enabled)
1567 if (!Enable_MTP())
1568 Disable_MTP();
1569 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001570}
1571
1572int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001573 int index, ret;
1574 char lun_file[255], ch[2] = {0, 0};
1575 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001576
1577 for (index=0; index<2; index++) {
1578 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001579 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001580 if (ret < 0) {
1581 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001582 }
Dees_Troy8170a922012-09-18 15:40:25 -04001583 }
Dees_Troye58d5262012-09-21 12:27:57 -04001584 Mount_All_Storage();
1585 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001586 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001587 property_set("sys.storage.ums_enabled", "0");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001588 if (mtp_was_enabled)
1589 if (!Enable_MTP())
1590 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001591 if (ret < 0 && index == 0) {
1592 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1593 return false;
1594 } else {
1595 return true;
1596 }
Dees_Troy8170a922012-09-18 15:40:25 -04001597 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001598}
1599
1600void TWPartitionManager::Mount_All_Storage(void) {
1601 std::vector<TWPartition*>::iterator iter;
1602
1603 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1604 if ((*iter)->Is_Storage)
1605 (*iter)->Mount(false);
1606 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001607}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001608
Dees_Troyd0384ef2012-10-12 12:15:42 -04001609void TWPartitionManager::UnMount_Main_Partitions(void) {
1610 // Unmounts system and data if data is not data/media
1611 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001612 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001613
1614 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1615
1616 UnMount_By_Path("/system", true);
Ethan Yonker6277c792014-09-15 14:54:30 -05001617 if (!datamedia)
1618 UnMount_By_Path("/data", true);
1619
Dees_Troyd0384ef2012-10-12 12:15:42 -04001620 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1621 Boot_Partition->UnMount(true);
1622}
1623
Dees_Troy9350b8d2012-09-27 12:38:38 -04001624int TWPartitionManager::Partition_SDCard(void) {
1625 char mkdir_path[255], temp[255], line[512];
1626 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1627 int ext, swap, total_size = 0, fat_size;
1628 FILE* fp;
1629
Dees_Troy2673cec2013-04-02 20:22:16 +00001630 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001631#ifdef TW_EXTERNAL_STORAGE_PATH
1632 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1633#else
1634 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1635#endif
Ethan Yonker1eff6cd2014-09-15 13:30:42 -05001636 if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001637 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001638 return false;
1639 }
1640 if (!SDCard->UnMount(true))
1641 return false;
1642 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1643 if (SDext != NULL) {
1644 if (!SDext->UnMount(true))
1645 return false;
1646 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001647
1648 TWFunc::