blob: 081487e7948d67b1acbbf0123fc8555a76d56e15 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/* Partition class 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>
Dees_Troy5bf43922012-09-07 16:07:55 -040028#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040029#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040030#include <dirent.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040031
32#include "variables.h"
33#include "common.h"
34#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040035#include "data.hpp"
36extern "C" {
37 #include "extra-functions.h"
38 int __system(const char *command);
Dees_Troy5bf43922012-09-07 16:07:55 -040039}
Dees_Troy51a0e822012-09-05 15:24:24 -040040
41TWPartition::TWPartition(void) {
42 Can_Be_Mounted = false;
43 Can_Be_Wiped = false;
44 Wipe_During_Factory_Reset = false;
45 Wipe_Available_in_GUI = false;
46 Is_SubPartition = false;
47 SubPartition_Of = "";
48 Symlink_Path = "";
49 Symlink_Mount_Point = "";
50 Mount_Point = "";
51 Block_Device = "";
52 Alternate_Block_Device = "";
53 Removable = false;
54 Is_Present = false;
55 Length = 0;
56 Size = 0;
57 Used = 0;
58 Free = 0;
59 Backup_Size = 0;
60 Can_Be_Encrypted = false;
61 Is_Encrypted = false;
62 Is_Decrypted = false;
63 Decrypted_Block_Device = "";
64 Display_Name = "";
65 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040066 Backup_FileName = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040067 Backup_Method = NONE;
68 Has_Data_Media = false;
69 Is_Storage = false;
70 Storage_Path = "";
71 Current_File_System = "";
72 Fstab_File_System = "";
73 Format_Block_Size = 0;
74}
75
76TWPartition::~TWPartition(void) {
77 // Do nothing
78}
79
Dees_Troy5bf43922012-09-07 16:07:55 -040080bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
81 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
82 int line_len = Line.size(), index = 0, item_index = 0;
83 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -040084 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -040085
86 strncpy(full_line, Line.c_str(), line_len);
87
Dees_Troy51127312012-09-08 13:08:49 -040088 for (index = 0; index < line_len; index++) {
Dees_Troy5bf43922012-09-07 16:07:55 -040089 if (full_line[index] <= 32)
90 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -040091 }
92 string mount_pt(full_line);
93 Mount_Point = mount_pt;
94 index = Mount_Point.size();
95 while (index < line_len) {
96 while (index < line_len && full_line[index] == '\0')
97 index++;
98 if (index >= line_len)
99 continue;
100 ptr = full_line + index;
101 if (item_index == 0) {
102 // File System
103 Fstab_File_System = ptr;
104 Current_File_System = ptr;
105 item_index++;
106 } else if (item_index == 1) {
107 // Primary Block Device
108 if (*ptr != '/') {
109 if (Display_Error)
110 LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
111 else
112 LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
113 return 0;
114 }
115 Block_Device = ptr;
116 Find_Real_Block_Device(Block_Device, Display_Error);
117 item_index++;
118 } else if (item_index > 1) {
119 if (*ptr == '/') {
120 // Alternate Block Device
121 Alternate_Block_Device = ptr;
122 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
123 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
124 // Partition length
125 ptr += 7;
126 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400127 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
128 // Custom flags, save for later so that new values aren't overwritten by defaults
129 ptr += 6;
130 Flags = ptr;
Dees_Troy5bf43922012-09-07 16:07:55 -0400131 } else {
132 // Unhandled data
133 LOGI("Unhandled fstab information: '%s', %i\n", ptr, index);
134 }
135 }
136 while (index < line_len && full_line[index] != '\0')
137 index++;
138 }
139
140 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
141 if (Display_Error)
142 LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str());
143 else
144 LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str());
145 return 0;
146 } else if (Is_File_System(Fstab_File_System)) {
147 Setup_File_System(Display_Error);
148 if (Mount_Point == "/system") {
149 Display_Name = "System";
150 Wipe_Available_in_GUI = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400151 } else if (Mount_Point == "/data") {
152 Display_Name = "Data";
153 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400154 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400155#ifdef RECOVERY_SDCARD_ON_DATA
156 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400157 Is_Storage = true;
158 Storage_Path = "/data/media";
159 Make_Dir("/sdcard", Display_Error);
160 Make_Dir("/emmc", Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400161#endif
162#ifdef TW_INCLUDE_CRYPTO
163 Can_Be_Encrypted = true;
164 if (!Mount(false)) {
165 Is_Encrypted = true;
166 Is_Decrypted = false;
167 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
168 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
169 DataManager::SetValue("tw_crypto_display", "");
Dees_Troy51127312012-09-08 13:08:49 -0400170 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400171#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400172 } else if (Mount_Point == "/cache") {
173 Display_Name = "Cache";
174 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400175 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400176 Update_Size(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400177 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400178 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400179 Display_Name = "DataData";
180 Is_SubPartition = true;
181 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400182 DataManager::SetValue(TW_HAS_DATADATA, 1);
183 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400184 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400185 Display_Name = "SD-Ext";
186 Wipe_Available_in_GUI = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400187 } else
188 Update_Size(Display_Error);
189 } else if (Is_Image(Fstab_File_System)) {
190 Setup_Image(Display_Error);
191 if (Mount_Point == "/boot") {
192 int backup_display_size = (int)(Backup_Size / 1048576LLU);
193 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
194 if (Backup_Size == 0) {
195 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
196 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
197 } else
198 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
199 } else if (Mount_Point == "/recovery") {
200 int backup_display_size = (int)(Backup_Size / 1048576LLU);
201 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
202 if (Backup_Size == 0) {
203 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
204 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
205 } else
206 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
207 }
208 }
209
Dees_Troy51127312012-09-08 13:08:49 -0400210 // Process any custom flags
211 if (Flags.size() > 0)
212 Process_Flags(Flags, Display_Error);
213
214 return true;
215}
216
217bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
218 char flags[MAX_FSTAB_LINE_LENGTH];
219 int flags_len, index = 0;
220 char* ptr;
221
222 strcpy(flags, Flags.c_str());
223 flags_len = Flags.size();
224 for (index = 0; index < flags_len; index++) {
225 if (flags[index] == ';')
226 flags[index] = '\0';
227 }
228
229 index = 0;
230 while (index < flags_len) {
231 while (index < flags_len && flags[index] == '\0')
232 index++;
233 if (index >= flags_len)
234 continue;
235 ptr = flags + index;
236 if (strcmp(ptr, "removable") == 0) {
237 Removable = true;
238 } else if (strcmp(ptr, "storage") == 0) {
239 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400240 } else if (strcmp(ptr, "canbewiped") == 0) {
241 Can_Be_Wiped = true;
242 } else if (strcmp(ptr, "wipeingui") == 0) {
243 Can_Be_Wiped = true;
244 Wipe_Available_in_GUI = true;
245 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
246 Can_Be_Wiped = true;
247 Wipe_Available_in_GUI = true;
248 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400249 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
250 ptr += 13;
251 Is_SubPartition = true;
252 SubPartition_Of = ptr;
253 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
254 ptr += 8;
255 Symlink_Path = ptr;
256 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
257 ptr += 8;
258 Display_Name = ptr;
259 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
260 ptr += 10;
261 Format_Block_Size = atoi(ptr);
262 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
263 ptr += 7;
264 Length = atoi(ptr);
265 } else {
266 if (Display_Error)
267 LOGE("Unhandled flag: '%s'\n", ptr);
268 else
269 LOGI("Unhandled flag: '%s'\n", ptr);
270 }
271 while (index < flags_len && flags[index] != '\0')
272 index++;
273 }
274 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400275}
276
Dees_Troy5bf43922012-09-07 16:07:55 -0400277bool TWPartition::Is_File_System(string File_System) {
278 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400279 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400280 File_System == "ext4" ||
281 File_System == "vfat" ||
282 File_System == "ntfs" ||
283 File_System == "yaffs2" ||
284 File_System == "auto")
285 return true;
286 else
287 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400288}
289
Dees_Troy5bf43922012-09-07 16:07:55 -0400290bool TWPartition::Is_Image(string File_System) {
291 if (File_System == "emmc" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400292 File_System == "mtd")
Dees_Troy5bf43922012-09-07 16:07:55 -0400293 return true;
294 else
295 return false;
296}
297
Dees_Troy51127312012-09-08 13:08:49 -0400298bool TWPartition::Make_Dir(string Path, bool Display_Error) {
299 if (!Path_Exists(Path)) {
300 if (mkdir(Path.c_str(), 0777) == -1) {
301 if (Display_Error)
302 LOGE("Can not create '%s' folder.\n", Path.c_str());
303 else
304 LOGI("Can not create '%s' folder.\n", Path.c_str());
305 return false;
306 } else {
307 LOGI("Created '%s' folder.\n", Path.c_str());
308 return true;
309 }
310 }
311 return true;
312}
313
Dees_Troy5bf43922012-09-07 16:07:55 -0400314void TWPartition::Setup_File_System(bool Display_Error) {
315 struct statfs st;
316
317 Can_Be_Mounted = true;
318 Can_Be_Wiped = true;
319
320 // Check to see if the block device exists
321 if (Path_Exists(Block_Device)) {
322 Is_Present = true;
323 } else if (Alternate_Block_Device != "" && Path_Exists(Alternate_Block_Device)) {
324 Flip_Block_Device();
325 Is_Present = true;
326 }
327 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400328 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400329 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
330 Backup_Name = Display_Name;
331 Backup_Method = FILES;
332}
333
334void TWPartition::Setup_Image(bool Display_Error) {
335 if (Path_Exists(Block_Device)) {
336 Is_Present = true;
337 } else if (Alternate_Block_Device != "" && Path_Exists(Alternate_Block_Device)) {
338 Flip_Block_Device();
339 Is_Present = true;
340 }
341 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
342 Backup_Name = Display_Name;
343 if (Fstab_File_System == "emmc")
344 Backup_Method = DD;
345 else if (Fstab_File_System == "mtd")
346 Backup_Method = FLASH_UTILS;
347 else
348 LOGI("Unhandled file system '%s' on image '%s'\n", Fstab_File_System.c_str(), Display_Name.c_str());
349 if (Find_Partition_Size()) {
350 Used = Size;
351 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400352 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400353 if (Display_Error)
354 LOGE("Unable to find parition size for '%s'\n", Block_Device.c_str());
355 else
356 LOGI("Unable to find parition size for '%s'\n", Block_Device.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400357 }
358}
359
Dees_Troy5bf43922012-09-07 16:07:55 -0400360void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
361 char device[512], realDevice[512];
362
363 strcpy(device, Block.c_str());
364 memset(realDevice, 0, sizeof(realDevice));
365 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
366 {
367 strcpy(device, realDevice);
368 memset(realDevice, 0, sizeof(realDevice));
369 }
370
371 if (device[0] != '/') {
372 if (Display_Error)
373 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
374 else
375 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
376 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400377 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400378 Block = device;
379 return;
380 }
381}
382
Dees_Troy51127312012-09-08 13:08:49 -0400383bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
384 struct statfs st;
385 string Local_Path = Mount_Point + "/.";
386
387 if (!Mount(Display_Error))
388 return false;
389
390 if (statfs(Local_Path.c_str(), &st) != 0) {
391 if (!Removable) {
392 if (Display_Error)
393 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
394 else
395 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
396 }
397 return false;
398 }
399 Size = (st.f_blocks * st.f_bsize);
400 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
401 Free = (st.f_bfree * st.f_bsize);
402 Backup_Size = Used;
403 return true;
404}
405
406bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400407 FILE* fp;
408 char command[255], line[512];
409 int include_block = 1;
410 unsigned int min_len;
411
412 if (!Mount(Display_Error))
413 return false;
414
415 min_len = Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400416 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
417 __system(command);
418 fp = fopen("/tmp/dfoutput.txt", "rt");
419 if (fp == NULL) {
420 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400421 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400422 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400423
424 while (fgets(line, sizeof(line), fp) != NULL)
425 {
426 unsigned long blocks, used, available;
427 char device[64];
428 char tmpString[64];
429
430 if (strncmp(line, "Filesystem", 10) == 0)
431 continue;
432 if (strlen(line) < min_len) {
433 include_block = 0;
434 continue;
435 }
436 if (include_block) {
437 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
438 } else {
439 // The device block string is so long that the df information is on the next line
440 int space_count = 0;
441 while (tmpString[space_count] == 32)
442 space_count++;
443 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
444 }
445
446 // Adjust block size to byte size
447 Size = blocks * 1024ULL;
448 Used = used * 1024ULL;
449 Free = available * 1024ULL;
450 Backup_Size = Used;
451 }
452 fclose(fp);
453 return true;
454}
455
Dees_Troy51127312012-09-08 13:08:49 -0400456unsigned long long TWPartition::Get_Folder_Size(string Path, bool Display_Error) {
457 DIR* d;
458 struct dirent* de;
459 struct stat st;
460 char path2[1024], filename[1024];
461 unsigned long long dusize = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400462
Dees_Troy51127312012-09-08 13:08:49 -0400463 // Make a copy of path in case the data in the pointer gets overwritten later
464 strcpy(path2, Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400465
Dees_Troy51127312012-09-08 13:08:49 -0400466 d = opendir(path2);
467 if (d == NULL)
468 {
469 LOGE("error opening '%s'\n", path2);
470 return 0;
471 }
472
473 while ((de = readdir(d)) != NULL)
474 {
475 if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
476 {
477 strcpy(filename, path2);
478 strcat(filename, "/");
479 strcat(filename, de->d_name);
480 dusize += Get_Folder_Size(filename, Display_Error);
481 }
482 else if (de->d_type == DT_REG)
483 {
484 strcpy(filename, path2);
485 strcat(filename, "/");
486 strcat(filename, de->d_name);
487 stat(filename, &st);
488 dusize += (unsigned long long)(st.st_size);
489 }
490 }
491 closedir(d);
Dees_Troy5bf43922012-09-07 16:07:55 -0400492
Dees_Troy63c8df72012-09-10 14:02:05 -0400493 return dusize;
Dees_Troy5bf43922012-09-07 16:07:55 -0400494}
495
496bool TWPartition::Find_Partition_Size(void) {
497 FILE* fp;
498 char line[512];
499 string tmpdevice;
500
501 // In this case, we'll first get the partitions we care about (with labels)
502 fp = fopen("/proc/partitions", "rt");
503 if (fp == NULL)
504 return false;
505
506 while (fgets(line, sizeof(line), fp) != NULL)
507 {
508 unsigned long major, minor, blocks;
509 char device[512];
510 char tmpString[64];
511
Dees_Troy63c8df72012-09-10 14:02:05 -0400512 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400513 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
514
515 tmpdevice = "/dev/block/";
516 tmpdevice += device;
517 if (tmpdevice == Block_Device || tmpdevice == Alternate_Block_Device) {
518 // Adjust block size to byte size
519 Size = blocks * 1024ULL;
520 fclose(fp);
521 return true;
522 }
523 }
524 fclose(fp);
525 return false;
526}
527
528bool TWPartition::Path_Exists(string Path) {
529 // Check to see if the Path exists
530 struct statfs st;
531
532 if (statfs(Path.c_str(), &st) != 0)
533 return false;
534 else
535 return true;
536}
537
538void TWPartition::Flip_Block_Device(void) {
539 string temp;
540
541 temp = Alternate_Block_Device;
542 Block_Device = Alternate_Block_Device;
543 Alternate_Block_Device = temp;
544}
545
546bool TWPartition::Is_Mounted(void) {
547 if (!Can_Be_Mounted)
548 return false;
549
550 struct stat st1, st2;
551 string test_path;
552
553 // Check to see if the mount point directory exists
554 test_path = Mount_Point + "/.";
555 if (stat(test_path.c_str(), &st1) != 0) return false;
556
557 // Check to see if the directory above the mount point exists
558 test_path = Mount_Point + "/../.";
559 if (stat(test_path.c_str(), &st2) != 0) return false;
560
561 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
562 int ret = (st1.st_dev != st2.st_dev) ? true : false;
563
564 return ret;
565}
566
567bool TWPartition::Mount(bool Display_Error) {
568 if (Is_Mounted()) {
569 return true;
570 } else if (!Can_Be_Mounted) {
571 return false;
572 }
Dees_Troy51127312012-09-08 13:08:49 -0400573 if (Removable)
574 Check_FS_Type();
Dees_Troy5bf43922012-09-07 16:07:55 -0400575 if (Is_Decrypted) {
576 if (mount(Decrypted_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
577 Check_FS_Type();
578 if (mount(Decrypted_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
579 if (Display_Error)
580 LOGE("Unable to mount decrypted block device '%s' to '%s'\n", Decrypted_Block_Device.c_str(), Mount_Point.c_str());
581 else
582 LOGI("Unable to mount decrypted block device '%s' to '%s'\n", Decrypted_Block_Device.c_str(), Mount_Point.c_str());
583 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400584 } else {
585 if (Removable)
586 Update_Size(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400587 return true;
Dees_Troy51127312012-09-08 13:08:49 -0400588 }
589 } else {
590 if (Removable)
591 Update_Size(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400592 return true;
Dees_Troy51127312012-09-08 13:08:49 -0400593 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400594 }
595 if (mount(Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
596 Check_FS_Type();
597 if (mount(Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
598 if (Alternate_Block_Device != "" && Path_Exists(Alternate_Block_Device)) {
599 Flip_Block_Device();
600 Check_FS_Type();
601 if (mount(Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
602 if (Display_Error)
603 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
604 else
605 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
606 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400607 } else {
608 if (Removable)
609 Update_Size(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400610 return true;
Dees_Troy51127312012-09-08 13:08:49 -0400611 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400612 } else
613 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400614 } else {
615 if (Removable)
616 Update_Size(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400617 return true;
Dees_Troy51127312012-09-08 13:08:49 -0400618 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400619 }
Dees_Troy51127312012-09-08 13:08:49 -0400620 if (Removable)
621 Update_Size(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400622 return true;
623}
624
625bool TWPartition::UnMount(bool Display_Error) {
626 if (Is_Mounted()) {
627 int never_unmount_system;
628
629 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
630 if (never_unmount_system == 1 && Mount_Point == "/system")
631 return true; // Never unmount system if you're not supposed to unmount it
632
633 if (umount(Mount_Point.c_str()) != 0) {
634 if (Display_Error)
635 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
636 else
637 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
638 return false;
639 } else
640 return true;
641 } else {
642 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400643 }
644}
645
646bool TWPartition::Wipe() {
647 LOGI("STUB TWPartition::Wipe\n");
648 return 1;
649}
650
651bool TWPartition::Backup(string backup_folder) {
652 LOGI("STUB TWPartition::Backup, backup_folder: '%s'\n", backup_folder.c_str());
653 return 1;
654}
655
656bool TWPartition::Restore(string restore_folder) {
657 LOGI("STUB TWPartition::Restore, restore_folder: '%s'\n", restore_folder.c_str());
658 return 1;
659}
660
661string TWPartition::Backup_Method_By_Name() {
662 LOGI("STUB TWPartition::Backup_Method_By_Name\n");
663 return "STUB";
664}
665
666bool TWPartition::Decrypt(string Password) {
667 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
668 return 1;
669}
670
671bool TWPartition::Wipe_Encryption() {
672 LOGI("STUB TWPartition::Wipe_Encryption\n");
673 return 1;
674}
675
676void TWPartition::Check_FS_Type() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400677 FILE *fp;
678 string blkCommand;
679 char blkOutput[255];
680 char* blk;
681 char* arg;
682 char* ptr;
683
684 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd")
685 return; // Running blkid on some mtd devices causes a massive crash
686
687 if (Is_Decrypted)
Dees_Troy51127312012-09-08 13:08:49 -0400688 blkCommand = "blkid " + Decrypted_Block_Device + " > /tmp/blkidoutput.txt";
Dees_Troy5bf43922012-09-07 16:07:55 -0400689 else
Dees_Troy51127312012-09-08 13:08:49 -0400690 blkCommand = "blkid " + Block_Device + " > /tmp/blkidoutput.txt";
691
692 __system(blkCommand.c_str());
693 fp = fopen("/tmp/blkidoutput.txt", "rt");
Dees_Troy5bf43922012-09-07 16:07:55 -0400694 while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
695 {
696 blk = blkOutput;
697 ptr = blkOutput;
Dees_Troy63c8df72012-09-10 14:02:05 -0400698 while (*ptr > 32 && *ptr != ':') ptr++;
699 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400700 *ptr = 0;
701
702 // Increment by two, but verify that we don't hit a NULL
703 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400704 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400705
706 // Now, find the TYPE field
707 while (1)
708 {
709 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400710 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400711 if (*ptr != 0)
712 {
713 *ptr = 0;
714 ptr++;
715 }
716
717 if (strlen(arg) > 6)
718 {
719 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
720 }
721
722 if (*ptr == 0)
723 {
724 arg = NULL;
725 break;
726 }
727 }
728
729 if (arg && strlen(arg) > 7)
730 {
731 arg += 6; // Skip the TYPE=" portion
732 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
733 }
734 else
735 continue;
736
Dees_Troy63c8df72012-09-10 14:02:05 -0400737 if (strcmp(Current_File_System.c_str(), arg) != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400738 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
739 Current_File_System = arg;
740 }
741 }
742 __pclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -0400743 return;
744}
745
746bool TWPartition::Wipe_EXT23() {
747 LOGI("STUB TWPartition::Wipe_EXT23\n");
748 return 1;
749}
750
751bool TWPartition::Wipe_EXT4() {
752 LOGI("STUB TWPartition::Wipe_EXT4\n");
753 return 1;
754}
755
756bool TWPartition::Wipe_FAT() {
757 LOGI("STUB TWPartition::Wipe_FAT\n");
758 return 1;
759}
760
761bool TWPartition::Wipe_YAFFS2() {
762 LOGI("STUB TWPartition::Wipe_YAFFS2\n");
763 return 1;
764}
765
766bool TWPartition::Wipe_RMRF() {
767 LOGI("STUB TWPartition::Wipe_RMRF\n");
768 return 1;
769}
770
771bool TWPartition::Wipe_Data_Without_Wiping_Media() {
772 LOGI("STUB TWPartition::Wipe_Data_Without_Wiping_Media\n");
773 return 1;
774}
775
776bool TWPartition::Backup_Tar(string backup_folder) {
777 LOGI("STUB TWPartition::Backup_Tar, backup_folder: '%s'\n", backup_folder.c_str());
778 return 1;
779}
780
781bool TWPartition::Backup_DD(string backup_folder) {
782 LOGI("STUB TWPartition::Backup_DD, backup_folder: '%s'\n", backup_folder.c_str());
783 return 1;
784}
785
786bool TWPartition::Backup_Dump_Image(string backup_folder) {
787 LOGI("STUB TWPartition::Backup_Dump_Image, backup_folder: '%s'\n", backup_folder.c_str());
788 return 1;
789}
790
791bool TWPartition::Restore_Tar(string restore_folder) {
792 LOGI("STUB TWPartition::Restore_Tar, backup_folder: '%s'\n", restore_folder.c_str());
793 return 1;
794}
795
796bool TWPartition::Restore_DD(string restore_folder) {
797 LOGI("STUB TWPartition::Restore_DD, backup_folder: '%s'\n", restore_folder.c_str());
798 return 1;
799}
800
801bool TWPartition::Restore_Flash_Image(string restore_folder) {
802 LOGI("STUB TWPartition::Restore_Flash_Image, backup_folder: '%s'\n", restore_folder.c_str());
803 return 1;
804}
Dees_Troy5bf43922012-09-07 16:07:55 -0400805
806bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400807 bool ret = false;
808
Dees_Troy5bf43922012-09-07 16:07:55 -0400809 if (!Can_Be_Mounted)
810 return false;
811
Dees_Troy51127312012-09-08 13:08:49 -0400812 if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -0400813 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400814
815 ret = Get_Size_Via_statfs(Display_Error);
816 if (!ret || Size == 0)
817 if (!Get_Size_Via_df(Display_Error))
818 return false;
819
Dees_Troy5bf43922012-09-07 16:07:55 -0400820 if (Has_Data_Media) {
821 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -0400822 unsigned long long data_media_used, actual_data;
823 data_media_used = Get_Folder_Size("/data/media", Display_Error);
824 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -0400825 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -0400826 int bak = (int)(Backup_Size / 1048576LLU);
827 int total = (int)(Size / 1048576LLU);
828 int us = (int)(Used / 1048576LLU);
829 int fre = (int)(Free / 1048576LLU);
830 int datmed = (int)(data_media_used / 1048576LLU);
831 LOGI("Data backup size is %iMB, size: %iMB, used: %iMB, free: %iMB, in data/media: %iMB.\n", bak, total, us, fre, datmed);
Dees_Troy5bf43922012-09-07 16:07:55 -0400832 } else
833 return false;
834 }
835 return true;
Dees_Troy51127312012-09-08 13:08:49 -0400836}