blob: 091ae32881380d08d9c9b07e1de00d25bad50882 [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>
Dees_Troy51a0e822012-09-05 15:24:24 -040032#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000033#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040034#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040035#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040036#include "twrp-functions.hpp"
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040037#include "fixPermissions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050038#include "twrpDigest.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050039#include "twrpDU.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040
Dees Troy6f6441d2014-01-23 02:07:03 +000041extern "C" {
42 #include "cutils/properties.h"
43}
44
Dees_Troy5bf43922012-09-07 16:07:55 -040045#ifdef TW_INCLUDE_CRYPTO
46 #ifdef TW_INCLUDE_JB_CRYPTO
47 #include "crypto/jb/cryptfs.h"
48 #else
49 #include "crypto/ics/cryptfs.h"
50 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -040051#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040052
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050053TWPartitionManager::TWPartitionManager(void) {
54}
55
Dees_Troy51a0e822012-09-05 15:24:24 -040056int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040057 FILE *fstabFile;
58 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -050059 bool Found_Settings_Storage = false;
Dees_Troy5bf43922012-09-07 16:07:55 -040060
61 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
62 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000063 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040064 return false;
65 }
66
67 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
68 if (fstab_line[0] != '/')
69 continue;
70
Dees_Troy2a923582012-09-20 12:13:34 -040071 if (fstab_line[strlen(fstab_line) - 1] != '\n')
72 fstab_line[strlen(fstab_line)] = '\n';
73
Dees_Troy5bf43922012-09-07 16:07:55 -040074 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040075 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040076 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040077
Dees_Troy5bf43922012-09-07 16:07:55 -040078 if (partition->Process_Fstab_Line(line, Display_Error)) {
Dees_Troya13d74f2013-03-24 08:54:55 -050079 if (!Found_Settings_Storage && partition->Is_Settings_Storage) {
80 Found_Settings_Storage = true;
81 Partitions.push_back(partition);
82 DataManager::SetValue("tw_settings_path", partition->Storage_Path);
83 DataManager::SetValue("tw_storage_path", partition->Storage_Path);
Dees_Troy2673cec2013-04-02 20:22:16 +000084 LOGINFO("Settings storage is '%s'\n", partition->Storage_Path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -050085 } else {
86 partition->Is_Settings_Storage = false;
87 Partitions.push_back(partition);
88 }
Dees_Troy5bf43922012-09-07 16:07:55 -040089 } else {
90 delete partition;
91 }
92 }
93 fclose(fstabFile);
Dees_Troya13d74f2013-03-24 08:54:55 -050094 if (!Found_Settings_Storage) {
95 std::vector<TWPartition*>::iterator iter;
96 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
97 if ((*iter)->Is_Storage) {
98 (*iter)->Is_Settings_Storage = true;
99 Found_Settings_Storage = true;
100 DataManager::SetValue("tw_settings_path", (*iter)->Storage_Path);
101 DataManager::SetValue("tw_storage_path", (*iter)->Storage_Path);
Dees_Troy2673cec2013-04-02 20:22:16 +0000102 LOGINFO("Settings storage is '%s'\n", (*iter)->Storage_Path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500103 break;
104 }
105 }
106 if (!Found_Settings_Storage)
Dees_Troy2673cec2013-04-02 20:22:16 +0000107 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500108 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400109 if (!Write_Fstab()) {
110 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000111 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400112 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000113 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400114 }
Dees_Troy51127312012-09-08 13:08:49 -0400115 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400116 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400117 return true;
118}
119
120int TWPartitionManager::Write_Fstab(void) {
121 FILE *fp;
122 std::vector<TWPartition*>::iterator iter;
123 string Line;
124
125 fp = fopen("/etc/fstab", "w");
126 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000127 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400128 return false;
129 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400130 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400131 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400132 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400133 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000134 }
135 // Handle subpartition tracking
136 if ((*iter)->Is_SubPartition) {
137 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
138 if (ParentPartition)
139 ParentPartition->Has_SubPartition = true;
140 else
141 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 -0400142 }
143 }
144 fclose(fp);
145 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400146}
147
Dees_Troy8170a922012-09-18 15:40:25 -0400148void TWPartitionManager::Output_Partition_Logging(void) {
149 std::vector<TWPartition*>::iterator iter;
150
151 printf("\n\nPartition Logs:\n");
152 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
153 Output_Partition((*iter));
154}
155
156void TWPartitionManager::Output_Partition(TWPartition* Part) {
157 unsigned long long mb = 1048576;
158
Gary Peck004d48b2012-11-21 16:28:18 -0800159 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 -0400160 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800161 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 -0400162 }
Gary Peck004d48b2012-11-21 16:28:18 -0800163 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500164 if (Part->Can_Be_Mounted)
165 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800166 if (Part->Can_Be_Wiped)
167 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700168 if (Part->Use_Rm_Rf)
169 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500170 if (Part->Can_Be_Backed_Up)
171 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800172 if (Part->Wipe_During_Factory_Reset)
173 printf("Wipe_During_Factory_Reset ");
174 if (Part->Wipe_Available_in_GUI)
175 printf("Wipe_Available_in_GUI ");
176 if (Part->Is_SubPartition)
177 printf("Is_SubPartition ");
178 if (Part->Has_SubPartition)
179 printf("Has_SubPartition ");
180 if (Part->Removable)
181 printf("Removable ");
182 if (Part->Is_Present)
183 printf("IsPresent ");
184 if (Part->Can_Be_Encrypted)
185 printf("Can_Be_Encrypted ");
186 if (Part->Is_Encrypted)
187 printf("Is_Encrypted ");
188 if (Part->Is_Decrypted)
189 printf("Is_Decrypted ");
190 if (Part->Has_Data_Media)
191 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000192 if (Part->Can_Encrypt_Backup)
193 printf("Can_Encrypt_Backup ");
194 if (Part->Use_Userdata_Encryption)
195 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800196 if (Part->Has_Android_Secure)
197 printf("Has_Android_Secure ");
198 if (Part->Is_Storage)
199 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500200 if (Part->Is_Settings_Storage)
201 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000202 if (Part->Ignore_Blkid)
203 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000204 if (Part->Retain_Layout_Version)
205 printf("Retain_Layout_Version ");
Gary Peck004d48b2012-11-21 16:28:18 -0800206 printf("\n");
207 if (!Part->SubPartition_Of.empty())
208 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
209 if (!Part->Symlink_Path.empty())
210 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
211 if (!Part->Symlink_Mount_Point.empty())
212 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
213 if (!Part->Primary_Block_Device.empty())
214 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
215 if (!Part->Alternate_Block_Device.empty())
216 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
217 if (!Part->Decrypted_Block_Device.empty())
218 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
219 if (Part->Length != 0)
220 printf(" Length: %i\n", Part->Length);
221 if (!Part->Display_Name.empty())
222 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500223 if (!Part->Storage_Name.empty())
224 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800225 if (!Part->Backup_Path.empty())
226 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
227 if (!Part->Backup_Name.empty())
228 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500229 if (!Part->Backup_Display_Name.empty())
230 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800231 if (!Part->Backup_FileName.empty())
232 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
233 if (!Part->Storage_Path.empty())
234 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
235 if (!Part->Current_File_System.empty())
236 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
237 if (!Part->Fstab_File_System.empty())
238 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
239 if (Part->Format_Block_Size != 0)
240 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400241 if (!Part->MTD_Name.empty())
242 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400243 string back_meth = Part->Backup_Method_By_Name();
244 printf(" Backup_Method: %s\n\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800245 if (Part->Mount_Flags || !Part->Mount_Options.empty())
246 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400247}
248
Dees_Troy51a0e822012-09-05 15:24:24 -0400249int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400250 std::vector<TWPartition*>::iterator iter;
251 int ret = false;
252 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400253 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400254
Dees_Troyd93bda52013-07-03 19:55:19 +0000255 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400256 return true;
257
Dees_Troy5bf43922012-09-07 16:07:55 -0400258 // Iterate through all partitions
259 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400260 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 ret = (*iter)->Mount(Display_Error);
262 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400263 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400264 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400265 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400266 }
267 if (found) {
268 return ret;
269 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000270 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400271 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000272 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400273 }
274 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400275}
276
277int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400278 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400279
Dees_Troy51127312012-09-08 13:08:49 -0400280 if (Part) {
281 if (Part->Has_SubPartition) {
282 std::vector<TWPartition*>::iterator subpart;
283
284 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
285 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
286 (*subpart)->Mount(Display_Error);
287 }
288 return Part->Mount(Display_Error);
289 } else
290 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400291 }
292 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000293 LOGERR("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400294 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000295 LOGINFO("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400296 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400297}
298
299int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400300 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400301
Dees_Troy51127312012-09-08 13:08:49 -0400302 if (Part) {
303 if (Part->Has_SubPartition) {
304 std::vector<TWPartition*>::iterator subpart;
305
306 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
307 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
308 (*subpart)->Mount(Display_Error);
309 }
310 return Part->Mount(Display_Error);
311 } else
312 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400313 }
314 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000315 LOGERR("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400316 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000317 LOGINFO("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400318 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400319}
320
321int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400322 std::vector<TWPartition*>::iterator iter;
323 int ret = false;
324 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400325 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400326
327 // Iterate through all partitions
328 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400329 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400330 ret = (*iter)->UnMount(Display_Error);
331 found = true;
332 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
333 (*iter)->UnMount(Display_Error);
334 }
335 }
336 if (found) {
337 return ret;
338 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000339 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400340 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000341 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400342 }
343 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400344}
345
346int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400347 TWPartition* Part = Find_Partition_By_Block(Block);
348
349 if (Part) {
350 if (Part->Has_SubPartition) {
351 std::vector<TWPartition*>::iterator subpart;
352
353 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
354 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
355 (*subpart)->UnMount(Display_Error);
356 }
357 return Part->UnMount(Display_Error);
358 } else
359 return Part->UnMount(Display_Error);
360 }
361 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000362 LOGERR("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400363 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000364 LOGINFO("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400365 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400366}
367
368int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400369 TWPartition* Part = Find_Partition_By_Name(Name);
370
371 if (Part) {
372 if (Part->Has_SubPartition) {
373 std::vector<TWPartition*>::iterator subpart;
374
375 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
376 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
377 (*subpart)->UnMount(Display_Error);
378 }
379 return Part->UnMount(Display_Error);
380 } else
381 return Part->UnMount(Display_Error);
382 }
383 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000384 LOGERR("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400385 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000386 LOGINFO("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400387 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400388}
389
390int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400391 TWPartition* Part = Find_Partition_By_Path(Path);
392
393 if (Part)
394 return Part->Is_Mounted();
395 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000396 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400397 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400398}
399
400int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400401 TWPartition* Part = Find_Partition_By_Block(Block);
402
403 if (Part)
404 return Part->Is_Mounted();
405 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000406 LOGINFO("Is_Mounted: 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::Is_Mounted_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400411 TWPartition* Part = Find_Partition_By_Name(Name);
412
413 if (Part)
414 return Part->Is_Mounted();
415 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000416 LOGINFO("Is_Mounted: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400417 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400418}
419
Dees_Troy5bf43922012-09-07 16:07:55 -0400420int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400421 string current_storage_path = DataManager::GetCurrentStoragePath();
422
423 if (Mount_By_Path(current_storage_path, Display_Error)) {
424 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
425 if (FreeStorage)
426 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
427 return true;
428 }
429 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400430}
431
Dees_Troy5bf43922012-09-07 16:07:55 -0400432int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
433 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
434}
435
436TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
437 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400438 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400439
440 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400441 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400442 return (*iter);
443 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400444 return NULL;
445}
446
Dees_Troy5bf43922012-09-07 16:07:55 -0400447TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400448 std::vector<TWPartition*>::iterator iter;
449
450 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400451 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 -0400452 return (*iter);
453 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400454 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400455}
456
457TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400458 std::vector<TWPartition*>::iterator iter;
459
460 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
461 if ((*iter)->Display_Name == Name)
462 return (*iter);
463 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400464 return NULL;
465}
Dees_Troy51a0e822012-09-05 15:24:24 -0400466
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400467int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
468 // Check the backup name to ensure that it is the correct size and contains only valid characters
469 // and that a backup with that name doesn't already exist
470 char backup_name[MAX_BACKUP_NAME_LEN];
471 char backup_loc[255], tw_image_dir[255];
472 int copy_size;
473 int index, cur_char;
474 string Backup_Name, Backup_Loc;
475
476 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
477 copy_size = Backup_Name.size();
478 // Check size
479 if (copy_size > MAX_BACKUP_NAME_LEN) {
480 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000481 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400482 return -2;
483 }
484
485 // Check each character
486 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500487 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400488 return 0; // A "0" (zero) means to use the current timestamp for the backup name
489 for (index=0; index<copy_size; index++) {
490 cur_char = (int)backup_name[index];
491 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) {
492 // These are valid characters
493 // Numbers
494 // Upper case letters
495 // Lower case letters
496 // Space
497 // and -_.{}[]
498 } else {
499 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000500 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400501 return -3;
502 }
503 }
504
505 // Check to make sure that a backup with this name doesn't already exist
506 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
507 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500508 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400509 if (TWFunc::Path_Exists(tw_image_dir)) {
510 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000511 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400512 return -4;
513 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400514 // No problems found, return 0
515 return 0;
516}
517
Dees_Troy43d8b002012-09-17 16:00:01 -0400518bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
519{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500520 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400521 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500522 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500523 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400524
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500525 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400526 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400527
Dees_Troyb46a6842012-09-25 11:06:46 -0400528 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000529 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400530
531 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500532 md5sum.setfn(Backup_Folder + Backup_Filename);
533 if (md5sum.computeMD5() == 0)
534 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000535 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500536 else
537 return -1;
538 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000539 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400540 } else {
541 char filename[512];
542 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500543 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400544 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500545 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000546 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400547 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000548 if (TWFunc::Path_Exists(filename)) {
549 if (md5sum.computeMD5() == 0) {
550 if (md5sum.write_md5digest() != 0)
551 {
552 gui_print(" * MD5 Error.\n");
553 return false;
554 }
555 } else {
556 gui_print(" * Error computing MD5.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500557 return false;
558 }
559 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400560 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400561 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500562 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400563 }
564 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000565 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400566 return false;
567 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000568 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400569 }
570 return true;
571}
572
Dees_Troy093b7642012-09-21 15:59:38 -0400573bool 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 -0400574 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500575 int img_bps;
576 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400577 unsigned long total_time, remain_time, section_time;
578 int use_compression, backup_time;
579 float pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400580
581 if (Part == NULL)
582 return true;
583
Dees_Troy093b7642012-09-21 15:59:38 -0400584 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
585
586 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
587 if (use_compression)
588 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
589 else
590 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
591
592 // We know the speed for both, how far into the whole backup are we, based on time
593 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
594 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
595
596 pos = (total_time - remain_time) / (float) total_time;
Dees_Troy2673cec2013-04-02 20:22:16 +0000597 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400598
Dees_Troy2673cec2013-04-02 20:22:16 +0000599 LOGINFO("Estimated Total time: %lu Estimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400600
601 // And get the time
602 if (Part->Backup_Method == 1)
603 section_time = Part->Backup_Size / file_bps;
604 else
605 section_time = Part->Backup_Size / img_bps;
606
607 // Set the position
608 pos = section_time / (float) total_time;
Dees_Troy2673cec2013-04-02 20:22:16 +0000609 DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400610
Dees_Troy43d8b002012-09-17 16:00:01 -0400611 time(&start);
612
613 if (Part->Backup(Backup_Folder)) {
Dees_Troy8170a922012-09-18 15:40:25 -0400614 if (Part->Has_SubPartition) {
615 std::vector<TWPartition*>::iterator subpart;
616
617 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500618 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Dees_Troy8170a922012-09-18 15:40:25 -0400619 if (!(*subpart)->Backup(Backup_Folder))
620 return false;
Dees_Troy2727b992013-08-14 20:09:30 +0000621 sync();
622 sync();
Dees_Troy8170a922012-09-18 15:40:25 -0400623 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
624 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400625 if (Part->Backup_Method == 1) {
626 *file_bytes_remaining -= (*subpart)->Backup_Size;
627 } else {
628 *img_bytes_remaining -= (*subpart)->Backup_Size;
629 }
Dees_Troy8170a922012-09-18 15:40:25 -0400630 }
631 }
632 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400633 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400634 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000635 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400636 if (Part->Backup_Method == 1) {
637 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400638 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400639 } else {
640 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400641 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400642 }
643 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
644 } else {
645 return false;
646 }
647}
648
649int TWPartitionManager::Run_Backup(void) {
650 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500651 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400652 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 -0400653 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500654 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400655 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400656 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400657 struct tm *t;
658 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500659 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400660 seconds = time(0);
661 t = localtime(&seconds);
662
663 time(&total_start);
664
665 Update_System_Details();
666
667 if (!Mount_Current_Storage(true))
668 return false;
669
670 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400671 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400672 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400673 else
674 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400675
Dees_Troy43d8b002012-09-17 16:00:01 -0400676 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
677 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000678 if (Backup_Name == "(Current Date)") {
679 Backup_Name = TWFunc::Get_Current_Date();
680 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
681 TWFunc::Auto_Generate_Backup_Name();
682 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400683 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000684 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400685 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000686 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400687
Dees_Troy2673cec2013-04-02 20:22:16 +0000688 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500689 DataManager::GetValue("tw_backup_list", Backup_List);
690 if (!Backup_List.empty()) {
691 end_pos = Backup_List.find(";", start_pos);
692 while (end_pos != string::npos && start_pos < Backup_List.size()) {
693 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
694 backup_part = Find_Partition_By_Path(backup_path);
695 if (backup_part != NULL) {
696 partition_count++;
697 if (backup_part->Backup_Method == 1)
698 file_bytes += backup_part->Backup_Size;
699 else
700 img_bytes += backup_part->Backup_Size;
701 if (backup_part->Has_SubPartition) {
702 std::vector<TWPartition*>::iterator subpart;
703
704 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000705 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 -0500706 partition_count++;
707 if ((*subpart)->Backup_Method == 1)
708 file_bytes += (*subpart)->Backup_Size;
709 else
710 img_bytes += (*subpart)->Backup_Size;
711 }
712 }
Dees_Troy8170a922012-09-18 15:40:25 -0400713 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500714 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000715 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400716 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500717 start_pos = end_pos + 1;
718 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400719 }
720 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400721
722 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000723 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400724 return false;
725 }
726 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000727 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
728 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400729 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
730 if (storage != NULL) {
731 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000732 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400733 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000734 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400735 return false;
736 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000737 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400738 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000739 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400740 return false;
741 }
742 img_bytes_remaining = img_bytes;
743 file_bytes_remaining = file_bytes;
744
Dees_Troy2673cec2013-04-02 20:22:16 +0000745 gui_print("\n[BACKUP STARTED]\n");
746 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000747 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000748 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000749 return false;
750 }
751
Dees_Troy2673cec2013-04-02 20:22:16 +0000752 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400753
Dees_Troya13d74f2013-03-24 08:54:55 -0500754 start_pos = 0;
755 end_pos = Backup_List.find(";", start_pos);
756 while (end_pos != string::npos && start_pos < Backup_List.size()) {
757 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
758 backup_part = Find_Partition_By_Path(backup_path);
759 if (backup_part != NULL) {
760 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
761 return false;
762 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000763 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500764 }
765 start_pos = end_pos + 1;
766 end_pos = Backup_List.find(";", start_pos);
767 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400768
769 // Average BPS
770 if (img_time == 0)
771 img_time = 1;
772 if (file_time == 0)
773 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400774 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500775 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400776
Dees_Troy2673cec2013-04-02 20:22:16 +0000777 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
778 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400779
780 time(&total_stop);
781 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500782 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Dees_Troy43d8b002012-09-17 16:00:01 -0400783 actual_backup_size /= (1024LLU * 1024LLU);
784
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500785 int prev_img_bps, use_compression;
786 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400787 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
788 img_bps += (prev_img_bps * 4);
789 img_bps /= 5;
790
791 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
792 if (use_compression)
793 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
794 else
795 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
796 file_bps += (prev_file_bps * 4);
797 file_bps /= 5;
798
799 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
800 if (use_compression)
801 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
802 else
803 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
804
Dees_Troy2673cec2013-04-02 20:22:16 +0000805 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400806 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400807 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +0000808 gui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000809 string backup_log = Full_Backup_Path + "recovery.log";
810 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
811 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400812}
813
Dees_Troy093b7642012-09-21 15:59:38 -0400814bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400815 time_t Start, Stop;
816 time(&Start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000817 DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400818 if (!Part->Restore(Restore_Name))
819 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400820 if (Part->Has_SubPartition) {
821 std::vector<TWPartition*>::iterator subpart;
822
823 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
824 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
825 if (!(*subpart)->Restore(Restore_Name))
826 return false;
827 }
828 }
829 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400830 time(&Stop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000831 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 -0400832 return true;
833}
834
Dees_Troy51a0e822012-09-05 15:24:24 -0400835int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400836 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500837 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400838 time_t rStart, rStop;
839 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500840 string Restore_List, restore_path;
841 size_t start_pos = 0, end_pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400842
Dees_Troy2673cec2013-04-02 20:22:16 +0000843 gui_print("\n[RESTORE STARTED]\n\n");
844 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400845
Dees_Troy4a2a1262012-09-18 09:33:47 -0400846 if (!Mount_Current_Storage(true))
847 return false;
848
849 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500850 if (check_md5 > 0) {
851 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
852 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000853 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500854 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000855 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500856 }
857 DataManager::GetValue("tw_restore_selected", Restore_List);
858 if (!Restore_List.empty()) {
859 end_pos = Restore_List.find(";", start_pos);
860 while (end_pos != string::npos && start_pos < Restore_List.size()) {
861 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
862 restore_part = Find_Partition_By_Path(restore_path);
863 if (restore_part != NULL) {
864 partition_count++;
865 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
866 return false;
867 if (restore_part->Has_SubPartition) {
868 std::vector<TWPartition*>::iterator subpart;
869
870 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
871 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
872 if (!(*subpart)->Check_MD5(Restore_Name))
873 return false;
874 }
875 }
876 }
877 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500878 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500879 }
880 start_pos = end_pos + 1;
881 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400882 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400883 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400884
885 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000886 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400887 return false;
888 }
889
Dees_Troy2673cec2013-04-02 20:22:16 +0000890 gui_print("Restoring %i partitions...\n", partition_count);
891 DataManager::SetProgress(0.0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500892 start_pos = 0;
893 if (!Restore_List.empty()) {
894 end_pos = Restore_List.find(";", start_pos);
895 while (end_pos != string::npos && start_pos < Restore_List.size()) {
896 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
897 restore_part = Find_Partition_By_Path(restore_path);
898 if (restore_part != NULL) {
899 partition_count++;
900 if (!Restore_Partition(restore_part, Restore_Name, partition_count))
901 return false;
902 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000903 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500904 }
905 start_pos = end_pos + 1;
906 end_pos = Restore_List.find(";", start_pos);
907 }
908 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400909 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400910 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400911 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400912 time(&rStop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000913 gui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -0400914 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400915}
916
917void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400918 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500919 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000920 bool get_date = true, check_encryption = true;
921
922 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400923
924 DIR* d;
925 d = opendir(Restore_Name.c_str());
926 if (d == NULL)
927 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000928 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400929 return;
930 }
931
932 struct dirent* de;
933 while ((de = readdir(d)) != NULL)
934 {
935 // Strip off three components
936 char str[256];
937 char* label;
938 char* fstype = NULL;
939 char* extn = NULL;
940 char* ptr;
941
942 strcpy(str, de->d_name);
943 if (strlen(str) <= 2)
944 continue;
945
946 if (get_date) {
947 char file_path[255];
948 struct stat st;
949
950 strcpy(file_path, Restore_Name.c_str());
951 strcat(file_path, "/");
952 strcat(file_path, str);
953 stat(file_path, &st);
954 string backup_date = ctime((const time_t*)(&st.st_mtime));
955 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
956 get_date = false;
957 }
958
959 label = str;
960 ptr = label;
961 while (*ptr && *ptr != '.') ptr++;
962 if (*ptr == '.')
963 {
964 *ptr = 0x00;
965 ptr++;
966 fstype = ptr;
967 }
968 while (*ptr && *ptr != '.') ptr++;
969 if (*ptr == '.')
970 {
971 *ptr = 0x00;
972 ptr++;
973 extn = ptr;
974 }
975
Dees_Troy83bd4832013-05-04 12:39:56 +0000976 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -0500977 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +0000978 if (extnlength != 3 && extnlength != 6) continue;
979 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
980 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
981
982 if (check_encryption) {
983 string filename = Restore_Name + "/";
984 filename += de->d_name;
985 if (TWFunc::Get_File_Type(filename) == 2) {
986 LOGINFO("'%s' is encrypted\n", filename.c_str());
987 DataManager::SetValue("tw_restore_encrypted", 1);
988 }
989 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500990 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -0400991
992 TWPartition* Part = Find_Partition_By_Path(label);
993 if (Part == NULL)
994 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000995 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -0400996 continue;
997 }
998
999 Part->Backup_FileName = de->d_name;
1000 if (strlen(extn) > 3) {
1001 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1002 }
1003
Dees_Troya13d74f2013-03-24 08:54:55 -05001004 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001005 }
1006 closedir(d);
1007
Dees_Troya13d74f2013-03-24 08:54:55 -05001008 // Set the final value
1009 DataManager::SetValue("tw_restore_list", Restore_List);
1010 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001011 return;
1012}
1013
1014int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001015 std::vector<TWPartition*>::iterator iter;
1016 int ret = false;
1017 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001018 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001019
1020 // Iterate through all partitions
1021 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001022 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001023 if (Path == "/and-sec")
1024 ret = (*iter)->Wipe_AndSec();
1025 else
1026 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001027 found = true;
1028 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1029 (*iter)->Wipe();
1030 }
1031 }
1032 if (found) {
1033 return ret;
1034 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001035 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001036 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001037}
1038
1039int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001040 TWPartition* Part = Find_Partition_By_Block(Block);
1041
1042 if (Part) {
1043 if (Part->Has_SubPartition) {
1044 std::vector<TWPartition*>::iterator subpart;
1045
1046 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1047 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1048 (*subpart)->Wipe();
1049 }
1050 return Part->Wipe();
1051 } else
1052 return Part->Wipe();
1053 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001054 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001055 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001056}
1057
1058int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001059 TWPartition* Part = Find_Partition_By_Name(Name);
1060
1061 if (Part) {
1062 if (Part->Has_SubPartition) {
1063 std::vector<TWPartition*>::iterator subpart;
1064
1065 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1066 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1067 (*subpart)->Wipe();
1068 }
1069 return Part->Wipe();
1070 } else
1071 return Part->Wipe();
1072 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001073 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001074 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001075}
1076
1077int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001078 std::vector<TWPartition*>::iterator iter;
1079 int ret = true;
1080
1081 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001082 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001083 if (!(*iter)->Wipe())
1084 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001085 } else if ((*iter)->Has_Android_Secure) {
1086 if (!(*iter)->Wipe_AndSec())
1087 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001088 }
1089 }
1090 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001091}
1092
Dees_Troy38bd7602012-09-14 13:33:53 -04001093int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1094 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001095 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001096
1097 if (!Mount_By_Path("/data", true))
1098 return false;
1099
1100 if (!Mount_By_Path("/cache", true))
1101 return false;
1102
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001103 dir.push_back("/data/dalvik-cache");
1104 dir.push_back("/cache/dalvik-cache");
1105 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001106 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001107 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001108 if (stat(dir.at(i).c_str(), &st) == 0) {
1109 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001110 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001111 }
1112 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001113 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001114 if (sdext && sdext->Is_Present && sdext->Mount(false))
1115 {
1116 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1117 {
1118 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
1119 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001120 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001121 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001122 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001123 return true;
1124}
1125
1126int TWPartitionManager::Wipe_Rotate_Data(void) {
1127 if (!Mount_By_Path("/data", true))
1128 return false;
1129
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001130 unlink("/data/misc/akmd*");
1131 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001132 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001133 return true;
1134}
1135
1136int TWPartitionManager::Wipe_Battery_Stats(void) {
1137 struct stat st;
1138
1139 if (!Mount_By_Path("/data", true))
1140 return false;
1141
1142 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001143 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001144 } else {
1145 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001146 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001147 }
1148 return true;
1149}
1150
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001151int TWPartitionManager::Wipe_Android_Secure(void) {
1152 std::vector<TWPartition*>::iterator iter;
1153 int ret = false;
1154 bool found = false;
1155
1156 // Iterate through all partitions
1157 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1158 if ((*iter)->Has_Android_Secure) {
1159 ret = (*iter)->Wipe_AndSec();
1160 found = true;
1161 }
1162 }
1163 if (found) {
1164 return ret;
1165 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001166 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001167 }
1168 return false;
1169}
1170
Dees_Troy38bd7602012-09-14 13:33:53 -04001171int TWPartitionManager::Format_Data(void) {
1172 TWPartition* dat = Find_Partition_By_Path("/data");
1173
1174 if (dat != NULL) {
1175 if (!dat->UnMount(true))
1176 return false;
1177
1178 return dat->Wipe_Encryption();
1179 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001180 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001181 return false;
1182 }
1183 return false;
1184}
1185
1186int TWPartitionManager::Wipe_Media_From_Data(void) {
1187 TWPartition* dat = Find_Partition_By_Path("/data");
1188
1189 if (dat != NULL) {
1190 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001191 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001192 return false;
1193 }
1194 if (!dat->Mount(true))
1195 return false;
1196
Dees_Troy2673cec2013-04-02 20:22:16 +00001197 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001198 TWFunc::removeDir("/data/media", false);
1199 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
1200 return -1;
Dees_Troy38bd7602012-09-14 13:33:53 -04001201 if (dat->Has_Data_Media) {
1202 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001203 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1204 dat->UnMount(false);
1205 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001206 }
1207 return true;
1208 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001209 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001210 return false;
1211 }
1212 return false;
1213}
1214
Dees_Troy51a0e822012-09-05 15:24:24 -04001215void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001216 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001217 return;
1218}
1219
1220void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001221 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001222 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001223
Dees_Troy2673cec2013-04-02 20:22:16 +00001224 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001225 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001226 if ((*iter)->Can_Be_Mounted) {
1227 (*iter)->Update_Size(true);
1228 if ((*iter)->Mount_Point == "/system") {
1229 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1230 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1231 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1232 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1233 } else if ((*iter)->Mount_Point == "/cache") {
1234 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1235 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1236 } else if ((*iter)->Mount_Point == "/sd-ext") {
1237 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1238 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1239 if ((*iter)->Backup_Size == 0) {
1240 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1241 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1242 } else
1243 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001244 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001245 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001246 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001247 if ((*iter)->Backup_Size == 0) {
1248 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1249 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1250 } else
1251 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001252 } else if ((*iter)->Mount_Point == "/boot") {
1253 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1254 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1255 if ((*iter)->Backup_Size == 0) {
1256 DataManager::SetValue("tw_has_boot_partition", 0);
1257 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1258 } else
1259 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001260 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001261#ifdef SP1_NAME
1262 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1263 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1264 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1265 }
1266#endif
1267#ifdef SP2_NAME
1268 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1269 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1270 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1271 }
1272#endif
1273#ifdef SP3_NAME
1274 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1275 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1276 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1277 }
1278#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001279 } else {
1280 // Handle unmountable partitions in case we reset defaults
1281 if ((*iter)->Mount_Point == "/boot") {
1282 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1283 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1284 if ((*iter)->Backup_Size == 0) {
1285 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1286 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1287 } else
1288 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1289 } else if ((*iter)->Mount_Point == "/recovery") {
1290 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1291 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1292 if ((*iter)->Backup_Size == 0) {
1293 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1294 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1295 } else
1296 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001297 } else if ((*iter)->Mount_Point == "/data") {
1298 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001299 }
1300#ifdef SP1_NAME
1301 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1302 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1303 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1304 }
1305#endif
1306#ifdef SP2_NAME
1307 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1308 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1309 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1310 }
1311#endif
1312#ifdef SP3_NAME
1313 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1314 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1315 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1316 }
1317#endif
Dees_Troy51127312012-09-08 13:08:49 -04001318 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001319 }
Dees_Troy51127312012-09-08 13:08:49 -04001320 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1321 string current_storage_path = DataManager::GetCurrentStoragePath();
1322 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001323 if (FreeStorage != NULL) {
1324 // Attempt to mount storage
1325 if (!FreeStorage->Mount(false)) {
1326 // We couldn't mount storage... check to see if we have dual storage
1327 int has_dual_storage;
1328 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1329 if (has_dual_storage == 1) {
1330 // We have dual storage, see if we're using the internal storage that should always be present
1331 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001332 if (!FreeStorage->Is_Encrypted) {
1333 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001334 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001335 }
Dees_Troy8170a922012-09-18 15:40:25 -04001336 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1337 } else {
1338 // We were using external, flip to internal
1339 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1340 current_storage_path = DataManager::GetCurrentStoragePath();
1341 FreeStorage = Find_Partition_By_Path(current_storage_path);
1342 if (FreeStorage != NULL) {
1343 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1344 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001345 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001346 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1347 }
1348 }
1349 } else {
1350 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001351 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001352 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1353 }
1354 } else {
1355 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1356 }
1357 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001358 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001359 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001360 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001361 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001362 return;
1363}
1364
1365int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001366#ifdef TW_INCLUDE_CRYPTO
1367 int ret_val, password_len;
1368 char crypto_blkdev[255], cPassword[255];
1369 size_t result;
1370
1371 property_set("ro.crypto.state", "encrypted");
1372#ifdef TW_INCLUDE_JB_CRYPTO
1373 // No extra flags needed
1374#else
1375 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1376 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1377 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1378 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1379 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1380 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001381
1382#ifdef CRYPTO_SD_FS_TYPE
1383 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1384 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1385 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001386#endif
a39552696ff55ce2013-01-08 16:14:56 +00001387
1388 property_set("rw.km_fips_status", "ready");
1389
1390#endif
1391
1392 // some samsung devices store "footer" on efs partition
1393 TWPartition *efs = Find_Partition_By_Path("/efs");
1394 if(efs && !efs->Is_Mounted())
1395 efs->Mount(false);
1396 else
1397 efs = 0;
1398#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001399#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001400 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001401 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001402 property_set("ro.crypto.external_encrypted", "1");
1403 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1404 } else {
1405 property_set("ro.crypto.external_encrypted", "0");
1406 }
1407#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001408#endif
a39552696ff55ce2013-01-08 16:14:56 +00001409
Dees_Troy5bf43922012-09-07 16:07:55 -04001410 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001411 int pwret = cryptfs_check_passwd(cPassword);
1412
1413 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001414 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001415 return -1;
1416 }
a39552696ff55ce2013-01-08 16:14:56 +00001417
1418 if(efs)
1419 efs->UnMount(false);
1420
Dees_Troy5bf43922012-09-07 16:07:55 -04001421 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1422 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001423 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001424 } else {
1425 TWPartition* dat = Find_Partition_By_Path("/data");
1426 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001427 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001428 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1429 dat->Is_Decrypted = true;
1430 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001431 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001432 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 +00001433 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001434
1435#ifdef CRYPTO_SD_FS_TYPE
1436 char crypto_blkdev_sd[255];
1437 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1438 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001439 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001440 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001441 emmc->Is_Decrypted = true;
1442 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1443 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001444 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001445 }
a39552696ff55ce2013-01-08 16:14:56 +00001446#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001447#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001448#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001449 char is_external_decrypted[255];
1450 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1451 if (strcmp(is_external_decrypted, "1") == 0) {
1452 sdcard->Is_Decrypted = true;
1453 sdcard->EcryptFS_Password = Password;
1454 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001455 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1456 MetaEcfsFile += "/.MetaEcfsFile";
1457 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1458 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1459 sdcard->UnMount(false);
1460 sdcard->Mount(false);
1461 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001462 } else {
Dees_Troy066eb302013-08-23 17:20:32 +00001463 LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001464 sdcard->Is_Decrypted = false;
1465 sdcard->Decrypted_Block_Device = "";
1466 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001467#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001468#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001469
Dees_Troy5bf43922012-09-07 16:07:55 -04001470 // Sleep for a bit so that the device will be ready
1471 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001472#ifdef RECOVERY_SDCARD_ON_DATA
1473 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1474 dat->Storage_Path = "/data/media/0";
1475 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001476 DataManager::SetValue("tw_storage_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001477 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001478 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001479 }
1480#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001481 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001482 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001483 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001484 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001485 }
1486 return 0;
1487#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001488 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001489 return -1;
1490#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001491 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001492}
1493
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001494int TWPartitionManager::Fix_Permissions(void) {
1495 int result = 0;
1496 if (!Mount_By_Path("/data", true))
1497 return false;
1498
1499 if (!Mount_By_Path("/system", true))
1500 return false;
1501
1502 Mount_By_Path("/sd-ext", false);
1503
1504 fixPermissions perms;
1505 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001506 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001507 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001508 return result;
1509}
1510
Dees_Troyd21618c2012-10-14 18:48:49 -04001511int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001512 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1513
1514 if (Part == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001515 LOGERR("Unable to locate volume information for USB storage mode.");
Dees_Troyd21618c2012-10-14 18:48:49 -04001516 return false;
1517 }
1518 if (!Part->UnMount(true))
1519 return false;
1520
Dees_Troy2673cec2013-04-02 20:22:16 +00001521 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1522 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001523 return false;
1524 }
Matt Mowerd9cb9062013-12-14 20:34:45 -06001525 property_set("sys.storage.ums_enabled", "1");
Dees_Troyd21618c2012-10-14 18:48:49 -04001526 return true;
1527}
1528
Dees_Troy8170a922012-09-18 15:40:25 -04001529int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001530 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001531 char lun_file[255];
Dees_Troy8170a922012-09-18 15:40:25 -04001532 string ext_path;
Dees_Troyd21618c2012-10-14 18:48:49 -04001533 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001534
1535 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1536 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1537 if (has_dual == 1 && has_data_media == 0) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001538 string Lun_File_str = CUSTOM_LUN_FILE;
1539 size_t found = Lun_File_str.find("%");
1540 if (found != string::npos) {
1541 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1542 if (TWFunc::Path_Exists(lun_file))
1543 has_multiple_lun = true;
1544 }
1545 if (!has_multiple_lun) {
Dees_Troyf4ca6122012-10-13 22:17:20 -04001546 // Device doesn't have multiple lun files, mount current storage
Dees_Troyf4ca6122012-10-13 22:17:20 -04001547 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001548 return Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001549 } else {
1550 // Device has multiple lun files
Dees_Troyf4ca6122012-10-13 22:17:20 -04001551 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001552 if (!Open_Lun_File(DataManager::GetSettingsStoragePath(), lun_file))
Dees_Troyf4ca6122012-10-13 22:17:20 -04001553 return false;
Dees_Troyf4ca6122012-10-13 22:17:20 -04001554 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001555 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Dees_Troyd21618c2012-10-14 18:48:49 -04001556 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001557 }
Dees_Troy8170a922012-09-18 15:40:25 -04001558 } else {
1559 if (has_data_media == 0)
1560 ext_path = DataManager::GetCurrentStoragePath();
1561 else
1562 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001563 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001564 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001565 }
1566 return true;
1567}
1568
1569int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001570 int index, ret;
1571 char lun_file[255], ch[2] = {0, 0};
1572 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001573
1574 for (index=0; index<2; index++) {
1575 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001576 ret = TWFunc::write_file(lun_file, str);
1577 Mount_All_Storage();
1578 Update_System_Details();
1579 if (ret < 0) {
1580 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001581 }
Dees_Troy8170a922012-09-18 15:40:25 -04001582 }
Dees_Troye58d5262012-09-21 12:27:57 -04001583 Mount_All_Storage();
1584 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001585 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001586 property_set("sys.storage.ums_enabled", "0");
Dees_Troy2673cec2013-04-02 20:22:16 +00001587 if (ret < 0 && index == 0) {
1588 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1589 return false;
1590 } else {
1591 return true;
1592 }
Dees_Troy8170a922012-09-18 15:40:25 -04001593 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001594}
1595
1596void TWPartitionManager::Mount_All_Storage(void) {
1597 std::vector<TWPartition*>::iterator iter;
1598
1599 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1600 if ((*iter)->Is_Storage)
1601 (*iter)->Mount(false);
1602 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001603}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001604
Dees_Troyd0384ef2012-10-12 12:15:42 -04001605void TWPartitionManager::UnMount_Main_Partitions(void) {
1606 // Unmounts system and data if data is not data/media
1607 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001608 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001609
1610 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1611
1612 UnMount_By_Path("/system", true);
1613#ifndef RECOVERY_SDCARD_ON_DATA
1614 UnMount_By_Path("/data", true);
1615#endif
1616 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1617 Boot_Partition->UnMount(true);
1618}
1619
Dees_Troy9350b8d2012-09-27 12:38:38 -04001620int TWPartitionManager::Partition_SDCard(void) {
1621 char mkdir_path[255], temp[255], line[512];
1622 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1623 int ext, swap, total_size = 0, fat_size;
1624 FILE* fp;
1625
Dees_Troy2673cec2013-04-02 20:22:16 +00001626 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001627#ifdef TW_EXTERNAL_STORAGE_PATH
1628 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1629#else
1630 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1631#endif
1632 if (SDCard == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001633 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001634 return false;
1635 }
1636 if (!SDCard->UnMount(true))
1637 return false;
1638 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1639 if (SDext != NULL) {
1640 if (!SDext->UnMount(true))
1641 return false;
1642 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001643
1644 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001645 Device = SDCard->Actual_Block_Device;
1646 // Just use the root block device
1647 Device.resize(strlen("/dev/block/mmcblkX"));
1648
1649 // Find the size of the block device:
1650 fp = fopen("/proc/partitions", "rt");
1651 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001652 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001653 return false;
1654 }
1655
1656 while (fgets(line, sizeof(line), fp) != NULL)
1657 {
1658 unsigned long major, minor, blocks;
1659 char device[512];
1660 char tmpString[64];
1661
1662 if (strlen(line) < 7 || line[0] == 'm') continue;
1663 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1664
1665 tmpdevice = "/dev/block/";
1666 tmpdevice += device;
1667 if (tmpdevice == Device) {
1668 // Adjust block size to byte size
1669 total_size = (int)(blocks * 1024ULL / 1000000LLU);
1670 break;
1671 }
1672 }
1673 fclose(fp);
1674
1675 DataManager::GetValue("tw_sdext_size", ext);
1676 DataManager::GetValue("tw_swap_size", swap);
1677 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1678 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001679 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 -04001680 memset(temp, 0, sizeof(temp));
1681 sprintf(temp, "%i", fat_size);
1682 fat_str = temp;
1683 memset(temp, 0, sizeof(temp));
1684 sprintf(temp, "%i", fat_size + ext);
1685 ext_str = temp;
1686 memset(temp, 0, sizeof(temp));
1687 sprintf(temp, "%i", fat_size + ext + swap);
1688 swap_str = temp;
1689 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001690 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001691 return false;
1692 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001693 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001694 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001695 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001696 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001697 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001698 Update_System_Details();
1699 return false;
1700 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001701 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001702 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001703 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001704 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001705 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001706 return false;
1707 }
1708 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001709 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001710 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001711 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001712 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001713 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001714 Update_System_Details();
1715 return false;
1716 }
1717 }
1718 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001719 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001720 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001721 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001722 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001723 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001724 Update_System_Details();
1725 return false;
1726 }
1727 }
1728 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1729#ifdef TW_EXTERNAL_STORAGE_PATH
1730 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1731 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1732 memset(mkdir_path, 0, sizeof(mkdir_path));
1733 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1734#else
1735 Mount_By_Path("/sdcard", 1);
1736 strcpy(mkdir_path, "/sdcard/TWRP");
1737#endif
1738 mkdir(mkdir_path, 0777);
1739 DataManager::Flush();
1740#ifdef TW_EXTERNAL_STORAGE_PATH
1741 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1742 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1743 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1744#else
1745 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1746 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1747 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1748#endif
1749 if (ext > 0) {
1750 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001751 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001752 return false;
1753 }
1754 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001755 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1756 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001757 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001758 }
1759
1760 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00001761 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001762 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001763}
Dees_Troya13d74f2013-03-24 08:54:55 -05001764
1765void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1766 std::vector<TWPartition*>::iterator iter;
1767 if (ListType == "mount") {
1768 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1769 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1770 struct PartitionList part;
1771 part.Display_Name = (*iter)->Display_Name;
1772 part.Mount_Point = (*iter)->Mount_Point;
1773 part.selected = (*iter)->Is_Mounted();
1774 Partition_List->push_back(part);
1775 }
1776 }
1777 } else if (ListType == "storage") {
1778 char free_space[255];
1779 string Current_Storage = DataManager::GetCurrentStoragePath();
1780 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1781 if ((*iter)->Is_Storage) {
1782 struct PartitionList part;
1783 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1784 part.Display_Name = (*iter)->Storage_Name + " (";
1785 part.Display_Name += free_space;
1786 part.Display_Name += "MB)";
1787 part.Mount_Point = (*iter)->Storage_Path;
1788 if ((*iter)->Storage_Path == Current_Storage)
1789 part.selected = 1;
1790 else
1791 part.selected = 0;
1792 Partition_List->push_back(part);
1793 }
1794 }
1795 } else if (ListType == "backup") {
1796 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001797 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001798 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001799 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001800 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001801 Backup_Size = (*iter)->Backup_Size;
1802 if ((*iter)->Has_SubPartition) {
1803 std::vector<TWPartition*>::iterator subpart;
1804
1805 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1806 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1807 Backup_Size += (*subpart)->Backup_Size;
1808 }
1809 }
1810 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001811 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1812 part.Display_Name += backup_size;
1813 part.Display_Name += "MB)";
1814 part.Mount_Point = (*iter)->Backup_Path;
1815 part.selected = 0;
1816 Partition_List->push_back(part);
1817 }
1818 }
1819 } else if (ListType == "restore") {
1820 string Restore_List, restore_path;
1821 TWPartition* restore_part = NULL;
1822
1823 DataManager::GetValue("tw_restore_list", Restore_List);
1824 if (!Restore_List.empty()) {
1825 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1826 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1827 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05001828 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
1829 if (restore_part->Backup_Name == "recovery" || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001830 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05001831 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05001832 } else {
1833 struct PartitionList part;
1834 part.Display_Name = restore_part->Backup_Display_Name;
1835 part.Mount_Point = restore_part->Backup_Path;
1836 part.selected = 1;
1837 Partition_List->push_back(part);
1838 }
1839 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001840 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001841 }
1842 start_pos = end_pos + 1;
1843 end_pos = Restore_List.find(";", start_pos);
1844 }
1845 }
1846 } else if (ListType == "wipe") {
1847 struct PartitionList dalvik;
1848 dalvik.Display_Name = "Dalvik Cache";
1849 dalvik.Mount_Point = "DALVIK";
1850 dalvik.selected = 0;
1851 Partition_List->push_back(dalvik);
1852 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1853 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1854 struct PartitionList part;
1855 part.Display_Name = (*iter)->Display_Name;
1856 part.Mount_Point = (*iter)->Mount_Point;
1857 part.selected = 0;
1858 Partition_List->push_back(part);
1859 }
1860 if ((*iter)->Has_Android_Secure) {
1861 struct PartitionList part;
1862 part.Display_Name = (*iter)->Backup_Display_Name;
1863 part.Mount_Point = (*iter)->Backup_Path;
1864 part.selected = 0;
1865 Partition_List->push_back(part);
1866 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001867 if ((*iter)->Has_Data_Media) {
1868 struct PartitionList datamedia;
1869 datamedia.Display_Name = (*iter)->Storage_Name;
1870 datamedia.Mount_Point = "INTERNAL";
1871 datamedia.selected = 0;
1872 Partition_List->push_back(datamedia);
1873 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001874 }
1875 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001876 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001877 }
1878}
1879
1880int TWPartitionManager::Fstab_Processed(void) {
1881 return Partitions.size();
1882}
Dees_Troyd93bda52013-07-03 19:55:19 +00001883
1884void TWPartitionManager::Output_Storage_Fstab(void) {
1885 std::vector<TWPartition*>::iterator iter;
1886 char storage_partition[255];
1887 string Temp;
1888 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
1889
1890 if (fp == NULL) {
1891 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
1892 return;
1893 }
1894
1895 // Iterate through all partitions
1896 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1897 if ((*iter)->Is_Storage) {
1898 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
1899 strcpy(storage_partition, Temp.c_str());
1900 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
1901 }
1902 }
1903 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001904}