forked from TeamWin/Team-Win-Recovery-Project
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ddftw.c
914 lines (790 loc) · 27.9 KB
/
ddftw.c
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
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <unistd.h>
#include "ddftw.h"
#include "common.h"
#include "extra-functions.h"
#include "bootloader.h"
#include "backstore.h"
#include "data.h"
#ifdef TW_INCLUDE_CRYPTO
#ifdef TW_INCLUDE_JB_CRYPTO
#include "crypto/jb/cryptfs.h"
#else
#include "crypto/ics/cryptfs.h"
#endif
#include "cutils/properties.h"
#endif
struct dInfo tmp, sys, dat, boo, rec, cac, sdcext, sdcint, ase, sde, sp1, sp2, sp3, datdat;
char tw_device_name[20];
void dumpPartitionTable(void);
static int isMTDdevice = 0, isEMMCdevice = 0;
struct dInfo* findDeviceByLabel(const char* label)
{
if (!label) return NULL;
if (strcmp(label, "system") == 0) return &sys;
if (strcmp(label, "userdata") == 0) return &dat;
if (strcmp(label, "data") == 0) return &dat;
if (strcmp(label, "boot") == 0) return &boo;
if (strcmp(label, "recovery") == 0) return &rec;
if (strcmp(label, "cache") == 0) return &cac;
if (strcmp(label, "sd-ext") == 0) return &sde;
if (strcmp(label, "and-sec") == 0) return &ase;
if (strcmp(label, "datadata") == 0) {
DataManager_SetIntValue(TW_HAS_DATADATA, 1);
return &datdat;
}
// New sdcard methods
if (DataManager_GetIntValue(TW_HAS_INTERNAL) == 1) {
if (strcmp(label, DataManager_GetStrValue(TW_EXTERNAL_LABEL)) == 0) return &sdcext;
if (strcmp(label, DataManager_GetStrValue(TW_INTERNAL_LABEL)) == 0) return &sdcint;
if (strcmp(label, "sdcard") == 0) {
if (DataManager_GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0) {
return &sdcint;
} else {
return &sdcext;
}
return 0;
}
} else
if (strcmp(label, DataManager_GetStrValue(TW_EXTERNAL_LABEL)) == 0) return &sdcext;
// Special Partitions (such as WiMAX, efs, and PDS)
if (strcmp(label, EXPAND(SP1_NAME)) == 0) return &sp1;
if (strcmp(label, EXPAND(SP2_NAME)) == 0) return &sp2;
if (strcmp(label, EXPAND(SP3_NAME)) == 0) return &sp3;
return NULL;
}
struct dInfo* findDeviceByBlockDevice(const char* blockDevice)
{
if (!blockDevice) return NULL;
if (strcmp(blockDevice, sys.blk) == 0) return &sys;
if (strcmp(blockDevice, dat.blk) == 0) return &dat;
if (strcmp(blockDevice, datdat.blk) == 0) {
DataManager_SetIntValue(TW_HAS_DATADATA, 1);
return &datdat;
}
if (strcmp(blockDevice, boo.blk) == 0) return &boo;
if (strcmp(blockDevice, rec.blk) == 0) return &rec;
if (strcmp(blockDevice, cac.blk) == 0) return &cac;
if (strcmp(blockDevice, sde.blk) == 0) return &sde;
if (strcmp(blockDevice, sdcext.blk) == 0) return &sdcext;
if (strcmp(blockDevice, sdcint.blk) == 0) return &sdcint;
if (strcmp(blockDevice, sp1.blk) == 0) return &sp1;
if (strcmp(blockDevice, sp2.blk) == 0) return &sp2;
if (strcmp(blockDevice, sp3.blk) == 0) return &sp3;
return NULL;
}
#define SAFE_STR(str) (str ? str : "<NULL>")
// This routine handles the case where we can't open either /proc/mtd or /proc/emmc
int setLocationData(const char* label, const char* blockDevice, const char* mtdDevice, const char* fstype, unsigned long long size, const char* alternate)
{
struct dInfo* loc = NULL;
if (label) loc = findDeviceByLabel(label);
if (!loc && blockDevice) loc = findDeviceByBlockDevice(blockDevice);
if (!loc)
return -1;
if (blockDevice) strcpy(loc->blk, blockDevice);
if (label) strcpy(loc->mnt, label);
if (mtdDevice){
strcpy(loc->dev, mtdDevice);
loc->memory_type = mtd;
if (strcmp(loc->mnt, "data") == 0)
strcpy(loc->format_location, "userdata");
else
strcpy(loc->format_location, loc->mnt);
} else {
loc->memory_type = emmc;
strcpy(loc->format_location, loc->blk);
}
// This is a simple
if (strcmp(loc->mnt, "boot") == 0 && fstype)
{
loc->mountable = 0;
if (strcmp(fstype, "vfat") == 0 || memcmp(fstype, "ext", 3) == 0 || strcmp(fstype, "auto") == 0)
loc->mountable = 1;
}
if (fstype) strcpy(loc->fst, fstype);
if (size && loc->sze == 0) loc->sze = size;
if (alternate) strcpy(loc->alt, alternate);
return 0;
}
int getSizeViaDf(struct dInfo* mMnt)
{
FILE* fp;
char command[255], line[512];
int ret = 0, include_block = 1;
unsigned int min_len;
min_len = strlen(mMnt->blk) + 2;
sprintf(command, "df %s", mMnt->blk);
fp = __popen(command, "r");
if (fp == NULL)
return -1;
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;
while (tmpString[space_count] == 32)
space_count++;
sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
}
// Adjust block size to byte size
unsigned long long size = blocks * 1024ULL;
sprintf(tmpString, "%s%s", tw_block, device);
setLocationData(NULL, mMnt->blk, NULL, NULL, size, NULL);
}
fclose(fp);
return 0;
}
int getSizesViaPartitions()
{
FILE* fp;
char line[512];
int ret = 0;
// In this case, we'll first get the partitions we care about (with labels)
fp = fopen("/proc/partitions", "rt");
if (fp == NULL)
return -1;
while (fgets(line, sizeof(line), fp) != NULL)
{
unsigned long major, minor, blocks;
char device[64];
char tmpString[64];
if (strlen(line) < 7 || line[0] == 'm') continue;
sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
// Adjust block size to byte size
unsigned long long size = blocks * 1024ULL;
sprintf(tmpString, "%s%s", tw_block, device);
setLocationData(NULL, tmpString, NULL, NULL, size, NULL);
}
fclose(fp);
return 0;
}
int getLocationsViafstab()
{
FILE* fp;
char line[512];
int ret = 0;
// In this case, we'll first get the partitions we care about (with labels)
fp = fopen("/etc/recovery.fstab", "rt");
if (fp == NULL)
{
LOGE("=> Unable to open /etc/recovery.fstab\n");
return -1;
}
while (fgets(line, sizeof(line), fp) != NULL)
{
char mount[32], fstype[32], device[256], alternate[256];
char* pDevice = device;
char* aDevice = alternate;
if (line[0] != '/') continue;
sscanf(line + 1, "%s %s %s %s", mount, fstype, device, alternate);
// Attempt to flip mounts until we find the block device
char realDevice[256];
memset(realDevice, 0, sizeof(realDevice));
while (readlink(device, realDevice, sizeof(realDevice)) > 0)
{
strcpy(device, realDevice);
memset(realDevice, 0, sizeof(realDevice));
}
if (device[0] != '/') pDevice = NULL;
memset(realDevice, 0, sizeof(realDevice));
while (readlink(alternate, realDevice, sizeof(realDevice)) > 0)
{
strcpy(alternate, realDevice);
memset(realDevice, 0, sizeof(realDevice));
}
if (alternate[0] != '/') aDevice = NULL;
setLocationData(mount, pDevice, pDevice, fstype, 0, aDevice);
}
fclose(fp);
// We can ignore the results of this call. But if it works, it at least helps get details
getSizesViaPartitions();
// Now, let's retrieve base partition sizes
if (isEMMCdevice)
{
fp = __popen("fdisk -l /dev/block/mmcblk0","r");
if (fp == NULL)
{
LOGE("=> Unable to retrieve partition sizes via fdisk\n");
return -1;
}
while (fgets(line, sizeof(line), fp) != NULL)
{
char isBoot[64], device[64], blocks[2][16], *pSizeBlock;
unsigned long long size = 0;
if (line[0] != '/') continue;
sscanf(line, "%s %s %*s %s %s", device, isBoot, blocks[0], blocks[1]);
if (isBoot[0] == '*') pSizeBlock = blocks[1];
else pSizeBlock = blocks[0];
// If the block size isn't accurate, don't record it.
if (pSizeBlock[strlen(pSizeBlock)-1] == '+') pSizeBlock = NULL;
// This could be NULL if we decided the size wasn't accurate
if (pSizeBlock)
{
size = ((unsigned long long) atol(pSizeBlock)) * 1024ULL;
}
if (size && (setLocationData(NULL, device, NULL, NULL, size, NULL) == 0))
{
// LOGI(" Mount %s size: %d\n", device, size);
}
}
fclose(fp);
}
return ret;
}
// get locations from our device.info
int getLocationsViaProc(const char* fstype)
{
FILE *fp = NULL;
char line[255];
if (fstype == NULL)
{
LOGE("Invalid argument to getLocationsViaProc\n");
return -1;
}
sprintf(line, "/proc/%s", fstype);
fp = fopen(line, "rt");
if (fp == NULL)
{
LOGW("Device does not support %s\n", line);
return -1;
}
while (fgets(line, sizeof(line), fp) != NULL)
{
char device[32], label[32];
unsigned long size = 0;
char mtdDevice[32];
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 : and " from the details
device[strlen(device)-1] = '\0';
label[strlen(label)-1] = '\0';
if (sscanf(device,"mtd%d", &deviceId) == 1)
{
isMTDdevice = 1;
sprintf(mtdDevice, "%s%s", tw_mtd, device);
sprintf(device, "%smtdblock%d", tw_block, deviceId);
strcpy(tmp.fst,"mtd");
fstype = "mtd";
}
else
{
isEMMCdevice = 1;
strcpy(mtdDevice, device);
fstype = "emmc";
}
if (strcmp(label, "efs") == 0)
fstype = "yaffs2";
setLocationData(label, device, mtdDevice, fstype, (unsigned long long) size, NULL);
}
fclose(fp);
// We still proceed on to use the fstab to query devices as well
return getLocationsViafstab();
}
unsigned long long getUsedSizeViaDu(const char* path)
{
char cmd[512];
sprintf(cmd, "du -sk %s | awk '{ print $1 }'", path);
FILE *fp;
fp = __popen(cmd, "r");
char str[512];
fgets(str, sizeof(str), fp);
__pclose(fp);
unsigned long long size = atol(str);
size *= 1024ULL;
return size;
}
void updateMntUsedSize(struct dInfo* mMnt)
{
char path[512];
if (strcmp(mMnt->mnt, ".android_secure") == 0)
{
// android_secure is a little different - we mount sdcard and use du to figure out how much space is being taken up by android_secure
if (DataManager_GetIntValue(TW_HAS_INTERNAL)) {
// We can ignore any error from this, it doesn't matter
tw_mount(sdcint);
} else {
// We can ignore any error from this, it doesn't matter
tw_mount(sdcext);
}
mMnt->used = getUsedSizeViaDu(mMnt->dev);
mMnt->sze = mMnt->used;
mMnt->bsze = mMnt->used;
return;
}
struct statfs st;
int mounted;
if (!mMnt->mountable)
{
// Since this partition isn't mountable, we're going to mark it's used size as it's standard size
mMnt->used = mMnt->sze;
mMnt->bsze = mMnt->used;
return;
}
mounted = tw_isMounted(*mMnt);
if (!mounted)
{
if (tw_mount(*mMnt)) {
return;
}
}
if (mMnt->sze == 0) // We weren't able to get the size earlier, try another method
getSizeViaDf(mMnt);
sprintf(path, "/%s/.", mMnt->mnt);
if (statfs(path, &st) != 0)
{
if (strncmp(path, "/sd-ext", 7) == 0)
return;
if (DataManager_GetIntValue(TW_HAS_DUAL_STORAGE)) {
char external_mount_point[255];
memset(external_mount_point, 0, sizeof(external_mount_point));
sprintf(external_mount_point, "/%s/.", DataManager_GetStrValue(TW_EXTERNAL_PATH));
if (strcmp(path, external_mount_point) == 0)
return; // This prevents an error from showing on devices that have internal and external storage, but there is no sdcard installed.
sprintf(external_mount_point, "%s/.", DataManager_GetStrValue(TW_EXTERNAL_PATH));
if (strcmp(path, external_mount_point) == 0)
return; // This prevents an error from showing on devices that have internal and external storage, but there is no sdcard installed.
}
LOGE("Unable to stat '%s'\n", path);
return;
}
mMnt->used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
if (DataManager_GetIntValue(TW_HAS_DATA_MEDIA) == 1 && strcmp(mMnt->blk, dat.blk) == 0) {
LOGI("Device has /data/media\n");
unsigned long long data_used, data_media_used, actual_data;
data_used = getUsedSizeViaDu("/data/");
LOGI("Total used space on /data is: %llu\n", data_used);
data_media_used = getUsedSizeViaDu("/data/media/");
LOGI("Total in /data/media is: %llu\n", data_media_used);
actual_data = data_used - data_media_used;
LOGI("Actual data used: %llu\n", actual_data);
mMnt->bsze = actual_data;
} else
mMnt->bsze = mMnt->used;
if (!mounted) tw_unmount(*mMnt);
return;
}
void updateUsedSized()
{
updateMntUsedSize(&boo);
updateMntUsedSize(&sys);
updateMntUsedSize(&dat);
if (DataManager_GetIntValue(TW_HAS_DATADATA) == 1)
updateMntUsedSize(&datdat);
updateMntUsedSize(&cac);
updateMntUsedSize(&rec);
updateMntUsedSize(&sdcext);
updateMntUsedSize(&sdcint);
updateMntUsedSize(&sde);
updateMntUsedSize(&ase);
updateMntUsedSize(&sp1);
updateMntUsedSize(&sp2);
updateMntUsedSize(&sp3);
if (boo.used == 0 && boo.sze == 0) {
DataManager_SetIntValue(TW_HAS_BOOT_PARTITION, 0);
DataManager_SetIntValue(TW_BACKUP_BOOT_VAR, 0);
} else
DataManager_SetIntValue(TW_HAS_BOOT_PARTITION, 1);
if (rec.used == 0 && rec.sze == 0) {
DataManager_SetIntValue(TW_HAS_RECOVERY_PARTITION, 0);
DataManager_SetIntValue(TW_BACKUP_RECOVERY_VAR, 0);
} else
DataManager_SetIntValue(TW_HAS_RECOVERY_PARTITION, 1);
if (sde.used == 0 && sde.sze == 0) {
DataManager_SetIntValue(TW_HAS_SDEXT_PARTITION, 0);
DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 0);
} else
DataManager_SetIntValue(TW_HAS_SDEXT_PARTITION, 1);
if ((ase.used == 0 && ase.sze == 0) || DataManager_GetIntValue(TW_HAS_DATA_MEDIA) == 1) {
DataManager_SetIntValue(TW_HAS_ANDROID_SECURE, 0);
DataManager_SetIntValue(TW_BACKUP_ANDSEC_VAR, 0);
} else
DataManager_SetIntValue(TW_HAS_ANDROID_SECURE, 1);
// Store sizes in data manager for GUI in MB
DataManager_SetIntValue(TW_BACKUP_SYSTEM_SIZE, (int)(sys.bsze / 1048576LLU));
if (DataManager_GetIntValue(TW_HAS_DATADATA) == 1)
DataManager_SetIntValue(TW_BACKUP_DATA_SIZE, (int)(dat.bsze / 1048576LLU) + (int)(datdat.bsze / 1048576LLU));
else
DataManager_SetIntValue(TW_BACKUP_DATA_SIZE, (int)(dat.bsze / 1048576LLU));
DataManager_SetIntValue(TW_BACKUP_BOOT_SIZE, (int)(boo.bsze / 1048576LLU));
DataManager_SetIntValue(TW_BACKUP_RECOVERY_SIZE, (int)(rec.bsze / 1048576LLU));
DataManager_SetIntValue(TW_BACKUP_CACHE_SIZE, (int)(cac.bsze / 1048576LLU));
DataManager_SetIntValue(TW_BACKUP_ANDSEC_SIZE, (int)(ase.bsze / 1048576LLU));
DataManager_SetIntValue(TW_BACKUP_SDEXT_SIZE, (int)(sde.bsze / 1048576LLU));
DataManager_SetIntValue(TW_BACKUP_SP1_SIZE, (int)(sp1.bsze / 1048576LLU));
DataManager_SetIntValue(TW_BACKUP_SP2_SIZE, (int)(sp2.bsze / 1048576LLU));
DataManager_SetIntValue(TW_BACKUP_SP3_SIZE, (int)(sp3.bsze / 1048576LLU));
if (DataManager_GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
DataManager_SetIntValue(TW_STORAGE_FREE_SIZE, (int)((sdcext.sze - sdcext.used) / 1048576LLU));
else if (DataManager_GetIntValue(TW_HAS_DATA_MEDIA) == 1)
DataManager_SetIntValue(TW_STORAGE_FREE_SIZE, (int)((dat.sze - dat.used) / 1048576LLU));
else
DataManager_SetIntValue(TW_STORAGE_FREE_SIZE, (int)((sdcint.sze - sdcint.used) / 1048576LLU));
dumpPartitionTable();
return;
}
void listMntInfo(struct dInfo* mMnt, char* variable_name)
{
LOGI("%s information:\n mnt: '%s'\n blk: '%s'\n dev: '%s'\n alt: '%s'\n fst: '%s'\n fnm: '%s'\n format location: '%s'\n mountable: %i\n backup method: %i\n memory type: %i\n\n", variable_name, mMnt->mnt, mMnt->blk, mMnt->dev, mMnt->alt, mMnt->fst, mMnt->fnm, mMnt->format_location, mMnt->mountable, mMnt->backup, mMnt->memory_type);
}
void update_system_details()
{
ui_print(" * Verifying filesystems...\n");
createFstab();
ui_print(" * Verifying partition sizes...\n");
updateUsedSized();
}
int getLocations()
{
// This decides if a partition can be mounted and appears in the fstab
sys.mountable = 1;
dat.mountable = 1;
datdat.mountable = 1;
cac.mountable = 1;
sde.mountable = 1;
// boo.mountable = 1; // Boot is detected earlier
rec.mountable = 0;
sdcext.mountable = 1;
sdcint.mountable = 1;
ase.mountable = 0;
sp1.mountable = SP1_MOUNTABLE;
sp2.mountable = SP2_MOUNTABLE;
sp3.mountable = SP3_MOUNTABLE;
sys.is_sub_partition = 0;
dat.is_sub_partition = 0;
datdat.is_sub_partition = 1;
cac.is_sub_partition = 0;
sde.is_sub_partition = 0;
boo.is_sub_partition = 0;
rec.is_sub_partition = 0;
sdcext.is_sub_partition = 0;
sdcint.is_sub_partition = 0;
ase.is_sub_partition = 0;
sp1.is_sub_partition = 0;
sp2.is_sub_partition = 0;
sp3.is_sub_partition = 0;
// This decides how we backup/restore a block
sys.backup = files;
dat.backup = files;
datdat.backup = files;
cac.backup = files;
sde.backup = files;
boo.backup = image;
rec.backup = image;
sdcext.backup = none;
sdcint.backup = none;
ase.backup = files;
sp1.backup = SP1_BACKUP_METHOD;
sp2.backup = SP2_BACKUP_METHOD;
sp3.backup = SP3_BACKUP_METHOD;
sys.is_encrypted = 0;
dat.is_encrypted = 0;
datdat.is_encrypted = 0;
cac.is_encrypted = 0;
sde.is_encrypted = 0;
boo.is_encrypted = 0;
rec.is_encrypted = 0;
sdcext.is_encrypted = 0;
sdcint.is_encrypted = 0;
ase.is_encrypted = 0;
sp1.is_encrypted = 0;
sp2.is_encrypted = 0;
sp3.is_encrypted = 0;
if (getLocationsViaProc("emmc") != 0 && getLocationsViaProc("mtd") != 0 && getLocationsViafstab() != 0)
{
LOGE("E: Unable to get device locations.\n");
return -1;
}
// Handle .android_secure
char path[255];
if (DataManager_GetIntValue(TW_HAS_INTERNAL)) {
sprintf(ase.dev, "%s/.android_secure", DataManager_GetSettingsStoragePath());
strcpy(ase.blk, DataManager_GetSettingsStoragePath());
} else {
sprintf(ase.dev, "%s/.android_secure", DataManager_GetStrValue(TW_EXTERNAL_PATH));
strcpy(ase.blk, DataManager_GetStrValue(TW_EXTERNAL_PATH));
}
strcpy(ase.mnt, ".android_secure");
strcpy(ase.fst, "vfat");
if (strlen(sdcext.blk) > 0)
{
int tmpInt;
char tmpBase[50];
char tmpWildCard[50];
// We make the base via sdcard block
strcpy(sde.mnt, "sd-ext");
strcpy(tmpBase, sdcext.blk);
tmpBase[strlen(tmpBase)-1] = '\0';
sprintf(tmpWildCard,"%s%%d",tmpBase);
sscanf(sdcext.blk, tmpWildCard, &tmpInt); // sdcard block used as sd-ext base
sprintf(sde.blk, "%s%d",tmpBase, tmpInt+1);
}
get_device_id();
update_system_details();
// Now, let's update the data manager...
DataManager_SetIntValue("tw_boot_is_mountable", boo.mountable ? 1 : 0);
DataManager_SetIntValue("tw_system_is_mountable", sys.mountable ? 1 : 0);
DataManager_SetIntValue("tw_data_is_mountable", dat.mountable ? 1 : 0);
DataManager_SetIntValue("tw_cache_is_mountable", cac.mountable ? 1 : 0);
DataManager_SetIntValue("tw_sdcext_is_mountable", sdcext.mountable ? 1 : 0);
DataManager_SetIntValue("tw_sdcint_is_mountable", sdcint.mountable ? 1 : 0);
DataManager_SetIntValue("tw_sd-ext_is_mountable", sde.mountable ? 1 : 0);
DataManager_SetIntValue("tw_sp1_is_mountable", sp1.mountable ? 1 : 0);
DataManager_SetIntValue("tw_sp2_is_mountable", sp2.mountable ? 1 : 0);
DataManager_SetIntValue("tw_sp3_is_mountable", sp3.mountable ? 1 : 0);
listMntInfo(&boo, "boot");
listMntInfo(&sys, "system");
listMntInfo(&dat, "data");
if (DataManager_GetIntValue(TW_HAS_DATADATA) == 1)
listMntInfo(&datdat, "datadata");
listMntInfo(&cac, "cache");
listMntInfo(&rec, "recovery");
listMntInfo(&sdcext, "sdcext");
listMntInfo(&sdcint, "sdcint");
listMntInfo(&sde, "sd-ext");
listMntInfo(&ase, "android_secure");
listMntInfo(&sp1, "special 1");
listMntInfo(&sp2, "special 2");
listMntInfo(&sp3, "special 3");
return 0;
}
static void createFstabEntry(FILE* fp, struct dInfo* mnt)
{
char tmpString[255];
struct stat st;
if (!*(mnt->blk))
{
mnt->mountable = 0;
return;
}
if (mnt->alt != NULL && stat(mnt->blk, &st) != 0 && stat(mnt->alt, &st) == 0) {
char switch_block[100];
LOGI("Switching '%s' to alternate '%s'\n", mnt->blk, mnt->alt);
strcpy(switch_block, mnt->blk);
strcpy(mnt->blk, mnt->alt);
strcpy(mnt->dev, mnt->alt);
strcpy(mnt->alt, switch_block);
}
sprintf(tmpString,"%s /%s %s rw\n", mnt->blk, mnt->mnt, mnt->fst);
fputs(tmpString, fp);
sprintf(tmpString, "/%s", mnt->mnt);
// We only create the folder if the block device exists
if (stat(mnt->blk, &st) == 0 && stat(tmpString, &st) != 0)
{
if (mkdir(tmpString, 0777) == -1)
LOGI("=> Can not create %s folder.\n", tmpString);
else
LOGI("=> Created %s folder.\n", tmpString);
}
return;
}
void verifyFst()
{
FILE *fp;
char blkCommand[255];
char blkOutput[255];
char* blk;
char* arg;
char* ptr;
struct dInfo* dat;
// This has a tendency to hang on MTD devices.
if (isMTDdevice) {
struct statfs st;
if (statfs(sde.blk, &st) != 0)
return; // No sd-ext present
sprintf(blkCommand, "blkid %s", sde.blk);
} else
strcpy(blkCommand, "blkid");
LOGI("=> Let's update filesystem types via verifyFst aka blkid, command: '%s'.\n", blkCommand);
fp = __popen(blkCommand,"r");
while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
{
blk = blkOutput;
ptr = blkOutput;
while (*ptr > 32 && *ptr != ':') ptr++;
if (*ptr == 0) continue;
*ptr = 0;
// Increment by two, but verify that we don't hit a NULL
ptr++;
if (*ptr != 0) ptr++;
// Now, find the TYPE field
while (1)
{
arg = ptr;
while (*ptr > 32) ptr++;
if (*ptr != 0)
{
*ptr = 0;
ptr++;
}
if (strlen(arg) > 6)
{
if (memcmp(arg, "TYPE=\"", 6) == 0) break;
}
if (*ptr == 0)
{
arg = NULL;
break;
}
}
if (arg && strlen(arg) > 7)
{
arg += 6; // Skip the TYPE=" portion
arg[strlen(arg)-1] = '\0'; // Drop the tail quote
}
else
continue;
dat = findDeviceByBlockDevice(blk);
if (dat && strcmp(dat->fst, arg) != 0) {
LOGI("'%s' was '%s' now set to '%s'\n", dat->mnt, dat->fst, arg);
strcpy(dat->fst,arg);
}
}
__pclose(fp);
}
// write fstab so we can mount in adb shell
void createFstab()
{
FILE *fp;
fp = fopen("/etc/fstab", "w");
if (fp == NULL)
LOGI("=> Can not open /etc/fstab.\n");
else
{
verifyFst();
if (boo.mountable) createFstabEntry(fp, &boo);
if (sys.mountable) createFstabEntry(fp, &sys);
if (dat.mountable) createFstabEntry(fp, &dat);
if (strcmp(DataManager_GetStrValue(TW_DATA_BLK_DEVICE), "0") == 0)
DataManager_SetStrValue(TW_DATA_BLK_DEVICE, dat.blk);
if (DataManager_GetIntValue(TW_HAS_DATADATA) == 1)
createFstabEntry(fp, &datdat);
if (cac.mountable) createFstabEntry(fp, &cac);
if (sdcext.mountable) createFstabEntry(fp, &sdcext);
if (sdcint.mountable) createFstabEntry(fp, &sdcint);
if (sde.mountable) createFstabEntry(fp, &sde);
if (sp1.mountable) createFstabEntry(fp, &sp1);
if (sp2.mountable) createFstabEntry(fp, &sp2);
if (sp3.mountable) createFstabEntry(fp, &sp3);
}
fclose(fp);
}
char backupToChar(enum backup_method method)
{
switch (method)
{
case unknown:
default:
return 'u';
case none:
return 'n';
case image:
return 'i';
case files:
return 'f';
}
return 'u';
}
void dumpPartitionEntry(struct dInfo* mnt)
{
char* mntName = mnt->mnt;
if (strcmp(mntName, ".android_secure") == 0) mntName = (char*) "andsec";
fprintf(stderr, "| %8s | %27s | %6s | %8lu | %8lu | %d | %c |\n",
mntName, mnt->blk, mnt->fst, (unsigned long) (mnt->sze / 1024), (unsigned long) (mnt->used / 1024), mnt->mountable ? 1 : 0,
backupToChar(mnt->backup));
}
void dumpPartitionTable(void)
{
fprintf(stderr, "+----------+-----------------------------+--------+----------+----------+---+---+\n");
fprintf(stderr, "| Mount | Block Device | fst | Size(KB) | Used(KB) | M | B |\n");
fprintf(stderr, "+----------+-----------------------------+--------+----------+----------+---+---+\n");
dumpPartitionEntry(&tmp);
dumpPartitionEntry(&sys);
dumpPartitionEntry(&dat);
if (DataManager_GetIntValue(TW_HAS_DATADATA) == 1)
dumpPartitionEntry(&datdat);
dumpPartitionEntry(&boo);
dumpPartitionEntry(&rec);
dumpPartitionEntry(&cac);
dumpPartitionEntry(&sdcext);
dumpPartitionEntry(&sdcint);
dumpPartitionEntry(&ase);
dumpPartitionEntry(&sde);
dumpPartitionEntry(&sp1);
dumpPartitionEntry(&sp2);
dumpPartitionEntry(&sp3);
fprintf(stderr, "+----------+-----------------------------+--------+----------+----------+---+---+\n");
}
int decrypt_device(void)
{
#ifdef TW_INCLUDE_CRYPTO
int ret_val, password_len;
char crypto_blkdev[255], password[255];
size_t result;
strcpy(password, DataManager_GetStrValue(TW_CRYPTO_PASSWORD));
property_set("ro.crypto.state", "encrypted");
#ifdef TW_INCLUDE_JB_CRYPTO
// No extra flags needed
#else
property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
#endif
if (cryptfs_check_passwd(password) != 0) {
LOGE("Failed to decrypt data\n");
return -1;
}
property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
if (strcmp(crypto_blkdev, "error") == 0) {
LOGE("Error retrieving decrypted data block device.\n");
} else {
DataManager_SetStrValue(TW_DATA_BLK_DEVICE, dat.blk);
DataManager_SetIntValue(TW_IS_DECRYPTED, 1);
strcpy(dat.blk, crypto_blkdev);
LOGI("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
// Sleep for a bit so that the device will be ready
sleep(1);
update_system_details();
}
return 0;
#else
LOGE("No crypto support was compiled into this build.\n");
return -1;
#endif
}