blob: 0063ff3ea716bce9279f5b8a30aca15e044c09fd [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
24#include <unistd.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040025#include <vector>
Dees_Troy63c8df72012-09-10 14:02:05 -040026#include <dirent.h>
27#include <time.h>
Dees_Troy8170a922012-09-18 15:40:25 -040028#include <errno.h>
29#include <fcntl.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050030#include <iostream>
31#include <iomanip>
Ethan Yonker8613dc02014-09-11 09:28:20 -050032#include <sys/wait.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040033#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000034#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040036#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040037#include "twrp-functions.hpp"
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040038#include "fixPermissions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050039#include "twrpDigest.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050040#include "twrpDU.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040041
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040042#ifdef TW_HAS_MTP
43#include "mtp/mtp_MtpServer.hpp"
44#include "mtp/twrpMtp.hpp"
45#endif
46
Dees Troy6f6441d2014-01-23 02:07:03 +000047extern "C" {
48 #include "cutils/properties.h"
49}
50
Dees_Troy5bf43922012-09-07 16:07:55 -040051#ifdef TW_INCLUDE_CRYPTO
52 #ifdef TW_INCLUDE_JB_CRYPTO
53 #include "crypto/jb/cryptfs.h"
54 #else
55 #include "crypto/ics/cryptfs.h"
56 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -040057#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040058
Ethan Yonker6277c792014-09-15 14:54:30 -050059extern bool datamedia;
60
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050061TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040062 mtpid = 100;
63 mtp_was_enabled = false;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050064}
65
Dees_Troy51a0e822012-09-05 15:24:24 -040066int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040067 FILE *fstabFile;
68 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +000069 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050070 TWPartition* andsec_partition = NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -040071
72 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
73 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000074 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040075 return false;
76 }
77
78 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
79 if (fstab_line[0] != '/')
80 continue;
81
Dees_Troy2a923582012-09-20 12:13:34 -040082 if (fstab_line[strlen(fstab_line) - 1] != '\n')
83 fstab_line[strlen(fstab_line)] = '\n';
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040084 TWPartition* partition = new TWPartition(&mtpid);
Dees_Troy2a923582012-09-20 12:13:34 -040085 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040086 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040087
Dees_Troy5bf43922012-09-07 16:07:55 -040088 if (partition->Process_Fstab_Line(line, Display_Error)) {
Matt Mowered71fa32014-04-16 13:21:47 -050089 if (!settings_partition && partition->Is_Settings_Storage && partition->Is_Present) {
Dees Troy02a64532014-03-19 15:23:32 +000090 settings_partition = partition;
Dees_Troya13d74f2013-03-24 08:54:55 -050091 } else {
92 partition->Is_Settings_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050093 }
Matt Mowered71fa32014-04-16 13:21:47 -050094 if (!andsec_partition && partition->Has_Android_Secure && partition->Is_Present) {
Matt Mowerbf4efa32014-04-14 23:25:26 -050095 andsec_partition = partition;
96 } else {
97 partition->Has_Android_Secure = false;
98 }
Ethan Yonkerc05c5982014-03-13 09:19:56 -050099 Partitions.push_back(partition);
Dees_Troy5bf43922012-09-07 16:07:55 -0400100 } else {
101 delete partition;
102 }
103 }
104 fclose(fstabFile);
Ethan Yonker6277c792014-09-15 14:54:30 -0500105 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) {
106 // Attempt to automatically identify /data/media emulated storage devices
107 TWPartition* Dat = Find_Partition_By_Path("/data");
108 if (Dat) {
109 LOGINFO("Using automatic handling for /data/media emulated storage device.\n");
110 datamedia = true;
111 Dat->Setup_Data_Media(++mtpid);
112 settings_partition = Dat;
113 }
114 }
Dees Troy02a64532014-03-19 15:23:32 +0000115 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500116 std::vector<TWPartition*>::iterator iter;
117 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
118 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000119 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500120 break;
121 }
122 }
Dees Troy02a64532014-03-19 15:23:32 +0000123 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000124 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500125 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400126 if (!Write_Fstab()) {
127 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000128 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400129 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000130 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400131 }
Matt Mowered71fa32014-04-16 13:21:47 -0500132
Matt Mowerbf4efa32014-04-14 23:25:26 -0500133 if (andsec_partition) {
134 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500135 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500136 Setup_Android_Secure_Location(settings_partition);
137 }
Matt Mowered71fa32014-04-16 13:21:47 -0500138 if (settings_partition) {
139 Setup_Settings_Storage_Partition(settings_partition);
140 }
Dees_Troy51127312012-09-08 13:08:49 -0400141 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400142 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400143 return true;
144}
145
146int TWPartitionManager::Write_Fstab(void) {
147 FILE *fp;
148 std::vector<TWPartition*>::iterator iter;
149 string Line;
150
151 fp = fopen("/etc/fstab", "w");
152 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000153 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400154 return false;
155 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400156 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400157 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400158 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400159 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000160 }
161 // Handle subpartition tracking
162 if ((*iter)->Is_SubPartition) {
163 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
164 if (ParentPartition)
165 ParentPartition->Has_SubPartition = true;
166 else
167 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 -0400168 }
169 }
170 fclose(fp);
171 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400172}
173
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500174void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500175 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
176 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
177 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
178}
179
Matt Mowerbf4efa32014-04-14 23:25:26 -0500180void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
181 if (Part->Has_Android_Secure)
182 Part->Setup_AndSec();
Ethan Yonker6277c792014-09-15 14:54:30 -0500183 else if (!datamedia)
Matt Mowerbf4efa32014-04-14 23:25:26 -0500184 Part->Setup_AndSec();
Matt Mowerbf4efa32014-04-14 23:25:26 -0500185}
186
Dees_Troy8170a922012-09-18 15:40:25 -0400187void TWPartitionManager::Output_Partition_Logging(void) {
188 std::vector<TWPartition*>::iterator iter;
189
190 printf("\n\nPartition Logs:\n");
191 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
192 Output_Partition((*iter));
193}
194
195void TWPartitionManager::Output_Partition(TWPartition* Part) {
196 unsigned long long mb = 1048576;
197
Gary Peck004d48b2012-11-21 16:28:18 -0800198 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 -0400199 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800200 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 -0400201 }
Gary Peck004d48b2012-11-21 16:28:18 -0800202 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500203 if (Part->Can_Be_Mounted)
204 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800205 if (Part->Can_Be_Wiped)
206 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700207 if (Part->Use_Rm_Rf)
208 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500209 if (Part->Can_Be_Backed_Up)
210 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800211 if (Part->Wipe_During_Factory_Reset)
212 printf("Wipe_During_Factory_Reset ");
213 if (Part->Wipe_Available_in_GUI)
214 printf("Wipe_Available_in_GUI ");
215 if (Part->Is_SubPartition)
216 printf("Is_SubPartition ");
217 if (Part->Has_SubPartition)
218 printf("Has_SubPartition ");
219 if (Part->Removable)
220 printf("Removable ");
221 if (Part->Is_Present)
222 printf("IsPresent ");
223 if (Part->Can_Be_Encrypted)
224 printf("Can_Be_Encrypted ");
225 if (Part->Is_Encrypted)
226 printf("Is_Encrypted ");
227 if (Part->Is_Decrypted)
228 printf("Is_Decrypted ");
229 if (Part->Has_Data_Media)
230 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000231 if (Part->Can_Encrypt_Backup)
232 printf("Can_Encrypt_Backup ");
233 if (Part->Use_Userdata_Encryption)
234 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800235 if (Part->Has_Android_Secure)
236 printf("Has_Android_Secure ");
237 if (Part->Is_Storage)
238 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500239 if (Part->Is_Settings_Storage)
240 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000241 if (Part->Ignore_Blkid)
242 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000243 if (Part->Retain_Layout_Version)
244 printf("Retain_Layout_Version ");
Gary Peck004d48b2012-11-21 16:28:18 -0800245 printf("\n");
246 if (!Part->SubPartition_Of.empty())
247 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
248 if (!Part->Symlink_Path.empty())
249 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
250 if (!Part->Symlink_Mount_Point.empty())
251 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
252 if (!Part->Primary_Block_Device.empty())
253 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
254 if (!Part->Alternate_Block_Device.empty())
255 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
256 if (!Part->Decrypted_Block_Device.empty())
257 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
258 if (Part->Length != 0)
259 printf(" Length: %i\n", Part->Length);
260 if (!Part->Display_Name.empty())
261 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500262 if (!Part->Storage_Name.empty())
263 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800264 if (!Part->Backup_Path.empty())
265 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
266 if (!Part->Backup_Name.empty())
267 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500268 if (!Part->Backup_Display_Name.empty())
269 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800270 if (!Part->Backup_FileName.empty())
271 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
272 if (!Part->Storage_Path.empty())
273 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
274 if (!Part->Current_File_System.empty())
275 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
276 if (!Part->Fstab_File_System.empty())
277 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
278 if (Part->Format_Block_Size != 0)
279 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400280 if (!Part->MTD_Name.empty())
281 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400282 string back_meth = Part->Backup_Method_By_Name();
Ethan Yonker6277c792014-09-15 14:54:30 -0500283 printf(" Backup_Method: %s\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800284 if (Part->Mount_Flags || !Part->Mount_Options.empty())
285 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Ethan Yonker6277c792014-09-15 14:54:30 -0500286 if (Part->mtpid)
287 printf(" MTP Storage ID: %i\n", Part->mtpid);
288 printf("\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400289}
290
Dees_Troy51a0e822012-09-05 15:24:24 -0400291int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400292 std::vector<TWPartition*>::iterator iter;
293 int ret = false;
294 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400295 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400296
Dees_Troyd93bda52013-07-03 19:55:19 +0000297 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400298 return true;
299
Dees_Troy5bf43922012-09-07 16:07:55 -0400300 // Iterate through all partitions
301 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400302 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400303 ret = (*iter)->Mount(Display_Error);
304 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400305 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400306 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400307 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400308 }
309 if (found) {
310 return ret;
311 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000312 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400313 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000314 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400315 }
316 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400317}
318
319int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400320 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400321
Dees_Troy51127312012-09-08 13:08:49 -0400322 if (Part) {
323 if (Part->Has_SubPartition) {
324 std::vector<TWPartition*>::iterator subpart;
325
326 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
327 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
328 (*subpart)->Mount(Display_Error);
329 }
330 return Part->Mount(Display_Error);
331 } else
332 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400333 }
334 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000335 LOGERR("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400336 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000337 LOGINFO("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400338 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400339}
340
341int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400342 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400343
Dees_Troy51127312012-09-08 13:08:49 -0400344 if (Part) {
345 if (Part->Has_SubPartition) {
346 std::vector<TWPartition*>::iterator subpart;
347
348 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
349 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
350 (*subpart)->Mount(Display_Error);
351 }
352 return Part->Mount(Display_Error);
353 } else
354 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400355 }
356 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000357 LOGERR("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400358 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000359 LOGINFO("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400360 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400361}
362
363int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400364 std::vector<TWPartition*>::iterator iter;
365 int ret = false;
366 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400367 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400368
369 // Iterate through all partitions
370 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400371 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400372 ret = (*iter)->UnMount(Display_Error);
373 found = true;
374 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
375 (*iter)->UnMount(Display_Error);
376 }
377 }
378 if (found) {
379 return ret;
380 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000381 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400382 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000383 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400384 }
385 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400386}
387
388int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400389 TWPartition* Part = Find_Partition_By_Block(Block);
390
391 if (Part) {
392 if (Part->Has_SubPartition) {
393 std::vector<TWPartition*>::iterator subpart;
394
395 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
396 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
397 (*subpart)->UnMount(Display_Error);
398 }
399 return Part->UnMount(Display_Error);
400 } else
401 return Part->UnMount(Display_Error);
402 }
403 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000404 LOGERR("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400405 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000406 LOGINFO("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400407 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400408}
409
410int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400411 TWPartition* Part = Find_Partition_By_Name(Name);
412
413 if (Part) {
414 if (Part->Has_SubPartition) {
415 std::vector<TWPartition*>::iterator subpart;
416
417 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
418 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
419 (*subpart)->UnMount(Display_Error);
420 }
421 return Part->UnMount(Display_Error);
422 } else
423 return Part->UnMount(Display_Error);
424 }
425 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000426 LOGERR("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400427 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000428 LOGINFO("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400429 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400430}
431
432int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400433 TWPartition* Part = Find_Partition_By_Path(Path);
434
435 if (Part)
436 return Part->Is_Mounted();
437 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000438 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400439 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400440}
441
442int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400443 TWPartition* Part = Find_Partition_By_Block(Block);
444
445 if (Part)
446 return Part->Is_Mounted();
447 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000448 LOGINFO("Is_Mounted: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400449 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400450}
451
452int TWPartitionManager::Is_Mounted_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400453 TWPartition* Part = Find_Partition_By_Name(Name);
454
455 if (Part)
456 return Part->Is_Mounted();
457 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000458 LOGINFO("Is_Mounted: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400459 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400460}
461
Dees_Troy5bf43922012-09-07 16:07:55 -0400462int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400463 string current_storage_path = DataManager::GetCurrentStoragePath();
464
465 if (Mount_By_Path(current_storage_path, Display_Error)) {
466 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
467 if (FreeStorage)
468 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
469 return true;
470 }
471 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400472}
473
Dees_Troy5bf43922012-09-07 16:07:55 -0400474int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
475 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
476}
477
478TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
479 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400480 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400481
482 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400483 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400484 return (*iter);
485 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400486 return NULL;
487}
488
Dees_Troy5bf43922012-09-07 16:07:55 -0400489TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400490 std::vector<TWPartition*>::iterator iter;
491
492 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400493 if ((*iter)->Primary_Block_Device == Block || (*iter)->Alternate_Block_Device == Block || ((*iter)->Is_Decrypted && (*iter)->Decrypted_Block_Device == Block))
Dees_Troy51127312012-09-08 13:08:49 -0400494 return (*iter);
495 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400496 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400497}
498
499TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400500 std::vector<TWPartition*>::iterator iter;
501
502 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
503 if ((*iter)->Display_Name == Name)
504 return (*iter);
505 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400506 return NULL;
507}
Dees_Troy51a0e822012-09-05 15:24:24 -0400508
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400509int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
510 // Check the backup name to ensure that it is the correct size and contains only valid characters
511 // and that a backup with that name doesn't already exist
512 char backup_name[MAX_BACKUP_NAME_LEN];
513 char backup_loc[255], tw_image_dir[255];
514 int copy_size;
515 int index, cur_char;
516 string Backup_Name, Backup_Loc;
517
518 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
519 copy_size = Backup_Name.size();
520 // Check size
521 if (copy_size > MAX_BACKUP_NAME_LEN) {
522 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000523 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400524 return -2;
525 }
526
527 // Check each character
528 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500529 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400530 return 0; // A "0" (zero) means to use the current timestamp for the backup name
531 for (index=0; index<copy_size; index++) {
532 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500533 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 -0400534 // These are valid characters
535 // Numbers
536 // Upper case letters
537 // Lower case letters
538 // Space
539 // and -_.{}[]
540 } else {
541 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000542 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400543 return -3;
544 }
545 }
546
547 // Check to make sure that a backup with this name doesn't already exist
548 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
549 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500550 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500551 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400552 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000553 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400554 return -4;
555 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400556 // No problems found, return 0
557 return 0;
558}
559
Dees_Troy43d8b002012-09-17 16:00:01 -0400560bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
561{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500562 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400563 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500564 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500565 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400566
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500567 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400568 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400569
Dees_Troyb46a6842012-09-25 11:06:46 -0400570 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000571 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400572
573 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500574 md5sum.setfn(Backup_Folder + Backup_Filename);
575 if (md5sum.computeMD5() == 0)
576 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000577 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500578 else
579 return -1;
580 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000581 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400582 } else {
583 char filename[512];
584 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500585 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400586 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500587 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000588 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400589 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000590 if (TWFunc::Path_Exists(filename)) {
591 if (md5sum.computeMD5() == 0) {
592 if (md5sum.write_md5digest() != 0)
593 {
594 gui_print(" * MD5 Error.\n");
595 return false;
596 }
597 } else {
598 gui_print(" * Error computing MD5.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500599 return false;
600 }
601 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400602 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400603 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500604 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400605 }
606 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000607 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400608 return false;
609 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000610 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400611 }
612 return true;
613}
614
Dees_Troy093b7642012-09-21 15:59:38 -0400615bool 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 -0400616 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500617 int img_bps;
618 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400619 unsigned long total_time, remain_time, section_time;
620 int use_compression, backup_time;
621 float pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500622 unsigned long long total_size, current_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400623
624 if (Part == NULL)
625 return true;
626
Dees_Troy093b7642012-09-21 15:59:38 -0400627 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
628
629 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
630 if (use_compression)
631 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
632 else
633 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
634
635 // We know the speed for both, how far into the whole backup are we, based on time
636 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
637 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
638
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500639 //pos = (total_time - remain_time) / (float) total_time;
640 total_size = *file_bytes + *img_bytes;
641 current_size = *file_bytes + *img_bytes - *file_bytes_remaining - *img_bytes_remaining;
642 pos = ((float)(current_size) / (float)(total_size));
Dees_Troy2673cec2013-04-02 20:22:16 +0000643 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400644
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500645 LOGINFO("Estimated total time: %lu\nEstimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400646
647 // And get the time
648 if (Part->Backup_Method == 1)
649 section_time = Part->Backup_Size / file_bps;
650 else
651 section_time = Part->Backup_Size / img_bps;
652
653 // Set the position
654 pos = section_time / (float) total_time;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500655 //DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400656
Dees_Troy43d8b002012-09-17 16:00:01 -0400657 time(&start);
658
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500659 if (Part->Backup(Backup_Folder, &total_size, &current_size)) {
660 current_size += Part->Backup_Size;
661 pos = (float)((float)(current_size) / (float)(total_size));
662 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400663 if (Part->Has_SubPartition) {
664 std::vector<TWPartition*>::iterator subpart;
665
666 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500667 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500668 if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size))
Dees_Troy8170a922012-09-18 15:40:25 -0400669 return false;
Dees_Troy2727b992013-08-14 20:09:30 +0000670 sync();
671 sync();
Dees_Troy8170a922012-09-18 15:40:25 -0400672 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
673 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400674 if (Part->Backup_Method == 1) {
675 *file_bytes_remaining -= (*subpart)->Backup_Size;
676 } else {
677 *img_bytes_remaining -= (*subpart)->Backup_Size;
678 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500679 current_size += Part->Backup_Size;
680 pos = (float)(current_size / total_size);
681 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400682 }
683 }
684 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400685 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400686 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000687 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400688 if (Part->Backup_Method == 1) {
689 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400690 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400691 } else {
692 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400693 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400694 }
695 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
696 } else {
697 return false;
698 }
699}
700
701int TWPartitionManager::Run_Backup(void) {
702 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500703 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400704 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 -0400705 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500706 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400707 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400708 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400709 struct tm *t;
710 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500711 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400712 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500713 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400714
715 time(&total_start);
716
717 Update_System_Details();
718
719 if (!Mount_Current_Storage(true))
720 return false;
721
722 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400723 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400724 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400725 else
726 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400727
Dees_Troy43d8b002012-09-17 16:00:01 -0400728 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
729 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000730 if (Backup_Name == "(Current Date)") {
731 Backup_Name = TWFunc::Get_Current_Date();
732 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
733 TWFunc::Auto_Generate_Backup_Name();
734 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400735 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000736 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400737 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000738 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400739
Dees_Troy2673cec2013-04-02 20:22:16 +0000740 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500741 DataManager::GetValue("tw_backup_list", Backup_List);
742 if (!Backup_List.empty()) {
743 end_pos = Backup_List.find(";", start_pos);
744 while (end_pos != string::npos && start_pos < Backup_List.size()) {
745 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
746 backup_part = Find_Partition_By_Path(backup_path);
747 if (backup_part != NULL) {
748 partition_count++;
749 if (backup_part->Backup_Method == 1)
750 file_bytes += backup_part->Backup_Size;
751 else
752 img_bytes += backup_part->Backup_Size;
753 if (backup_part->Has_SubPartition) {
754 std::vector<TWPartition*>::iterator subpart;
755
756 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000757 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 -0500758 partition_count++;
759 if ((*subpart)->Backup_Method == 1)
760 file_bytes += (*subpart)->Backup_Size;
761 else
762 img_bytes += (*subpart)->Backup_Size;
763 }
764 }
Dees_Troy8170a922012-09-18 15:40:25 -0400765 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500766 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000767 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400768 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500769 start_pos = end_pos + 1;
770 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400771 }
772 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400773
774 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000775 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400776 return false;
777 }
778 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000779 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500780 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400781 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
782 if (storage != NULL) {
783 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000784 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400785 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000786 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400787 return false;
788 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000789 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400790 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000791 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400792 return false;
793 }
794 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500795 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400796
Dees_Troy2673cec2013-04-02 20:22:16 +0000797 gui_print("\n[BACKUP STARTED]\n");
798 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000799 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000800 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000801 return false;
802 }
803
Dees_Troy2673cec2013-04-02 20:22:16 +0000804 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400805
Dees_Troya13d74f2013-03-24 08:54:55 -0500806 start_pos = 0;
807 end_pos = Backup_List.find(";", start_pos);
808 while (end_pos != string::npos && start_pos < Backup_List.size()) {
809 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
810 backup_part = Find_Partition_By_Path(backup_path);
811 if (backup_part != NULL) {
812 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
813 return false;
814 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000815 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500816 }
817 start_pos = end_pos + 1;
818 end_pos = Backup_List.find(";", start_pos);
819 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400820
821 // Average BPS
822 if (img_time == 0)
823 img_time = 1;
824 if (file_time == 0)
825 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400826 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500827 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400828
Dees_Troy2673cec2013-04-02 20:22:16 +0000829 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
830 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400831
832 time(&total_stop);
833 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500834 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500835 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400836
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500837 int prev_img_bps, use_compression;
838 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400839 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
840 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500841 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400842
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500843 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400844 if (use_compression)
845 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500846 else
Dees_Troy093b7642012-09-21 15:59:38 -0400847 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
848 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500849 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400850
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500851 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400852 if (use_compression)
853 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
854 else
855 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
856
Dees_Troy2673cec2013-04-02 20:22:16 +0000857 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400858 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400859 UnMount_Main_Partitions();
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500860 gui_print_color("highlight", "[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000861 string backup_log = Full_Backup_Path + "recovery.log";
862 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
863 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400864}
865
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500866bool 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 -0400867 time_t Start, Stop;
868 time(&Start);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500869 //DataManager::ShowProgress(1.0 / (float)partition_count, 150);
870 if (!Part->Restore(Restore_Name, total_restore_size, already_restored_size))
Dees_Troy4a2a1262012-09-18 09:33:47 -0400871 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400872 if (Part->Has_SubPartition) {
873 std::vector<TWPartition*>::iterator subpart;
874
875 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
876 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500877 if (!(*subpart)->Restore(Restore_Name, total_restore_size, already_restored_size))
Dees_Troy8170a922012-09-18 15:40:25 -0400878 return false;
879 }
880 }
881 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400882 time(&Stop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000883 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 -0400884 return true;
885}
886
Dees_Troy51a0e822012-09-05 15:24:24 -0400887int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400888 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500889 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400890 time_t rStart, rStop;
891 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500892 string Restore_List, restore_path;
893 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500894 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400895
Dees_Troy2673cec2013-04-02 20:22:16 +0000896 gui_print("\n[RESTORE STARTED]\n\n");
897 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400898
Dees_Troy4a2a1262012-09-18 09:33:47 -0400899 if (!Mount_Current_Storage(true))
900 return false;
901
902 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500903 if (check_md5 > 0) {
904 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
905 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000906 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500907 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000908 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500909 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500910 gui_print("Calculating restore details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500911 DataManager::GetValue("tw_restore_selected", Restore_List);
912 if (!Restore_List.empty()) {
913 end_pos = Restore_List.find(";", start_pos);
914 while (end_pos != string::npos && start_pos < Restore_List.size()) {
915 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
916 restore_part = Find_Partition_By_Path(restore_path);
917 if (restore_part != NULL) {
918 partition_count++;
919 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
920 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500921 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500922 if (restore_part->Has_SubPartition) {
923 std::vector<TWPartition*>::iterator subpart;
924
925 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
926 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400927 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500928 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500929 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500930 }
931 }
932 }
933 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500934 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500935 }
936 start_pos = end_pos + 1;
937 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400938 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400939 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400940
941 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000942 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400943 return false;
944 }
945
Dees_Troy2673cec2013-04-02 20:22:16 +0000946 gui_print("Restoring %i partitions...\n", partition_count);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500947 gui_print("Total restore size is %lluMB\n", total_restore_size / 1048576);
Dees_Troy2673cec2013-04-02 20:22:16 +0000948 DataManager::SetProgress(0.0);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500949
Dees_Troya13d74f2013-03-24 08:54:55 -0500950 start_pos = 0;
951 if (!Restore_List.empty()) {
952 end_pos = Restore_List.find(";", start_pos);
953 while (end_pos != string::npos && start_pos < Restore_List.size()) {
954 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
955 restore_part = Find_Partition_By_Path(restore_path);
956 if (restore_part != NULL) {
957 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500958 if (!Restore_Partition(restore_part, Restore_Name, partition_count, &total_restore_size, &already_restored_size))
Dees_Troya13d74f2013-03-24 08:54:55 -0500959 return false;
960 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000961 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500962 }
963 start_pos = end_pos + 1;
964 end_pos = Restore_List.find(";", start_pos);
965 }
966 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400967 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400968 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400969 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400970 time(&rStop);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500971 gui_print_color("highlight", "[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500972 DataManager::SetValue("tw_file_progress", "");
Dees_Troy63c8df72012-09-10 14:02:05 -0400973 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400974}
975
976void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400977 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500978 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000979 bool get_date = true, check_encryption = true;
980
981 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400982
983 DIR* d;
984 d = opendir(Restore_Name.c_str());
985 if (d == NULL)
986 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000987 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400988 return;
989 }
990
991 struct dirent* de;
992 while ((de = readdir(d)) != NULL)
993 {
994 // Strip off three components
995 char str[256];
996 char* label;
997 char* fstype = NULL;
998 char* extn = NULL;
999 char* ptr;
1000
1001 strcpy(str, de->d_name);
1002 if (strlen(str) <= 2)
1003 continue;
1004
1005 if (get_date) {
1006 char file_path[255];
1007 struct stat st;
1008
1009 strcpy(file_path, Restore_Name.c_str());
1010 strcat(file_path, "/");
1011 strcat(file_path, str);
1012 stat(file_path, &st);
1013 string backup_date = ctime((const time_t*)(&st.st_mtime));
1014 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
1015 get_date = false;
1016 }
1017
1018 label = str;
1019 ptr = label;
1020 while (*ptr && *ptr != '.') ptr++;
1021 if (*ptr == '.')
1022 {
1023 *ptr = 0x00;
1024 ptr++;
1025 fstype = ptr;
1026 }
1027 while (*ptr && *ptr != '.') ptr++;
1028 if (*ptr == '.')
1029 {
1030 *ptr = 0x00;
1031 ptr++;
1032 extn = ptr;
1033 }
1034
Dees_Troy83bd4832013-05-04 12:39:56 +00001035 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -05001036 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +00001037 if (extnlength != 3 && extnlength != 6) continue;
1038 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
1039 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
1040
1041 if (check_encryption) {
1042 string filename = Restore_Name + "/";
1043 filename += de->d_name;
1044 if (TWFunc::Get_File_Type(filename) == 2) {
1045 LOGINFO("'%s' is encrypted\n", filename.c_str());
1046 DataManager::SetValue("tw_restore_encrypted", 1);
1047 }
1048 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001049 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -04001050
1051 TWPartition* Part = Find_Partition_By_Path(label);
1052 if (Part == NULL)
1053 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001054 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -04001055 continue;
1056 }
1057
1058 Part->Backup_FileName = de->d_name;
1059 if (strlen(extn) > 3) {
1060 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1061 }
1062
Dees_Troya13d74f2013-03-24 08:54:55 -05001063 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001064 }
1065 closedir(d);
1066
Dees_Troya13d74f2013-03-24 08:54:55 -05001067 // Set the final value
1068 DataManager::SetValue("tw_restore_list", Restore_List);
1069 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001070 return;
1071}
1072
1073int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001074 std::vector<TWPartition*>::iterator iter;
1075 int ret = false;
1076 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001077 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001078
1079 // Iterate through all partitions
1080 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001081 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001082 if (Path == "/and-sec")
1083 ret = (*iter)->Wipe_AndSec();
1084 else
1085 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001086 found = true;
1087 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1088 (*iter)->Wipe();
1089 }
1090 }
1091 if (found) {
1092 return ret;
1093 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001094 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001095 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001096}
1097
1098int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001099 TWPartition* Part = Find_Partition_By_Block(Block);
1100
1101 if (Part) {
1102 if (Part->Has_SubPartition) {
1103 std::vector<TWPartition*>::iterator subpart;
1104
1105 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1106 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1107 (*subpart)->Wipe();
1108 }
1109 return Part->Wipe();
1110 } else
1111 return Part->Wipe();
1112 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001113 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001114 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001115}
1116
1117int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001118 TWPartition* Part = Find_Partition_By_Name(Name);
1119
1120 if (Part) {
1121 if (Part->Has_SubPartition) {
1122 std::vector<TWPartition*>::iterator subpart;
1123
1124 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1125 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1126 (*subpart)->Wipe();
1127 }
1128 return Part->Wipe();
1129 } else
1130 return Part->Wipe();
1131 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001132 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001133 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001134}
1135
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001136int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1137 std::vector<TWPartition*>::iterator iter;
1138 int ret = false;
1139 bool found = false;
1140 string Local_Path = TWFunc::Get_Root_Path(Path);
1141
1142 // Iterate through all partitions
1143 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1144 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1145 if (Path == "/and-sec")
1146 ret = (*iter)->Wipe_AndSec();
1147 else
1148 ret = (*iter)->Wipe(New_File_System);
1149 found = true;
1150 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1151 (*iter)->Wipe(New_File_System);
1152 }
1153 }
1154 if (found) {
1155 return ret;
1156 } else
1157 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1158 return false;
1159}
1160
1161int TWPartitionManager::Wipe_By_Block(string Block, string New_File_System) {
1162 TWPartition* Part = Find_Partition_By_Block(Block);
1163
1164 if (Part) {
1165 if (Part->Has_SubPartition) {
1166 std::vector<TWPartition*>::iterator subpart;
1167
1168 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1169 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1170 (*subpart)->Wipe(New_File_System);
1171 }
1172 return Part->Wipe(New_File_System);
1173 } else
1174 return Part->Wipe(New_File_System);
1175 }
1176 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
1177 return false;
1178}
1179
1180int TWPartitionManager::Wipe_By_Name(string Name, string New_File_System) {
1181 TWPartition* Part = Find_Partition_By_Name(Name);
1182
1183 if (Part) {
1184 if (Part->Has_SubPartition) {
1185 std::vector<TWPartition*>::iterator subpart;
1186
1187 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1188 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1189 (*subpart)->Wipe();
1190 }
1191 return Part->Wipe(New_File_System);
1192 } else
1193 return Part->Wipe(New_File_System);
1194 }
1195 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
1196 return false;
1197}
1198
Dees_Troy51a0e822012-09-05 15:24:24 -04001199int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001200 std::vector<TWPartition*>::iterator iter;
1201 int ret = true;
1202
1203 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001204 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001205 if (!(*iter)->Wipe())
1206 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001207 } else if ((*iter)->Has_Android_Secure) {
1208 if (!(*iter)->Wipe_AndSec())
1209 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001210 }
1211 }
1212 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001213}
1214
Dees_Troy38bd7602012-09-14 13:33:53 -04001215int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1216 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001217 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001218
1219 if (!Mount_By_Path("/data", true))
1220 return false;
1221
1222 if (!Mount_By_Path("/cache", true))
1223 return false;
1224
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001225 dir.push_back("/data/dalvik-cache");
1226 dir.push_back("/cache/dalvik-cache");
1227 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001228 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001229 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001230 if (stat(dir.at(i).c_str(), &st) == 0) {
1231 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001232 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001233 }
1234 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001235 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001236 if (sdext && sdext->Is_Present && sdext->Mount(false))
1237 {
1238 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1239 {
1240 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001241 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001242 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001243 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001244 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001245 return true;
1246}
1247
1248int TWPartitionManager::Wipe_Rotate_Data(void) {
1249 if (!Mount_By_Path("/data", true))
1250 return false;
1251
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001252 unlink("/data/misc/akmd*");
1253 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001254 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001255 return true;
1256}
1257
1258int TWPartitionManager::Wipe_Battery_Stats(void) {
1259 struct stat st;
1260
1261 if (!Mount_By_Path("/data", true))
1262 return false;
1263
1264 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001265 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001266 } else {
1267 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001268 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001269 }
1270 return true;
1271}
1272
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001273int TWPartitionManager::Wipe_Android_Secure(void) {
1274 std::vector<TWPartition*>::iterator iter;
1275 int ret = false;
1276 bool found = false;
1277
1278 // Iterate through all partitions
1279 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1280 if ((*iter)->Has_Android_Secure) {
1281 ret = (*iter)->Wipe_AndSec();
1282 found = true;
1283 }
1284 }
1285 if (found) {
1286 return ret;
1287 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001288 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001289 }
1290 return false;
1291}
1292
Dees_Troy38bd7602012-09-14 13:33:53 -04001293int TWPartitionManager::Format_Data(void) {
1294 TWPartition* dat = Find_Partition_By_Path("/data");
1295
1296 if (dat != NULL) {
1297 if (!dat->UnMount(true))
1298 return false;
1299
1300 return dat->Wipe_Encryption();
1301 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001302 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001303 return false;
1304 }
1305 return false;
1306}
1307
1308int TWPartitionManager::Wipe_Media_From_Data(void) {
1309 TWPartition* dat = Find_Partition_By_Path("/data");
1310
1311 if (dat != NULL) {
1312 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001313 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001314 return false;
1315 }
1316 if (!dat->Mount(true))
1317 return false;
1318
Dees_Troy2673cec2013-04-02 20:22:16 +00001319 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001320 mtp_was_enabled = TWFunc::Toggle_MTP(false);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001321 TWFunc::removeDir("/data/media", false);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001322 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
1323 if (mtp_was_enabled) {
1324 if (!Enable_MTP())
1325 Disable_MTP();
1326 }
1327 return false;
1328 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001329 if (dat->Has_Data_Media) {
1330 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001331 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1332 dat->UnMount(false);
1333 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001334 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001335 if (mtp_was_enabled) {
1336 if (!Enable_MTP())
1337 Disable_MTP();
1338 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001339 return true;
1340 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001341 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001342 return false;
1343 }
1344 return false;
1345}
1346
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001347int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1348 std::vector<TWPartition*>::iterator iter;
1349 int ret = false;
1350 bool found = false;
1351 string Local_Path = TWFunc::Get_Root_Path(Path);
1352
1353 if (Local_Path == "/tmp" || Local_Path == "/")
1354 return true;
1355
1356 // Iterate through all partitions
1357 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1358 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1359 ret = (*iter)->Repair();
1360 found = true;
1361 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1362 (*iter)->Repair();
1363 }
1364 }
1365 if (found) {
1366 return ret;
1367 } else if (Display_Error) {
1368 LOGERR("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1369 } else {
1370 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1371 }
1372 return false;
1373}
1374
1375int TWPartitionManager::Repair_By_Block(string Block, bool Display_Error) {
1376 TWPartition* Part = Find_Partition_By_Block(Block);
1377
1378 if (Part) {
1379 if (Part->Has_SubPartition) {
1380 std::vector<TWPartition*>::iterator subpart;
1381
1382 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1383 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1384 (*subpart)->Repair();
1385 }
1386 return Part->Repair();
1387 } else
1388 return Part->Repair();
1389 }
1390 if (Display_Error)
1391 LOGERR("Repair: Unable to find partition for block '%s'\n", Block.c_str());
1392 else
1393 LOGINFO("Repair: Unable to find partition for block '%s'\n", Block.c_str());
1394 return false;
1395}
1396
1397int TWPartitionManager::Repair_By_Name(string Name, bool Display_Error) {
1398 TWPartition* Part = Find_Partition_By_Name(Name);
1399
1400 if (Part) {
1401 if (Part->Has_SubPartition) {
1402 std::vector<TWPartition*>::iterator subpart;
1403
1404 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1405 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1406 (*subpart)->Repair();
1407 }
1408 return Part->Repair();
1409 } else
1410 return Part->Repair();
1411 }
1412 if (Display_Error)
1413 LOGERR("Repair: Unable to find partition for name '%s'\n", Name.c_str());
1414 else
1415 LOGINFO("Repair: Unable to find partition for name '%s'\n", Name.c_str());
1416 return false;
1417}
1418
Dees_Troy51a0e822012-09-05 15:24:24 -04001419void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001420 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001421 return;
1422}
1423
1424void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001425 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001426 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001427
Dees_Troy2673cec2013-04-02 20:22:16 +00001428 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001429 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001430 if ((*iter)->Can_Be_Mounted) {
1431 (*iter)->Update_Size(true);
1432 if ((*iter)->Mount_Point == "/system") {
1433 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1434 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1435 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1436 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1437 } else if ((*iter)->Mount_Point == "/cache") {
1438 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1439 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1440 } else if ((*iter)->Mount_Point == "/sd-ext") {
1441 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1442 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1443 if ((*iter)->Backup_Size == 0) {
1444 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1445 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1446 } else
1447 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001448 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001449 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001450 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001451 if ((*iter)->Backup_Size == 0) {
1452 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1453 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1454 } else
1455 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001456 } else if ((*iter)->Mount_Point == "/boot") {
1457 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1458 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1459 if ((*iter)->Backup_Size == 0) {
1460 DataManager::SetValue("tw_has_boot_partition", 0);
1461 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1462 } else
1463 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001464 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001465#ifdef SP1_NAME
1466 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1467 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1468 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1469 }
1470#endif
1471#ifdef SP2_NAME
1472 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1473 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1474 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1475 }
1476#endif
1477#ifdef SP3_NAME
1478 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1479 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1480 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1481 }
1482#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001483 } else {
1484 // Handle unmountable partitions in case we reset defaults
1485 if ((*iter)->Mount_Point == "/boot") {
1486 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1487 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1488 if ((*iter)->Backup_Size == 0) {
1489 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1490 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1491 } else
1492 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1493 } else if ((*iter)->Mount_Point == "/recovery") {
1494 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1495 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1496 if ((*iter)->Backup_Size == 0) {
1497 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1498 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1499 } else
1500 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001501 } else if ((*iter)->Mount_Point == "/data") {
1502 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001503 }
1504#ifdef SP1_NAME
1505 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1506 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1507 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1508 }
1509#endif
1510#ifdef SP2_NAME
1511 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1512 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1513 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1514 }
1515#endif
1516#ifdef SP3_NAME
1517 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1518 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1519 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1520 }
1521#endif
Dees_Troy51127312012-09-08 13:08:49 -04001522 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001523 }
Dees_Troy51127312012-09-08 13:08:49 -04001524 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1525 string current_storage_path = DataManager::GetCurrentStoragePath();
1526 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001527 if (FreeStorage != NULL) {
1528 // Attempt to mount storage
1529 if (!FreeStorage->Mount(false)) {
1530 // We couldn't mount storage... check to see if we have dual storage
1531 int has_dual_storage;
1532 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1533 if (has_dual_storage == 1) {
1534 // We have dual storage, see if we're using the internal storage that should always be present
1535 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001536 if (!FreeStorage->Is_Encrypted) {
1537 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001538 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001539 }
Dees_Troy8170a922012-09-18 15:40:25 -04001540 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1541 } else {
1542 // We were using external, flip to internal
1543 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1544 current_storage_path = DataManager::GetCurrentStoragePath();
1545 FreeStorage = Find_Partition_By_Path(current_storage_path);
1546 if (FreeStorage != NULL) {
1547 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1548 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001549 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001550 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1551 }
1552 }
1553 } else {
1554 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001555 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001556 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1557 }
1558 } else {
1559 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1560 }
1561 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001562 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001563 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001564 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001565 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001566 return;
1567}
1568
1569int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001570#ifdef TW_INCLUDE_CRYPTO
1571 int ret_val, password_len;
1572 char crypto_blkdev[255], cPassword[255];
1573 size_t result;
1574
1575 property_set("ro.crypto.state", "encrypted");
1576#ifdef TW_INCLUDE_JB_CRYPTO
1577 // No extra flags needed
1578#else
1579 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1580 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1581 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1582 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1583 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1584 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001585
1586#ifdef CRYPTO_SD_FS_TYPE
Ethan Yonker71413f42014-02-26 13:36:08 -06001587 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1588 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1589 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001590#endif
a39552696ff55ce2013-01-08 16:14:56 +00001591
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001592 property_set("rw.km_fips_status", "ready");
a39552696ff55ce2013-01-08 16:14:56 +00001593
1594#endif
1595
1596 // some samsung devices store "footer" on efs partition
1597 TWPartition *efs = Find_Partition_By_Path("/efs");
1598 if(efs && !efs->Is_Mounted())
1599 efs->Mount(false);
1600 else
1601 efs = 0;
1602#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001603#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001604 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001605 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001606 property_set("ro.crypto.external_encrypted", "1");
1607 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1608 } else {
1609 property_set("ro.crypto.external_encrypted", "0");
1610 }
1611#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001612#endif
a39552696ff55ce2013-01-08 16:14:56 +00001613
Dees_Troy5bf43922012-09-07 16:07:55 -04001614 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001615 int pwret = cryptfs_check_passwd(cPassword);
1616
1617 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001618 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001619 return -1;
1620 }
a39552696ff55ce2013-01-08 16:14:56 +00001621
1622 if(efs)
1623 efs->UnMount(false);
1624
Dees_Troy5bf43922012-09-07 16:07:55 -04001625 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1626 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001627 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001628 } else {
1629 TWPartition* dat = Find_Partition_By_Path("/data");
1630 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001631 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001632 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1633 dat->Is_Decrypted = true;
1634 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001635 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001636 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 +00001637 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001638
1639#ifdef CRYPTO_SD_FS_TYPE
1640 char crypto_blkdev_sd[255];
1641 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1642 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001643 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001644 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001645 emmc->Is_Decrypted = true;
1646 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1647 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001648 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001649 }
a39552696ff55ce2013-01-08 16:14:56 +00001650#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001651#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001652#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001653 char is_external_decrypted[255];
1654 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1655 if (strcmp(is_external_decrypted, "1") == 0) {
1656 sdcard->Is_Decrypted = true;
1657 sdcard->EcryptFS_Password = Password;
1658 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001659 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1660 MetaEcfsFile += "/.MetaEcfsFile";
1661 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1662 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1663 sdcard->UnMount(false);
1664 sdcard->Mount(false);
1665 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001666 } else {
Dees_Troy066eb302013-08-23 17:20:32 +00001667 LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001668 sdcard->Is_Decrypted = false;
1669 sdcard->Decrypted_Block_Device = "";
1670 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001671#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001672#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001673
Dees_Troy5bf43922012-09-07 16:07:55 -04001674 // Sleep for a bit so that the device will be ready
1675 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001676 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001677 dat->Storage_Path = "/data/media/0";
1678 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001679 DataManager::SetValue("tw_storage_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001680 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001681 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001682 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001683 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001684 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001685 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001686 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001687 }
1688 return 0;
1689#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001690 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001691 return -1;
1692#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001693 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001694}
1695
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001696int TWPartitionManager::Fix_Permissions(void) {
1697 int result = 0;
1698 if (!Mount_By_Path("/data", true))
1699 return false;
1700
1701 if (!Mount_By_Path("/system", true))
1702 return false;
1703
1704 Mount_By_Path("/sd-ext", false);
1705
1706 fixPermissions perms;
1707 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001708 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001709 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001710 return result;
1711}
1712
Ethan Yonker47360be2014-04-01 10:34:34 -05001713TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1714 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1715
1716 if (!Path.empty()) {
1717 string Search_Path = TWFunc::Get_Root_Path(Path);
1718 for (; iter != Partitions.end(); iter++) {
1719 if ((*iter)->Mount_Point == Search_Path) {
1720 iter++;
1721 break;
1722 }
1723 }
1724 }
1725
1726 for (; iter != Partitions.end(); iter++) {
1727 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) {
1728 return (*iter);
1729 }
1730 }
1731
1732 return NULL;
1733}
1734
Dees_Troyd21618c2012-10-14 18:48:49 -04001735int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001736 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1737
1738 if (Part == NULL) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001739 LOGERR("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
Dees_Troyd21618c2012-10-14 18:48:49 -04001740 return false;
1741 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001742 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1743 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001744 return false;
1745
Dees_Troy2673cec2013-04-02 20:22:16 +00001746 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1747 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001748 return false;
1749 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001750 return true;
1751}
1752
Dees_Troy8170a922012-09-18 15:40:25 -04001753int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001754 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001755 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001756 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001757
Dees_Troy8170a922012-09-18 15:40:25 -04001758 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001759 string Lun_File_str = CUSTOM_LUN_FILE;
1760 size_t found = Lun_File_str.find("%");
1761 if (found != string::npos) {
1762 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1763 if (TWFunc::Path_Exists(lun_file))
1764 has_multiple_lun = true;
1765 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001766 mtp_was_enabled = TWFunc::Toggle_MTP(false);
Ethan Yonker47360be2014-04-01 10:34:34 -05001767 if (!has_multiple_lun) {
1768 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1769 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1770 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
1771 TWPartition* Mount = Find_Next_Storage("", "/data");
1772 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001773 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1774 goto error_handle;
1775 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001776 } else {
1777 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001778 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001779 }
1780 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001781 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001782 }
Dees_Troy8170a922012-09-18 15:40:25 -04001783 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001784 LOGINFO("Device has multiple lun files\n");
1785 TWPartition* Mount1;
1786 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001787 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker47360be2014-04-01 10:34:34 -05001788 Mount1 = Find_Next_Storage("", "/data");
1789 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001790 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1791 goto error_handle;
1792 }
Matt Moweree717062014-04-26 01:46:46 -05001793 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker47360be2014-04-01 10:34:34 -05001794 Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data");
1795 if (Mount2) {
Matt Moweree717062014-04-26 01:46:46 -05001796 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001797 }
1798 } else {
1799 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001800 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001801 }
Dees_Troy8170a922012-09-18 15:40:25 -04001802 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001803 property_set("sys.storage.ums_enabled", "1");
Dees_Troy8170a922012-09-18 15:40:25 -04001804 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001805error_handle:
1806 if (mtp_was_enabled)
1807 if (!Enable_MTP())
1808 Disable_MTP();
1809 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001810}
1811
1812int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001813 int index, ret;
1814 char lun_file[255], ch[2] = {0, 0};
1815 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001816
1817 for (index=0; index<2; index++) {
1818 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001819 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001820 if (ret < 0) {
1821 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001822 }
Dees_Troy8170a922012-09-18 15:40:25 -04001823 }
Dees_Troye58d5262012-09-21 12:27:57 -04001824 Mount_All_Storage();
1825 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001826 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001827 property_set("sys.storage.ums_enabled", "0");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001828 if (mtp_was_enabled)
1829 if (!Enable_MTP())
1830 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001831 if (ret < 0 && index == 0) {
1832 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1833 return false;
1834 } else {
1835 return true;
1836 }
Dees_Troy8170a922012-09-18 15:40:25 -04001837 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001838}
1839
1840void TWPartitionManager::Mount_All_Storage(void) {
1841 std::vector<TWPartition*>::iterator iter;
1842
1843 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1844 if ((*iter)->Is_Storage)
1845 (*iter)->Mount(false);
1846 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001847}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001848
Dees_Troyd0384ef2012-10-12 12:15:42 -04001849void TWPartitionManager::UnMount_Main_Partitions(void) {
1850 // Unmounts system and data if data is not data/media
1851 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001852 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001853
1854 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1855
1856 UnMount_By_Path("/system", true);
Ethan Yonker6277c792014-09-15 14:54:30 -05001857 if (!datamedia)
1858 UnMount_By_Path("/data", true);
1859
Dees_Troyd0384ef2012-10-12 12:15:42 -04001860 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1861 Boot_Partition->UnMount(true);
1862}
1863
Dees_Troy9350b8d2012-09-27 12:38:38 -04001864int TWPartitionManager::Partition_SDCard(void) {
1865 char mkdir_path[255], temp[255], line[512];
1866 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1867 int ext, swap, total_size = 0, fat_size;
1868 FILE* fp;
1869
Dees_Troy2673cec2013-04-02 20:22:16 +00001870 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001871#ifdef TW_EXTERNAL_STORAGE_PATH
1872 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1873#else
1874 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1875#endif
Ethan Yonker1eff6cd2014-09-15 13:30:42 -05001876 if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001877 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001878 return false;
1879 }
1880 if (!SDCard->UnMount(true))
1881 return false;
1882 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1883 if (SDext != NULL) {
1884 if (!SDext->UnMount(true))
1885 return false;
1886 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001887
1888 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001889 Device = SDCard->Actual_Block_Device;
1890 // Just use the root block device
1891 Device.resize(strlen("/dev/block/mmcblkX"));
1892
1893 // Find the size of the block device:
1894 fp = fopen("/proc/partitions", "rt");
1895 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001896 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001897 return false;
1898 }
1899
1900 while (fgets(line, sizeof(line), fp) != NULL)
1901 {
1902 unsigned long major, minor, blocks;
1903 char device[512];
1904 char tmpString[64];
1905
1906 if (strlen(line) < 7 || line[0] == 'm') continue;
1907 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1908
1909 tmpdevice = "/dev/block/";
1910 tmpdevice += device;
1911 if (tmpdevice == Device) {
1912 // Adjust block size to byte size
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001913 total_size = (int)(blocks * 1024ULL / 1000000LLU);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001914 break;
1915 }
1916 }
1917 fclose(fp);
1918
1919 DataManager::GetValue("tw_sdext_size", ext);
1920 DataManager::GetValue("tw_swap_size", swap);
1921 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1922 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001923 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 -04001924 memset(temp, 0, sizeof(temp));
1925 sprintf(temp, "%i", fat_size);
1926 fat_str = temp;
1927 memset(temp, 0, sizeof(temp));
1928 sprintf(temp, "%i", fat_size + ext);
1929 ext_str = temp;
1930 memset(temp, 0, sizeof(temp));
1931 sprintf(temp, "%i", fat_size + ext + swap);
1932 swap_str = temp;
1933 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001934 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001935 return false;
1936 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001937 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001938 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001939 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001940 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001941 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001942 Update_System_Details();
1943 return false;
1944 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001945 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001946 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001947 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001948 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001949 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001950 return false;
1951 }
1952 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001953 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001954 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001955 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001956 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001957 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001958 Update_System_Details();
1959 return false;
1960 }
1961 }
1962 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001963 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001964 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001965 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001966 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001967 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001968 Update_System_Details();
1969 return false;
1970 }
1971 }
1972 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1973#ifdef TW_EXTERNAL_STORAGE_PATH
1974 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1975 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1976 memset(mkdir_path, 0, sizeof(mkdir_path));
1977 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1978#else
1979 Mount_By_Path("/sdcard", 1);
1980 strcpy(mkdir_path, "/sdcard/TWRP");
1981#endif
1982 mkdir(mkdir_path, 0777);
1983 DataManager::Flush();
1984#ifdef TW_EXTERNAL_STORAGE_PATH
1985 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1986 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1987 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1988#else
1989 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1990 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1991 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1992#endif
1993 if (ext > 0) {
1994 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001995 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001996 return false;
1997 }
1998 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001999 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
2000 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002001 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04002002 }
2003
2004 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00002005 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04002006 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04002007}
Dees_Troya13d74f2013-03-24 08:54:55 -05002008
2009void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
2010 std::vector<TWPartition*>::iterator iter;
2011 if (ListType == "mount") {
2012 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2013 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
2014 struct PartitionList part;
2015 part.Display_Name = (*iter)->Display_Name;
2016 part.Mount_Point = (*iter)->Mount_Point;
2017 part.selected = (*iter)->Is_Mounted();
2018 Partition_List->push_back(part);
2019 }
2020 }
2021 } else if (ListType == "storage") {
2022 char free_space[255];
2023 string Current_Storage = DataManager::GetCurrentStoragePath();
2024 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2025 if ((*iter)->Is_Storage) {
2026 struct PartitionList part;
2027 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
2028 part.Display_Name = (*iter)->Storage_Name + " (";
2029 part.Display_Name += free_space;
2030 part.Display_Name += "MB)";
2031 part.Mount_Point = (*iter)->Storage_Path;
2032 if ((*iter)->Storage_Path == Current_Storage)
2033 part.selected = 1;
2034 else
2035 part.selected = 0;
2036 Partition_List->push_back(part);
2037 }
2038 }
2039 } else if (ListType == "backup") {
2040 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002041 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05002042 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002043 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05002044 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002045 Backup_Size = (*iter)->Backup_Size;
2046 if ((*iter)->Has_SubPartition) {
2047 std::vector<TWPartition*>::iterator subpart;
2048
2049 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
2050 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
2051 Backup_Size += (*subpart)->Backup_Size;
2052 }
2053 }
2054 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05002055 part.Display_Name = (*iter)->Backup_Display_Name + " (";
2056 part.Display_Name += backup_size;
2057 part.Display_Name += "MB)";
2058 part.Mount_Point = (*iter)->Backup_Path;
2059 part.selected = 0;
2060 Partition_List->push_back(part);
2061 }
2062 }
2063 } else if (ListType == "restore") {
2064 string Restore_List, restore_path;
2065 TWPartition* restore_part = NULL;
2066
2067 DataManager::GetValue("tw_restore_list", Restore_List);
2068 if (!Restore_List.empty()) {
2069 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
2070 while (end_pos != string::npos && start_pos < Restore_List.size()) {
2071 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05002072 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00002073 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05002074 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05002075 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05002076 } else {
2077 struct PartitionList part;
2078 part.Display_Name = restore_part->Backup_Display_Name;
2079 part.Mount_Point = restore_part->Backup_Path;
2080 part.selected = 1;
2081 Partition_List->push_back(part);
2082 }
2083 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00002084 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05002085 }
2086 start_pos = end_pos + 1;
2087 end_pos = Restore_List.find(";", start_pos);
2088 }
2089 }
2090 } else if (ListType == "wipe") {
2091 struct PartitionList dalvik;
2092 dalvik.Display_Name = "Dalvik Cache";
2093 dalvik.Mount_Point = "DALVIK";
2094 dalvik.selected = 0;
2095 Partition_List->push_back(dalvik);
2096 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2097 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
2098 struct PartitionList part;
2099 part.Display_Name = (*iter)->Display_Name;
2100 part.Mount_Point = (*iter)->Mount_Point;
2101 part.selected = 0;
2102 Partition_List->push_back(part);
2103 }
2104 if ((*iter)->Has_Android_Secure) {
2105 struct PartitionList part;
2106 part.Display_Name = (*iter)->Backup_Display_Name;
2107 part.Mount_Point = (*iter)->Backup_Path;
2108 part.selected = 0;
2109 Partition_List->push_back(part);
2110 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00002111 if ((*iter)->Has_Data_Media) {
2112 struct PartitionList datamedia;
2113 datamedia.Display_Name = (*iter)->Storage_Name;
2114 datamedia.Mount_Point = "INTERNAL";
2115 datamedia.selected = 0;
2116 Partition_List->push_back(datamedia);
2117 }
Dees_Troya13d74f2013-03-24 08:54:55 -05002118 }
2119 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00002120 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05002121 }
2122}
2123
2124int TWPartitionManager::Fstab_Processed(void) {
2125 return Partitions.size();
2126}
Dees_Troyd93bda52013-07-03 19:55:19 +00002127
2128void TWPartitionManager::Output_Storage_Fstab(void) {
2129 std::vector<TWPartition*>::iterator iter;
2130 char storage_partition[255];
2131 string Temp;
2132 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
2133
2134 if (fp == NULL) {
2135 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
2136 return;
2137 }
2138
2139 // Iterate through all partitions
2140 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2141 if ((*iter)->Is_Storage) {
2142 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
2143 strcpy(storage_partition, Temp.c_str());
2144 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
2145 }
2146 }
2147 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002148}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02002149
2150TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
2151{
2152 TWPartition *res = NULL;
2153 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
2154 if(!(*iter)->Is_Storage)
2155 continue;
2156
2157 if((*iter)->Is_Settings_Storage)
2158 return *iter;
2159
2160 if(!res)
2161 res = *iter;
2162 }
2163 return res;
2164}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002165
2166bool TWPartitionManager::Enable_MTP(void) {
2167#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002168 if (mtppid) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002169 LOGERR("MTP already enabled\n");
2170 return true;
2171 }
2172 //Launch MTP Responder
2173 LOGINFO("Starting MTP\n");
2174 char vendor[PROPERTY_VALUE_MAX];
2175 char product[PROPERTY_VALUE_MAX];
2176 int count = 0;
2177 property_set("sys.usb.config", "none");
2178 property_get("usb.vendor", vendor, "18D1");
2179 property_get("usb.product.mtpadb", product, "4EE2");
2180 string vendorstr = vendor;
2181 string productstr = product;
2182 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2183 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
2184 property_set("sys.usb.config", "mtp,adb");
2185 std::vector<TWPartition*>::iterator iter;
Ethan Yonker6d154c42014-09-03 14:19:43 -05002186 /* To enable MTP debug, use the twrp command line feature to
2187 * twrp set tw_mtp_debug 1
2188 */
2189 twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug"));
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002190 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2191 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false)) {
2192 printf("twrp mtpid: %d\n", (*iter)->mtpid);
2193 mtp->addStorage((*iter)->Storage_Name, (*iter)->Storage_Path, (*iter)->mtpid);
2194 count++;
2195 }
2196 }
2197 if (count) {
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002198 mtppid = mtp->forkserver();
2199 if (mtppid) {
2200 DataManager::SetValue("tw_mtp_enabled", 1);
2201 return true;
2202 } else {
2203 LOGERR("Failed to enable MTP\n");
2204 return false;
2205 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002206 }
2207 LOGERR("No valid storage partitions found for MTP.\n");
2208#else
2209 LOGERR("MTP support not included\n");
2210#endif
2211 DataManager::SetValue("tw_mtp_enabled", 0);
2212 return false;
2213}
2214
2215bool TWPartitionManager::Disable_MTP(void) {
2216#ifdef TW_HAS_MTP
2217 char vendor[PROPERTY_VALUE_MAX];
2218 char product[PROPERTY_VALUE_MAX];
2219 property_set("sys.usb.config", "none");
2220 property_get("usb.vendor", vendor, "18D1");
2221 property_get("usb.product.adb", product, "D002");
2222 string vendorstr = vendor;
2223 string productstr = product;
2224 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2225 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002226 if (mtppid) {
Ethan Yonker8613dc02014-09-11 09:28:20 -05002227 LOGINFO("Disabling MTP\n");
2228 int status;
2229 kill(mtppid, SIGKILL);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002230 mtppid = 0;
Ethan Yonker8613dc02014-09-11 09:28:20 -05002231 // We don't care about the exit value, but this prevents a zombie process
2232 waitpid(mtppid, &status, 0);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002233 }
2234 property_set("sys.usb.config", "adb");
2235 DataManager::SetValue("tw_mtp_enabled", 0);
2236 return true;
2237#else
2238 LOGERR("MTP support not included\n");
2239 DataManager::SetValue("tw_mtp_enabled", 0);
2240 return false;
2241#endif
2242}