-
Notifications
You must be signed in to change notification settings - Fork 5
/
partition.cpp
2778 lines (2515 loc) · 81.2 KB
/
partition.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2013 to 2016 TeamWin
This file is part of TWRP/TeamWin Recovery Project.
TWRP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TWRP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TWRP. If not, see <http://www.gnu.org/licenses/>.
*/
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <sparse_format.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <unistd.h>
#ifdef TW_INCLUDE_CRYPTO
#include <cutils/properties.h>
#endif
#ifdef HAVE_SELINUX
#include <selinux/label.h>
#include <selinux/selinux.h>
#endif
#ifdef HAVE_CAPABILITIES
#include <linux/xattr.h>
#include <sys/capability.h>
#include <sys/xattr.h>
#endif
extern "C" {
#ifdef USE_EXT4
#include <make_ext4fs.h>
#endif
}
#include <string>
#include <vector>
#include "data.hpp"
#include "gui/console.h"
#include "gui/gui.h"
#include "gui/twmsg.hpp"
#include "infomanager.hpp"
#include "libblkid/include/blkid.h"
#include "partitions.hpp"
#include "progresstracking.hpp"
#include "set_metadata.h"
#include "twcommon.h"
#include "twrp-functions.hpp"
#include "twrpDigest.hpp"
#include "twrpDU.hpp"
#include "twrpTar.hpp"
#include "variables.h"
extern "C" {
#include "mtdutils/mtdutils.h"
#ifdef TW_INCLUDE_CRYPTO
#include "crypto/lollipop/cryptfs.h"
#include "gpt/gpt.h"
#endif
}
#ifndef TW_INCLUDE_CRYPTO
#define CRYPT_FOOTER_OFFSET 0x4000
#endif
using namespace std;
extern struct selabel_handle *selinux_handle;
extern bool datamedia;
TWPartition::TWPartition() {
Can_Be_Mounted = false;
Can_Be_Wiped = false;
Can_Be_Backed_Up = false;
Use_Rm_Rf = false;
Wipe_During_Factory_Reset = false;
Wipe_Available_in_GUI = false;
Is_SubPartition = false;
Has_SubPartition = false;
SubPartition_Of = "";
Symlink_Path = "";
Symlink_Mount_Point = "";
Mount_Point = "";
Backup_Path = "";
Actual_Block_Device = "";
Primary_Block_Device = "";
Removable = false;
Is_Present = false;
Length = 0;
Size = 0;
Used = 0;
Free = 0;
Backup_Size = 0;
Can_Be_Encrypted = false;
Is_Encrypted = false;
Is_Decrypted = false;
Mount_To_Decrypt = false;
Decrypted_Block_Device = "";
Display_Name = "";
Backup_Display_Name = "";
Storage_Name = "";
Backup_Name = "";
Backup_FileName = "";
MTD_Name = "";
Backup_Method = NONE;
Can_Encrypt_Backup = false;
Use_Userdata_Encryption = false;
Has_Data_Media = false;
Has_Android_Secure = false;
Is_Storage = false;
Is_Settings_Storage = false;
Storage_Path = "";
Current_File_System = "";
Fstab_File_System = "";
Mount_Flags = 0;
Mount_Options = "";
Format_Block_Size = 0;
Ignore_Blkid = false;
Retain_Layout_Version = false;
Crypto_Key_Location = "footer";
MTP_Storage_ID = 0;
Can_Flash_Img = false;
Mount_Read_Only = false;
Is_Adopted_Storage = false;
Adopted_GUID = "";
}
TWPartition::~TWPartition(void) {
// Do nothing
}
bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error) {
char full_line[MAX_FSTAB_LINE_LENGTH];
char twflags[MAX_FSTAB_LINE_LENGTH] = "";
char* ptr;
int line_len = strlen(fstab_line), index = 0, item_index = 0;
bool skip = false;
strlcpy(full_line, fstab_line, sizeof(full_line));
for (index = 0; index < line_len; index++) {
if (full_line[index] == 34)
skip = !skip;
if (!skip && full_line[index] <= 32)
full_line[index] = '\0';
}
Primary_Block_Device = full_line;
LOGINFO("Processing '%s'\n", Primary_Block_Device.c_str());
index = Primary_Block_Device.size();
while (index < line_len) {
while (index < line_len && full_line[index] == '\0')
index++;
if (index >= line_len)
continue;
ptr = full_line + index;
if (item_index == 0) { // Mount point
Mount_Point = ptr;
Backup_Path = Mount_Point;
Storage_Path = Mount_Point;
Display_Name = ptr + 1;
Backup_Display_Name = Display_Name;
Storage_Name = Display_Name;
item_index++;
} else if (item_index == 1) { // Filesystem
Fstab_File_System = ptr;
Current_File_System = ptr;
if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
MTD_Name = Primary_Block_Device;
Find_MTD_Block_Device(MTD_Name);
} else if (Fstab_File_System == "bml") {
if (Mount_Point == "/boot")
MTD_Name = "boot";
else if (Mount_Point == "/recovery")
MTD_Name = "recovery";
} else {
Find_Real_Block_Device(Primary_Block_Device, Display_Error);
}
item_index++;
} else if (item_index == 2) { // Mount flags/options
Process_FS_Flags(ptr);
if (Mount_Point == "/system") // ro/rw /system is custom-handled by TWRP
Mount_Flags &= ~MS_RDONLY;
item_index++;
} else if (item_index == 3) { // fs_mgr flags
Process_Fsmgr_Flags(ptr, Display_Error);
item_index++;
} else if (item_index > 3) { // twrp flags
if (strlen(ptr) > 5 && strncmp(ptr, "twrp=", 5) == 0) {
// Custom flags, save for later so that new values aren't overwritten by defaults
ptr += 5;
strlcpy(twflags, ptr, sizeof(twflags));
}
}
while (index < line_len && full_line[index] != '\0')
index++;
}
if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
if (Display_Error)
LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
else
LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
return false;
} else if (Is_File_System(Fstab_File_System)) {
Find_Actual_Block_Device();
Setup_File_System(Display_Error);
if (Mount_Point == "/system") {
Display_Name = "System";
Backup_Display_Name = Display_Name;
Storage_Name = Display_Name;
Wipe_Available_in_GUI = true;
Can_Be_Backed_Up = true;
Mount_Read_Only = true;
} else if (Mount_Point == "/data") {
Display_Name = "Data";
Backup_Display_Name = Display_Name;
Storage_Name = Display_Name;
Wipe_Available_in_GUI = true;
Wipe_During_Factory_Reset = true;
Can_Be_Backed_Up = true;
Can_Encrypt_Backup = true;
Use_Userdata_Encryption = true;
} else if (Mount_Point == "/cache") {
Display_Name = "Cache";
Backup_Display_Name = Display_Name;
Storage_Name = Display_Name;
Wipe_Available_in_GUI = true;
Wipe_During_Factory_Reset = true;
Can_Be_Backed_Up = true;
} else if (Mount_Point == "/datadata") {
Wipe_During_Factory_Reset = true;
Display_Name = "DataData";
Backup_Display_Name = Display_Name;
Storage_Name = Display_Name;
Is_SubPartition = true;
SubPartition_Of = "/data";
DataManager::SetValue(TW_HAS_DATADATA, 1);
Can_Be_Backed_Up = true;
Can_Encrypt_Backup = true;
Use_Userdata_Encryption = false; // This whole partition should be encrypted
} else if (Mount_Point == "/sd-ext") {
Wipe_During_Factory_Reset = true;
Display_Name = "SD-Ext";
Backup_Display_Name = Display_Name;
Storage_Name = Display_Name;
Wipe_Available_in_GUI = true;
Removable = true;
Can_Be_Backed_Up = true;
Can_Encrypt_Backup = true;
Use_Userdata_Encryption = true;
} else if (Mount_Point == "/boot") {
Display_Name = "Boot";
Backup_Display_Name = Display_Name;
DataManager::SetValue("tw_boot_is_mountable", 1);
Can_Be_Backed_Up = true;
} else if (Mount_Point == "/vendor") {
Display_Name = "Vendor";
Backup_Display_Name = Display_Name;
Storage_Name = Display_Name;
Mount_Read_Only = true;
}
#ifdef TW_EXTERNAL_STORAGE_PATH
if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
Is_Storage = true;
Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Removable = true;
Wipe_Available_in_GUI = true;
#else
if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Is_Storage = true;
Removable = true;
Wipe_Available_in_GUI = true;
#endif
}
#ifdef TW_INTERNAL_STORAGE_PATH
if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
Is_Storage = true;
Is_Settings_Storage = true;
Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Wipe_Available_in_GUI = true;
}
#else
if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Is_Storage = true;
Is_Settings_Storage = true;
Wipe_Available_in_GUI = true;
}
#endif
} else if (Is_Image(Fstab_File_System)) {
Find_Actual_Block_Device();
Setup_Image(Display_Error);
if (Mount_Point == "/boot") {
Display_Name = "Boot";
Backup_Display_Name = Display_Name;
Can_Be_Backed_Up = true;
Can_Flash_Img = true;
} else if (Mount_Point == "/recovery") {
Display_Name = "Recovery";
Backup_Display_Name = Display_Name;
Can_Flash_Img = true;
} else if (Mount_Point == "/system_image") {
Display_Name = "System Image";
Backup_Display_Name = Display_Name;
Can_Flash_Img = true;
Can_Be_Backed_Up = true;
} else if (Mount_Point == "/vendor_image") {
Display_Name = "Vendor Image";
Backup_Display_Name = Display_Name;
Can_Flash_Img = true;
Can_Be_Backed_Up = true;
}
}
// Process TWRP fstab flags
if (strlen(twflags) > 0) {
string Prev_Display_Name = Display_Name;
string Prev_Storage_Name = Storage_Name;
string Prev_Backup_Display_Name = Backup_Display_Name;
Display_Name = "";
Storage_Name = "";
Backup_Display_Name = "";
Process_TW_Flags(twflags, Display_Error);
bool has_display_name = !Display_Name.empty();
bool has_storage_name = !Storage_Name.empty();
bool has_backup_name = !Backup_Display_Name.empty();
if (!has_display_name) Display_Name = Prev_Display_Name;
if (!has_storage_name) Storage_Name = Prev_Storage_Name;
if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
if (has_display_name && !has_storage_name)
Storage_Name = Display_Name;
if (!has_display_name && has_storage_name)
Display_Name = Storage_Name;
if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Backup_Display_Name = Display_Name;
if (!has_display_name && has_backup_name)
Display_Name = Backup_Display_Name;
}
return true;
}
void TWPartition::Partition_Post_Processing(bool Display_Error) {
if (Mount_Point == "/data")
Setup_Data_Partition(Display_Error);
else if (Mount_Point == "/cache")
Setup_Cache_Partition(Display_Error);
}
void TWPartition::Setup_Data_Partition(bool Display_Error) {
if (Mount_Point != "/data")
return;
// Ensure /data is not mounted as tmpfs for qcom hardware decrypt
UnMount(false);
#ifdef TW_INCLUDE_CRYPTO
if (datamedia)
Setup_Data_Media();
Can_Be_Encrypted = true;
char crypto_blkdev[255];
property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
if (strcmp(crypto_blkdev, "error") != 0) {
DataManager::SetValue(TW_IS_DECRYPTED, 1);
Is_Encrypted = true;
Is_Decrypted = true;
Decrypted_Block_Device = crypto_blkdev;
LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
} else if (!Mount(false)) {
if (Is_Present) {
set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
if (cryptfs_check_footer() == 0) {
Is_Encrypted = true;
Is_Decrypted = false;
Can_Be_Mounted = false;
Current_File_System = "emmc";
Setup_Image(Display_Error);
DataManager::SetValue(TW_IS_ENCRYPTED, 1);
DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
DataManager::SetValue("tw_crypto_display", "");
} else {
gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
}
} else {
LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
}
} else {
// Filesystem is not encrypted and the mount succeeded, so return to
// the original unmounted state
UnMount(false);
}
if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
Setup_Data_Media();
Recreate_Media_Folder();
}
#else
if (datamedia) {
Setup_Data_Media();
Recreate_Media_Folder();
}
#endif
}
void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
if (Mount_Point != "/cache")
return;
if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
LOGINFO("Recreating /cache/recovery folder\n");
if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
LOGERR("Could not create /cache/recovery\n");
}
}
void TWPartition::Process_FS_Flags(const char *str) {
char *options = strdup(str);
char *ptr, *savep;
Mount_Options = "";
// Avoid issues with potentially nested strtok by using strtok_r
ptr = strtok_r(options, ",", &savep);
while (ptr) {
const struct flag_list* mount_flag = mount_flags;
for (; mount_flag->name; mount_flag++) {
// mount_flags are never postfixed by '=',
// so only match identical strings (including length)
if (strcmp(ptr, mount_flag->name) == 0) {
Mount_Flags |= mount_flag->flag;
break;
}
}
if (mount_flag->flag == MS_RDONLY)
Mount_Read_Only = true;
if (mount_flag->name != 0) {
if (!Mount_Options.empty())
Mount_Options += ",";
Mount_Options += mount_flag->name;
} else {
LOGINFO("Unhandled mount flag: '%s'\n", ptr);
}
ptr = strtok_r(NULL, ",", &savep);
}
free(options);
}
void TWPartition::Apply_Fsmgr_Flag(const unsigned flag, const char* str, const bool val) {
switch (flag) {
case MF_CRYPT:
Crypto_Key_Location = str;
break;
case MF_DEFAULTS:
// Do nothing
break;
case MF_LENGTH:
Length = atoi(str);
break;
default:
// Should not get here
LOGINFO("fsmgr flag identified for processing, but later unmatched: %i\n", flag);
break;
}
}
void TWPartition::Process_Fsmgr_Flags(const char *str, bool Display_Error) {
char *flags = strdup(str);
char *ptr, *savep;
// Avoid issues with potentially nested strtok by using strtok_r
ptr = strtok_r(flags, ",", &savep);
while (ptr) {
int ptr_len = strlen(ptr);
const struct flag_list* fs_mgr_flag = fs_mgr_flags;
for (; fs_mgr_flag->name; fs_mgr_flag++) {
int flag_len = strlen(fs_mgr_flag->name);
if (strncmp(ptr, fs_mgr_flag->name, flag_len) == 0) {
bool flag_val = false;
if (ptr_len > flag_len && (fs_mgr_flag->name)[flag_len-1] != '='
&& ptr[flag_len] != '=') {
// Handle flags with same starting string
// (hypothetical e.g. length and lengthier)
continue;
} else if (ptr_len == flag_len
&& (fs_mgr_flag->name)[flag_len-1] == '=') {
// Skip flags missing argument after =
// (e.g. backupname=)
LOGINFO("fsmgr flag missing argument: %s\n", fs_mgr_flag->name);
break;
} else if (ptr_len > flag_len
&& (fs_mgr_flag->name)[flag_len-1] == '=') {
// Handle arguments to flags
// (e.g. backupname="My Stuff")
ptr += flag_len;
TWFunc::Strip_Quotes(ptr);
// Skip flags with empty argument
// (e.g. backupname="")
if (strlen(ptr) == 0) {
LOGINFO("fsmgr flag missing argument: %s\n", fs_mgr_flag->name);
break;
}
} else if (ptr_len == flag_len) {
// Handle flags without arguments
// (e.g. defaults)
flag_val = true;
} else {
LOGINFO("fsmgr flag matched, but could not be processed: %s\n", ptr);
break;
}
Apply_Fsmgr_Flag(fs_mgr_flag->flag, ptr, flag_val);
break;
}
}
if (fs_mgr_flag->name == 0) {
if (Display_Error)
LOGERR("Unhandled fsmgr flag: '%s'\n", ptr);
else
LOGINFO("Unhandled fsmgr flag: '%s'\n", ptr);
}
ptr = strtok_r(NULL, ",", &savep);
}
free(flags);
}
void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
switch (flag) {
case TWFLAG_ANDSEC:
Has_Android_Secure = val;
break;
case TWFLAG_BACKUP:
Can_Be_Backed_Up = val;
break;
case TWFLAG_BACKUPNAME:
Backup_Display_Name = str;
break;
case TWFLAG_BLOCKSIZE:
Format_Block_Size = (unsigned long)(atol(str));
break;
case TWFLAG_CANBEWIPED:
Can_Be_Wiped = val;
break;
case TWFLAG_CANENCRYPTBACKUP:
Can_Encrypt_Backup = val;
break;
case TWFLAG_DEFAULTS:
// Do nothing
break;
case TWFLAG_DISPLAY:
Display_Name = str;
break;
case TWFLAG_ENCRYPTABLE:
case TWFLAG_FORCEENCRYPT:
Crypto_Key_Location = str;
break;
case TWFLAG_FLASHIMG:
Can_Flash_Img = val;
break;
case TWFLAG_IGNOREBLKID:
Ignore_Blkid = val;
break;
case TWFLAG_LENGTH:
Length = atoi(str);
break;
case TWFLAG_MOUNTTODECRYPT:
Mount_To_Decrypt = val;
break;
case TWFLAG_REMOVABLE:
Removable = val;
break;
case TWFLAG_RETAINLAYOUTVERSION:
Retain_Layout_Version = val;
break;
case TWFLAG_RW:
Mount_Flags &= ~MS_RDONLY;
break;
case TWFLAG_SETTINGSSTORAGE:
Is_Settings_Storage = val;
if (Is_Settings_Storage)
Is_Storage = true;
break;
case TWFLAG_STORAGE:
Is_Storage = val;
break;
case TWFLAG_STORAGENAME:
Storage_Name = str;
break;
case TWFLAG_SUBPARTITIONOF:
Is_SubPartition = true;
SubPartition_Of = str;
break;
case TWFLAG_SYMLINK:
Symlink_Path = str;
break;
case TWFLAG_USERDATAENCRYPTBACKUP:
Use_Userdata_Encryption = val;
if (Use_Userdata_Encryption)
Can_Encrypt_Backup = true;
break;
case TWFLAG_USERMRF:
Use_Rm_Rf = val;
break;
case TWFLAG_WIPEDURINGFACTORYRESET:
Wipe_During_Factory_Reset = val;
if (Wipe_During_Factory_Reset) {
Can_Be_Wiped = true;
Wipe_Available_in_GUI = true;
}
break;
case TWFLAG_WIPEINGUI:
Wipe_Available_in_GUI = val;
if (Wipe_Available_in_GUI)
Can_Be_Wiped = true;
break;
default:
// Should not get here
LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
break;
}
}
void TWPartition::Process_TW_Flags(char *flags, bool Display_Error) {
char separator[2] = {'\n', 0};
char *ptr, *savep;
// Semicolons within double-quotes are not forbidden, so replace
// only the semicolons intended as separators with '\n' for strtok
for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
if (flags[i] == '\"')
skip = !skip;
if (!skip && flags[i] == ';')
flags[i] = separator[0];
}
// Avoid issues with potentially nested strtok by using strtok_r
ptr = strtok_r(flags, separator, &savep);
while (ptr) {
int ptr_len = strlen(ptr);
const struct flag_list* tw_flag = tw_flags;
for (; tw_flag->name; tw_flag++) {
int flag_len = strlen(tw_flag->name);
if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
bool flag_val = false;
if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
&& ptr[flag_len] != '=') {
// Handle flags with same starting string
// (e.g. backup and backupname)
continue;
} else if (ptr_len > flag_len && ptr[flag_len] == '=') {
// Handle flags with dual format: Part 1
// (e.g. backup and backup=y. backup=y handled here)
ptr += flag_len + 1;
TWFunc::Strip_Quotes(ptr);
// Skip flags with empty argument
// (e.g. backup=)
if (strlen(ptr) == 0) {
LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
break;
}
flag_val = strchr("yY1", *ptr) != NULL;
} else if (ptr_len == flag_len
&& (tw_flag->name)[flag_len-1] == '=') {
// Skip flags missing argument after =
// (e.g. backupname=)
LOGINFO("Flag missing argument: %s\n", tw_flag->name);
break;
} else if (ptr_len > flag_len
&& (tw_flag->name)[flag_len-1] == '=') {
// Handle arguments to flags
// (e.g. backupname="My Stuff")
ptr += flag_len;
TWFunc::Strip_Quotes(ptr);
// Skip flags with empty argument
// (e.g. backupname="")
if (strlen(ptr) == 0) {
LOGINFO("Flag missing argument: %s\n", tw_flag->name);
break;
}
} else if (ptr_len == flag_len) {
// Handle flags with dual format: Part 2
// (e.g. backup and backup=y. backup handled here)
flag_val = true;
} else {
LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
break;
}
Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
break;
}
}
if (tw_flag->name == 0) {
if (Display_Error)
LOGERR("Unhandled flag: '%s'\n", ptr);
else
LOGINFO("Unhandled flag: '%s'\n", ptr);
}
ptr = strtok_r(NULL, separator, &savep);
}
}
bool TWPartition::Is_File_System(string File_System) {
if (File_System == "ext2" ||
File_System == "ext3" ||
File_System == "ext4" ||
File_System == "vfat" ||
File_System == "ntfs" ||
File_System == "yaffs2" ||
File_System == "exfat" ||
File_System == "f2fs" ||
File_System == "auto")
return true;
else
return false;
}
bool TWPartition::Is_Image(string File_System) {
if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
return true;
else
return false;
}
bool TWPartition::Make_Dir(string Path, bool Display_Error) {
if (!TWFunc::Path_Exists(Path)) {
if (mkdir(Path.c_str(), 0777) == -1) {
if (Display_Error)
gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
else
LOGINFO("Can not create '%s' folder.\n", Path.c_str());
return false;
} else {
LOGINFO("Created '%s' folder.\n", Path.c_str());
return true;
}
}
return true;
}
void TWPartition::Setup_File_System(bool Display_Error) {
struct statfs st;
Can_Be_Mounted = true;
Can_Be_Wiped = true;
// Make the mount point folder if it doesn't exist
Make_Dir(Mount_Point, Display_Error);
Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
Backup_Name = Display_Name;
Backup_Method = FILES;
}
void TWPartition::Setup_Image(bool Display_Error) {
Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
Backup_Name = Display_Name;
if (Current_File_System == "emmc")
Backup_Method = DD;
else if (Current_File_System == "mtd" || Current_File_System == "bml")
Backup_Method = FLASH_UTILS;
else
LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str());
if (Find_Partition_Size()) {
Used = Size;
Backup_Size = Size;
} else {
if (Display_Error)
LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
else
LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
}
}
void TWPartition::Setup_AndSec(void) {
Backup_Display_Name = "Android Secure";
Backup_Name = "and-sec";
Can_Be_Backed_Up = true;
Has_Android_Secure = true;
Symlink_Path = Mount_Point + "/.android_secure";
Symlink_Mount_Point = "/and-sec";
Backup_Path = Symlink_Mount_Point;
Make_Dir("/and-sec", true);
Recreate_AndSec_Folder();
Mount_Storage_Retry(true);
}
void TWPartition::Setup_Data_Media() {
LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
if (Storage_Name.empty() || Storage_Name == "Data")
Storage_Name = "Internal Storage";
Has_Data_Media = true;
Is_Storage = true;
Storage_Path = Mount_Point + "/media";
Symlink_Path = Storage_Path;
if (Mount_Point == "/data") {
Is_Settings_Storage = true;
if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
Make_Dir("/emmc", false);
Symlink_Mount_Point = "/emmc";
} else {
Make_Dir("/sdcard", false);
Symlink_Mount_Point = "/sdcard";
}
if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
Storage_Path = Mount_Point + "/media/0";
Symlink_Path = Storage_Path;
DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
UnMount(true);
}
DataManager::SetValue("tw_has_internal", 1);
DataManager::SetValue("tw_has_data_media", 1);
du.add_absolute_dir(Mount_Point + "/misc/vold");
du.add_absolute_dir(Mount_Point + "/.layout_version");
du.add_absolute_dir(Mount_Point + "/system/storage.xml");
} else {
if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
Storage_Path = Mount_Point + "/media/0";
Symlink_Path = Storage_Path;
UnMount(true);
}
}
du.add_absolute_dir(Mount_Point + "/media");
}
void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
char device[512], realDevice[512];
strcpy(device, Block.c_str());
memset(realDevice, 0, sizeof(realDevice));
while (readlink(device, realDevice, sizeof(realDevice)) > 0)
{
strcpy(device, realDevice);
memset(realDevice, 0, sizeof(realDevice));
}
if (device[0] != '/') {
if (Display_Error)
LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
else
LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
return;
} else {
Block = device;
return;
}
}
bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
// On some devices, storage doesn't want to mount right away, retry and sleep
if (!Mount(Display_Error)) {
int retry_count = 5;
while (retry_count > 0 && !Mount(false)) {
usleep(500000);
retry_count--;
}
return Mount(Display_Error);
}
return true;
}
bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
FILE *fp = NULL;
char line[255];
fp = fopen("/proc/mtd", "rt");
if (fp == NULL) {
LOGERR("Device does not support /proc/mtd\n");
return false;
}
while (fgets(line, sizeof(line), fp) != NULL)
{
char device[32], label[32];
unsigned long size = 0;
char* fstype = NULL;
int deviceId;
sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
// Skip header and blank lines
if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
continue;
// Strip off the trailing " from the label
label[strlen(label)-1] = '\0';
if (strcmp(label, MTD_Name.c_str()) == 0) {
// We found our device
// Strip off the trailing : from the device
device[strlen(device)-1] = '\0';
if (sscanf(device,"mtd%d", &deviceId) == 1) {
sprintf(device, "/dev/block/mtdblock%d", deviceId);
Primary_Block_Device = device;
fclose(fp);
return true;
}
}
}
fclose(fp);
return false;
}
bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
struct statfs st;
string Local_Path = Mount_Point + "/.";
if (!Mount(Display_Error))
return false;
if (statfs(Local_Path.c_str(), &st) != 0) {
if (!Removable) {
if (Display_Error)
LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
else
LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
}
return false;
}
Size = (st.f_blocks * st.f_bsize);
Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
Free = (st.f_bfree * st.f_bsize);
Backup_Size = Used;
return true;
}
bool TWPartition::Get_Size_Via_df(bool Display_Error) {
FILE* fp;
char command[255], line[512];
int include_block = 1;
unsigned int min_len;
if (!Mount(Display_Error))
return false;
min_len = Actual_Block_Device.size() + 2;
sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
TWFunc::Exec_Cmd(command);
fp = fopen("/tmp/dfoutput.txt", "rt");
if (fp == NULL) {
LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
return false;
}
while (fgets(line, sizeof(line), fp) != NULL)
{
unsigned long blocks, used, available;
char device[64];
char tmpString[64];
if (strncmp(line, "Filesystem", 10) == 0)
continue;
if (strlen(line) < min_len) {
include_block = 0;
continue;
}
if (include_block) {
sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
} else {
// The device block string is so long that the df information is on the next line
int space_count = 0;
sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
while (tmpString[space_count] == 32)
space_count++;
sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
}
// Adjust block size to byte size
Size = blocks * 1024ULL;
Used = used * 1024ULL;
Free = available * 1024ULL;
Backup_Size = Used;
}
fclose(fp);