blob: d2c91be7adb3a9aa78e29bf796493d74395a4689 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
24#include <unistd.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040025#include <vector>
Dees_Troy63c8df72012-09-10 14:02:05 -040026#include <dirent.h>
27#include <time.h>
Dees_Troy8170a922012-09-18 15:40:25 -040028#include <errno.h>
29#include <fcntl.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050030#include <iostream>
31#include <iomanip>
Ethan Yonker8613dc02014-09-11 09:28:20 -050032#include <sys/wait.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040033#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000034#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040036#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040037#include "twrp-functions.hpp"
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040038#include "fixPermissions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050039#include "twrpDigest.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050040#include "twrpDU.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040041
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040042#ifdef TW_HAS_MTP
43#include "mtp/mtp_MtpServer.hpp"
44#include "mtp/twrpMtp.hpp"
45#endif
46
Dees Troy6f6441d2014-01-23 02:07:03 +000047extern "C" {
48 #include "cutils/properties.h"
49}
50
Dees_Troy5bf43922012-09-07 16:07:55 -040051#ifdef TW_INCLUDE_CRYPTO
52 #ifdef TW_INCLUDE_JB_CRYPTO
53 #include "crypto/jb/cryptfs.h"
Ethan Yonker4eca40d2014-11-11 14:52:28 -060054 #elif defined(TW_INCLUDE_L_CRYPTO)
55 #include "crypto/lollipop/cryptfs.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040056 #else
57 #include "crypto/ics/cryptfs.h"
58 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -040059#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040060
Ethan Yonker6277c792014-09-15 14:54:30 -050061extern bool datamedia;
62
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050063TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040064 mtp_was_enabled = false;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050065}
66
Dees_Troy51a0e822012-09-05 15:24:24 -040067int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040068 FILE *fstabFile;
69 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +000070 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050071 TWPartition* andsec_partition = NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -040072
73 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
74 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000075 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040076 return false;
77 }
78
79 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
80 if (fstab_line[0] != '/')
81 continue;
82
Dees_Troy2a923582012-09-20 12:13:34 -040083 if (fstab_line[strlen(fstab_line) - 1] != '\n')
84 fstab_line[strlen(fstab_line)] = '\n';
that9e0593e2014-10-08 00:01:24 +020085 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040086 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040087 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040088
Dees_Troy5bf43922012-09-07 16:07:55 -040089 if (partition->Process_Fstab_Line(line, Display_Error)) {
Matt Mowered71fa32014-04-16 13:21:47 -050090 if (!settings_partition && partition->Is_Settings_Storage && partition->Is_Present) {
Dees Troy02a64532014-03-19 15:23:32 +000091 settings_partition = partition;
Dees_Troya13d74f2013-03-24 08:54:55 -050092 } else {
93 partition->Is_Settings_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050094 }
Matt Mowered71fa32014-04-16 13:21:47 -050095 if (!andsec_partition && partition->Has_Android_Secure && partition->Is_Present) {
Matt Mowerbf4efa32014-04-14 23:25:26 -050096 andsec_partition = partition;
97 } else {
98 partition->Has_Android_Secure = false;
99 }
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500100 Partitions.push_back(partition);
Dees_Troy5bf43922012-09-07 16:07:55 -0400101 } else {
102 delete partition;
103 }
104 }
105 fclose(fstabFile);
Ethan Yonker6277c792014-09-15 14:54:30 -0500106 if (!datamedia && !settings_partition && Find_Partition_By_Path("/sdcard") == NULL && Find_Partition_By_Path("/internal_sd") == NULL && Find_Partition_By_Path("/internal_sdcard") == NULL && Find_Partition_By_Path("/emmc") == NULL) {
107 // Attempt to automatically identify /data/media emulated storage devices
108 TWPartition* Dat = Find_Partition_By_Path("/data");
109 if (Dat) {
110 LOGINFO("Using automatic handling for /data/media emulated storage device.\n");
111 datamedia = true;
that9e0593e2014-10-08 00:01:24 +0200112 Dat->Setup_Data_Media();
Ethan Yonker6277c792014-09-15 14:54:30 -0500113 settings_partition = Dat;
114 }
115 }
Dees Troy02a64532014-03-19 15:23:32 +0000116 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500117 std::vector<TWPartition*>::iterator iter;
118 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
119 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000120 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500121 break;
122 }
123 }
Dees Troy02a64532014-03-19 15:23:32 +0000124 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000125 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500126 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400127 if (!Write_Fstab()) {
128 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000129 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400130 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000131 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400132 }
Matt Mowered71fa32014-04-16 13:21:47 -0500133
Matt Mowerbf4efa32014-04-14 23:25:26 -0500134 if (andsec_partition) {
135 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500136 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500137 Setup_Android_Secure_Location(settings_partition);
138 }
Matt Mowered71fa32014-04-16 13:21:47 -0500139 if (settings_partition) {
140 Setup_Settings_Storage_Partition(settings_partition);
141 }
Dees_Troy51127312012-09-08 13:08:49 -0400142 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400143 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400144 return true;
145}
146
147int TWPartitionManager::Write_Fstab(void) {
148 FILE *fp;
149 std::vector<TWPartition*>::iterator iter;
150 string Line;
151
152 fp = fopen("/etc/fstab", "w");
153 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000154 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400155 return false;
156 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400157 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400158 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400159 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400160 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000161 }
162 // Handle subpartition tracking
163 if ((*iter)->Is_SubPartition) {
164 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
165 if (ParentPartition)
166 ParentPartition->Has_SubPartition = true;
167 else
168 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 -0400169 }
170 }
171 fclose(fp);
172 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400173}
174
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500175void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500176 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
177 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
178 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
179}
180
Matt Mowerbf4efa32014-04-14 23:25:26 -0500181void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
182 if (Part->Has_Android_Secure)
183 Part->Setup_AndSec();
Ethan Yonker6277c792014-09-15 14:54:30 -0500184 else if (!datamedia)
Matt Mowerbf4efa32014-04-14 23:25:26 -0500185 Part->Setup_AndSec();
Matt Mowerbf4efa32014-04-14 23:25:26 -0500186}
187
Dees_Troy8170a922012-09-18 15:40:25 -0400188void TWPartitionManager::Output_Partition_Logging(void) {
189 std::vector<TWPartition*>::iterator iter;
190
191 printf("\n\nPartition Logs:\n");
192 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
193 Output_Partition((*iter));
194}
195
196void TWPartitionManager::Output_Partition(TWPartition* Part) {
197 unsigned long long mb = 1048576;
198
Gary Peck004d48b2012-11-21 16:28:18 -0800199 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 -0400200 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800201 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 -0400202 }
Gary Peck004d48b2012-11-21 16:28:18 -0800203 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500204 if (Part->Can_Be_Mounted)
205 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800206 if (Part->Can_Be_Wiped)
207 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700208 if (Part->Use_Rm_Rf)
209 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500210 if (Part->Can_Be_Backed_Up)
211 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800212 if (Part->Wipe_During_Factory_Reset)
213 printf("Wipe_During_Factory_Reset ");
214 if (Part->Wipe_Available_in_GUI)
215 printf("Wipe_Available_in_GUI ");
216 if (Part->Is_SubPartition)
217 printf("Is_SubPartition ");
218 if (Part->Has_SubPartition)
219 printf("Has_SubPartition ");
220 if (Part->Removable)
221 printf("Removable ");
222 if (Part->Is_Present)
223 printf("IsPresent ");
224 if (Part->Can_Be_Encrypted)
225 printf("Can_Be_Encrypted ");
226 if (Part->Is_Encrypted)
227 printf("Is_Encrypted ");
228 if (Part->Is_Decrypted)
229 printf("Is_Decrypted ");
230 if (Part->Has_Data_Media)
231 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000232 if (Part->Can_Encrypt_Backup)
233 printf("Can_Encrypt_Backup ");
234 if (Part->Use_Userdata_Encryption)
235 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800236 if (Part->Has_Android_Secure)
237 printf("Has_Android_Secure ");
238 if (Part->Is_Storage)
239 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500240 if (Part->Is_Settings_Storage)
241 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000242 if (Part->Ignore_Blkid)
243 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000244 if (Part->Retain_Layout_Version)
245 printf("Retain_Layout_Version ");
Gary Peck004d48b2012-11-21 16:28:18 -0800246 printf("\n");
247 if (!Part->SubPartition_Of.empty())
248 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
249 if (!Part->Symlink_Path.empty())
250 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
251 if (!Part->Symlink_Mount_Point.empty())
252 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
253 if (!Part->Primary_Block_Device.empty())
254 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
255 if (!Part->Alternate_Block_Device.empty())
256 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
257 if (!Part->Decrypted_Block_Device.empty())
258 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
259 if (Part->Length != 0)
260 printf(" Length: %i\n", Part->Length);
261 if (!Part->Display_Name.empty())
262 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500263 if (!Part->Storage_Name.empty())
264 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800265 if (!Part->Backup_Path.empty())
266 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
267 if (!Part->Backup_Name.empty())
268 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500269 if (!Part->Backup_Display_Name.empty())
270 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800271 if (!Part->Backup_FileName.empty())
272 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
273 if (!Part->Storage_Path.empty())
274 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
275 if (!Part->Current_File_System.empty())
276 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
277 if (!Part->Fstab_File_System.empty())
278 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
279 if (Part->Format_Block_Size != 0)
280 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400281 if (!Part->MTD_Name.empty())
282 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400283 string back_meth = Part->Backup_Method_By_Name();
Ethan Yonker6277c792014-09-15 14:54:30 -0500284 printf(" Backup_Method: %s\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800285 if (Part->Mount_Flags || !Part->Mount_Options.empty())
286 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Ethan Yonker6277c792014-09-15 14:54:30 -0500287 printf("\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400288}
289
Dees_Troy51a0e822012-09-05 15:24:24 -0400290int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400291 std::vector<TWPartition*>::iterator iter;
292 int ret = false;
293 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400294 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400295
Dees_Troyd93bda52013-07-03 19:55:19 +0000296 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400297 return true;
298
Dees_Troy5bf43922012-09-07 16:07:55 -0400299 // Iterate through all partitions
300 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400301 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400302 ret = (*iter)->Mount(Display_Error);
303 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400304 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400305 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400306 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400307 }
308 if (found) {
309 return ret;
310 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000311 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400312 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000313 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400314 }
315 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400316}
317
Dees_Troy51a0e822012-09-05 15:24:24 -0400318int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400319 std::vector<TWPartition*>::iterator iter;
320 int ret = false;
321 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400322 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400323
324 // Iterate through all partitions
325 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400326 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400327 ret = (*iter)->UnMount(Display_Error);
328 found = true;
329 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
330 (*iter)->UnMount(Display_Error);
331 }
332 }
333 if (found) {
334 return ret;
335 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000336 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400337 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000338 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400339 }
340 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400341}
342
Dees_Troy51a0e822012-09-05 15:24:24 -0400343int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400344 TWPartition* Part = Find_Partition_By_Path(Path);
345
346 if (Part)
347 return Part->Is_Mounted();
348 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000349 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400350 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400351}
352
Dees_Troy5bf43922012-09-07 16:07:55 -0400353int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400354 string current_storage_path = DataManager::GetCurrentStoragePath();
355
356 if (Mount_By_Path(current_storage_path, Display_Error)) {
357 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
358 if (FreeStorage)
359 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
360 return true;
361 }
362 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400363}
364
Dees_Troy5bf43922012-09-07 16:07:55 -0400365int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
366 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
367}
368
369TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
370 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400371 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400372
373 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400374 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400375 return (*iter);
376 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400377 return NULL;
378}
379
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400380int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
381 // Check the backup name to ensure that it is the correct size and contains only valid characters
382 // and that a backup with that name doesn't already exist
383 char backup_name[MAX_BACKUP_NAME_LEN];
384 char backup_loc[255], tw_image_dir[255];
385 int copy_size;
386 int index, cur_char;
387 string Backup_Name, Backup_Loc;
388
389 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
390 copy_size = Backup_Name.size();
391 // Check size
392 if (copy_size > MAX_BACKUP_NAME_LEN) {
393 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000394 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400395 return -2;
396 }
397
398 // Check each character
399 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500400 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400401 return 0; // A "0" (zero) means to use the current timestamp for the backup name
402 for (index=0; index<copy_size; index++) {
403 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500404 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 -0400405 // These are valid characters
406 // Numbers
407 // Upper case letters
408 // Lower case letters
409 // Space
410 // and -_.{}[]
411 } else {
412 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000413 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400414 return -3;
415 }
416 }
417
418 // Check to make sure that a backup with this name doesn't already exist
419 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
420 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500421 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500422 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400423 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000424 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400425 return -4;
426 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400427 // No problems found, return 0
428 return 0;
429}
430
Dees_Troy43d8b002012-09-17 16:00:01 -0400431bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
432{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500433 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400434 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500435 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500436 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400437
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500438 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400439 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400440
Dees_Troyb46a6842012-09-25 11:06:46 -0400441 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000442 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400443
444 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500445 md5sum.setfn(Backup_Folder + Backup_Filename);
446 if (md5sum.computeMD5() == 0)
447 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000448 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500449 else
450 return -1;
451 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000452 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400453 } else {
454 char filename[512];
455 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500456 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400457 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500458 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000459 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400460 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000461 if (TWFunc::Path_Exists(filename)) {
462 if (md5sum.computeMD5() == 0) {
463 if (md5sum.write_md5digest() != 0)
464 {
465 gui_print(" * MD5 Error.\n");
466 return false;
467 }
468 } else {
469 gui_print(" * Error computing MD5.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500470 return false;
471 }
472 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400473 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400474 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500475 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400476 }
477 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000478 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400479 return false;
480 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000481 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400482 }
483 return true;
484}
485
Dees_Troy093b7642012-09-21 15:59:38 -0400486bool 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 -0400487 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500488 int img_bps;
489 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400490 unsigned long total_time, remain_time, section_time;
491 int use_compression, backup_time;
492 float pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500493 unsigned long long total_size, current_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400494
495 if (Part == NULL)
496 return true;
497
Dees_Troy093b7642012-09-21 15:59:38 -0400498 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
499
500 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
501 if (use_compression)
502 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
503 else
504 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
505
506 // We know the speed for both, how far into the whole backup are we, based on time
507 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
508 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
509
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500510 //pos = (total_time - remain_time) / (float) total_time;
511 total_size = *file_bytes + *img_bytes;
512 current_size = *file_bytes + *img_bytes - *file_bytes_remaining - *img_bytes_remaining;
513 pos = ((float)(current_size) / (float)(total_size));
Dees_Troy2673cec2013-04-02 20:22:16 +0000514 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400515
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500516 LOGINFO("Estimated total time: %lu\nEstimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400517
518 // And get the time
519 if (Part->Backup_Method == 1)
520 section_time = Part->Backup_Size / file_bps;
521 else
522 section_time = Part->Backup_Size / img_bps;
523
524 // Set the position
525 pos = section_time / (float) total_time;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500526 //DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400527
Tom Hite5a926722014-09-15 01:31:03 +0000528 TWFunc::SetPerformanceMode(true);
Dees_Troy43d8b002012-09-17 16:00:01 -0400529 time(&start);
530
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500531 if (Part->Backup(Backup_Folder, &total_size, &current_size)) {
Tom Hite5a926722014-09-15 01:31:03 +0000532 bool md5Success = false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500533 current_size += Part->Backup_Size;
534 pos = (float)((float)(current_size) / (float)(total_size));
535 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400536 if (Part->Has_SubPartition) {
537 std::vector<TWPartition*>::iterator subpart;
538
539 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500540 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Tom Hite5a926722014-09-15 01:31:03 +0000541 if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size)) {
542 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400543 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000544 }
Dees_Troy2727b992013-08-14 20:09:30 +0000545 sync();
546 sync();
Tom Hite5a926722014-09-15 01:31:03 +0000547 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName)) {
548 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400549 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000550 }
Dees_Troy093b7642012-09-21 15:59:38 -0400551 if (Part->Backup_Method == 1) {
552 *file_bytes_remaining -= (*subpart)->Backup_Size;
553 } else {
554 *img_bytes_remaining -= (*subpart)->Backup_Size;
555 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500556 current_size += Part->Backup_Size;
557 pos = (float)(current_size / total_size);
558 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400559 }
560 }
561 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400562 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400563 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000564 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400565 if (Part->Backup_Method == 1) {
566 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400567 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400568 } else {
569 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400570 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400571 }
Tom Hite5a926722014-09-15 01:31:03 +0000572
573 md5Success = Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
574 TWFunc::SetPerformanceMode(false);
575 return md5Success;
Dees_Troy43d8b002012-09-17 16:00:01 -0400576 } else {
Tom Hite5a926722014-09-15 01:31:03 +0000577 TWFunc::SetPerformanceMode(false);
Dees_Troy43d8b002012-09-17 16:00:01 -0400578 return false;
579 }
580}
581
582int TWPartitionManager::Run_Backup(void) {
583 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500584 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400585 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 -0400586 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500587 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400588 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400589 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400590 struct tm *t;
591 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500592 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400593 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500594 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400595
596 time(&total_start);
597
598 Update_System_Details();
599
600 if (!Mount_Current_Storage(true))
601 return false;
602
603 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400604 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400605 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400606 else
607 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400608
Dees_Troy43d8b002012-09-17 16:00:01 -0400609 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
610 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000611 if (Backup_Name == "(Current Date)") {
612 Backup_Name = TWFunc::Get_Current_Date();
613 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
614 TWFunc::Auto_Generate_Backup_Name();
615 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400616 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000617 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400618 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000619 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400620
Dees_Troy2673cec2013-04-02 20:22:16 +0000621 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500622 DataManager::GetValue("tw_backup_list", Backup_List);
623 if (!Backup_List.empty()) {
624 end_pos = Backup_List.find(";", start_pos);
625 while (end_pos != string::npos && start_pos < Backup_List.size()) {
626 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
627 backup_part = Find_Partition_By_Path(backup_path);
628 if (backup_part != NULL) {
629 partition_count++;
630 if (backup_part->Backup_Method == 1)
631 file_bytes += backup_part->Backup_Size;
632 else
633 img_bytes += backup_part->Backup_Size;
634 if (backup_part->Has_SubPartition) {
635 std::vector<TWPartition*>::iterator subpart;
636
637 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000638 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 -0500639 partition_count++;
640 if ((*subpart)->Backup_Method == 1)
641 file_bytes += (*subpart)->Backup_Size;
642 else
643 img_bytes += (*subpart)->Backup_Size;
644 }
645 }
Dees_Troy8170a922012-09-18 15:40:25 -0400646 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500647 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000648 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400649 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500650 start_pos = end_pos + 1;
651 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400652 }
653 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400654
655 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000656 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400657 return false;
658 }
659 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000660 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500661 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400662 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
663 if (storage != NULL) {
664 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000665 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400666 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000667 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400668 return false;
669 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000670 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400671 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000672 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400673 return false;
674 }
675 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500676 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400677
Dees_Troy2673cec2013-04-02 20:22:16 +0000678 gui_print("\n[BACKUP STARTED]\n");
679 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000680 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000681 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000682 return false;
683 }
684
Dees_Troy2673cec2013-04-02 20:22:16 +0000685 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400686
Dees_Troya13d74f2013-03-24 08:54:55 -0500687 start_pos = 0;
688 end_pos = Backup_List.find(";", start_pos);
689 while (end_pos != string::npos && start_pos < Backup_List.size()) {
690 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
691 backup_part = Find_Partition_By_Path(backup_path);
692 if (backup_part != NULL) {
693 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
694 return false;
695 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000696 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500697 }
698 start_pos = end_pos + 1;
699 end_pos = Backup_List.find(";", start_pos);
700 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400701
702 // Average BPS
703 if (img_time == 0)
704 img_time = 1;
705 if (file_time == 0)
706 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400707 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500708 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400709
Dees_Troy2673cec2013-04-02 20:22:16 +0000710 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
711 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400712
713 time(&total_stop);
714 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500715 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500716 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400717
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500718 int prev_img_bps, use_compression;
719 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400720 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
721 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500722 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400723
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500724 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400725 if (use_compression)
726 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500727 else
Dees_Troy093b7642012-09-21 15:59:38 -0400728 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
729 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500730 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400731
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500732 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400733 if (use_compression)
734 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
735 else
736 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
737
Dees_Troy2673cec2013-04-02 20:22:16 +0000738 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400739 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400740 UnMount_Main_Partitions();
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500741 gui_print_color("highlight", "[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000742 string backup_log = Full_Backup_Path + "recovery.log";
743 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
744 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400745}
746
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500747bool 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 -0400748 time_t Start, Stop;
Tom Hite5a926722014-09-15 01:31:03 +0000749 TWFunc::SetPerformanceMode(true);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400750 time(&Start);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500751 //DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Tom Hite5a926722014-09-15 01:31:03 +0000752 if (!Part->Restore(Restore_Name, total_restore_size, already_restored_size)) {
753 TWFunc::SetPerformanceMode(false);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400754 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000755 }
Dees_Troy8170a922012-09-18 15:40:25 -0400756 if (Part->Has_SubPartition) {
757 std::vector<TWPartition*>::iterator subpart;
758
759 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
760 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Tom Hite5a926722014-09-15 01:31:03 +0000761 if (!(*subpart)->Restore(Restore_Name, total_restore_size, already_restored_size)) {
762 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400763 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000764 }
Dees_Troy8170a922012-09-18 15:40:25 -0400765 }
766 }
767 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400768 time(&Stop);
Tom Hite5a926722014-09-15 01:31:03 +0000769 TWFunc::SetPerformanceMode(false);
Dees_Troy2673cec2013-04-02 20:22:16 +0000770 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 -0400771 return true;
772}
773
Dees_Troy51a0e822012-09-05 15:24:24 -0400774int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400775 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500776 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400777 time_t rStart, rStop;
778 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500779 string Restore_List, restore_path;
780 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500781 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400782
Dees_Troy2673cec2013-04-02 20:22:16 +0000783 gui_print("\n[RESTORE STARTED]\n\n");
784 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400785
Dees_Troy4a2a1262012-09-18 09:33:47 -0400786 if (!Mount_Current_Storage(true))
787 return false;
788
789 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500790 if (check_md5 > 0) {
791 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
792 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000793 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500794 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000795 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500796 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500797 gui_print("Calculating restore details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500798 DataManager::GetValue("tw_restore_selected", Restore_List);
799 if (!Restore_List.empty()) {
800 end_pos = Restore_List.find(";", start_pos);
801 while (end_pos != string::npos && start_pos < Restore_List.size()) {
802 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
803 restore_part = Find_Partition_By_Path(restore_path);
804 if (restore_part != NULL) {
805 partition_count++;
806 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
807 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500808 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500809 if (restore_part->Has_SubPartition) {
810 std::vector<TWPartition*>::iterator subpart;
811
812 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
813 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400814 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500815 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500816 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500817 }
818 }
819 }
820 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500821 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500822 }
823 start_pos = end_pos + 1;
824 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400825 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400826 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400827
828 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000829 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400830 return false;
831 }
832
Dees_Troy2673cec2013-04-02 20:22:16 +0000833 gui_print("Restoring %i partitions...\n", partition_count);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500834 gui_print("Total restore size is %lluMB\n", total_restore_size / 1048576);
Dees_Troy2673cec2013-04-02 20:22:16 +0000835 DataManager::SetProgress(0.0);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500836
Dees_Troya13d74f2013-03-24 08:54:55 -0500837 start_pos = 0;
838 if (!Restore_List.empty()) {
839 end_pos = Restore_List.find(";", start_pos);
840 while (end_pos != string::npos && start_pos < Restore_List.size()) {
841 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
842 restore_part = Find_Partition_By_Path(restore_path);
843 if (restore_part != NULL) {
844 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500845 if (!Restore_Partition(restore_part, Restore_Name, partition_count, &total_restore_size, &already_restored_size))
Dees_Troya13d74f2013-03-24 08:54:55 -0500846 return false;
847 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000848 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500849 }
850 start_pos = end_pos + 1;
851 end_pos = Restore_List.find(";", start_pos);
852 }
853 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400854 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400855 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400856 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400857 time(&rStop);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500858 gui_print_color("highlight", "[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500859 DataManager::SetValue("tw_file_progress", "");
Dees_Troy63c8df72012-09-10 14:02:05 -0400860 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400861}
862
863void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400864 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500865 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000866 bool get_date = true, check_encryption = true;
867
868 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400869
870 DIR* d;
871 d = opendir(Restore_Name.c_str());
872 if (d == NULL)
873 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000874 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400875 return;
876 }
877
878 struct dirent* de;
879 while ((de = readdir(d)) != NULL)
880 {
881 // Strip off three components
882 char str[256];
883 char* label;
884 char* fstype = NULL;
885 char* extn = NULL;
886 char* ptr;
887
888 strcpy(str, de->d_name);
889 if (strlen(str) <= 2)
890 continue;
891
892 if (get_date) {
893 char file_path[255];
894 struct stat st;
895
896 strcpy(file_path, Restore_Name.c_str());
897 strcat(file_path, "/");
898 strcat(file_path, str);
899 stat(file_path, &st);
900 string backup_date = ctime((const time_t*)(&st.st_mtime));
901 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
902 get_date = false;
903 }
904
905 label = str;
906 ptr = label;
907 while (*ptr && *ptr != '.') ptr++;
908 if (*ptr == '.')
909 {
910 *ptr = 0x00;
911 ptr++;
912 fstype = ptr;
913 }
914 while (*ptr && *ptr != '.') ptr++;
915 if (*ptr == '.')
916 {
917 *ptr = 0x00;
918 ptr++;
919 extn = ptr;
920 }
921
Dees_Troy83bd4832013-05-04 12:39:56 +0000922 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -0500923 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +0000924 if (extnlength != 3 && extnlength != 6) continue;
925 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
926 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
927
928 if (check_encryption) {
929 string filename = Restore_Name + "/";
930 filename += de->d_name;
931 if (TWFunc::Get_File_Type(filename) == 2) {
932 LOGINFO("'%s' is encrypted\n", filename.c_str());
933 DataManager::SetValue("tw_restore_encrypted", 1);
934 }
935 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500936 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -0400937
938 TWPartition* Part = Find_Partition_By_Path(label);
939 if (Part == NULL)
940 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000941 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -0400942 continue;
943 }
944
945 Part->Backup_FileName = de->d_name;
946 if (strlen(extn) > 3) {
947 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
948 }
949
Dees_Troya13d74f2013-03-24 08:54:55 -0500950 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -0400951 }
952 closedir(d);
953
Dees_Troya13d74f2013-03-24 08:54:55 -0500954 // Set the final value
955 DataManager::SetValue("tw_restore_list", Restore_List);
956 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -0400957 return;
958}
959
960int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400961 std::vector<TWPartition*>::iterator iter;
962 int ret = false;
963 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400964 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -0400965
966 // Iterate through all partitions
967 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400968 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -0400969 if (Path == "/and-sec")
970 ret = (*iter)->Wipe_AndSec();
971 else
972 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -0400973 found = true;
974 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
975 (*iter)->Wipe();
976 }
977 }
978 if (found) {
979 return ret;
980 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000981 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400982 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400983}
984
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500985int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
986 std::vector<TWPartition*>::iterator iter;
987 int ret = false;
988 bool found = false;
989 string Local_Path = TWFunc::Get_Root_Path(Path);
990
991 // Iterate through all partitions
992 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
993 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
994 if (Path == "/and-sec")
995 ret = (*iter)->Wipe_AndSec();
996 else
997 ret = (*iter)->Wipe(New_File_System);
998 found = true;
999 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1000 (*iter)->Wipe(New_File_System);
1001 }
1002 }
1003 if (found) {
1004 return ret;
1005 } else
1006 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1007 return false;
1008}
1009
Dees_Troy51a0e822012-09-05 15:24:24 -04001010int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001011 std::vector<TWPartition*>::iterator iter;
1012 int ret = true;
1013
1014 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001015 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001016 if (!(*iter)->Wipe())
1017 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001018 } else if ((*iter)->Has_Android_Secure) {
1019 if (!(*iter)->Wipe_AndSec())
1020 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001021 }
1022 }
1023 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001024}
1025
Dees_Troy38bd7602012-09-14 13:33:53 -04001026int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1027 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001028 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001029
1030 if (!Mount_By_Path("/data", true))
1031 return false;
1032
1033 if (!Mount_By_Path("/cache", true))
1034 return false;
1035
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001036 dir.push_back("/data/dalvik-cache");
1037 dir.push_back("/cache/dalvik-cache");
1038 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001039 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001040 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001041 if (stat(dir.at(i).c_str(), &st) == 0) {
1042 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001043 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001044 }
1045 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001046 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001047 if (sdext && sdext->Is_Present && sdext->Mount(false))
1048 {
1049 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1050 {
1051 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001052 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001053 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001054 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001055 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001056 return true;
1057}
1058
1059int TWPartitionManager::Wipe_Rotate_Data(void) {
1060 if (!Mount_By_Path("/data", true))
1061 return false;
1062
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001063 unlink("/data/misc/akmd*");
1064 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001065 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001066 return true;
1067}
1068
1069int TWPartitionManager::Wipe_Battery_Stats(void) {
1070 struct stat st;
1071
1072 if (!Mount_By_Path("/data", true))
1073 return false;
1074
1075 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001076 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001077 } else {
1078 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001079 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001080 }
1081 return true;
1082}
1083
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001084int TWPartitionManager::Wipe_Android_Secure(void) {
1085 std::vector<TWPartition*>::iterator iter;
1086 int ret = false;
1087 bool found = false;
1088
1089 // Iterate through all partitions
1090 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1091 if ((*iter)->Has_Android_Secure) {
1092 ret = (*iter)->Wipe_AndSec();
1093 found = true;
1094 }
1095 }
1096 if (found) {
1097 return ret;
1098 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001099 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001100 }
1101 return false;
1102}
1103
Dees_Troy38bd7602012-09-14 13:33:53 -04001104int TWPartitionManager::Format_Data(void) {
1105 TWPartition* dat = Find_Partition_By_Path("/data");
1106
1107 if (dat != NULL) {
1108 if (!dat->UnMount(true))
1109 return false;
1110
1111 return dat->Wipe_Encryption();
1112 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001113 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001114 return false;
1115 }
1116 return false;
1117}
1118
1119int TWPartitionManager::Wipe_Media_From_Data(void) {
1120 TWPartition* dat = Find_Partition_By_Path("/data");
1121
1122 if (dat != NULL) {
1123 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001124 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001125 return false;
1126 }
1127 if (!dat->Mount(true))
1128 return false;
1129
Dees_Troy2673cec2013-04-02 20:22:16 +00001130 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001131 mtp_was_enabled = TWFunc::Toggle_MTP(false);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001132 TWFunc::removeDir("/data/media", false);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001133 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
1134 if (mtp_was_enabled) {
1135 if (!Enable_MTP())
1136 Disable_MTP();
1137 }
1138 return false;
1139 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001140 if (dat->Has_Data_Media) {
1141 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001142 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1143 dat->UnMount(false);
1144 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001145 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001146 if (mtp_was_enabled) {
1147 if (!Enable_MTP())
1148 Disable_MTP();
1149 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001150 return true;
1151 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001152 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001153 return false;
1154 }
1155 return false;
1156}
1157
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001158int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1159 std::vector<TWPartition*>::iterator iter;
1160 int ret = false;
1161 bool found = false;
1162 string Local_Path = TWFunc::Get_Root_Path(Path);
1163
1164 if (Local_Path == "/tmp" || Local_Path == "/")
1165 return true;
1166
1167 // Iterate through all partitions
1168 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1169 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1170 ret = (*iter)->Repair();
1171 found = true;
1172 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1173 (*iter)->Repair();
1174 }
1175 }
1176 if (found) {
1177 return ret;
1178 } else if (Display_Error) {
1179 LOGERR("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1180 } else {
1181 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1182 }
1183 return false;
1184}
1185
Dees_Troy51a0e822012-09-05 15:24:24 -04001186void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001187 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001188 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001189
Dees_Troy2673cec2013-04-02 20:22:16 +00001190 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001191 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001192 if ((*iter)->Can_Be_Mounted) {
1193 (*iter)->Update_Size(true);
1194 if ((*iter)->Mount_Point == "/system") {
1195 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1196 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1197 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1198 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1199 } else if ((*iter)->Mount_Point == "/cache") {
1200 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1201 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1202 } else if ((*iter)->Mount_Point == "/sd-ext") {
1203 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1204 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1205 if ((*iter)->Backup_Size == 0) {
1206 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1207 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1208 } else
1209 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001210 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001211 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001212 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001213 if ((*iter)->Backup_Size == 0) {
1214 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1215 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1216 } else
1217 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001218 } else if ((*iter)->Mount_Point == "/boot") {
1219 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1220 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1221 if ((*iter)->Backup_Size == 0) {
1222 DataManager::SetValue("tw_has_boot_partition", 0);
1223 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1224 } else
1225 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001226 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001227#ifdef SP1_NAME
1228 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1229 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1230 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1231 }
1232#endif
1233#ifdef SP2_NAME
1234 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1235 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1236 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1237 }
1238#endif
1239#ifdef SP3_NAME
1240 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1241 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1242 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1243 }
1244#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001245 } else {
1246 // Handle unmountable partitions in case we reset defaults
1247 if ((*iter)->Mount_Point == "/boot") {
1248 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1249 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1250 if ((*iter)->Backup_Size == 0) {
1251 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1252 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1253 } else
1254 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1255 } else if ((*iter)->Mount_Point == "/recovery") {
1256 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1257 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1258 if ((*iter)->Backup_Size == 0) {
1259 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1260 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1261 } else
1262 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001263 } else if ((*iter)->Mount_Point == "/data") {
1264 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001265 }
1266#ifdef SP1_NAME
1267 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1268 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1269 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1270 }
1271#endif
1272#ifdef SP2_NAME
1273 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1274 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1275 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1276 }
1277#endif
1278#ifdef SP3_NAME
1279 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1280 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1281 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1282 }
1283#endif
Dees_Troy51127312012-09-08 13:08:49 -04001284 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001285 }
that39b7c162013-11-22 22:07:33 +01001286 gui_print("...done\n");
Dees_Troy51127312012-09-08 13:08:49 -04001287 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1288 string current_storage_path = DataManager::GetCurrentStoragePath();
1289 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001290 if (FreeStorage != NULL) {
1291 // Attempt to mount storage
1292 if (!FreeStorage->Mount(false)) {
1293 // We couldn't mount storage... check to see if we have dual storage
1294 int has_dual_storage;
1295 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1296 if (has_dual_storage == 1) {
1297 // We have dual storage, see if we're using the internal storage that should always be present
1298 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001299 if (!FreeStorage->Is_Encrypted) {
1300 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001301 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001302 }
Dees_Troy8170a922012-09-18 15:40:25 -04001303 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1304 } else {
1305 // We were using external, flip to internal
1306 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1307 current_storage_path = DataManager::GetCurrentStoragePath();
1308 FreeStorage = Find_Partition_By_Path(current_storage_path);
1309 if (FreeStorage != NULL) {
1310 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1311 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001312 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001313 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1314 }
1315 }
1316 } else {
1317 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001318 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001319 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1320 }
1321 } else {
1322 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1323 }
1324 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001325 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001326 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001327 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001328 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001329 return;
1330}
1331
1332int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001333#ifdef TW_INCLUDE_CRYPTO
1334 int ret_val, password_len;
1335 char crypto_blkdev[255], cPassword[255];
1336 size_t result;
1337
1338 property_set("ro.crypto.state", "encrypted");
Ethan Yonker4eca40d2014-11-11 14:52:28 -06001339#if defined(TW_INCLUDE_JB_CRYPTO) || defined(TW_INCLUDE_L_CRYPTO)
Dees_Troy5bf43922012-09-07 16:07:55 -04001340 // No extra flags needed
1341#else
1342 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1343 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1344 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1345 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1346 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1347 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001348
1349#ifdef CRYPTO_SD_FS_TYPE
Ethan Yonker71413f42014-02-26 13:36:08 -06001350 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1351 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1352 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001353#endif
a39552696ff55ce2013-01-08 16:14:56 +00001354
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001355 property_set("rw.km_fips_status", "ready");
a39552696ff55ce2013-01-08 16:14:56 +00001356
1357#endif
1358
1359 // some samsung devices store "footer" on efs partition
1360 TWPartition *efs = Find_Partition_By_Path("/efs");
1361 if(efs && !efs->Is_Mounted())
1362 efs->Mount(false);
1363 else
1364 efs = 0;
1365#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001366#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001367 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001368 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001369 property_set("ro.crypto.external_encrypted", "1");
1370 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1371 } else {
1372 property_set("ro.crypto.external_encrypted", "0");
1373 }
1374#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001375#endif
a39552696ff55ce2013-01-08 16:14:56 +00001376
Dees_Troy5bf43922012-09-07 16:07:55 -04001377 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001378 int pwret = cryptfs_check_passwd(cPassword);
1379
1380 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001381 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001382 return -1;
1383 }
a39552696ff55ce2013-01-08 16:14:56 +00001384
1385 if(efs)
1386 efs->UnMount(false);
1387
Dees_Troy5bf43922012-09-07 16:07:55 -04001388 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1389 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001390 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001391 } else {
1392 TWPartition* dat = Find_Partition_By_Path("/data");
1393 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001394 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001395 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1396 dat->Is_Decrypted = true;
1397 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001398 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001399 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 +00001400 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001401
1402#ifdef CRYPTO_SD_FS_TYPE
1403 char crypto_blkdev_sd[255];
1404 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1405 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001406 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001407 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001408 emmc->Is_Decrypted = true;
1409 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1410 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001411 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001412 }
a39552696ff55ce2013-01-08 16:14:56 +00001413#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001414#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001415#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001416 char is_external_decrypted[255];
1417 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1418 if (strcmp(is_external_decrypted, "1") == 0) {
1419 sdcard->Is_Decrypted = true;
1420 sdcard->EcryptFS_Password = Password;
1421 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001422 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1423 MetaEcfsFile += "/.MetaEcfsFile";
1424 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1425 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1426 sdcard->UnMount(false);
1427 sdcard->Mount(false);
1428 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001429 } else {
Dees_Troy066eb302013-08-23 17:20:32 +00001430 LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001431 sdcard->Is_Decrypted = false;
1432 sdcard->Decrypted_Block_Device = "";
1433 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001434#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001435#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001436
Dees_Troy5bf43922012-09-07 16:07:55 -04001437 // Sleep for a bit so that the device will be ready
1438 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001439 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001440 dat->Storage_Path = "/data/media/0";
1441 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001442 DataManager::SetValue("tw_storage_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001443 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001444 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001445 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001446 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001447 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001448 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001449 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001450 }
1451 return 0;
1452#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001453 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001454 return -1;
1455#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001456 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001457}
1458
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001459int TWPartitionManager::Fix_Permissions(void) {
1460 int result = 0;
1461 if (!Mount_By_Path("/data", true))
1462 return false;
1463
1464 if (!Mount_By_Path("/system", true))
1465 return false;
1466
1467 Mount_By_Path("/sd-ext", false);
1468
1469 fixPermissions perms;
1470 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001471 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001472 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001473 return result;
1474}
1475
Ethan Yonker47360be2014-04-01 10:34:34 -05001476TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1477 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1478
1479 if (!Path.empty()) {
1480 string Search_Path = TWFunc::Get_Root_Path(Path);
1481 for (; iter != Partitions.end(); iter++) {
1482 if ((*iter)->Mount_Point == Search_Path) {
1483 iter++;
1484 break;
1485 }
1486 }
1487 }
1488
1489 for (; iter != Partitions.end(); iter++) {
1490 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) {
1491 return (*iter);
1492 }
1493 }
1494
1495 return NULL;
1496}
1497
Dees_Troyd21618c2012-10-14 18:48:49 -04001498int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001499 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1500
1501 if (Part == NULL) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001502 LOGERR("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
Dees_Troyd21618c2012-10-14 18:48:49 -04001503 return false;
1504 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001505 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1506 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001507 return false;
1508
Dees_Troy2673cec2013-04-02 20:22:16 +00001509 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1510 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001511 return false;
1512 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001513 return true;
1514}
1515
Dees_Troy8170a922012-09-18 15:40:25 -04001516int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001517 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001518 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001519 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001520
Ethan Yonker9eb1cd42014-11-03 22:14:08 -06001521 property_set("sys.storage.ums_enabled", "1");
1522 sleep(1);
Dees_Troy8170a922012-09-18 15:40:25 -04001523 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001524 string Lun_File_str = CUSTOM_LUN_FILE;
1525 size_t found = Lun_File_str.find("%");
1526 if (found != string::npos) {
1527 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1528 if (TWFunc::Path_Exists(lun_file))
1529 has_multiple_lun = true;
1530 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001531 mtp_was_enabled = TWFunc::Toggle_MTP(false);
Ethan Yonker47360be2014-04-01 10:34:34 -05001532 if (!has_multiple_lun) {
1533 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1534 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1535 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
1536 TWPartition* Mount = Find_Next_Storage("", "/data");
1537 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001538 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1539 goto error_handle;
1540 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001541 } else {
1542 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001543 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001544 }
1545 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001546 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001547 }
Dees_Troy8170a922012-09-18 15:40:25 -04001548 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001549 LOGINFO("Device has multiple lun files\n");
1550 TWPartition* Mount1;
1551 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001552 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker47360be2014-04-01 10:34:34 -05001553 Mount1 = Find_Next_Storage("", "/data");
1554 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001555 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1556 goto error_handle;
1557 }
Matt Moweree717062014-04-26 01:46:46 -05001558 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker47360be2014-04-01 10:34:34 -05001559 Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data");
1560 if (Mount2) {
Matt Moweree717062014-04-26 01:46:46 -05001561 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001562 }
1563 } else {
1564 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001565 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001566 }
Dees_Troy8170a922012-09-18 15:40:25 -04001567 }
1568 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001569error_handle:
1570 if (mtp_was_enabled)
1571 if (!Enable_MTP())
1572 Disable_MTP();
Ethan Yonker9eb1cd42014-11-03 22:14:08 -06001573 property_set("sys.storage.ums_enabled", "0");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001574 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001575}
1576
1577int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001578 int index, ret;
1579 char lun_file[255], ch[2] = {0, 0};
1580 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001581
1582 for (index=0; index<2; index++) {
1583 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001584 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001585 if (ret < 0) {
1586 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001587 }
Dees_Troy8170a922012-09-18 15:40:25 -04001588 }
Dees_Troye58d5262012-09-21 12:27:57 -04001589 Mount_All_Storage();
1590 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001591 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001592 property_set("sys.storage.ums_enabled", "0");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001593 if (mtp_was_enabled)
1594 if (!Enable_MTP())
1595 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001596 if (ret < 0 && index == 0) {
1597 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1598 return false;
1599 } else {
1600 return true;
1601 }
Dees_Troy8170a922012-09-18 15:40:25 -04001602 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001603}
1604
1605void TWPartitionManager::Mount_All_Storage(void) {
1606 std::vector<TWPartition*>::iterator iter;
1607
1608 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1609 if ((*iter)->Is_Storage)
1610 (*iter)->Mount(false);
1611 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001612}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001613
Dees_Troyd0384ef2012-10-12 12:15:42 -04001614void TWPartitionManager::UnMount_Main_Partitions(void) {
1615 // Unmounts system and data if data is not data/media
1616 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001617 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001618
1619 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1620
1621 UnMount_By_Path("/system", true);
Ethan Yonker6277c792014-09-15 14:54:30 -05001622 if (!datamedia)
1623 UnMount_By_Path("/data", true);
1624
Dees_Troyd0384ef2012-10-12 12:15:42 -04001625 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1626 Boot_Partition->UnMount(true);
1627}
1628
Dees_Troy9350b8d2012-09-27 12:38:38 -04001629int TWPartitionManager::Partition_SDCard(void) {
1630 char mkdir_path[255], temp[255], line[512];
1631 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1632 int ext, swap, total_size = 0, fat_size;
1633 FILE* fp;
1634
Dees_Troy2673cec2013-04-02 20:22:16 +00001635 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001636#ifdef TW_EXTERNAL_STORAGE_PATH
1637 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1638#else
1639 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1640#endif
Ethan Yonker1eff6cd2014-09-15 13:30:42 -05001641 if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001642 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001643 return false;
1644 }
1645 if (!SDCard->UnMount(true))
1646 return false;
1647 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1648 if (SDext != NULL) {
1649 if (!SDext->UnMount(true))
1650 return false;
1651 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001652
1653 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001654 Device = SDCard->Actual_Block_Device;
1655 // Just use the root block device
1656 Device.resize(strlen("/dev/block/mmcblkX"));
1657
1658 // Find the size of the block device:
1659 fp = fopen("/proc/partitions", "rt");
1660 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001661 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001662 return false;
1663 }
1664
1665 while (fgets(line, sizeof(line), fp) != NULL)
1666 {
1667 unsigned long major, minor, blocks;
1668 char device[512];
1669 char tmpString[64];
1670
1671 if (strlen(line) < 7 || line[0] == 'm') continue;
1672 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1673
1674 tmpdevice = "/dev/block/";
1675 tmpdevice += device;
1676 if (tmpdevice == Device) {
1677 // Adjust block size to byte size
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001678 total_size = (int)(blocks * 1024ULL / 1000000LLU);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001679 break;
1680 }
1681 }
1682 fclose(fp);
1683
1684 DataManager::GetValue("tw_sdext_size", ext);
1685 DataManager::GetValue("tw_swap_size", swap);
1686 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1687 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001688 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 -04001689 memset(temp, 0, sizeof(temp));
1690 sprintf(temp, "%i", fat_size);
1691 fat_str = temp;
1692 memset(temp, 0, sizeof(temp));
1693 sprintf(temp, "%i", fat_size + ext);
1694 ext_str = temp;
1695 memset(temp, 0, sizeof(temp));
1696 sprintf(temp, "%i", fat_size + ext + swap);
1697 swap_str = temp;
1698 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001699 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001700 return false;
1701 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001702 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001703 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001704 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001705 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001706 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001707 Update_System_Details();
1708 return false;
1709 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001710 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001711 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001712 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001713 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001714 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001715 return false;
1716 }
1717 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001718 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001719 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001720 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001721 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001722 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001723 Update_System_Details();
1724 return false;
1725 }
1726 }
1727 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001728 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001729 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001730 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001731 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001732 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001733 Update_System_Details();
1734 return false;
1735 }
1736 }
1737 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1738#ifdef TW_EXTERNAL_STORAGE_PATH
1739 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1740 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1741 memset(mkdir_path, 0, sizeof(mkdir_path));
1742 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1743#else
1744 Mount_By_Path("/sdcard", 1);
1745 strcpy(mkdir_path, "/sdcard/TWRP");
1746#endif
1747 mkdir(mkdir_path, 0777);
1748 DataManager::Flush();
1749#ifdef TW_EXTERNAL_STORAGE_PATH
1750 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1751 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1752 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1753#else
1754 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1755 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1756 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1757#endif
1758 if (ext > 0) {
1759 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001760 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001761 return false;
1762 }
1763 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001764 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1765 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001766 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001767 }
1768
1769 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00001770 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001771 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001772}
Dees_Troya13d74f2013-03-24 08:54:55 -05001773
1774void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1775 std::vector<TWPartition*>::iterator iter;
1776 if (ListType == "mount") {
1777 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1778 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1779 struct PartitionList part;
1780 part.Display_Name = (*iter)->Display_Name;
1781 part.Mount_Point = (*iter)->Mount_Point;
1782 part.selected = (*iter)->Is_Mounted();
1783 Partition_List->push_back(part);
1784 }
1785 }
1786 } else if (ListType == "storage") {
1787 char free_space[255];
1788 string Current_Storage = DataManager::GetCurrentStoragePath();
1789 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1790 if ((*iter)->Is_Storage) {
1791 struct PartitionList part;
1792 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1793 part.Display_Name = (*iter)->Storage_Name + " (";
1794 part.Display_Name += free_space;
1795 part.Display_Name += "MB)";
1796 part.Mount_Point = (*iter)->Storage_Path;
1797 if ((*iter)->Storage_Path == Current_Storage)
1798 part.selected = 1;
1799 else
1800 part.selected = 0;
1801 Partition_List->push_back(part);
1802 }
1803 }
1804 } else if (ListType == "backup") {
1805 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001806 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001807 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001808 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001809 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001810 Backup_Size = (*iter)->Backup_Size;
1811 if ((*iter)->Has_SubPartition) {
1812 std::vector<TWPartition*>::iterator subpart;
1813
1814 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1815 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1816 Backup_Size += (*subpart)->Backup_Size;
1817 }
1818 }
1819 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001820 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1821 part.Display_Name += backup_size;
1822 part.Display_Name += "MB)";
1823 part.Mount_Point = (*iter)->Backup_Path;
1824 part.selected = 0;
1825 Partition_List->push_back(part);
1826 }
1827 }
1828 } else if (ListType == "restore") {
1829 string Restore_List, restore_path;
1830 TWPartition* restore_part = NULL;
1831
1832 DataManager::GetValue("tw_restore_list", Restore_List);
1833 if (!Restore_List.empty()) {
1834 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1835 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1836 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05001837 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00001838 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001839 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05001840 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05001841 } else {
1842 struct PartitionList part;
1843 part.Display_Name = restore_part->Backup_Display_Name;
1844 part.Mount_Point = restore_part->Backup_Path;
1845 part.selected = 1;
1846 Partition_List->push_back(part);
1847 }
1848 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001849 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001850 }
1851 start_pos = end_pos + 1;
1852 end_pos = Restore_List.find(";", start_pos);
1853 }
1854 }
1855 } else if (ListType == "wipe") {
1856 struct PartitionList dalvik;
1857 dalvik.Display_Name = "Dalvik Cache";
1858 dalvik.Mount_Point = "DALVIK";
1859 dalvik.selected = 0;
1860 Partition_List->push_back(dalvik);
1861 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1862 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1863 struct PartitionList part;
1864 part.Display_Name = (*iter)->Display_Name;
1865 part.Mount_Point = (*iter)->Mount_Point;
1866 part.selected = 0;
1867 Partition_List->push_back(part);
1868 }
1869 if ((*iter)->Has_Android_Secure) {
1870 struct PartitionList part;
1871 part.Display_Name = (*iter)->Backup_Display_Name;
1872 part.Mount_Point = (*iter)->Backup_Path;
1873 part.selected = 0;
1874 Partition_List->push_back(part);
1875 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001876 if ((*iter)->Has_Data_Media) {
1877 struct PartitionList datamedia;
1878 datamedia.Display_Name = (*iter)->Storage_Name;
1879 datamedia.Mount_Point = "INTERNAL";
1880 datamedia.selected = 0;
1881 Partition_List->push_back(datamedia);
1882 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001883 }
1884 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001885 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001886 }
1887}
1888
1889int TWPartitionManager::Fstab_Processed(void) {
1890 return Partitions.size();
1891}
Dees_Troyd93bda52013-07-03 19:55:19 +00001892
1893void TWPartitionManager::Output_Storage_Fstab(void) {
1894 std::vector<TWPartition*>::iterator iter;
1895 char storage_partition[255];
1896 string Temp;
1897 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
1898
1899 if (fp == NULL) {
1900 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
1901 return;
1902 }
1903
1904 // Iterate through all partitions
1905 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1906 if ((*iter)->Is_Storage) {
1907 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
1908 strcpy(storage_partition, Temp.c_str());
1909 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
1910 }
1911 }
1912 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001913}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02001914
1915TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
1916{
1917 TWPartition *res = NULL;
1918 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
1919 if(!(*iter)->Is_Storage)
1920 continue;
1921
1922 if((*iter)->Is_Settings_Storage)
1923 return *iter;
1924
1925 if(!res)
1926 res = *iter;
1927 }
1928 return res;
1929}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001930
1931bool TWPartitionManager::Enable_MTP(void) {
1932#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001933 if (mtppid) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001934 LOGERR("MTP already enabled\n");
1935 return true;
1936 }
1937 //Launch MTP Responder
1938 LOGINFO("Starting MTP\n");
1939 char vendor[PROPERTY_VALUE_MAX];
1940 char product[PROPERTY_VALUE_MAX];
1941 int count = 0;
1942 property_set("sys.usb.config", "none");
1943 property_get("usb.vendor", vendor, "18D1");
1944 property_get("usb.product.mtpadb", product, "4EE2");
1945 string vendorstr = vendor;
1946 string productstr = product;
1947 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
1948 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
1949 property_set("sys.usb.config", "mtp,adb");
1950 std::vector<TWPartition*>::iterator iter;
Ethan Yonker6d154c42014-09-03 14:19:43 -05001951 /* To enable MTP debug, use the twrp command line feature to
1952 * twrp set tw_mtp_debug 1
1953 */
1954 twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug"));
that9e0593e2014-10-08 00:01:24 +02001955 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 -04001956 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1957 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false)) {
that9e0593e2014-10-08 00:01:24 +02001958 ++storageid;
1959 printf("twrp addStorage %s, mtpstorageid: %u\n", (*iter)->Storage_Path.c_str(), storageid);
1960 mtp->addStorage((*iter)->Storage_Name, (*iter)->Storage_Path, storageid);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001961 count++;
1962 }
1963 }
1964 if (count) {
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001965 mtppid = mtp->forkserver();
1966 if (mtppid) {
1967 DataManager::SetValue("tw_mtp_enabled", 1);
1968 return true;
1969 } else {
1970 LOGERR("Failed to enable MTP\n");
1971 return false;
1972 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001973 }
1974 LOGERR("No valid storage partitions found for MTP.\n");
1975#else
1976 LOGERR("MTP support not included\n");
1977#endif
1978 DataManager::SetValue("tw_mtp_enabled", 0);
1979 return false;
1980}
1981
1982bool TWPartitionManager::Disable_MTP(void) {
1983#ifdef TW_HAS_MTP
1984 char vendor[PROPERTY_VALUE_MAX];
1985 char product[PROPERTY_VALUE_MAX];
1986 property_set("sys.usb.config", "none");
1987 property_get("usb.vendor", vendor, "18D1");
1988 property_get("usb.product.adb", product, "D002");
1989 string vendorstr = vendor;
1990 string productstr = product;
1991 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
1992 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001993 if (mtppid) {
Ethan Yonker8613dc02014-09-11 09:28:20 -05001994 LOGINFO("Disabling MTP\n");
1995 int status;
1996 kill(mtppid, SIGKILL);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001997 mtppid = 0;
Ethan Yonker8613dc02014-09-11 09:28:20 -05001998 // We don't care about the exit value, but this prevents a zombie process
1999 waitpid(mtppid, &status, 0);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002000 }
2001 property_set("sys.usb.config", "adb");
2002 DataManager::SetValue("tw_mtp_enabled", 0);
2003 return true;
2004#else
2005 LOGERR("MTP support not included\n");
2006 DataManager::SetValue("tw_mtp_enabled", 0);
2007 return false;
2008#endif
2009}