blob: d4e371551a9cd7ccf0209ad03fc8d61bbfbb53b9 [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 Troy02a64532014-03-19 15:23:32 +000059 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050060 TWPartition* andsec_partition = NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -040061
62 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
63 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000064 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040065 return false;
66 }
67
68 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
69 if (fstab_line[0] != '/')
70 continue;
71
Dees_Troy2a923582012-09-20 12:13:34 -040072 if (fstab_line[strlen(fstab_line) - 1] != '\n')
73 fstab_line[strlen(fstab_line)] = '\n';
74
Dees_Troy5bf43922012-09-07 16:07:55 -040075 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040076 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040077 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040078
Dees_Troy5bf43922012-09-07 16:07:55 -040079 if (partition->Process_Fstab_Line(line, Display_Error)) {
Dees Troy02a64532014-03-19 15:23:32 +000080 if (!settings_partition && partition->Is_Settings_Storage) {
81 settings_partition = partition;
Dees_Troya13d74f2013-03-24 08:54:55 -050082 } else {
83 partition->Is_Settings_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050084 }
Matt Mowerbf4efa32014-04-14 23:25:26 -050085 if (!andsec_partition && partition->Has_Android_Secure) {
86 andsec_partition = partition;
87 } else {
88 partition->Has_Android_Secure = false;
89 }
Ethan Yonkerc05c5982014-03-13 09:19:56 -050090 Partitions.push_back(partition);
Dees_Troy5bf43922012-09-07 16:07:55 -040091 } else {
92 delete partition;
93 }
94 }
95 fclose(fstabFile);
Dees Troy02a64532014-03-19 15:23:32 +000096 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -050097 std::vector<TWPartition*>::iterator iter;
98 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
99 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000100 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500101 break;
102 }
103 }
Dees Troy02a64532014-03-19 15:23:32 +0000104 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000105 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500106 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400107 if (!Write_Fstab()) {
108 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000109 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400110 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000111 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400112 }
Matt Mowerbf4efa32014-04-14 23:25:26 -0500113 if (andsec_partition) {
114 Setup_Android_Secure_Location(andsec_partition);
115 } else {
116 Setup_Android_Secure_Location(settings_partition);
117 }
Matt Mowered426902014-04-01 23:46:37 -0500118 Setup_Settings_Storage_Partition(settings_partition);
Dees_Troy51127312012-09-08 13:08:49 -0400119 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400120 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400121 return true;
122}
123
124int TWPartitionManager::Write_Fstab(void) {
125 FILE *fp;
126 std::vector<TWPartition*>::iterator iter;
127 string Line;
128
129 fp = fopen("/etc/fstab", "w");
130 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000131 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400132 return false;
133 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400134 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400135 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400136 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400137 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000138 }
139 // Handle subpartition tracking
140 if ((*iter)->Is_SubPartition) {
141 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
142 if (ParentPartition)
143 ParentPartition->Has_SubPartition = true;
144 else
145 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 -0400146 }
147 }
148 fclose(fp);
149 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400150}
151
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500152void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500153 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
154 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
155 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
156}
157
Matt Mowerbf4efa32014-04-14 23:25:26 -0500158void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
159 if (Part->Has_Android_Secure)
160 Part->Setup_AndSec();
161#ifndef RECOVERY_SDCARD_ON_DATA
162 else
163 Part->Setup_AndSec();
164#endif
165}
166
Dees_Troy8170a922012-09-18 15:40:25 -0400167void TWPartitionManager::Output_Partition_Logging(void) {
168 std::vector<TWPartition*>::iterator iter;
169
170 printf("\n\nPartition Logs:\n");
171 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
172 Output_Partition((*iter));
173}
174
175void TWPartitionManager::Output_Partition(TWPartition* Part) {
176 unsigned long long mb = 1048576;
177
Gary Peck004d48b2012-11-21 16:28:18 -0800178 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 -0400179 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800180 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 -0400181 }
Gary Peck004d48b2012-11-21 16:28:18 -0800182 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500183 if (Part->Can_Be_Mounted)
184 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800185 if (Part->Can_Be_Wiped)
186 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700187 if (Part->Use_Rm_Rf)
188 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500189 if (Part->Can_Be_Backed_Up)
190 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800191 if (Part->Wipe_During_Factory_Reset)
192 printf("Wipe_During_Factory_Reset ");
193 if (Part->Wipe_Available_in_GUI)
194 printf("Wipe_Available_in_GUI ");
195 if (Part->Is_SubPartition)
196 printf("Is_SubPartition ");
197 if (Part->Has_SubPartition)
198 printf("Has_SubPartition ");
199 if (Part->Removable)
200 printf("Removable ");
201 if (Part->Is_Present)
202 printf("IsPresent ");
203 if (Part->Can_Be_Encrypted)
204 printf("Can_Be_Encrypted ");
205 if (Part->Is_Encrypted)
206 printf("Is_Encrypted ");
207 if (Part->Is_Decrypted)
208 printf("Is_Decrypted ");
209 if (Part->Has_Data_Media)
210 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000211 if (Part->Can_Encrypt_Backup)
212 printf("Can_Encrypt_Backup ");
213 if (Part->Use_Userdata_Encryption)
214 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800215 if (Part->Has_Android_Secure)
216 printf("Has_Android_Secure ");
217 if (Part->Is_Storage)
218 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500219 if (Part->Is_Settings_Storage)
220 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000221 if (Part->Ignore_Blkid)
222 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000223 if (Part->Retain_Layout_Version)
224 printf("Retain_Layout_Version ");
Gary Peck004d48b2012-11-21 16:28:18 -0800225 printf("\n");
226 if (!Part->SubPartition_Of.empty())
227 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
228 if (!Part->Symlink_Path.empty())
229 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
230 if (!Part->Symlink_Mount_Point.empty())
231 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
232 if (!Part->Primary_Block_Device.empty())
233 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
234 if (!Part->Alternate_Block_Device.empty())
235 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
236 if (!Part->Decrypted_Block_Device.empty())
237 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
238 if (Part->Length != 0)
239 printf(" Length: %i\n", Part->Length);
240 if (!Part->Display_Name.empty())
241 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500242 if (!Part->Storage_Name.empty())
243 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800244 if (!Part->Backup_Path.empty())
245 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
246 if (!Part->Backup_Name.empty())
247 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500248 if (!Part->Backup_Display_Name.empty())
249 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800250 if (!Part->Backup_FileName.empty())
251 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
252 if (!Part->Storage_Path.empty())
253 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
254 if (!Part->Current_File_System.empty())
255 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
256 if (!Part->Fstab_File_System.empty())
257 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
258 if (Part->Format_Block_Size != 0)
259 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400260 if (!Part->MTD_Name.empty())
261 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400262 string back_meth = Part->Backup_Method_By_Name();
263 printf(" Backup_Method: %s\n\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800264 if (Part->Mount_Flags || !Part->Mount_Options.empty())
265 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400266}
267
Dees_Troy51a0e822012-09-05 15:24:24 -0400268int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400269 std::vector<TWPartition*>::iterator iter;
270 int ret = false;
271 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400272 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400273
Dees_Troyd93bda52013-07-03 19:55:19 +0000274 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400275 return true;
276
Dees_Troy5bf43922012-09-07 16:07:55 -0400277 // Iterate through all partitions
278 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400279 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400280 ret = (*iter)->Mount(Display_Error);
281 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400282 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400283 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400284 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400285 }
286 if (found) {
287 return ret;
288 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000289 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400290 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000291 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400292 }
293 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400294}
295
296int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400297 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400298
Dees_Troy51127312012-09-08 13:08:49 -0400299 if (Part) {
300 if (Part->Has_SubPartition) {
301 std::vector<TWPartition*>::iterator subpart;
302
303 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
304 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
305 (*subpart)->Mount(Display_Error);
306 }
307 return Part->Mount(Display_Error);
308 } else
309 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400310 }
311 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000312 LOGERR("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400313 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000314 LOGINFO("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400315 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400316}
317
318int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400319 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400320
Dees_Troy51127312012-09-08 13:08:49 -0400321 if (Part) {
322 if (Part->Has_SubPartition) {
323 std::vector<TWPartition*>::iterator subpart;
324
325 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
326 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
327 (*subpart)->Mount(Display_Error);
328 }
329 return Part->Mount(Display_Error);
330 } else
331 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400332 }
333 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000334 LOGERR("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400335 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000336 LOGINFO("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400337 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400338}
339
340int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400341 std::vector<TWPartition*>::iterator iter;
342 int ret = false;
343 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400344 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400345
346 // Iterate through all partitions
347 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400348 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400349 ret = (*iter)->UnMount(Display_Error);
350 found = true;
351 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
352 (*iter)->UnMount(Display_Error);
353 }
354 }
355 if (found) {
356 return ret;
357 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000358 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400359 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000360 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400361 }
362 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400363}
364
365int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400366 TWPartition* Part = Find_Partition_By_Block(Block);
367
368 if (Part) {
369 if (Part->Has_SubPartition) {
370 std::vector<TWPartition*>::iterator subpart;
371
372 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
373 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
374 (*subpart)->UnMount(Display_Error);
375 }
376 return Part->UnMount(Display_Error);
377 } else
378 return Part->UnMount(Display_Error);
379 }
380 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000381 LOGERR("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400382 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000383 LOGINFO("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400384 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400385}
386
387int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400388 TWPartition* Part = Find_Partition_By_Name(Name);
389
390 if (Part) {
391 if (Part->Has_SubPartition) {
392 std::vector<TWPartition*>::iterator subpart;
393
394 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
395 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
396 (*subpart)->UnMount(Display_Error);
397 }
398 return Part->UnMount(Display_Error);
399 } else
400 return Part->UnMount(Display_Error);
401 }
402 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000403 LOGERR("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400404 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000405 LOGINFO("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400406 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400407}
408
409int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400410 TWPartition* Part = Find_Partition_By_Path(Path);
411
412 if (Part)
413 return Part->Is_Mounted();
414 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000415 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400416 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400417}
418
419int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400420 TWPartition* Part = Find_Partition_By_Block(Block);
421
422 if (Part)
423 return Part->Is_Mounted();
424 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000425 LOGINFO("Is_Mounted: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400426 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400427}
428
429int TWPartitionManager::Is_Mounted_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400430 TWPartition* Part = Find_Partition_By_Name(Name);
431
432 if (Part)
433 return Part->Is_Mounted();
434 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000435 LOGINFO("Is_Mounted: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400436 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400437}
438
Dees_Troy5bf43922012-09-07 16:07:55 -0400439int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400440 string current_storage_path = DataManager::GetCurrentStoragePath();
441
442 if (Mount_By_Path(current_storage_path, Display_Error)) {
443 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
444 if (FreeStorage)
445 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
446 return true;
447 }
448 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400449}
450
Dees_Troy5bf43922012-09-07 16:07:55 -0400451int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
452 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
453}
454
455TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
456 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400457 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400458
459 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400460 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400461 return (*iter);
462 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400463 return NULL;
464}
465
Dees_Troy5bf43922012-09-07 16:07:55 -0400466TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400467 std::vector<TWPartition*>::iterator iter;
468
469 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400470 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 -0400471 return (*iter);
472 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400473 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400474}
475
476TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400477 std::vector<TWPartition*>::iterator iter;
478
479 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
480 if ((*iter)->Display_Name == Name)
481 return (*iter);
482 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400483 return NULL;
484}
Dees_Troy51a0e822012-09-05 15:24:24 -0400485
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400486int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
487 // Check the backup name to ensure that it is the correct size and contains only valid characters
488 // and that a backup with that name doesn't already exist
489 char backup_name[MAX_BACKUP_NAME_LEN];
490 char backup_loc[255], tw_image_dir[255];
491 int copy_size;
492 int index, cur_char;
493 string Backup_Name, Backup_Loc;
494
495 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
496 copy_size = Backup_Name.size();
497 // Check size
498 if (copy_size > MAX_BACKUP_NAME_LEN) {
499 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000500 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400501 return -2;
502 }
503
504 // Check each character
505 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500506 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400507 return 0; // A "0" (zero) means to use the current timestamp for the backup name
508 for (index=0; index<copy_size; index++) {
509 cur_char = (int)backup_name[index];
510 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) {
511 // These are valid characters
512 // Numbers
513 // Upper case letters
514 // Lower case letters
515 // Space
516 // and -_.{}[]
517 } else {
518 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000519 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400520 return -3;
521 }
522 }
523
524 // Check to make sure that a backup with this name doesn't already exist
525 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
526 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500527 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400528 if (TWFunc::Path_Exists(tw_image_dir)) {
529 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000530 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400531 return -4;
532 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400533 // No problems found, return 0
534 return 0;
535}
536
Dees_Troy43d8b002012-09-17 16:00:01 -0400537bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
538{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500539 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400540 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500541 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500542 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400543
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500544 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400545 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400546
Dees_Troyb46a6842012-09-25 11:06:46 -0400547 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000548 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400549
550 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500551 md5sum.setfn(Backup_Folder + Backup_Filename);
552 if (md5sum.computeMD5() == 0)
553 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000554 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500555 else
556 return -1;
557 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000558 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400559 } else {
560 char filename[512];
561 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500562 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400563 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500564 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000565 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400566 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000567 if (TWFunc::Path_Exists(filename)) {
568 if (md5sum.computeMD5() == 0) {
569 if (md5sum.write_md5digest() != 0)
570 {
571 gui_print(" * MD5 Error.\n");
572 return false;
573 }
574 } else {
575 gui_print(" * Error computing MD5.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500576 return false;
577 }
578 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400579 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400580 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500581 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400582 }
583 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000584 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400585 return false;
586 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000587 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400588 }
589 return true;
590}
591
Dees_Troy093b7642012-09-21 15:59:38 -0400592bool 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 -0400593 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500594 int img_bps;
595 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400596 unsigned long total_time, remain_time, section_time;
597 int use_compression, backup_time;
598 float pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400599
600 if (Part == NULL)
601 return true;
602
Dees_Troy093b7642012-09-21 15:59:38 -0400603 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
604
605 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
606 if (use_compression)
607 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
608 else
609 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
610
611 // We know the speed for both, how far into the whole backup are we, based on time
612 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
613 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
614
615 pos = (total_time - remain_time) / (float) total_time;
Dees_Troy2673cec2013-04-02 20:22:16 +0000616 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400617
Dees_Troy2673cec2013-04-02 20:22:16 +0000618 LOGINFO("Estimated Total time: %lu Estimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400619
620 // And get the time
621 if (Part->Backup_Method == 1)
622 section_time = Part->Backup_Size / file_bps;
623 else
624 section_time = Part->Backup_Size / img_bps;
625
626 // Set the position
627 pos = section_time / (float) total_time;
Dees_Troy2673cec2013-04-02 20:22:16 +0000628 DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400629
Dees_Troy43d8b002012-09-17 16:00:01 -0400630 time(&start);
631
632 if (Part->Backup(Backup_Folder)) {
Dees_Troy8170a922012-09-18 15:40:25 -0400633 if (Part->Has_SubPartition) {
634 std::vector<TWPartition*>::iterator subpart;
635
636 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500637 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Dees_Troy8170a922012-09-18 15:40:25 -0400638 if (!(*subpart)->Backup(Backup_Folder))
639 return false;
Dees_Troy2727b992013-08-14 20:09:30 +0000640 sync();
641 sync();
Dees_Troy8170a922012-09-18 15:40:25 -0400642 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
643 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400644 if (Part->Backup_Method == 1) {
645 *file_bytes_remaining -= (*subpart)->Backup_Size;
646 } else {
647 *img_bytes_remaining -= (*subpart)->Backup_Size;
648 }
Dees_Troy8170a922012-09-18 15:40:25 -0400649 }
650 }
651 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400652 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400653 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000654 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400655 if (Part->Backup_Method == 1) {
656 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400657 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400658 } else {
659 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400660 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400661 }
662 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
663 } else {
664 return false;
665 }
666}
667
668int TWPartitionManager::Run_Backup(void) {
669 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500670 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400671 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 -0400672 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500673 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400674 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400675 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400676 struct tm *t;
677 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500678 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400679 seconds = time(0);
680 t = localtime(&seconds);
681
682 time(&total_start);
683
684 Update_System_Details();
685
686 if (!Mount_Current_Storage(true))
687 return false;
688
689 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400690 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400691 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400692 else
693 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400694
Dees_Troy43d8b002012-09-17 16:00:01 -0400695 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
696 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000697 if (Backup_Name == "(Current Date)") {
698 Backup_Name = TWFunc::Get_Current_Date();
699 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
700 TWFunc::Auto_Generate_Backup_Name();
701 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400702 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000703 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400704 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000705 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400706
Dees_Troy2673cec2013-04-02 20:22:16 +0000707 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500708 DataManager::GetValue("tw_backup_list", Backup_List);
709 if (!Backup_List.empty()) {
710 end_pos = Backup_List.find(";", start_pos);
711 while (end_pos != string::npos && start_pos < Backup_List.size()) {
712 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
713 backup_part = Find_Partition_By_Path(backup_path);
714 if (backup_part != NULL) {
715 partition_count++;
716 if (backup_part->Backup_Method == 1)
717 file_bytes += backup_part->Backup_Size;
718 else
719 img_bytes += backup_part->Backup_Size;
720 if (backup_part->Has_SubPartition) {
721 std::vector<TWPartition*>::iterator subpart;
722
723 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000724 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 -0500725 partition_count++;
726 if ((*subpart)->Backup_Method == 1)
727 file_bytes += (*subpart)->Backup_Size;
728 else
729 img_bytes += (*subpart)->Backup_Size;
730 }
731 }
Dees_Troy8170a922012-09-18 15:40:25 -0400732 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500733 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000734 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400735 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500736 start_pos = end_pos + 1;
737 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400738 }
739 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400740
741 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000742 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400743 return false;
744 }
745 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000746 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
747 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400748 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
749 if (storage != NULL) {
750 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000751 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400752 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000753 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400754 return false;
755 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000756 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400757 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000758 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400759 return false;
760 }
761 img_bytes_remaining = img_bytes;
762 file_bytes_remaining = file_bytes;
763
Dees_Troy2673cec2013-04-02 20:22:16 +0000764 gui_print("\n[BACKUP STARTED]\n");
765 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000766 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000767 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000768 return false;
769 }
770
Dees_Troy2673cec2013-04-02 20:22:16 +0000771 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400772
Dees_Troya13d74f2013-03-24 08:54:55 -0500773 start_pos = 0;
774 end_pos = Backup_List.find(";", start_pos);
775 while (end_pos != string::npos && start_pos < Backup_List.size()) {
776 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
777 backup_part = Find_Partition_By_Path(backup_path);
778 if (backup_part != NULL) {
779 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
780 return false;
781 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000782 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500783 }
784 start_pos = end_pos + 1;
785 end_pos = Backup_List.find(";", start_pos);
786 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400787
788 // Average BPS
789 if (img_time == 0)
790 img_time = 1;
791 if (file_time == 0)
792 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400793 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500794 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400795
Dees_Troy2673cec2013-04-02 20:22:16 +0000796 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
797 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400798
799 time(&total_stop);
800 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500801 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Dees_Troy43d8b002012-09-17 16:00:01 -0400802 actual_backup_size /= (1024LLU * 1024LLU);
803
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500804 int prev_img_bps, use_compression;
805 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400806 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
807 img_bps += (prev_img_bps * 4);
808 img_bps /= 5;
809
810 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
811 if (use_compression)
812 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
813 else
814 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
815 file_bps += (prev_file_bps * 4);
816 file_bps /= 5;
817
818 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
819 if (use_compression)
820 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
821 else
822 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
823
Dees_Troy2673cec2013-04-02 20:22:16 +0000824 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400825 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400826 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +0000827 gui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000828 string backup_log = Full_Backup_Path + "recovery.log";
829 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
830 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400831}
832
Dees_Troy093b7642012-09-21 15:59:38 -0400833bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400834 time_t Start, Stop;
835 time(&Start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000836 DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400837 if (!Part->Restore(Restore_Name))
838 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400839 if (Part->Has_SubPartition) {
840 std::vector<TWPartition*>::iterator subpart;
841
842 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
843 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
844 if (!(*subpart)->Restore(Restore_Name))
845 return false;
846 }
847 }
848 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400849 time(&Stop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000850 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 -0400851 return true;
852}
853
Dees_Troy51a0e822012-09-05 15:24:24 -0400854int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400855 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500856 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400857 time_t rStart, rStop;
858 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500859 string Restore_List, restore_path;
860 size_t start_pos = 0, end_pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400861
Dees_Troy2673cec2013-04-02 20:22:16 +0000862 gui_print("\n[RESTORE STARTED]\n\n");
863 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400864
Dees_Troy4a2a1262012-09-18 09:33:47 -0400865 if (!Mount_Current_Storage(true))
866 return false;
867
868 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500869 if (check_md5 > 0) {
870 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
871 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000872 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500873 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000874 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500875 }
876 DataManager::GetValue("tw_restore_selected", Restore_List);
877 if (!Restore_List.empty()) {
878 end_pos = Restore_List.find(";", start_pos);
879 while (end_pos != string::npos && start_pos < Restore_List.size()) {
880 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
881 restore_part = Find_Partition_By_Path(restore_path);
882 if (restore_part != NULL) {
883 partition_count++;
884 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
885 return false;
886 if (restore_part->Has_SubPartition) {
887 std::vector<TWPartition*>::iterator subpart;
888
889 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
890 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400891 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500892 return false;
893 }
894 }
895 }
896 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500897 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500898 }
899 start_pos = end_pos + 1;
900 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400901 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400902 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400903
904 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000905 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400906 return false;
907 }
908
Dees_Troy2673cec2013-04-02 20:22:16 +0000909 gui_print("Restoring %i partitions...\n", partition_count);
910 DataManager::SetProgress(0.0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500911 start_pos = 0;
912 if (!Restore_List.empty()) {
913 end_pos = Restore_List.find(";", start_pos);
914 while (end_pos != string::npos && start_pos < Restore_List.size()) {
915 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
916 restore_part = Find_Partition_By_Path(restore_path);
917 if (restore_part != NULL) {
918 partition_count++;
919 if (!Restore_Partition(restore_part, Restore_Name, partition_count))
920 return false;
921 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000922 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500923 }
924 start_pos = end_pos + 1;
925 end_pos = Restore_List.find(";", start_pos);
926 }
927 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400928 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400929 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400930 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400931 time(&rStop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000932 gui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -0400933 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400934}
935
936void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400937 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500938 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000939 bool get_date = true, check_encryption = true;
940
941 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400942
943 DIR* d;
944 d = opendir(Restore_Name.c_str());
945 if (d == NULL)
946 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000947 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400948 return;
949 }
950
951 struct dirent* de;
952 while ((de = readdir(d)) != NULL)
953 {
954 // Strip off three components
955 char str[256];
956 char* label;
957 char* fstype = NULL;
958 char* extn = NULL;
959 char* ptr;
960
961 strcpy(str, de->d_name);
962 if (strlen(str) <= 2)
963 continue;
964
965 if (get_date) {
966 char file_path[255];
967 struct stat st;
968
969 strcpy(file_path, Restore_Name.c_str());
970 strcat(file_path, "/");
971 strcat(file_path, str);
972 stat(file_path, &st);
973 string backup_date = ctime((const time_t*)(&st.st_mtime));
974 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
975 get_date = false;
976 }
977
978 label = str;
979 ptr = label;
980 while (*ptr && *ptr != '.') ptr++;
981 if (*ptr == '.')
982 {
983 *ptr = 0x00;
984 ptr++;
985 fstype = ptr;
986 }
987 while (*ptr && *ptr != '.') ptr++;
988 if (*ptr == '.')
989 {
990 *ptr = 0x00;
991 ptr++;
992 extn = ptr;
993 }
994
Dees_Troy83bd4832013-05-04 12:39:56 +0000995 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -0500996 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +0000997 if (extnlength != 3 && extnlength != 6) continue;
998 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
999 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
1000
1001 if (check_encryption) {
1002 string filename = Restore_Name + "/";
1003 filename += de->d_name;
1004 if (TWFunc::Get_File_Type(filename) == 2) {
1005 LOGINFO("'%s' is encrypted\n", filename.c_str());
1006 DataManager::SetValue("tw_restore_encrypted", 1);
1007 }
1008 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001009 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -04001010
1011 TWPartition* Part = Find_Partition_By_Path(label);
1012 if (Part == NULL)
1013 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001014 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -04001015 continue;
1016 }
1017
1018 Part->Backup_FileName = de->d_name;
1019 if (strlen(extn) > 3) {
1020 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1021 }
1022
Dees_Troya13d74f2013-03-24 08:54:55 -05001023 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001024 }
1025 closedir(d);
1026
Dees_Troya13d74f2013-03-24 08:54:55 -05001027 // Set the final value
1028 DataManager::SetValue("tw_restore_list", Restore_List);
1029 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001030 return;
1031}
1032
1033int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001034 std::vector<TWPartition*>::iterator iter;
1035 int ret = false;
1036 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001037 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001038
1039 // Iterate through all partitions
1040 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001041 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001042 if (Path == "/and-sec")
1043 ret = (*iter)->Wipe_AndSec();
1044 else
1045 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001046 found = true;
1047 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1048 (*iter)->Wipe();
1049 }
1050 }
1051 if (found) {
1052 return ret;
1053 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001054 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.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_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001059 TWPartition* Part = Find_Partition_By_Block(Block);
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 block '%s'\n", Block.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001074 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001075}
1076
1077int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001078 TWPartition* Part = Find_Partition_By_Name(Name);
1079
1080 if (Part) {
1081 if (Part->Has_SubPartition) {
1082 std::vector<TWPartition*>::iterator subpart;
1083
1084 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1085 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1086 (*subpart)->Wipe();
1087 }
1088 return Part->Wipe();
1089 } else
1090 return Part->Wipe();
1091 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001092 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001093 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001094}
1095
1096int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001097 std::vector<TWPartition*>::iterator iter;
1098 int ret = true;
1099
1100 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001101 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001102 if (!(*iter)->Wipe())
1103 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001104 } else if ((*iter)->Has_Android_Secure) {
1105 if (!(*iter)->Wipe_AndSec())
1106 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001107 }
1108 }
1109 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001110}
1111
Dees_Troy38bd7602012-09-14 13:33:53 -04001112int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1113 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001114 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001115
1116 if (!Mount_By_Path("/data", true))
1117 return false;
1118
1119 if (!Mount_By_Path("/cache", true))
1120 return false;
1121
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001122 dir.push_back("/data/dalvik-cache");
1123 dir.push_back("/cache/dalvik-cache");
1124 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001125 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001126 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001127 if (stat(dir.at(i).c_str(), &st) == 0) {
1128 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001129 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001130 }
1131 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001132 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001133 if (sdext && sdext->Is_Present && sdext->Mount(false))
1134 {
1135 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1136 {
1137 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
1138 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001139 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001140 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001141 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001142 return true;
1143}
1144
1145int TWPartitionManager::Wipe_Rotate_Data(void) {
1146 if (!Mount_By_Path("/data", true))
1147 return false;
1148
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001149 unlink("/data/misc/akmd*");
1150 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001151 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001152 return true;
1153}
1154
1155int TWPartitionManager::Wipe_Battery_Stats(void) {
1156 struct stat st;
1157
1158 if (!Mount_By_Path("/data", true))
1159 return false;
1160
1161 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001162 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001163 } else {
1164 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001165 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001166 }
1167 return true;
1168}
1169
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001170int TWPartitionManager::Wipe_Android_Secure(void) {
1171 std::vector<TWPartition*>::iterator iter;
1172 int ret = false;
1173 bool found = false;
1174
1175 // Iterate through all partitions
1176 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1177 if ((*iter)->Has_Android_Secure) {
1178 ret = (*iter)->Wipe_AndSec();
1179 found = true;
1180 }
1181 }
1182 if (found) {
1183 return ret;
1184 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001185 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001186 }
1187 return false;
1188}
1189
Dees_Troy38bd7602012-09-14 13:33:53 -04001190int TWPartitionManager::Format_Data(void) {
1191 TWPartition* dat = Find_Partition_By_Path("/data");
1192
1193 if (dat != NULL) {
1194 if (!dat->UnMount(true))
1195 return false;
1196
1197 return dat->Wipe_Encryption();
1198 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001199 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001200 return false;
1201 }
1202 return false;
1203}
1204
1205int TWPartitionManager::Wipe_Media_From_Data(void) {
1206 TWPartition* dat = Find_Partition_By_Path("/data");
1207
1208 if (dat != NULL) {
1209 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001210 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001211 return false;
1212 }
1213 if (!dat->Mount(true))
1214 return false;
1215
Dees_Troy2673cec2013-04-02 20:22:16 +00001216 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001217 TWFunc::removeDir("/data/media", false);
1218 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
1219 return -1;
Dees_Troy38bd7602012-09-14 13:33:53 -04001220 if (dat->Has_Data_Media) {
1221 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001222 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1223 dat->UnMount(false);
1224 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001225 }
1226 return true;
1227 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001228 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001229 return false;
1230 }
1231 return false;
1232}
1233
Dees_Troy51a0e822012-09-05 15:24:24 -04001234void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001235 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001236 return;
1237}
1238
1239void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001240 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001241 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001242
Dees_Troy2673cec2013-04-02 20:22:16 +00001243 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001244 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001245 if ((*iter)->Can_Be_Mounted) {
1246 (*iter)->Update_Size(true);
1247 if ((*iter)->Mount_Point == "/system") {
1248 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1249 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1250 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1251 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1252 } else if ((*iter)->Mount_Point == "/cache") {
1253 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1254 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1255 } else if ((*iter)->Mount_Point == "/sd-ext") {
1256 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1257 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1258 if ((*iter)->Backup_Size == 0) {
1259 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1260 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1261 } else
1262 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001263 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001264 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001265 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001266 if ((*iter)->Backup_Size == 0) {
1267 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1268 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1269 } else
1270 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001271 } else if ((*iter)->Mount_Point == "/boot") {
1272 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1273 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1274 if ((*iter)->Backup_Size == 0) {
1275 DataManager::SetValue("tw_has_boot_partition", 0);
1276 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1277 } else
1278 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001279 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001280#ifdef SP1_NAME
1281 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1282 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1283 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1284 }
1285#endif
1286#ifdef SP2_NAME
1287 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1288 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1289 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1290 }
1291#endif
1292#ifdef SP3_NAME
1293 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1294 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1295 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1296 }
1297#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001298 } else {
1299 // Handle unmountable partitions in case we reset defaults
1300 if ((*iter)->Mount_Point == "/boot") {
1301 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1302 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1303 if ((*iter)->Backup_Size == 0) {
1304 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1305 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1306 } else
1307 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1308 } else if ((*iter)->Mount_Point == "/recovery") {
1309 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1310 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1311 if ((*iter)->Backup_Size == 0) {
1312 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1313 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1314 } else
1315 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001316 } else if ((*iter)->Mount_Point == "/data") {
1317 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001318 }
1319#ifdef SP1_NAME
1320 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1321 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1322 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1323 }
1324#endif
1325#ifdef SP2_NAME
1326 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1327 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1328 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1329 }
1330#endif
1331#ifdef SP3_NAME
1332 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1333 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1334 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1335 }
1336#endif
Dees_Troy51127312012-09-08 13:08:49 -04001337 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001338 }
Dees_Troy51127312012-09-08 13:08:49 -04001339 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1340 string current_storage_path = DataManager::GetCurrentStoragePath();
1341 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001342 if (FreeStorage != NULL) {
1343 // Attempt to mount storage
1344 if (!FreeStorage->Mount(false)) {
1345 // We couldn't mount storage... check to see if we have dual storage
1346 int has_dual_storage;
1347 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1348 if (has_dual_storage == 1) {
1349 // We have dual storage, see if we're using the internal storage that should always be present
1350 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001351 if (!FreeStorage->Is_Encrypted) {
1352 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001353 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001354 }
Dees_Troy8170a922012-09-18 15:40:25 -04001355 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1356 } else {
1357 // We were using external, flip to internal
1358 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1359 current_storage_path = DataManager::GetCurrentStoragePath();
1360 FreeStorage = Find_Partition_By_Path(current_storage_path);
1361 if (FreeStorage != NULL) {
1362 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1363 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001364 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001365 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1366 }
1367 }
1368 } else {
1369 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001370 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001371 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1372 }
1373 } else {
1374 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1375 }
1376 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001377 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001378 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001379 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001380 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001381 return;
1382}
1383
1384int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001385#ifdef TW_INCLUDE_CRYPTO
1386 int ret_val, password_len;
1387 char crypto_blkdev[255], cPassword[255];
1388 size_t result;
1389
1390 property_set("ro.crypto.state", "encrypted");
1391#ifdef TW_INCLUDE_JB_CRYPTO
1392 // No extra flags needed
1393#else
1394 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1395 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1396 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1397 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1398 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1399 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001400
1401#ifdef CRYPTO_SD_FS_TYPE
Ethan Yonker71413f42014-02-26 13:36:08 -06001402 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1403 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1404 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001405#endif
a39552696ff55ce2013-01-08 16:14:56 +00001406
1407 property_set("rw.km_fips_status", "ready");
1408
1409#endif
1410
1411 // some samsung devices store "footer" on efs partition
1412 TWPartition *efs = Find_Partition_By_Path("/efs");
1413 if(efs && !efs->Is_Mounted())
1414 efs->Mount(false);
1415 else
1416 efs = 0;
1417#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001418#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001419 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001420 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001421 property_set("ro.crypto.external_encrypted", "1");
1422 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1423 } else {
1424 property_set("ro.crypto.external_encrypted", "0");
1425 }
1426#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001427#endif
a39552696ff55ce2013-01-08 16:14:56 +00001428
Dees_Troy5bf43922012-09-07 16:07:55 -04001429 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001430 int pwret = cryptfs_check_passwd(cPassword);
1431
1432 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001433 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001434 return -1;
1435 }
a39552696ff55ce2013-01-08 16:14:56 +00001436
1437 if(efs)
1438 efs->UnMount(false);
1439
Dees_Troy5bf43922012-09-07 16:07:55 -04001440 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1441 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001442 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001443 } else {
1444 TWPartition* dat = Find_Partition_By_Path("/data");
1445 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001446 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001447 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1448 dat->Is_Decrypted = true;
1449 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001450 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001451 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 +00001452 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001453
1454#ifdef CRYPTO_SD_FS_TYPE
1455 char crypto_blkdev_sd[255];
1456 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1457 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001458 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001459 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001460 emmc->Is_Decrypted = true;
1461 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1462 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001463 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001464 }
a39552696ff55ce2013-01-08 16:14:56 +00001465#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001466#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001467#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001468 char is_external_decrypted[255];
1469 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1470 if (strcmp(is_external_decrypted, "1") == 0) {
1471 sdcard->Is_Decrypted = true;
1472 sdcard->EcryptFS_Password = Password;
1473 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001474 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1475 MetaEcfsFile += "/.MetaEcfsFile";
1476 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1477 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1478 sdcard->UnMount(false);
1479 sdcard->Mount(false);
1480 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001481 } else {
Dees_Troy066eb302013-08-23 17:20:32 +00001482 LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001483 sdcard->Is_Decrypted = false;
1484 sdcard->Decrypted_Block_Device = "";
1485 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001486#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001487#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001488
Dees_Troy5bf43922012-09-07 16:07:55 -04001489 // Sleep for a bit so that the device will be ready
1490 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001491#ifdef RECOVERY_SDCARD_ON_DATA
1492 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1493 dat->Storage_Path = "/data/media/0";
1494 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001495 DataManager::SetValue("tw_storage_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001496 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001497 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001498 }
1499#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001500 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001501 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001502 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001503 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001504 }
1505 return 0;
1506#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001507 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001508 return -1;
1509#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001510 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001511}
1512
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001513int TWPartitionManager::Fix_Permissions(void) {
1514 int result = 0;
1515 if (!Mount_By_Path("/data", true))
1516 return false;
1517
1518 if (!Mount_By_Path("/system", true))
1519 return false;
1520
1521 Mount_By_Path("/sd-ext", false);
1522
1523 fixPermissions perms;
1524 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001525 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001526 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001527 return result;
1528}
1529
Ethan Yonker47360be2014-04-01 10:34:34 -05001530TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1531 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1532
1533 if (!Path.empty()) {
1534 string Search_Path = TWFunc::Get_Root_Path(Path);
1535 for (; iter != Partitions.end(); iter++) {
1536 if ((*iter)->Mount_Point == Search_Path) {
1537 iter++;
1538 break;
1539 }
1540 }
1541 }
1542
1543 for (; iter != Partitions.end(); iter++) {
1544 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) {
1545 return (*iter);
1546 }
1547 }
1548
1549 return NULL;
1550}
1551
Dees_Troyd21618c2012-10-14 18:48:49 -04001552int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001553 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1554
1555 if (Part == NULL) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001556 LOGERR("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
Dees_Troyd21618c2012-10-14 18:48:49 -04001557 return false;
1558 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001559 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1560 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001561 return false;
1562
Dees_Troy2673cec2013-04-02 20:22:16 +00001563 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1564 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001565 return false;
1566 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001567 return true;
1568}
1569
Dees_Troy8170a922012-09-18 15:40:25 -04001570int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001571 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001572 char lun_file[255];
Dees_Troy8170a922012-09-18 15:40:25 -04001573 string ext_path;
Dees_Troyd21618c2012-10-14 18:48:49 -04001574 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001575
Dees_Troy8170a922012-09-18 15:40:25 -04001576 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001577 string Lun_File_str = CUSTOM_LUN_FILE;
1578 size_t found = Lun_File_str.find("%");
1579 if (found != string::npos) {
1580 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1581 if (TWFunc::Path_Exists(lun_file))
1582 has_multiple_lun = true;
1583 }
1584 if (!has_multiple_lun) {
1585 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1586 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1587 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
1588 TWPartition* Mount = Find_Next_Storage("", "/data");
1589 if (Mount) {
1590 if (!Open_Lun_File(Mount->Mount_Point, lun_file))
1591 return false;
1592 } else {
1593 LOGERR("Unable to find storage partition to mount to USB\n");
Dees_Troyf4ca6122012-10-13 22:17:20 -04001594 return false;
Ethan Yonker47360be2014-04-01 10:34:34 -05001595 }
1596 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
1597 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001598 }
Dees_Troy8170a922012-09-18 15:40:25 -04001599 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001600 LOGINFO("Device has multiple lun files\n");
1601 TWPartition* Mount1;
1602 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001603 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker47360be2014-04-01 10:34:34 -05001604 Mount1 = Find_Next_Storage("", "/data");
1605 if (Mount1) {
1606 if (!Open_Lun_File(Mount1->Mount_Point, lun_file))
1607 return false;
1608 Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data");
1609 if (Mount2) {
1610 Open_Lun_File(ext_path, lun_file);
1611 }
1612 } else {
1613 LOGERR("Unable to find storage partition to mount to USB\n");
1614 return false;
1615 }
Dees_Troy8170a922012-09-18 15:40:25 -04001616 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001617 property_set("sys.storage.ums_enabled", "1");
Dees_Troy8170a922012-09-18 15:40:25 -04001618 return true;
1619}
1620
1621int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001622 int index, ret;
1623 char lun_file[255], ch[2] = {0, 0};
1624 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001625
1626 for (index=0; index<2; index++) {
1627 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001628 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001629 if (ret < 0) {
1630 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001631 }
Dees_Troy8170a922012-09-18 15:40:25 -04001632 }
Dees_Troye58d5262012-09-21 12:27:57 -04001633 Mount_All_Storage();
1634 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001635 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001636 property_set("sys.storage.ums_enabled", "0");
Dees_Troy2673cec2013-04-02 20:22:16 +00001637 if (ret < 0 && index == 0) {
1638 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1639 return false;
1640 } else {
1641 return true;
1642 }
Dees_Troy8170a922012-09-18 15:40:25 -04001643 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001644}
1645
1646void TWPartitionManager::Mount_All_Storage(void) {
1647 std::vector<TWPartition*>::iterator iter;
1648
1649 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1650 if ((*iter)->Is_Storage)
1651 (*iter)->Mount(false);
1652 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001653}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001654
Dees_Troyd0384ef2012-10-12 12:15:42 -04001655void TWPartitionManager::UnMount_Main_Partitions(void) {
1656 // Unmounts system and data if data is not data/media
1657 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001658 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001659
1660 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1661
1662 UnMount_By_Path("/system", true);
1663#ifndef RECOVERY_SDCARD_ON_DATA
1664 UnMount_By_Path("/data", true);
1665#endif
1666 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1667 Boot_Partition->UnMount(true);
1668}
1669
Dees_Troy9350b8d2012-09-27 12:38:38 -04001670int TWPartitionManager::Partition_SDCard(void) {
1671 char mkdir_path[255], temp[255], line[512];
1672 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1673 int ext, swap, total_size = 0, fat_size;
1674 FILE* fp;
1675
Dees_Troy2673cec2013-04-02 20:22:16 +00001676 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001677#ifdef TW_EXTERNAL_STORAGE_PATH
1678 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1679#else
1680 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1681#endif
1682 if (SDCard == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001683 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001684 return false;
1685 }
1686 if (!SDCard->UnMount(true))
1687 return false;
1688 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1689 if (SDext != NULL) {
1690 if (!SDext->UnMount(true))
1691 return false;
1692 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001693
1694 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001695 Device = SDCard->Actual_Block_Device;
1696 // Just use the root block device
1697 Device.resize(strlen("/dev/block/mmcblkX"));
1698
1699 // Find the size of the block device:
1700 fp = fopen("/proc/partitions", "rt");
1701 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001702 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001703 return false;
1704 }
1705
1706 while (fgets(line, sizeof(line), fp) != NULL)
1707 {
1708 unsigned long major, minor, blocks;
1709 char device[512];
1710 char tmpString[64];
1711
1712 if (strlen(line) < 7 || line[0] == 'm') continue;
1713 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1714
1715 tmpdevice = "/dev/block/";
1716 tmpdevice += device;
1717 if (tmpdevice == Device) {
1718 // Adjust block size to byte size
1719 total_size = (int)(blocks * 1024ULL / 1000000LLU);
1720 break;
1721 }
1722 }
1723 fclose(fp);
1724
1725 DataManager::GetValue("tw_sdext_size", ext);
1726 DataManager::GetValue("tw_swap_size", swap);
1727 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1728 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001729 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 -04001730 memset(temp, 0, sizeof(temp));
1731 sprintf(temp, "%i", fat_size);
1732 fat_str = temp;
1733 memset(temp, 0, sizeof(temp));
1734 sprintf(temp, "%i", fat_size + ext);
1735 ext_str = temp;
1736 memset(temp, 0, sizeof(temp));
1737 sprintf(temp, "%i", fat_size + ext + swap);
1738 swap_str = temp;
1739 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001740 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001741 return false;
1742 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001743 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001744 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001745 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001746 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001747 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001748 Update_System_Details();
1749 return false;
1750 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001751 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001752 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001753 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001754 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001755 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001756 return false;
1757 }
1758 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001759 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001760 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001761 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001762 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001763 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001764 Update_System_Details();
1765 return false;
1766 }
1767 }
1768 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001769 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001770 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001771 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001772 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001773 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001774 Update_System_Details();
1775 return false;
1776 }
1777 }
1778 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1779#ifdef TW_EXTERNAL_STORAGE_PATH
1780 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1781 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1782 memset(mkdir_path, 0, sizeof(mkdir_path));
1783 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1784#else
1785 Mount_By_Path("/sdcard", 1);
1786 strcpy(mkdir_path, "/sdcard/TWRP");
1787#endif
1788 mkdir(mkdir_path, 0777);
1789 DataManager::Flush();
1790#ifdef TW_EXTERNAL_STORAGE_PATH
1791 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1792 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1793 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1794#else
1795 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1796 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1797 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1798#endif
1799 if (ext > 0) {
1800 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001801 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001802 return false;
1803 }
1804 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001805 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1806 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001807 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001808 }
1809
1810 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00001811 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001812 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001813}
Dees_Troya13d74f2013-03-24 08:54:55 -05001814
1815void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1816 std::vector<TWPartition*>::iterator iter;
1817 if (ListType == "mount") {
1818 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1819 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1820 struct PartitionList part;
1821 part.Display_Name = (*iter)->Display_Name;
1822 part.Mount_Point = (*iter)->Mount_Point;
1823 part.selected = (*iter)->Is_Mounted();
1824 Partition_List->push_back(part);
1825 }
1826 }
1827 } else if (ListType == "storage") {
1828 char free_space[255];
1829 string Current_Storage = DataManager::GetCurrentStoragePath();
1830 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1831 if ((*iter)->Is_Storage) {
1832 struct PartitionList part;
1833 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1834 part.Display_Name = (*iter)->Storage_Name + " (";
1835 part.Display_Name += free_space;
1836 part.Display_Name += "MB)";
1837 part.Mount_Point = (*iter)->Storage_Path;
1838 if ((*iter)->Storage_Path == Current_Storage)
1839 part.selected = 1;
1840 else
1841 part.selected = 0;
1842 Partition_List->push_back(part);
1843 }
1844 }
1845 } else if (ListType == "backup") {
1846 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001847 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001848 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001849 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001850 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001851 Backup_Size = (*iter)->Backup_Size;
1852 if ((*iter)->Has_SubPartition) {
1853 std::vector<TWPartition*>::iterator subpart;
1854
1855 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1856 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1857 Backup_Size += (*subpart)->Backup_Size;
1858 }
1859 }
1860 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001861 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1862 part.Display_Name += backup_size;
1863 part.Display_Name += "MB)";
1864 part.Mount_Point = (*iter)->Backup_Path;
1865 part.selected = 0;
1866 Partition_List->push_back(part);
1867 }
1868 }
1869 } else if (ListType == "restore") {
1870 string Restore_List, restore_path;
1871 TWPartition* restore_part = NULL;
1872
1873 DataManager::GetValue("tw_restore_list", Restore_List);
1874 if (!Restore_List.empty()) {
1875 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1876 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1877 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05001878 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00001879 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001880 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05001881 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05001882 } else {
1883 struct PartitionList part;
1884 part.Display_Name = restore_part->Backup_Display_Name;
1885 part.Mount_Point = restore_part->Backup_Path;
1886 part.selected = 1;
1887 Partition_List->push_back(part);
1888 }
1889 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001890 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001891 }
1892 start_pos = end_pos + 1;
1893 end_pos = Restore_List.find(";", start_pos);
1894 }
1895 }
1896 } else if (ListType == "wipe") {
1897 struct PartitionList dalvik;
1898 dalvik.Display_Name = "Dalvik Cache";
1899 dalvik.Mount_Point = "DALVIK";
1900 dalvik.selected = 0;
1901 Partition_List->push_back(dalvik);
1902 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1903 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1904 struct PartitionList part;
1905 part.Display_Name = (*iter)->Display_Name;
1906 part.Mount_Point = (*iter)->Mount_Point;
1907 part.selected = 0;
1908 Partition_List->push_back(part);
1909 }
1910 if ((*iter)->Has_Android_Secure) {
1911 struct PartitionList part;
1912 part.Display_Name = (*iter)->Backup_Display_Name;
1913 part.Mount_Point = (*iter)->Backup_Path;
1914 part.selected = 0;
1915 Partition_List->push_back(part);
1916 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001917 if ((*iter)->Has_Data_Media) {
1918 struct PartitionList datamedia;
1919 datamedia.Display_Name = (*iter)->Storage_Name;
1920 datamedia.Mount_Point = "INTERNAL";
1921 datamedia.selected = 0;
1922 Partition_List->push_back(datamedia);
1923 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001924 }
1925 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001926 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001927 }
1928}
1929
1930int TWPartitionManager::Fstab_Processed(void) {
1931 return Partitions.size();
1932}
Dees_Troyd93bda52013-07-03 19:55:19 +00001933
1934void TWPartitionManager::Output_Storage_Fstab(void) {
1935 std::vector<TWPartition*>::iterator iter;
1936 char storage_partition[255];
1937 string Temp;
1938 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
1939
1940 if (fp == NULL) {
1941 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
1942 return;
1943 }
1944
1945 // Iterate through all partitions
1946 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1947 if ((*iter)->Is_Storage) {
1948 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
1949 strcpy(storage_partition, Temp.c_str());
1950 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
1951 }
1952 }
1953 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001954}