blob: 5c325773a1f781182c6417152070cc863bb747cb [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/* Partition Management classes for TWRP
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 *
17 * The code was written from scratch by Dees_Troy dees_troy at
18 * yahoo
19 *
20 * Copyright (c) 2012
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/vfs.h>
28#include <unistd.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040029#include <vector>
Dees_Troy63c8df72012-09-10 14:02:05 -040030#include <dirent.h>
31#include <time.h>
Dees_Troy8170a922012-09-18 15:40:25 -040032#include <errno.h>
33#include <fcntl.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050034#include <iostream>
35#include <iomanip>
Dees_Troy51a0e822012-09-05 15:24:24 -040036#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040039#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "twrp-functions.hpp"
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040041#include "fixPermissions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050042#include "twrpDigest.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040043
Dees_Troy5bf43922012-09-07 16:07:55 -040044#ifdef TW_INCLUDE_CRYPTO
45 #ifdef TW_INCLUDE_JB_CRYPTO
46 #include "crypto/jb/cryptfs.h"
47 #else
48 #include "crypto/ics/cryptfs.h"
49 #endif
50 #include "cutils/properties.h"
51#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040052
53int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040054 FILE *fstabFile;
55 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -050056 bool Found_Settings_Storage = false;
Dees_Troy5bf43922012-09-07 16:07:55 -040057
58 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
59 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000060 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040061 return false;
62 }
63
64 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
65 if (fstab_line[0] != '/')
66 continue;
67
Dees_Troy2a923582012-09-20 12:13:34 -040068 if (fstab_line[strlen(fstab_line) - 1] != '\n')
69 fstab_line[strlen(fstab_line)] = '\n';
70
Dees_Troy5bf43922012-09-07 16:07:55 -040071 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040072 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040073 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040074
Dees_Troy5bf43922012-09-07 16:07:55 -040075 if (partition->Process_Fstab_Line(line, Display_Error)) {
Dees_Troya13d74f2013-03-24 08:54:55 -050076 if (!Found_Settings_Storage && partition->Is_Settings_Storage) {
77 Found_Settings_Storage = true;
78 Partitions.push_back(partition);
79 DataManager::SetValue("tw_settings_path", partition->Storage_Path);
80 DataManager::SetValue("tw_storage_path", partition->Storage_Path);
Dees_Troy2673cec2013-04-02 20:22:16 +000081 LOGINFO("Settings storage is '%s'\n", partition->Storage_Path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -050082 } else {
83 partition->Is_Settings_Storage = false;
84 Partitions.push_back(partition);
85 }
Dees_Troy5bf43922012-09-07 16:07:55 -040086 } else {
87 delete partition;
88 }
89 }
90 fclose(fstabFile);
Dees_Troya13d74f2013-03-24 08:54:55 -050091 if (!Found_Settings_Storage) {
92 std::vector<TWPartition*>::iterator iter;
93 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
94 if ((*iter)->Is_Storage) {
95 (*iter)->Is_Settings_Storage = true;
96 Found_Settings_Storage = true;
97 DataManager::SetValue("tw_settings_path", (*iter)->Storage_Path);
98 DataManager::SetValue("tw_storage_path", (*iter)->Storage_Path);
Dees_Troy2673cec2013-04-02 20:22:16 +000099 LOGINFO("Settings storage is '%s'\n", (*iter)->Storage_Path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500100 break;
101 }
102 }
103 if (!Found_Settings_Storage)
Dees_Troy2673cec2013-04-02 20:22:16 +0000104 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500105 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400106 if (!Write_Fstab()) {
107 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000108 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400109 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000110 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400111 }
Dees_Troy51127312012-09-08 13:08:49 -0400112 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400113 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400114 return true;
115}
116
117int TWPartitionManager::Write_Fstab(void) {
118 FILE *fp;
119 std::vector<TWPartition*>::iterator iter;
120 string Line;
121
122 fp = fopen("/etc/fstab", "w");
123 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000124 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400125 return false;
126 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400127 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400128 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400129 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400130 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000131 }
132 // Handle subpartition tracking
133 if ((*iter)->Is_SubPartition) {
134 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
135 if (ParentPartition)
136 ParentPartition->Has_SubPartition = true;
137 else
138 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 -0400139 }
140 }
141 fclose(fp);
142 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400143}
144
Dees_Troy8170a922012-09-18 15:40:25 -0400145void TWPartitionManager::Output_Partition_Logging(void) {
146 std::vector<TWPartition*>::iterator iter;
147
148 printf("\n\nPartition Logs:\n");
149 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
150 Output_Partition((*iter));
151}
152
153void TWPartitionManager::Output_Partition(TWPartition* Part) {
154 unsigned long long mb = 1048576;
155
Gary Peck004d48b2012-11-21 16:28:18 -0800156 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 -0400157 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800158 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 -0400159 }
Gary Peck004d48b2012-11-21 16:28:18 -0800160 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500161 if (Part->Can_Be_Mounted)
162 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800163 if (Part->Can_Be_Wiped)
164 printf("Can_Be_Wiped ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500165 if (Part->Can_Be_Backed_Up)
166 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800167 if (Part->Wipe_During_Factory_Reset)
168 printf("Wipe_During_Factory_Reset ");
169 if (Part->Wipe_Available_in_GUI)
170 printf("Wipe_Available_in_GUI ");
171 if (Part->Is_SubPartition)
172 printf("Is_SubPartition ");
173 if (Part->Has_SubPartition)
174 printf("Has_SubPartition ");
175 if (Part->Removable)
176 printf("Removable ");
177 if (Part->Is_Present)
178 printf("IsPresent ");
179 if (Part->Can_Be_Encrypted)
180 printf("Can_Be_Encrypted ");
181 if (Part->Is_Encrypted)
182 printf("Is_Encrypted ");
183 if (Part->Is_Decrypted)
184 printf("Is_Decrypted ");
185 if (Part->Has_Data_Media)
186 printf("Has_Data_Media ");
187 if (Part->Has_Android_Secure)
188 printf("Has_Android_Secure ");
189 if (Part->Is_Storage)
190 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500191 if (Part->Is_Settings_Storage)
192 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000193 if (Part->Ignore_Blkid)
194 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000195 if (Part->Retain_Layout_Version)
196 printf("Retain_Layout_Version ");
Gary Peck004d48b2012-11-21 16:28:18 -0800197 printf("\n");
198 if (!Part->SubPartition_Of.empty())
199 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
200 if (!Part->Symlink_Path.empty())
201 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
202 if (!Part->Symlink_Mount_Point.empty())
203 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
204 if (!Part->Primary_Block_Device.empty())
205 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
206 if (!Part->Alternate_Block_Device.empty())
207 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
208 if (!Part->Decrypted_Block_Device.empty())
209 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
210 if (Part->Length != 0)
211 printf(" Length: %i\n", Part->Length);
212 if (!Part->Display_Name.empty())
213 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500214 if (!Part->Storage_Name.empty())
215 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800216 if (!Part->Backup_Path.empty())
217 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
218 if (!Part->Backup_Name.empty())
219 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500220 if (!Part->Backup_Display_Name.empty())
221 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800222 if (!Part->Backup_FileName.empty())
223 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
224 if (!Part->Storage_Path.empty())
225 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
226 if (!Part->Current_File_System.empty())
227 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
228 if (!Part->Fstab_File_System.empty())
229 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
230 if (Part->Format_Block_Size != 0)
231 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400232 if (!Part->MTD_Name.empty())
233 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400234 string back_meth = Part->Backup_Method_By_Name();
235 printf(" Backup_Method: %s\n\n", back_meth.c_str());
236}
237
Dees_Troy51a0e822012-09-05 15:24:24 -0400238int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400239 std::vector<TWPartition*>::iterator iter;
240 int ret = false;
241 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400242 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400243
Dees_Troy43d8b002012-09-17 16:00:01 -0400244 if (Local_Path == "/tmp")
245 return true;
246
Dees_Troy5bf43922012-09-07 16:07:55 -0400247 // Iterate through all partitions
248 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400249 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400250 ret = (*iter)->Mount(Display_Error);
251 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400252 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400253 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400254 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400255 }
256 if (found) {
257 return ret;
258 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000259 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400260 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000261 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400262 }
263 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400264}
265
266int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400267 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400268
Dees_Troy51127312012-09-08 13:08:49 -0400269 if (Part) {
270 if (Part->Has_SubPartition) {
271 std::vector<TWPartition*>::iterator subpart;
272
273 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
274 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
275 (*subpart)->Mount(Display_Error);
276 }
277 return Part->Mount(Display_Error);
278 } else
279 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400280 }
281 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000282 LOGERR("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400283 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000284 LOGINFO("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400285 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400286}
287
288int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400289 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400290
Dees_Troy51127312012-09-08 13:08:49 -0400291 if (Part) {
292 if (Part->Has_SubPartition) {
293 std::vector<TWPartition*>::iterator subpart;
294
295 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
296 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
297 (*subpart)->Mount(Display_Error);
298 }
299 return Part->Mount(Display_Error);
300 } else
301 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400302 }
303 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000304 LOGERR("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400305 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000306 LOGINFO("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400307 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400308}
309
310int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400311 std::vector<TWPartition*>::iterator iter;
312 int ret = false;
313 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400314 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400315
316 // Iterate through all partitions
317 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400318 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400319 ret = (*iter)->UnMount(Display_Error);
320 found = true;
321 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
322 (*iter)->UnMount(Display_Error);
323 }
324 }
325 if (found) {
326 return ret;
327 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000328 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400329 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000330 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400331 }
332 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400333}
334
335int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400336 TWPartition* Part = Find_Partition_By_Block(Block);
337
338 if (Part) {
339 if (Part->Has_SubPartition) {
340 std::vector<TWPartition*>::iterator subpart;
341
342 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
343 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
344 (*subpart)->UnMount(Display_Error);
345 }
346 return Part->UnMount(Display_Error);
347 } else
348 return Part->UnMount(Display_Error);
349 }
350 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000351 LOGERR("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400352 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000353 LOGINFO("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400354 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400355}
356
357int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400358 TWPartition* Part = Find_Partition_By_Name(Name);
359
360 if (Part) {
361 if (Part->Has_SubPartition) {
362 std::vector<TWPartition*>::iterator subpart;
363
364 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
365 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
366 (*subpart)->UnMount(Display_Error);
367 }
368 return Part->UnMount(Display_Error);
369 } else
370 return Part->UnMount(Display_Error);
371 }
372 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000373 LOGERR("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400374 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000375 LOGINFO("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400376 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400377}
378
379int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400380 TWPartition* Part = Find_Partition_By_Path(Path);
381
382 if (Part)
383 return Part->Is_Mounted();
384 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000385 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400386 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400387}
388
389int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400390 TWPartition* Part = Find_Partition_By_Block(Block);
391
392 if (Part)
393 return Part->Is_Mounted();
394 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000395 LOGINFO("Is_Mounted: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400396 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400397}
398
399int TWPartitionManager::Is_Mounted_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400400 TWPartition* Part = Find_Partition_By_Name(Name);
401
402 if (Part)
403 return Part->Is_Mounted();
404 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000405 LOGINFO("Is_Mounted: 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
Dees_Troy5bf43922012-09-07 16:07:55 -0400409int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400410 string current_storage_path = DataManager::GetCurrentStoragePath();
411
412 if (Mount_By_Path(current_storage_path, Display_Error)) {
413 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
414 if (FreeStorage)
415 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
416 return true;
417 }
418 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400419}
420
Dees_Troy5bf43922012-09-07 16:07:55 -0400421int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
422 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
423}
424
425TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
426 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400427 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400428
429 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400430 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400431 return (*iter);
432 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400433 return NULL;
434}
435
Dees_Troy5bf43922012-09-07 16:07:55 -0400436TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400437 std::vector<TWPartition*>::iterator iter;
438
439 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400440 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 -0400441 return (*iter);
442 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400443 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400444}
445
446TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400447 std::vector<TWPartition*>::iterator iter;
448
449 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
450 if ((*iter)->Display_Name == Name)
451 return (*iter);
452 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400453 return NULL;
454}
Dees_Troy51a0e822012-09-05 15:24:24 -0400455
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400456int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
457 // Check the backup name to ensure that it is the correct size and contains only valid characters
458 // and that a backup with that name doesn't already exist
459 char backup_name[MAX_BACKUP_NAME_LEN];
460 char backup_loc[255], tw_image_dir[255];
461 int copy_size;
462 int index, cur_char;
463 string Backup_Name, Backup_Loc;
464
465 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
466 copy_size = Backup_Name.size();
467 // Check size
468 if (copy_size > MAX_BACKUP_NAME_LEN) {
469 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000470 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400471 return -2;
472 }
473
474 // Check each character
475 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500476 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400477 return 0; // A "0" (zero) means to use the current timestamp for the backup name
478 for (index=0; index<copy_size; index++) {
479 cur_char = (int)backup_name[index];
480 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) {
481 // These are valid characters
482 // Numbers
483 // Upper case letters
484 // Lower case letters
485 // Space
486 // and -_.{}[]
487 } else {
488 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000489 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400490 return -3;
491 }
492 }
493
494 // Check to make sure that a backup with this name doesn't already exist
495 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
496 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500497 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400498 if (TWFunc::Path_Exists(tw_image_dir)) {
499 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000500 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400501 return -4;
502 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400503 // No problems found, return 0
504 return 0;
505}
506
Dees_Troy43d8b002012-09-17 16:00:01 -0400507bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
508{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500509 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400510 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500511 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500512 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400513
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500514 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400515 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400516
Dees_Troyb46a6842012-09-25 11:06:46 -0400517 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000518 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400519
520 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500521 md5sum.setfn(Backup_Folder + Backup_Filename);
522 if (md5sum.computeMD5() == 0)
523 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000524 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500525 else
526 return -1;
527 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000528 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400529 } else {
530 char filename[512];
531 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500532 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400533 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500534 strfn = filename;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400535 while (TWFunc::Path_Exists(filename) == true) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400536 md5sum.setfn(filename);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500537 if (md5sum.computeMD5() == 0) {
538 if (md5sum.write_md5digest() != 0)
539 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000540 gui_print(" * MD5 Error.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500541 return false;
542 }
543 }
544 else {
545 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400546 }
547 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400548 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500549 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400550 }
551 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000552 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400553 return false;
554 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000555 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400556 }
557 return true;
558}
559
Dees_Troy093b7642012-09-21 15:59:38 -0400560bool 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 -0400561 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500562 int img_bps;
563 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400564 unsigned long total_time, remain_time, section_time;
565 int use_compression, backup_time;
566 float pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400567
568 if (Part == NULL)
569 return true;
570
Dees_Troy093b7642012-09-21 15:59:38 -0400571 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
572
573 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
574 if (use_compression)
575 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
576 else
577 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
578
579 // We know the speed for both, how far into the whole backup are we, based on time
580 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
581 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
582
583 pos = (total_time - remain_time) / (float) total_time;
Dees_Troy2673cec2013-04-02 20:22:16 +0000584 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400585
Dees_Troy2673cec2013-04-02 20:22:16 +0000586 LOGINFO("Estimated Total time: %lu Estimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400587
588 // And get the time
589 if (Part->Backup_Method == 1)
590 section_time = Part->Backup_Size / file_bps;
591 else
592 section_time = Part->Backup_Size / img_bps;
593
594 // Set the position
595 pos = section_time / (float) total_time;
Dees_Troy2673cec2013-04-02 20:22:16 +0000596 DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400597
Dees_Troy43d8b002012-09-17 16:00:01 -0400598 time(&start);
599
600 if (Part->Backup(Backup_Folder)) {
Dees_Troy8170a922012-09-18 15:40:25 -0400601 if (Part->Has_SubPartition) {
602 std::vector<TWPartition*>::iterator subpart;
603
604 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500605 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Dees_Troy8170a922012-09-18 15:40:25 -0400606 if (!(*subpart)->Backup(Backup_Folder))
607 return false;
608 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
609 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400610 if (Part->Backup_Method == 1) {
611 *file_bytes_remaining -= (*subpart)->Backup_Size;
612 } else {
613 *img_bytes_remaining -= (*subpart)->Backup_Size;
614 }
Dees_Troy8170a922012-09-18 15:40:25 -0400615 }
616 }
617 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400618 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400619 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000620 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400621 if (Part->Backup_Method == 1) {
622 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400623 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400624 } else {
625 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400626 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400627 }
628 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
629 } else {
630 return false;
631 }
632}
633
634int TWPartitionManager::Run_Backup(void) {
635 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500636 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400637 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 -0400638 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500639 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400640 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400641 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400642 struct tm *t;
643 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500644 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400645 seconds = time(0);
646 t = localtime(&seconds);
647
648 time(&total_start);
649
650 Update_System_Details();
651
652 if (!Mount_Current_Storage(true))
653 return false;
654
655 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400656 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400657 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400658 else
659 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400660
Dees_Troy43d8b002012-09-17 16:00:01 -0400661 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
662 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400663 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name.empty()) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400664 char timestamp[255];
665 sprintf(timestamp,"%04d-%02d-%02d--%02d-%02d-%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
666 Backup_Name = timestamp;
667 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000668 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400669 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000670 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400671
Dees_Troy2673cec2013-04-02 20:22:16 +0000672 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500673 DataManager::GetValue("tw_backup_list", Backup_List);
674 if (!Backup_List.empty()) {
675 end_pos = Backup_List.find(";", start_pos);
676 while (end_pos != string::npos && start_pos < Backup_List.size()) {
677 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
678 backup_part = Find_Partition_By_Path(backup_path);
679 if (backup_part != NULL) {
680 partition_count++;
681 if (backup_part->Backup_Method == 1)
682 file_bytes += backup_part->Backup_Size;
683 else
684 img_bytes += backup_part->Backup_Size;
685 if (backup_part->Has_SubPartition) {
686 std::vector<TWPartition*>::iterator subpart;
687
688 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
689 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == backup_part->Mount_Point) {
690 partition_count++;
691 if ((*subpart)->Backup_Method == 1)
692 file_bytes += (*subpart)->Backup_Size;
693 else
694 img_bytes += (*subpart)->Backup_Size;
695 }
696 }
Dees_Troy8170a922012-09-18 15:40:25 -0400697 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500698 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000699 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400700 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500701 start_pos = end_pos + 1;
702 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400703 }
704 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400705
706 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000707 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400708 return false;
709 }
710 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000711 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
712 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400713 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
714 if (storage != NULL) {
715 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000716 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400717 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000718 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400719 return false;
720 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000721 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400722 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000723 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400724 return false;
725 }
726 img_bytes_remaining = img_bytes;
727 file_bytes_remaining = file_bytes;
728
Dees_Troy2673cec2013-04-02 20:22:16 +0000729 gui_print("\n[BACKUP STARTED]\n");
730 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000731 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000732 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000733 return false;
734 }
735
Dees_Troy2673cec2013-04-02 20:22:16 +0000736 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400737
Dees_Troya13d74f2013-03-24 08:54:55 -0500738 start_pos = 0;
739 end_pos = Backup_List.find(";", start_pos);
740 while (end_pos != string::npos && start_pos < Backup_List.size()) {
741 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
742 backup_part = Find_Partition_By_Path(backup_path);
743 if (backup_part != NULL) {
744 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
745 return false;
746 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000747 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500748 }
749 start_pos = end_pos + 1;
750 end_pos = Backup_List.find(";", start_pos);
751 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400752
753 // Average BPS
754 if (img_time == 0)
755 img_time = 1;
756 if (file_time == 0)
757 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400758 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500759 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400760
Dees_Troy2673cec2013-04-02 20:22:16 +0000761 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
762 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400763
764 time(&total_stop);
765 int total_time = (int) difftime(total_stop, total_start);
766 unsigned long long actual_backup_size = TWFunc::Get_Folder_Size(Full_Backup_Path, true);
767 actual_backup_size /= (1024LLU * 1024LLU);
768
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500769 int prev_img_bps, use_compression;
770 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400771 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
772 img_bps += (prev_img_bps * 4);
773 img_bps /= 5;
774
775 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
776 if (use_compression)
777 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
778 else
779 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
780 file_bps += (prev_file_bps * 4);
781 file_bps /= 5;
782
783 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
784 if (use_compression)
785 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
786 else
787 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
788
Dees_Troy2673cec2013-04-02 20:22:16 +0000789 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400790 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400791 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +0000792 gui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000793 string backup_log = Full_Backup_Path + "recovery.log";
794 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
795 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400796}
797
Dees_Troy093b7642012-09-21 15:59:38 -0400798bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400799 time_t Start, Stop;
800 time(&Start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000801 DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400802 if (!Part->Restore(Restore_Name))
803 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400804 if (Part->Has_SubPartition) {
805 std::vector<TWPartition*>::iterator subpart;
806
807 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
808 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
809 if (!(*subpart)->Restore(Restore_Name))
810 return false;
811 }
812 }
813 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400814 time(&Stop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000815 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 -0400816 return true;
817}
818
Dees_Troy51a0e822012-09-05 15:24:24 -0400819int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400820 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500821 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400822 time_t rStart, rStop;
823 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500824 string Restore_List, restore_path;
825 size_t start_pos = 0, end_pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400826
Dees_Troy2673cec2013-04-02 20:22:16 +0000827 gui_print("\n[RESTORE STARTED]\n\n");
828 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400829
Dees_Troy4a2a1262012-09-18 09:33:47 -0400830 if (!Mount_Current_Storage(true))
831 return false;
832
833 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500834 if (check_md5 > 0) {
835 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
836 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000837 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500838 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000839 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500840 }
841 DataManager::GetValue("tw_restore_selected", Restore_List);
842 if (!Restore_List.empty()) {
843 end_pos = Restore_List.find(";", start_pos);
844 while (end_pos != string::npos && start_pos < Restore_List.size()) {
845 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
846 restore_part = Find_Partition_By_Path(restore_path);
847 if (restore_part != NULL) {
848 partition_count++;
849 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
850 return false;
851 if (restore_part->Has_SubPartition) {
852 std::vector<TWPartition*>::iterator subpart;
853
854 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
855 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
856 if (!(*subpart)->Check_MD5(Restore_Name))
857 return false;
858 }
859 }
860 }
861 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000862 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500863 }
864 start_pos = end_pos + 1;
865 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400866 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400867 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400868
869 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000870 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400871 return false;
872 }
873
Dees_Troy2673cec2013-04-02 20:22:16 +0000874 gui_print("Restoring %i partitions...\n", partition_count);
875 DataManager::SetProgress(0.0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500876 start_pos = 0;
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 (!Restore_Partition(restore_part, Restore_Name, partition_count))
885 return false;
886 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000887 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500888 }
889 start_pos = end_pos + 1;
890 end_pos = Restore_List.find(";", start_pos);
891 }
892 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400893
Dees_Troyb46a6842012-09-25 11:06:46 -0400894 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400895 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400896 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400897 time(&rStop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000898 gui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -0400899 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400900}
901
902void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400903 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500904 string Restore_List;
Dees_Troy63c8df72012-09-10 14:02:05 -0400905 bool get_date = true;
906
907 DIR* d;
908 d = opendir(Restore_Name.c_str());
909 if (d == NULL)
910 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000911 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400912 return;
913 }
914
915 struct dirent* de;
916 while ((de = readdir(d)) != NULL)
917 {
918 // Strip off three components
919 char str[256];
920 char* label;
921 char* fstype = NULL;
922 char* extn = NULL;
923 char* ptr;
924
925 strcpy(str, de->d_name);
926 if (strlen(str) <= 2)
927 continue;
928
929 if (get_date) {
930 char file_path[255];
931 struct stat st;
932
933 strcpy(file_path, Restore_Name.c_str());
934 strcat(file_path, "/");
935 strcat(file_path, str);
936 stat(file_path, &st);
937 string backup_date = ctime((const time_t*)(&st.st_mtime));
938 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
939 get_date = false;
940 }
941
942 label = str;
943 ptr = label;
944 while (*ptr && *ptr != '.') ptr++;
945 if (*ptr == '.')
946 {
947 *ptr = 0x00;
948 ptr++;
949 fstype = ptr;
950 }
951 while (*ptr && *ptr != '.') ptr++;
952 if (*ptr == '.')
953 {
954 *ptr = 0x00;
955 ptr++;
956 extn = ptr;
957 }
958
Dees_Troya13d74f2013-03-24 08:54:55 -0500959 if (strcmp(fstype, "log") == 0) continue;
960 int extnlength = strlen(extn);
961 if (extn == NULL || (extnlength != 3 && extnlength != 6)) continue;
962 if (extnlength == 3 && strncmp(extn, "win", 3) != 0) continue;
963 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -0400964
965 TWPartition* Part = Find_Partition_By_Path(label);
966 if (Part == NULL)
967 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000968 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -0400969 continue;
970 }
971
972 Part->Backup_FileName = de->d_name;
973 if (strlen(extn) > 3) {
974 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
975 }
976
Dees_Troya13d74f2013-03-24 08:54:55 -0500977 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -0400978 }
979 closedir(d);
980
Dees_Troya13d74f2013-03-24 08:54:55 -0500981 // Set the final value
982 DataManager::SetValue("tw_restore_list", Restore_List);
983 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -0400984 return;
985}
986
987int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400988 std::vector<TWPartition*>::iterator iter;
989 int ret = false;
990 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400991 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -0400992
993 // Iterate through all partitions
994 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400995 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -0400996 if (Path == "/and-sec")
997 ret = (*iter)->Wipe_AndSec();
998 else
999 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001000 found = true;
1001 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1002 (*iter)->Wipe();
1003 }
1004 }
1005 if (found) {
1006 return ret;
1007 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001008 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001009 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001010}
1011
1012int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001013 TWPartition* Part = Find_Partition_By_Block(Block);
1014
1015 if (Part) {
1016 if (Part->Has_SubPartition) {
1017 std::vector<TWPartition*>::iterator subpart;
1018
1019 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1020 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1021 (*subpart)->Wipe();
1022 }
1023 return Part->Wipe();
1024 } else
1025 return Part->Wipe();
1026 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001027 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001028 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001029}
1030
1031int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001032 TWPartition* Part = Find_Partition_By_Name(Name);
1033
1034 if (Part) {
1035 if (Part->Has_SubPartition) {
1036 std::vector<TWPartition*>::iterator subpart;
1037
1038 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1039 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1040 (*subpart)->Wipe();
1041 }
1042 return Part->Wipe();
1043 } else
1044 return Part->Wipe();
1045 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001046 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001047 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001048}
1049
1050int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001051 std::vector<TWPartition*>::iterator iter;
1052 int ret = true;
1053
1054 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001055 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001056 if (!(*iter)->Wipe())
1057 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001058 } else if ((*iter)->Has_Android_Secure) {
1059 if (!(*iter)->Wipe_AndSec())
1060 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001061 }
1062 }
1063 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001064}
1065
Dees_Troy38bd7602012-09-14 13:33:53 -04001066int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1067 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001068 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001069
1070 if (!Mount_By_Path("/data", true))
1071 return false;
1072
1073 if (!Mount_By_Path("/cache", true))
1074 return false;
1075
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001076 dir.push_back("/data/dalvik-cache");
1077 dir.push_back("/cache/dalvik-cache");
1078 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001079 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001080 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001081 if (stat(dir.at(i).c_str(), &st) == 0) {
1082 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001083 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001084 }
1085 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001086 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
1087 if (sdext != NULL) {
1088 if (sdext->Is_Present && sdext->Mount(false)) {
1089 if (stat("/sd-ext/dalvik-cache", &st) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001090 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001091 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001092 }
1093 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001094 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001095 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001096 return true;
1097}
1098
1099int TWPartitionManager::Wipe_Rotate_Data(void) {
1100 if (!Mount_By_Path("/data", true))
1101 return false;
1102
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001103 unlink("/data/misc/akmd*");
1104 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001105 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001106 return true;
1107}
1108
1109int TWPartitionManager::Wipe_Battery_Stats(void) {
1110 struct stat st;
1111
1112 if (!Mount_By_Path("/data", true))
1113 return false;
1114
1115 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001116 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001117 } else {
1118 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001119 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001120 }
1121 return true;
1122}
1123
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001124int TWPartitionManager::Wipe_Android_Secure(void) {
1125 std::vector<TWPartition*>::iterator iter;
1126 int ret = false;
1127 bool found = false;
1128
1129 // Iterate through all partitions
1130 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1131 if ((*iter)->Has_Android_Secure) {
1132 ret = (*iter)->Wipe_AndSec();
1133 found = true;
1134 }
1135 }
1136 if (found) {
1137 return ret;
1138 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001139 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001140 }
1141 return false;
1142}
1143
Dees_Troy38bd7602012-09-14 13:33:53 -04001144int TWPartitionManager::Format_Data(void) {
1145 TWPartition* dat = Find_Partition_By_Path("/data");
1146
1147 if (dat != NULL) {
1148 if (!dat->UnMount(true))
1149 return false;
1150
1151 return dat->Wipe_Encryption();
1152 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001153 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001154 return false;
1155 }
1156 return false;
1157}
1158
1159int TWPartitionManager::Wipe_Media_From_Data(void) {
1160 TWPartition* dat = Find_Partition_By_Path("/data");
1161
1162 if (dat != NULL) {
1163 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001164 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001165 return false;
1166 }
1167 if (!dat->Mount(true))
1168 return false;
1169
Dees_Troy2673cec2013-04-02 20:22:16 +00001170 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001171 TWFunc::removeDir("/data/media", false);
1172 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
1173 return -1;
Dees_Troy38bd7602012-09-14 13:33:53 -04001174 if (dat->Has_Data_Media) {
1175 dat->Recreate_Media_Folder();
1176 }
1177 return true;
1178 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001179 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001180 return false;
1181 }
1182 return false;
1183}
1184
Dees_Troy51a0e822012-09-05 15:24:24 -04001185void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001186 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001187 return;
1188}
1189
1190void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001191 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001192 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001193
Dees_Troy2673cec2013-04-02 20:22:16 +00001194 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001195 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001196 if ((*iter)->Can_Be_Mounted) {
1197 (*iter)->Update_Size(true);
1198 if ((*iter)->Mount_Point == "/system") {
1199 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1200 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1201 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1202 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1203 } else if ((*iter)->Mount_Point == "/cache") {
1204 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1205 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1206 } else if ((*iter)->Mount_Point == "/sd-ext") {
1207 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1208 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1209 if ((*iter)->Backup_Size == 0) {
1210 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1211 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1212 } else
1213 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001214 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001215 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001216 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001217 if ((*iter)->Backup_Size == 0) {
1218 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1219 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1220 } else
1221 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001222 } else if ((*iter)->Mount_Point == "/boot") {
1223 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1224 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1225 if ((*iter)->Backup_Size == 0) {
1226 DataManager::SetValue("tw_has_boot_partition", 0);
1227 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1228 } else
1229 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001230 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001231#ifdef SP1_NAME
1232 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1233 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1234 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1235 }
1236#endif
1237#ifdef SP2_NAME
1238 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1239 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1240 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1241 }
1242#endif
1243#ifdef SP3_NAME
1244 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1245 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1246 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1247 }
1248#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001249 } else {
1250 // Handle unmountable partitions in case we reset defaults
1251 if ((*iter)->Mount_Point == "/boot") {
1252 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1253 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1254 if ((*iter)->Backup_Size == 0) {
1255 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1256 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1257 } else
1258 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1259 } else if ((*iter)->Mount_Point == "/recovery") {
1260 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1261 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1262 if ((*iter)->Backup_Size == 0) {
1263 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1264 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1265 } else
1266 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001267 } else if ((*iter)->Mount_Point == "/data") {
1268 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001269 }
1270#ifdef SP1_NAME
1271 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1272 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1273 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1274 }
1275#endif
1276#ifdef SP2_NAME
1277 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1278 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1279 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1280 }
1281#endif
1282#ifdef SP3_NAME
1283 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1284 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1285 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1286 }
1287#endif
Dees_Troy51127312012-09-08 13:08:49 -04001288 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001289 }
Dees_Troy51127312012-09-08 13:08:49 -04001290 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1291 string current_storage_path = DataManager::GetCurrentStoragePath();
1292 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001293 if (FreeStorage != NULL) {
1294 // Attempt to mount storage
1295 if (!FreeStorage->Mount(false)) {
1296 // We couldn't mount storage... check to see if we have dual storage
1297 int has_dual_storage;
1298 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1299 if (has_dual_storage == 1) {
1300 // We have dual storage, see if we're using the internal storage that should always be present
1301 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001302 if (!FreeStorage->Is_Encrypted) {
1303 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001304 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001305 }
Dees_Troy8170a922012-09-18 15:40:25 -04001306 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1307 } else {
1308 // We were using external, flip to internal
1309 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1310 current_storage_path = DataManager::GetCurrentStoragePath();
1311 FreeStorage = Find_Partition_By_Path(current_storage_path);
1312 if (FreeStorage != NULL) {
1313 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1314 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001315 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001316 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1317 }
1318 }
1319 } else {
1320 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001321 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001322 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1323 }
1324 } else {
1325 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1326 }
1327 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001328 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001329 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001330 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001331 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001332 return;
1333}
1334
1335int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001336#ifdef TW_INCLUDE_CRYPTO
1337 int ret_val, password_len;
1338 char crypto_blkdev[255], cPassword[255];
1339 size_t result;
1340
1341 property_set("ro.crypto.state", "encrypted");
1342#ifdef TW_INCLUDE_JB_CRYPTO
1343 // No extra flags needed
1344#else
1345 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1346 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1347 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1348 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1349 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1350 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001351
1352#ifdef CRYPTO_SD_FS_TYPE
1353 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1354 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1355 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001356#endif
a39552696ff55ce2013-01-08 16:14:56 +00001357
1358 property_set("rw.km_fips_status", "ready");
1359
1360#endif
1361
1362 // some samsung devices store "footer" on efs partition
1363 TWPartition *efs = Find_Partition_By_Path("/efs");
1364 if(efs && !efs->Is_Mounted())
1365 efs->Mount(false);
1366 else
1367 efs = 0;
1368#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001369#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001370 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001371 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001372 property_set("ro.crypto.external_encrypted", "1");
1373 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1374 } else {
1375 property_set("ro.crypto.external_encrypted", "0");
1376 }
1377#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001378#endif
a39552696ff55ce2013-01-08 16:14:56 +00001379
Dees_Troy5bf43922012-09-07 16:07:55 -04001380 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001381 int pwret = cryptfs_check_passwd(cPassword);
1382
1383 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001384 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001385 return -1;
1386 }
a39552696ff55ce2013-01-08 16:14:56 +00001387
1388 if(efs)
1389 efs->UnMount(false);
1390
Dees_Troy5bf43922012-09-07 16:07:55 -04001391 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1392 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001393 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001394 } else {
1395 TWPartition* dat = Find_Partition_By_Path("/data");
1396 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001397 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001398 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1399 dat->Is_Decrypted = true;
1400 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001401 dat->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001402 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001403
1404#ifdef CRYPTO_SD_FS_TYPE
1405 char crypto_blkdev_sd[255];
1406 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1407 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001408 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001409 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001410 emmc->Is_Decrypted = true;
1411 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1412 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001413 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001414 }
a39552696ff55ce2013-01-08 16:14:56 +00001415#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001416#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001417#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001418 char is_external_decrypted[255];
1419 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1420 if (strcmp(is_external_decrypted, "1") == 0) {
1421 sdcard->Is_Decrypted = true;
1422 sdcard->EcryptFS_Password = Password;
1423 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001424 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1425 MetaEcfsFile += "/.MetaEcfsFile";
1426 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1427 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1428 sdcard->UnMount(false);
1429 sdcard->Mount(false);
1430 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001431 } else {
1432 sdcard->Is_Decrypted = false;
1433 sdcard->Decrypted_Block_Device = "";
1434 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001435#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001436#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001437
Dees_Troy5bf43922012-09-07 16:07:55 -04001438 // Sleep for a bit so that the device will be ready
1439 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001440#ifdef RECOVERY_SDCARD_ON_DATA
1441 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1442 dat->Storage_Path = "/data/media/0";
1443 dat->Symlink_Path = dat->Storage_Path;
1444 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
1445 dat->UnMount(false);
1446 DataManager::SetBackupFolder();
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001447 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001448 }
1449#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001450 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001451 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001452 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001453 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001454 }
1455 return 0;
1456#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001457 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001458 return -1;
1459#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001460 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001461}
1462
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001463int TWPartitionManager::Fix_Permissions(void) {
1464 int result = 0;
1465 if (!Mount_By_Path("/data", true))
1466 return false;
1467
1468 if (!Mount_By_Path("/system", true))
1469 return false;
1470
1471 Mount_By_Path("/sd-ext", false);
1472
1473 fixPermissions perms;
1474 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001475 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001476 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001477 return result;
1478}
1479
Dees_Troyd21618c2012-10-14 18:48:49 -04001480int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001481 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1482
1483 if (Part == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001484 LOGERR("Unable to locate volume information for USB storage mode.");
Dees_Troyd21618c2012-10-14 18:48:49 -04001485 return false;
1486 }
1487 if (!Part->UnMount(true))
1488 return false;
1489
Dees_Troy2673cec2013-04-02 20:22:16 +00001490 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1491 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001492 return false;
1493 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001494 return true;
1495}
1496
Dees_Troy8170a922012-09-18 15:40:25 -04001497int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001498 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001499 char lun_file[255];
Dees_Troy8170a922012-09-18 15:40:25 -04001500 string ext_path;
Dees_Troyd21618c2012-10-14 18:48:49 -04001501 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001502
1503 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1504 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1505 if (has_dual == 1 && has_data_media == 0) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001506 string Lun_File_str = CUSTOM_LUN_FILE;
1507 size_t found = Lun_File_str.find("%");
1508 if (found != string::npos) {
1509 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1510 if (TWFunc::Path_Exists(lun_file))
1511 has_multiple_lun = true;
1512 }
1513 if (!has_multiple_lun) {
Dees_Troyf4ca6122012-10-13 22:17:20 -04001514 // Device doesn't have multiple lun files, mount current storage
Dees_Troyf4ca6122012-10-13 22:17:20 -04001515 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001516 return Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001517 } else {
1518 // Device has multiple lun files
Dees_Troyf4ca6122012-10-13 22:17:20 -04001519 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001520 if (!Open_Lun_File(DataManager::GetSettingsStoragePath(), lun_file))
Dees_Troyf4ca6122012-10-13 22:17:20 -04001521 return false;
Dees_Troyf4ca6122012-10-13 22:17:20 -04001522 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001523 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Dees_Troyd21618c2012-10-14 18:48:49 -04001524 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001525 }
Dees_Troy8170a922012-09-18 15:40:25 -04001526 } else {
1527 if (has_data_media == 0)
1528 ext_path = DataManager::GetCurrentStoragePath();
1529 else
1530 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001531 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001532 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001533 }
1534 return true;
1535}
1536
1537int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001538 int index, ret;
1539 char lun_file[255], ch[2] = {0, 0};
1540 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001541
1542 for (index=0; index<2; index++) {
1543 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001544 ret = TWFunc::write_file(lun_file, str);
1545 Mount_All_Storage();
1546 Update_System_Details();
1547 if (ret < 0) {
1548 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001549 }
Dees_Troy8170a922012-09-18 15:40:25 -04001550 }
Dees_Troye58d5262012-09-21 12:27:57 -04001551 Mount_All_Storage();
1552 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001553 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001554 if (ret < 0 && index == 0) {
1555 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1556 return false;
1557 } else {
1558 return true;
1559 }
Dees_Troy8170a922012-09-18 15:40:25 -04001560 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001561}
1562
1563void TWPartitionManager::Mount_All_Storage(void) {
1564 std::vector<TWPartition*>::iterator iter;
1565
1566 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1567 if ((*iter)->Is_Storage)
1568 (*iter)->Mount(false);
1569 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001570}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001571
Dees_Troyd0384ef2012-10-12 12:15:42 -04001572void TWPartitionManager::UnMount_Main_Partitions(void) {
1573 // Unmounts system and data if data is not data/media
1574 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001575 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001576
1577 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1578
1579 UnMount_By_Path("/system", true);
1580#ifndef RECOVERY_SDCARD_ON_DATA
1581 UnMount_By_Path("/data", true);
1582#endif
1583 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1584 Boot_Partition->UnMount(true);
1585}
1586
Dees_Troy9350b8d2012-09-27 12:38:38 -04001587int TWPartitionManager::Partition_SDCard(void) {
1588 char mkdir_path[255], temp[255], line[512];
1589 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1590 int ext, swap, total_size = 0, fat_size;
1591 FILE* fp;
1592
Dees_Troy2673cec2013-04-02 20:22:16 +00001593 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001594#ifdef TW_EXTERNAL_STORAGE_PATH
1595 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1596#else
1597 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1598#endif
1599 if (SDCard == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001600 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001601 return false;
1602 }
1603 if (!SDCard->UnMount(true))
1604 return false;
1605 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1606 if (SDext != NULL) {
1607 if (!SDext->UnMount(true))
1608 return false;
1609 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001610 string result;
1611 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"", result);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001612 Device = SDCard->Actual_Block_Device;
1613 // Just use the root block device
1614 Device.resize(strlen("/dev/block/mmcblkX"));
1615
1616 // Find the size of the block device:
1617 fp = fopen("/proc/partitions", "rt");
1618 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001619 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001620 return false;
1621 }
1622
1623 while (fgets(line, sizeof(line), fp) != NULL)
1624 {
1625 unsigned long major, minor, blocks;
1626 char device[512];
1627 char tmpString[64];
1628
1629 if (strlen(line) < 7 || line[0] == 'm') continue;
1630 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1631
1632 tmpdevice = "/dev/block/";
1633 tmpdevice += device;
1634 if (tmpdevice == Device) {
1635 // Adjust block size to byte size
1636 total_size = (int)(blocks * 1024ULL / 1000000LLU);
1637 break;
1638 }
1639 }
1640 fclose(fp);
1641
1642 DataManager::GetValue("tw_sdext_size", ext);
1643 DataManager::GetValue("tw_swap_size", swap);
1644 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1645 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001646 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 -04001647 memset(temp, 0, sizeof(temp));
1648 sprintf(temp, "%i", fat_size);
1649 fat_str = temp;
1650 memset(temp, 0, sizeof(temp));
1651 sprintf(temp, "%i", fat_size + ext);
1652 ext_str = temp;
1653 memset(temp, 0, sizeof(temp));
1654 sprintf(temp, "%i", fat_size + ext + swap);
1655 swap_str = temp;
1656 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001657 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001658 return false;
1659 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001660 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001661 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001662 LOGINFO("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001663 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001664 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001665 Update_System_Details();
1666 return false;
1667 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001668 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001669 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001670 LOGINFO("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001671 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001672 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001673 return false;
1674 }
1675 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001676 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001677 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001678 LOGINFO("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001679 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001680 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001681 Update_System_Details();
1682 return false;
1683 }
1684 }
1685 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001686 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001687 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001688 LOGINFO("Command is: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001689 if (TWFunc::Exec_Cmd(Command, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001690 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001691 Update_System_Details();
1692 return false;
1693 }
1694 }
1695 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1696#ifdef TW_EXTERNAL_STORAGE_PATH
1697 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1698 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1699 memset(mkdir_path, 0, sizeof(mkdir_path));
1700 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1701#else
1702 Mount_By_Path("/sdcard", 1);
1703 strcpy(mkdir_path, "/sdcard/TWRP");
1704#endif
1705 mkdir(mkdir_path, 0777);
1706 DataManager::Flush();
1707#ifdef TW_EXTERNAL_STORAGE_PATH
1708 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1709 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1710 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1711#else
1712 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1713 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1714 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1715#endif
1716 if (ext > 0) {
1717 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001718 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001719 return false;
1720 }
1721 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001722 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1723 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001724 TWFunc::Exec_Cmd(Command, result);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001725 }
1726
1727 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00001728 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001729 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001730}
Dees_Troya13d74f2013-03-24 08:54:55 -05001731
1732void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1733 std::vector<TWPartition*>::iterator iter;
1734 if (ListType == "mount") {
1735 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1736 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1737 struct PartitionList part;
1738 part.Display_Name = (*iter)->Display_Name;
1739 part.Mount_Point = (*iter)->Mount_Point;
1740 part.selected = (*iter)->Is_Mounted();
1741 Partition_List->push_back(part);
1742 }
1743 }
1744 } else if (ListType == "storage") {
1745 char free_space[255];
1746 string Current_Storage = DataManager::GetCurrentStoragePath();
1747 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1748 if ((*iter)->Is_Storage) {
1749 struct PartitionList part;
1750 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1751 part.Display_Name = (*iter)->Storage_Name + " (";
1752 part.Display_Name += free_space;
1753 part.Display_Name += "MB)";
1754 part.Mount_Point = (*iter)->Storage_Path;
1755 if ((*iter)->Storage_Path == Current_Storage)
1756 part.selected = 1;
1757 else
1758 part.selected = 0;
1759 Partition_List->push_back(part);
1760 }
1761 }
1762 } else if (ListType == "backup") {
1763 char backup_size[255];
1764 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1765 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition) {
1766 struct PartitionList part;
1767 sprintf(backup_size, "%llu", (*iter)->Backup_Size / 1024 / 1024);
1768 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1769 part.Display_Name += backup_size;
1770 part.Display_Name += "MB)";
1771 part.Mount_Point = (*iter)->Backup_Path;
1772 part.selected = 0;
1773 Partition_List->push_back(part);
1774 }
1775 }
1776 } else if (ListType == "restore") {
1777 string Restore_List, restore_path;
1778 TWPartition* restore_part = NULL;
1779
1780 DataManager::GetValue("tw_restore_list", Restore_List);
1781 if (!Restore_List.empty()) {
1782 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1783 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1784 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
1785 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL && !restore_part->Is_SubPartition) {
1786 if (restore_part->Backup_Name == "recovery") {
1787 // Don't allow restore of recovery (causes problems on some devices)
1788 } else {
1789 struct PartitionList part;
1790 part.Display_Name = restore_part->Backup_Display_Name;
1791 part.Mount_Point = restore_part->Backup_Path;
1792 part.selected = 1;
1793 Partition_List->push_back(part);
1794 }
1795 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001796 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001797 }
1798 start_pos = end_pos + 1;
1799 end_pos = Restore_List.find(";", start_pos);
1800 }
1801 }
1802 } else if (ListType == "wipe") {
1803 struct PartitionList dalvik;
1804 dalvik.Display_Name = "Dalvik Cache";
1805 dalvik.Mount_Point = "DALVIK";
1806 dalvik.selected = 0;
1807 Partition_List->push_back(dalvik);
1808 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1809 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1810 struct PartitionList part;
1811 part.Display_Name = (*iter)->Display_Name;
1812 part.Mount_Point = (*iter)->Mount_Point;
1813 part.selected = 0;
1814 Partition_List->push_back(part);
1815 }
1816 if ((*iter)->Has_Android_Secure) {
1817 struct PartitionList part;
1818 part.Display_Name = (*iter)->Backup_Display_Name;
1819 part.Mount_Point = (*iter)->Backup_Path;
1820 part.selected = 0;
1821 Partition_List->push_back(part);
1822 }
1823 }
1824 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001825 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001826 }
1827}
1828
1829int TWPartitionManager::Fstab_Processed(void) {
1830 return Partitions.size();
1831}