forked from CyanogenMod/android_bootable_recovery
-
Notifications
You must be signed in to change notification settings - Fork 57
/
nandroid.c
1167 lines (983 loc) · 37.6 KB
/
nandroid.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
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 (C) 2014 The CyanogenMod 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 <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <libgen.h>
#include <limits.h>
#include <linux/input.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/limits.h>
#include <sys/reboot.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "bootloader.h"
#include "common.h"
#include "cutils/properties.h"
#include "extendedcommands.h"
#include "firmware.h"
#include "flashutils/flashutils.h"
#include "install.h"
#include "libcrecovery/common.h"
#include "minui/minui.h"
#include "minzip/DirUtil.h"
#include "mounts.h"
#include "nandroid.h"
#include "nandroid_md5.h"
#include "recovery_settings.h"
#include "recovery_ui.h"
#include "roots.h"
#define NANDROID_FIELD_DEDUPE_CLEARED_SPACE 1
typedef void (*file_event_callback)(const char* filename);
typedef int (*nandroid_backup_handler)(const char* backup_path, const char* backup_file_image, int callback);
typedef int (*nandroid_restore_handler)(const char* backup_file_image, const char* backup_path, int callback);
static int nandroid_backup_bitfield = 0;
static unsigned int nandroid_files_total = 0;
static unsigned int nandroid_files_count = 0;
#ifdef NEED_SELINUX_FIX
static int nochange;
static int verbose;
static int bakupcon_to_file(const char *pathname, const char *filename)
{
int ret = 0;
struct stat sb;
char* filecontext = NULL;
FILE * f = NULL;
if (lstat(pathname, &sb) < 0) {
LOGW("bakupcon_to_file: %s not found\n", pathname);
return -1;
}
if (lgetfilecon(pathname, &filecontext) < 0) {
LOGW("bakupcon_to_file: can't get %s context\n", pathname);
ret = 1;
}
else {
if ((f = fopen(filename, "a+")) == NULL) {
LOGE("bakupcon_to_file: can't create %s\n", filename);
return -1;
}
//fprintf(f, "chcon -h %s '%s'\n", filecontext, pathname);
fprintf(f, "%s\t%s\n", pathname, filecontext);
fclose(f);
freecon(filecontext);
}
//skip read symlink directory
if (S_ISLNK(sb.st_mode)) return 0;
DIR *dir = opendir(pathname);
// not a directory, carry on
if (dir == NULL) return 0;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
char *entryname;
if (!strcmp(entry->d_name, ".."))
continue;
if (!strcmp(entry->d_name, "."))
continue;
if (asprintf(&entryname, "%s/%s", pathname, entry->d_name) == -1)
continue;
if ((is_data_media() && (strncmp(entryname, "/data/media/", 12) == 0)) ||
strncmp(entryname, "/data/data/com.google.android.music/files/", 42) == 0 )
continue;
bakupcon_to_file(entryname, filename);
free(entryname);
}
closedir(dir);
return ret;
}
static int restorecon_from_file(const char *filename)
{
int ret = 0;
FILE * f = NULL;
if ((f = fopen(filename, "r")) == NULL)
{
LOGW("restorecon_from_file: can't open %s\n", filename);
return -1;
}
char linebuf[4096];
while(fgets(linebuf, 4096, f)) {
if (linebuf[strlen(linebuf)-1] == '\n')
linebuf[strlen(linebuf)-1] = '\0';
char *p1, *p2;
char *buf = linebuf;
p1 = strtok(buf, "\t");
if(!p1) continue;
p2 = strtok(NULL, "\t");
if(!p2) continue;
LOGI("%s %s\n", p1, p2);
if (lsetfilecon(p1, p2) < 0) {
LOGE("restorecon_from_file: can't setfilecon %s\n", p1);
ret++;
}
}
fclose(f);
return ret;
}
#endif
static void nandroid_generate_timestamp_path(char* backup_path) {
time_t t = time(NULL);
struct tm *tmp = localtime(&t);
if (tmp == NULL) {
struct timeval tp;
gettimeofday(&tp, NULL);
snprintf(backup_path, PATH_MAX, "%s/clockworkmod/backup/%ld", get_primary_storage_path(), tp.tv_sec);
} else {
char str[PATH_MAX];
strftime(str, PATH_MAX, "clockworkmod/backup/%F.%H.%M.%S", tmp);
snprintf(backup_path, PATH_MAX, "%s/%s", get_primary_storage_path(), str);
}
}
static void ensure_directory(const char* dir) {
char tmp[PATH_MAX];
sprintf(tmp, "mkdir -p %s && chmod 777 %s", dir, dir);
__system(tmp);
}
static int print_and_error(const char* message, int ret) {
ui_reset_progress();
ui_set_background(BACKGROUND_ICON_ERROR);
if (message != NULL)
LOGE("%s", message); // Assumes message has line termination
return ret;
}
static void nandroid_callback(const char* filename) {
if (filename == NULL)
return;
char tmp[PATH_MAX];
strcpy(tmp, filename);
if (tmp[strlen(tmp) - 1] == '\n')
tmp[strlen(tmp) - 1] = '\0';
LOGI("%s\n", tmp);
if (nandroid_files_total != 0) {
nandroid_files_count++;
float progress_decimal = (float)((double)nandroid_files_count /
(double)nandroid_files_total);
ui_set_progress(progress_decimal);
}
}
static void compute_directory_stats(const char* directory) {
char tmp[PATH_MAX];
char count_text[100];
// reset file count if we ever return before setting it
nandroid_files_count = 0;
nandroid_files_total = 0;
sprintf(tmp, "find %s | %s wc -l > /tmp/dircount", directory, strcmp(directory, "/data") == 0 && is_data_media() ? "grep -v /data/media |" : "");
__system(tmp);
FILE* f = fopen("/tmp/dircount", "r");
if (f == NULL)
return;
if (fgets(count_text, sizeof(count_text), f) == NULL) {
fclose(f);
return;
}
size_t len = strlen(count_text);
if (count_text[len - 1] == '\n')
count_text[len - 1] = '\0';
fclose(f);
nandroid_files_total = atoi(count_text);
ui_reset_progress();
ui_show_progress(1, 0);
}
static int mkyaffs2image_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
char tmp[PATH_MAX];
sprintf(tmp, "cd %s ; mkyaffs2image . %s.img ; exit $?", backup_path, backup_file_image);
FILE *fp = __popen(tmp, "r");
if (fp == NULL) {
ui_print("Unable to execute mkyaffs2image.\n");
return -1;
}
while (fgets(tmp, PATH_MAX, fp) != NULL) {
tmp[PATH_MAX - 1] = '\0';
if (callback)
nandroid_callback(tmp);
}
return __pclose(fp);
}
static int do_tar_compress(char* command, int callback) {
char buf[PATH_MAX];
set_perf_mode(1);
FILE *fp = __popen(command, "r");
if (fp == NULL) {
ui_print("Unable to execute tar command!\n");
set_perf_mode(0);
return -1;
}
while (fgets(buf, PATH_MAX, fp) != NULL) {
buf[PATH_MAX - 1] = '\0';
if (callback)
nandroid_callback(buf);
}
set_perf_mode(0);
return __pclose(fp);
}
static int tar_compress_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
char tmp[PATH_MAX];
sprintf(tmp, "cd $(dirname %s) ; touch %s.tar ; set -o pipefail ; (tar -cpv --exclude=data/data/com.google.android.music/files/* %s $(basename %s) | split -a 1 -b 1000000000 /proc/self/fd/0 %s.tar.) 2> /proc/self/fd/1 ; exit $?", backup_path, backup_file_image, strcmp(backup_path, "/data") == 0 && is_data_media() ? "--exclude=data/media" : "", backup_path, backup_file_image);
return do_tar_compress(tmp, callback);
}
static int tar_gzip_compress_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
char tmp[PATH_MAX];
sprintf(tmp, "cd $(dirname %s) ; touch %s.tar.gz ; set -o pipefail ; (tar -cpv --exclude=data/data/com.google.android.music/files/* %s $(basename %s) | pigz -c | split -a 1 -b 1000000000 /proc/self/fd/0 %s.tar.gz.) 2> /proc/self/fd/1 ; exit $?", backup_path, backup_file_image, strcmp(backup_path, "/data") == 0 && is_data_media() ? "--exclude=data/media" : "", backup_path, backup_file_image);
return do_tar_compress(tmp, callback);
}
static int tar_dump_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
char tmp[PATH_MAX];
sprintf(tmp, "cd $(dirname %s); set -o pipefail ; tar -cpv --exclude=data/data/com.google.android.music/files/* %s $(basename %s) 2> /dev/null | cat", backup_path, strcmp(backup_path, "/data") == 0 && is_data_media() ? "--exclude=data/media" : "", backup_path);
return __system(tmp);
}
void nandroid_dedupe_gc(const char* blob_dir) {
char backup_dir[PATH_MAX];
strcpy(backup_dir, blob_dir);
char *d = dirname(backup_dir);
strcpy(backup_dir, d);
strcat(backup_dir, "/backup");
ui_print("Freeing space...\n");
char tmp[PATH_MAX];
sprintf(tmp, "dedupe gc %s $(find %s -name '*.dup')", blob_dir, backup_dir);
__system(tmp);
ui_print("Done freeing space.\n");
}
static int dedupe_compress_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
char tmp[PATH_MAX];
char blob_dir[PATH_MAX];
strcpy(blob_dir, backup_file_image);
char *d = dirname(blob_dir);
strcpy(blob_dir, d);
d = dirname(blob_dir);
strcpy(blob_dir, d);
d = dirname(blob_dir);
strcpy(blob_dir, d);
strcat(blob_dir, "/blobs");
ensure_directory(blob_dir);
if (!(nandroid_backup_bitfield & NANDROID_FIELD_DEDUPE_CLEARED_SPACE)) {
nandroid_backup_bitfield |= NANDROID_FIELD_DEDUPE_CLEARED_SPACE;
nandroid_dedupe_gc(blob_dir);
}
sprintf(tmp, "dedupe c %s %s %s.dup %s", backup_path, blob_dir, backup_file_image, strcmp(backup_path, "/data") == 0 && is_data_media() ? "./media" : "");
FILE *fp = __popen(tmp, "r");
if (fp == NULL) {
ui_print("Unable to execute dedupe.\n");
return -1;
}
while (fgets(tmp, PATH_MAX, fp) != NULL) {
tmp[PATH_MAX - 1] = '\0';
if (callback)
nandroid_callback(tmp);
}
return __pclose(fp);
}
static void build_configuration_path(char *path_buf, const char *file) {
//sprintf(path_buf, "%s%s%s", get_primary_storage_path(), (is_data_media() ? "/0/" : "/"), file);
sprintf(path_buf, "%s/%s", get_primary_storage_path(), file);
}
static nandroid_backup_handler default_backup_handler = tar_compress_wrapper;
static char forced_backup_format[5] = "";
void nandroid_force_backup_format(const char* fmt) {
strcpy(forced_backup_format, fmt);
}
static void refresh_default_backup_handler() {
char fmt[5];
if (strlen(forced_backup_format) > 0) {
strcpy(fmt, forced_backup_format);
} else {
char path[PATH_MAX];
build_configuration_path(path, NANDROID_BACKUP_FORMAT_FILE);
ensure_path_mounted(path);
FILE* f = fopen(path, "r");
if (NULL == f) {
default_backup_handler = tar_compress_wrapper;
return;
}
fread(fmt, 1, sizeof(fmt), f);
fclose(f);
}
fmt[3] = '\0';
if (0 == strcmp(fmt, "dup"))
default_backup_handler = dedupe_compress_wrapper;
else if (0 == strcmp(fmt, "tgz"))
default_backup_handler = tar_gzip_compress_wrapper;
else if (0 == strcmp(fmt, "tar"))
default_backup_handler = tar_compress_wrapper;
else
default_backup_handler = tar_compress_wrapper;
}
unsigned int nandroid_get_default_backup_format() {
refresh_default_backup_handler();
if (default_backup_handler == dedupe_compress_wrapper) {
return NANDROID_BACKUP_FORMAT_DUP;
} else if (default_backup_handler == tar_gzip_compress_wrapper) {
return NANDROID_BACKUP_FORMAT_TGZ;
} else {
return NANDROID_BACKUP_FORMAT_TAR;
}
}
static nandroid_backup_handler get_backup_handler(const char *backup_path) {
Volume *v = volume_for_path(backup_path);
if (v == NULL) {
ui_print("Unable to find volume.\n");
return NULL;
}
const MountedVolume *mv = find_mounted_volume_by_mount_point(v->mount_point);
if (mv == NULL) {
ui_print("Unable to find mounted volume: %s\n", v->mount_point);
return NULL;
}
if (strcmp(backup_path, "/data") == 0 && is_data_media()) {
return default_backup_handler;
}
if (strlen(forced_backup_format) > 0)
return default_backup_handler;
// Disable tar backups of yaffs2 by default
char prefer_tar[PROPERTY_VALUE_MAX];
property_get("ro.cwm.prefer_tar", prefer_tar, "false");
if (strcmp("yaffs2", mv->filesystem) == 0 && strcmp("false", prefer_tar) == 0) {
return mkyaffs2image_wrapper;
}
return default_backup_handler;
}
static int nandroid_backup_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
int ret = 0;
char name[PATH_MAX];
char tmp[PATH_MAX];
strcpy(name, basename(mount_point));
struct stat file_info;
build_configuration_path(tmp, NANDROID_HIDE_PROGRESS_FILE);
ensure_path_mounted(tmp);
int callback = stat(tmp, &file_info) != 0;
ui_print("Backing up %s...\n", name);
if (0 != (ret = ensure_path_mounted(mount_point) != 0)) {
ui_print("Can't mount %s!\n", mount_point);
return ret;
}
compute_directory_stats(mount_point);
scan_mounted_volumes();
Volume *v = volume_for_path(mount_point);
const MountedVolume *mv = NULL;
if (v != NULL)
mv = find_mounted_volume_by_mount_point(v->mount_point);
if (strcmp(backup_path, "-") == 0)
sprintf(tmp, "/proc/self/fd/1");
else if (mv == NULL || mv->filesystem == NULL)
sprintf(tmp, "%s/%s.auto", backup_path, name);
else
sprintf(tmp, "%s/%s.%s", backup_path, name, mv->filesystem);
nandroid_backup_handler backup_handler = get_backup_handler(mount_point);
if (backup_handler == NULL) {
ui_print("Error finding an appropriate backup handler.\n");
return -2;
}
ret = backup_handler(mount_point, tmp, callback);
if (umount_when_finished) {
ensure_path_unmounted(mount_point);
}
if (0 != ret) {
ui_print("Error while making a backup image of %s!\n", mount_point);
return ret;
}
ui_print("Backup of %s completed.\n", name);
return 0;
}
static int nandroid_backup_partition(const char* backup_path, const char* root) {
Volume *vol = volume_for_path(root);
// make sure the volume exists before attempting anything...
if (vol == NULL || vol->fs_type == NULL)
return 0;
// see if we need a raw backup (mtd)
char tmp[PATH_MAX];
int ret;
if (strcmp(vol->fs_type, "mtd") == 0 ||
strcmp(vol->fs_type, "bml") == 0 ||
strcmp(vol->fs_type, "emmc") == 0) {
const char* name = basename(root);
if (strcmp(backup_path, "-") == 0)
strcpy(tmp, "/proc/self/fd/1");
else
sprintf(tmp, "%s/%s.img", backup_path, name);
ui_print("Backing up %s image...\n", name);
if (0 != (ret = backup_raw_partition(vol->fs_type, vol->blk_device, tmp))) {
ui_print("Error while backing up %s image!", name);
return ret;
}
ui_print("Backup of %s image completed.\n", name);
return 0;
}
return nandroid_backup_partition_extended(backup_path, root, 1);
}
int nandroid_backup(const char* backup_path) {
nandroid_backup_bitfield = 0;
refresh_default_backup_handler();
if (ensure_path_mounted(backup_path) != 0) {
return print_and_error("Can't mount backup path.\n", NANDROID_ERROR_GENERAL);
}
Volume* volume;
if (is_data_media_volume_path(backup_path))
volume = volume_for_path("/data");
else
volume = volume_for_path(backup_path);
if (NULL == volume)
return print_and_error("Unable to find volume for backup path.\n", NANDROID_ERROR_GENERAL);
int ret;
struct statfs sfs;
struct stat s;
if (NULL != volume) {
if (0 != (ret = statfs(volume->mount_point, &sfs)))
return print_and_error("Unable to stat backup path.\n", ret);
uint64_t bavail = sfs.f_bavail;
uint64_t bsize = sfs.f_bsize;
uint64_t sdcard_free = bavail * bsize;
uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024);
ui_print("SD Card space free: %lluMB\n", sdcard_free_mb);
if (sdcard_free_mb < 150)
ui_print("There may not be enough free space to complete backup... continuing...\n");
}
char tmp[PATH_MAX];
ensure_directory(backup_path);
ui_set_background(BACKGROUND_ICON_INSTALLING);
if (0 != (ret = nandroid_backup_partition(backup_path, "/boot")))
return print_and_error(NULL, ret);
if (0 != (ret = nandroid_backup_partition(backup_path, "/recovery")))
return print_and_error(NULL, ret);
Volume *vol = volume_for_path("/wimax");
if (vol != NULL && 0 == stat(vol->blk_device, &s)) {
char serialno[PROPERTY_VALUE_MAX];
ui_print("Backing up WiMAX...\n");
serialno[0] = 0;
property_get("ro.serialno", serialno, "");
sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno);
ret = backup_raw_partition(vol->fs_type, vol->blk_device, tmp);
if (0 != ret)
return print_and_error("Error while dumping WiMAX image!\n", NANDROID_ERROR_GENERAL);
}
if (0 != (ret = nandroid_backup_partition(backup_path, "/system")))
return print_and_error(NULL, ret);
if (volume_for_path("/preload") != NULL)
{
if (0 != (ret = nandroid_backup_partition(backup_path, "/preload")))
return print_and_error(NULL, ret);
}
if (0 != (ret = nandroid_backup_partition(backup_path, "/data")))
return print_and_error(NULL, ret);
if (has_datadata()) {
if (0 != (ret = nandroid_backup_partition(backup_path, "/datadata")))
return print_and_error(NULL, ret);
}
if (0 != stat(get_android_secure_path(), &s)) {
ui_print("No .android_secure found. Skipping backup of applications on external storage.\n");
} else {
if (0 != (ret = nandroid_backup_partition_extended(backup_path, get_android_secure_path(), 0)))
return print_and_error(NULL, ret);
}
if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/cache", 0)))
return print_and_error(NULL, ret);
vol = volume_for_path("/sd-ext");
if (vol == NULL || 0 != stat(vol->blk_device, &s)) {
LOGI("No sd-ext found. Skipping backup of sd-ext.\n");
} else {
if (0 != ensure_path_mounted("/sd-ext"))
LOGI("Could not mount sd-ext. sd-ext backup may not be supported on this device. Skipping backup of sd-ext.\n");
else if (0 != (ret = nandroid_backup_partition(backup_path, "/sd-ext")))
return print_and_error(NULL, ret);
}
if (0 != (ret = nandroid_backup_md5_gen(backup_path)))
return print_and_error(NULL, ret);
sprintf(tmp, "cp /tmp/recovery.log %s/recovery.log", backup_path);
__system(tmp);
char base_dir[PATH_MAX];
strcpy(base_dir, backup_path);
char *d = dirname(base_dir);
strcpy(base_dir, d);
d = dirname(base_dir);
strcpy(base_dir, d);
sprintf(tmp, "chmod -R 777 %s ; chmod -R u+r,u+w,g+r,g+w,o+r,o+w %s ; chmod u+x,g+x,o+x %s/backup", backup_path, base_dir, base_dir);
__system(tmp);
sprintf(tmp, "if [ -d %s/blobs ]; then chmod u+x,g+x,o+x %s/blobs; fi", base_dir, base_dir);
__system(tmp);
sync();
ui_set_background(BACKGROUND_ICON_CLOCKWORK);
ui_reset_progress();
ui_print("\nBackup complete!\n");
return 0;
}
static int nandroid_dump(const char* partition) {
// silence our ui_print statements and other logging
ui_set_log_stdout(0);
nandroid_backup_bitfield = 0;
refresh_default_backup_handler();
// override our default to be the basic tar dumper
default_backup_handler = tar_dump_wrapper;
if (strcmp(partition, "boot") == 0) {
Volume *vol = volume_for_path("/boot");
// make sure the volume exists before attempting anything...
if (vol == NULL || vol->fs_type == NULL)
return 1;
char cmd[PATH_MAX];
sprintf(cmd, "cat %s", vol->blk_device);
return __system(cmd);
// return nandroid_backup_partition("-", "/boot");
}
if (strcmp(partition, "recovery") == 0) {
return __system("set -o pipefail ; dump_image recovery /proc/self/fd/1 | cat");
}
if (strcmp(partition, "data") == 0) {
return nandroid_backup_partition("-", "/data");
}
if (strcmp(partition, "system") == 0) {
return nandroid_backup_partition("-", "/system");
}
return 1;
}
static int unyaffs_wrapper(const char* backup_file_image, const char* backup_path, int callback) {
char tmp[PATH_MAX];
sprintf(tmp, "cd %s ; unyaffs %s ; exit $?", backup_path, backup_file_image);
FILE *fp = __popen(tmp, "r");
if (fp == NULL) {
ui_print("Unable to execute unyaffs.\n");
return -1;
}
while (fgets(tmp, PATH_MAX, fp) != NULL) {
tmp[PATH_MAX - 1] = '\0';
if (callback)
nandroid_callback(tmp);
}
return __pclose(fp);
}
static int do_tar_extract(char* command, int callback) {
char buf[PATH_MAX];
set_perf_mode(1);
FILE *fp = __popen(command, "r");
if (fp == NULL) {
ui_print("Unable to execute tar command.\n");
set_perf_mode(0);
return -1;
}
while (fgets(buf, PATH_MAX, fp) != NULL) {
buf[PATH_MAX - 1] = '\0';
if (callback)
nandroid_callback(buf);
}
set_perf_mode(0);
return __pclose(fp);
}
static int tar_gzip_extract_wrapper(const char* backup_file_image, const char* backup_path, int callback) {
char tmp[PATH_MAX];
sprintf(tmp, "cd $(dirname %s) ; set -o pipefail ; cat %s* | pigz -d -c | tar -xpv ; exit $?", backup_path, backup_file_image);
return do_tar_extract(tmp, callback);
}
static int tar_extract_wrapper(const char* backup_file_image, const char* backup_path, int callback) {
char tmp[PATH_MAX];
sprintf(tmp, "cd $(dirname %s) ; set -o pipefail ; cat %s* | tar -xpv ; exit $?", backup_path, backup_file_image);
return do_tar_extract(tmp, callback);
}
static int dedupe_extract_wrapper(const char* backup_file_image, const char* backup_path, int callback) {
char tmp[PATH_MAX];
char blob_dir[PATH_MAX];
strcpy(blob_dir, backup_file_image);
char *bd = dirname(blob_dir);
strcpy(blob_dir, bd);
bd = dirname(blob_dir);
strcpy(blob_dir, bd);
bd = dirname(blob_dir);
sprintf(tmp, "dedupe x %s %s/blobs %s; exit $?", backup_file_image, bd, backup_path);
char path[PATH_MAX];
FILE *fp = __popen(tmp, "r");
if (fp == NULL) {
ui_print("Unable to execute dedupe.\n");
return -1;
}
while (fgets(path, PATH_MAX, fp) != NULL) {
if (callback)
nandroid_callback(path);
}
return __pclose(fp);
}
static int tar_undump_wrapper(const char* backup_file_image, const char* backup_path, int callback) {
char tmp[PATH_MAX];
sprintf(tmp, "cd $(dirname %s) ; tar -xpv ", backup_path);
return __system(tmp);
}
static nandroid_restore_handler get_restore_handler(const char *backup_path) {
Volume *v = volume_for_path(backup_path);
if (v == NULL) {
ui_print("Unable to find volume.\n");
return NULL;
}
scan_mounted_volumes();
const MountedVolume *mv = find_mounted_volume_by_mount_point(v->mount_point);
if (mv == NULL) {
ui_print("Unable to find mounted volume: %s\n", v->mount_point);
return NULL;
}
if (strcmp(backup_path, "/data") == 0 && is_data_media()) {
return tar_extract_wrapper;
}
// Disable tar backups of yaffs2 by default
char prefer_tar[PROPERTY_VALUE_MAX];
property_get("ro.cwm.prefer_tar", prefer_tar, "false");
if (strcmp("yaffs2", mv->filesystem) == 0 && strcmp("false", prefer_tar) == 0) {
return unyaffs_wrapper;
}
return tar_extract_wrapper;
}
static int nandroid_restore_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
int ret = 0;
char* name = basename(mount_point);
nandroid_restore_handler restore_handler = NULL;
const char *filesystems[] = { "yaffs2", "ext2", "ext3", "ext4", "vfat", "rfs", "f2fs", "exfat", NULL };
const char* backup_filesystem = NULL;
Volume *vol = volume_for_path(mount_point);
const char *device = NULL;
if (vol != NULL)
device = vol->blk_device;
char tmp[PATH_MAX];
sprintf(tmp, "%s/%s.img", backup_path, name);
struct stat file_info;
if (strcmp(backup_path, "-") == 0) {
if (vol)
backup_filesystem = vol->fs_type;
restore_handler = tar_extract_wrapper;
strcpy(tmp, "/proc/self/fd/0");
} else if (0 != (ret = stat(tmp, &file_info))) {
// can't find the backup, it may be the new backup format?
// iterate through the backup types
//printf("couldn't find default\n");
const char *filesystem;
int i = 0;
while ((filesystem = filesystems[i]) != NULL) {
sprintf(tmp, "%s/%s.%s.img", backup_path, name, filesystem);
if (0 == (ret = stat(tmp, &file_info))) {
backup_filesystem = filesystem;
restore_handler = unyaffs_wrapper;
break;
}
sprintf(tmp, "%s/%s.%s.tar", backup_path, name, filesystem);
if (0 == (ret = stat(tmp, &file_info))) {
backup_filesystem = filesystem;
restore_handler = tar_extract_wrapper;
break;
}
sprintf(tmp, "%s/%s.%s.tar.gz", backup_path, name, filesystem);
if (0 == (ret = stat(tmp, &file_info))) {
backup_filesystem = filesystem;
restore_handler = tar_gzip_extract_wrapper;
break;
}
sprintf(tmp, "%s/%s.%s.dup", backup_path, name, filesystem);
if (0 == (ret = stat(tmp, &file_info))) {
backup_filesystem = filesystem;
restore_handler = dedupe_extract_wrapper;
break;
}
i++;
}
if (backup_filesystem == NULL || restore_handler == NULL) {
ui_print("%s file not found. Skipping restore of %s.\n", name, mount_point);
return 0;
} else {
printf("Found new backup image: %s\n", tmp);
}
}
// If the fs_type of this volume is "auto" or mount_point is /data
// and is_data_media, let's revert
// to using a rm -rf, rather than trying to do a
// ext3/ext4/whatever format.
// This is because some phones (like DroidX) will freak out if you
// reformat the /system or /data partitions, and not boot due to
// a locked bootloader.
// Other devices, like the Galaxy Nexus, XOOM, and Galaxy Tab 10.1
// have a /sdcard symlinked to /data/media.
// Or of volume does not exist (.android_secure), just rm -rf.
if (vol == NULL || 0 == strcmp(vol->fs_type, "auto"))
backup_filesystem = NULL;
else if (0 == strcmp(vol->mount_point, "/data") && is_data_media())
backup_filesystem = NULL;
ensure_directory(mount_point);
char path[PATH_MAX];
build_configuration_path(path, NANDROID_HIDE_PROGRESS_FILE);
ensure_path_mounted(path);
int callback = stat(path, &file_info) != 0;
ui_print("Restoring %s...\n", name);
if (backup_filesystem == NULL) {
if (0 != (ret = format_volume(mount_point))) {
ui_print("Error while formatting %s!\n", mount_point);
return ret;
}
} else if (0 != (ret = format_device(device, mount_point, backup_filesystem))) {
ui_print("Error while formatting %s!\n", mount_point);
return ret;
}
if (0 != (ret = ensure_path_mounted(mount_point))) {
ui_print("Can't mount %s!\n", mount_point);
return ret;
}
if (restore_handler == NULL)
restore_handler = get_restore_handler(mount_point);
// override restore handler for undump
if (strcmp(backup_path, "-") == 0) {
restore_handler = tar_undump_wrapper;
}
if (restore_handler == NULL) {
ui_print("Error finding an appropriate restore handler.\n");
return -2;
}
if (0 != (ret = restore_handler(tmp, mount_point, callback))) {
ui_print("Error while restoring %s!\n", mount_point);
return ret;
}
#ifdef NEED_SELINUX_FIX
if (strcmp(backup_path, "-") == 0 || restore_handler == dedupe_extract_wrapper)
LOGI("don't need restore selinux context.\n");
else if (0 == strcmp(mount_point, "/data") ||
0 == strcmp(mount_point, "/system") ||
0 == strcmp(mount_point, "/cache"))
{
name = basename(mount_point);
sprintf(tmp, "%s/%s.context", backup_path, name);
struct stat sb;
if (lstat(tmp, &sb) == 0) {
ui_print("Restoring selinux context...\n");
ret = restorecon_from_file(tmp);
ui_print("Restore selinux context completed.\n");
}
}
#endif
if (umount_when_finished) {
ensure_path_unmounted(mount_point);
}
return 0;
}
static int nandroid_restore_partition(const char* backup_path, const char* root) {
Volume *vol = volume_for_path(root);
// make sure the volume exists...
if (vol == NULL || vol->fs_type == NULL)
return 0;
// see if we need a raw restore (mtd)
char tmp[PATH_MAX];
if (strcmp(vol->fs_type, "mtd") == 0 ||
strcmp(vol->fs_type, "bml") == 0 ||
strcmp(vol->fs_type, "emmc") == 0) {
int ret;
const char* name = basename(root);
ui_print("Erasing %s before restore...\n", name);
if (0 != (ret = format_volume(root))) {
ui_print("Error while erasing %s image!", name);
return ret;
}
if (strcmp(backup_path, "-") == 0)
strcpy(tmp, backup_path);
else
sprintf(tmp, "%s%s.img", backup_path, root);
ui_print("Restoring %s image...\n", name);
if (0 != (ret = restore_raw_partition(vol->fs_type, vol->blk_device, tmp))) {
ui_print("Error while flashing %s image!\n", name);
return ret;
}
return 0;
}
return nandroid_restore_partition_extended(backup_path, root, 1);
}
int nandroid_restore(const char* backup_path, unsigned char flags) {
ui_set_background(BACKGROUND_ICON_INSTALLING);
ui_show_indeterminate_progress();
nandroid_files_total = 0;
int ret;
int restore_boot = ((flags & NANDROID_BOOT) == NANDROID_BOOT);
int restore_system = ((flags & NANDROID_SYSTEM) == NANDROID_SYSTEM);
int restore_data = ((flags & NANDROID_DATA) == NANDROID_DATA);
int restore_cache = ((flags & NANDROID_CACHE) == NANDROID_CACHE);
int restore_sdext = ((flags & NANDROID_SDEXT) == NANDROID_SDEXT);
int restore_wimax = ((flags & NANDROID_WIMAX) == NANDROID_WIMAX);
if (ensure_path_mounted(backup_path) != 0)
return print_and_error("Can't mount backup path\n", NANDROID_ERROR_GENERAL);
struct stat s;
char tmp[PATH_MAX];
ensure_path_mounted("/sdcard");
if (0 == stat("/sdcard/clockworkmod/.no_md5sum", &s))
ui_print("Skip Check MD5...\n");
else {
if (0 != (ret = nandroid_restore_md5_check(backup_path, flags)))
return print_and_error(NULL, ret);
}
if (restore_boot && NULL != volume_for_path("/boot") && 0 != (ret = nandroid_restore_partition(backup_path, "/boot")))
return print_and_error(NULL, ret);
//struct stat s;
Volume *vol = volume_for_path("/wimax");
if (restore_wimax && vol != NULL && 0 == stat(vol->blk_device, &s)) {
char serialno[PROPERTY_VALUE_MAX];
serialno[0] = 0;
property_get("ro.serialno", serialno, "");
sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno);
struct stat st;
if (0 != stat(tmp, &st)) {
ui_print("WARNING: WiMAX partition exists, but nandroid\n");
ui_print(" backup does not contain WiMAX image.\n");
ui_print(" You should create a new backup to\n");
ui_print(" protect your WiMAX keys.\n");
} else {
ui_print("Erasing WiMAX before restore...\n");
if (0 != (ret = format_volume("/wimax")))
return print_and_error("Error while formatting wimax!\n", NANDROID_ERROR_GENERAL);
ui_print("Restoring WiMAX image...\n");
if (0 != (ret = restore_raw_partition(vol->fs_type, vol->blk_device, tmp)))
return print_and_error(NULL, ret);
}
}
if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "/system")))
return print_and_error(NULL, ret);
if (volume_for_path("/preload") != NULL)
{
if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "/preload")))
return print_and_error(NULL, ret);
}