forked from PhilZ-cwm6/philz_touch_cwm6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extendedcommands.c
4808 lines (4275 loc) · 171 KB
/
extendedcommands.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
/*
PhilZ Touch - touch_gui library
Copyright (C) <2014> <phytowardt@gmail.com>
This file is part of PhilZ Touch Recovery
PhilZ Touch 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.
PhilZ Touch 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 PhilZ Touch. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/limits.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <selinux/selinux.h>
#include <selinux/label.h>
#include "cutils/android_reboot.h"
#include "cutils/properties.h"
#include "libcrecovery/common.h"
#include "voldclient/voldclient.h"
#include "common.h"
#include "install.h"
#include "make_ext4fs.h"
#include "recovery_ui.h"
#include "roots.h"
#include "ui.h"
#include "extendedcommands.h"
#include "advanced_functions.h"
#include "recovery_settings.h"
#include "nandroid.h"
#include "adb_install.h"
// md5 display
#include <pthread.h>
#include "digest/md5.h"
#ifdef PHILZ_TOUCH_RECOVERY
#include "libtouch_gui/gui_settings.h"
#endif
extern struct selabel_handle *sehandle;
// ignore_android_secure = 1: this will force skipping android secure from backup/restore jobs
static int ignore_android_secure = 0;
int get_filtered_menu_selection(const char** headers, char** items, int menu_only, int initial_selection, int items_count) {
int index;
int offset = 0;
int* translate_table = (int*)malloc(sizeof(int) * items_count);
char* items_new[items_count];
for (index = 0; index < items_count; index++) {
items_new[index] = items[index];
}
for (index = 0; index < items_count; index++) {
if (items_new[index] == NULL)
continue;
char *item = items_new[index];
items_new[index] = NULL;
items_new[offset] = item;
translate_table[offset] = index;
offset++;
}
items_new[offset] = NULL;
initial_selection = translate_table[initial_selection];
int ret = get_menu_selection(headers, items_new, menu_only, initial_selection);
if (ret < 0 || ret >= offset) {
free(translate_table);
return ret;
}
ret = translate_table[ret];
free(translate_table);
return ret;
}
// pass in NULL for fileExtensionOrDirectory and you will get a directory chooser
// pass in "" to gather all files without filtering extension or filename
// returned directory (when NULL is passed as file extension) has a trailing / causing a wired // return path
// choose_file_menu returns NULL when no file is found or if we choose no file in selection
// no_files_found = 1 when no valid file was found, no_files_found = 0 when we found a valid file
// WARNING : CALLER MUST ALWAYS FREE THE RETURNED POINTER
int no_files_found = 0;
char* choose_file_menu(const char* basedir, const char* fileExtensionOrDirectory, const char* headers[]) {
const char* fixed_headers[20];
int numFiles = 0;
int numDirs = 0;
int i;
char* return_value = NULL;
char directory[PATH_MAX];
int dir_len = strlen(basedir);
strcpy(directory, basedir);
// Append a trailing slash if necessary
if (directory[dir_len - 1] != '/') {
strcat(directory, "/");
dir_len++;
}
i = 0;
while (headers[i]) {
fixed_headers[i] = headers[i];
i++;
}
fixed_headers[i] = directory;
// let's spare some header space
// fixed_headers[i + 1] = "";
// fixed_headers[i + 2] = NULL;
fixed_headers[i + 1] = NULL;
char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles);
char** dirs = NULL;
if (fileExtensionOrDirectory != NULL)
dirs = gather_files(directory, NULL, &numDirs);
int total = numDirs + numFiles;
if (total == 0) {
// we found no valid file to select
no_files_found = 1;
ui_print("No files found.\n");
} else {
// we found a valid file to select
no_files_found = 0;
char** list = (char**)malloc((total + 1) * sizeof(char*));
list[total] = NULL;
for (i = 0; i < numDirs; i++) {
list[i] = strdup(dirs[i] + dir_len);
}
for (i = 0; i < numFiles; i++) {
list[numDirs + i] = strdup(files[i] + dir_len);
}
for (;;) {
int chosen_item = get_menu_selection(fixed_headers, list, 0, 0);
if (chosen_item == GO_BACK || chosen_item == REFRESH)
break;
if (chosen_item < numDirs) {
char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers);
if (subret != NULL) {
// we selected either a folder (or a file from a re-entrant call)
return_value = strdup(subret);
free(subret);
break;
}
// the previous re-entrant call did a GO_BACK, REFRESH or no file found in a directory: subret == NULL
// we drop to up folder
continue;
}
// we selected a file
return_value = strdup(files[chosen_item - numDirs]);
break;
}
free_string_array(list);
}
free_string_array(files);
free_string_array(dirs);
return return_value;
}
int confirm_selection(const char* title, const char* confirm) {
// check if recovery needs no confirm, many confirm or a few confirm menus
char path[PATH_MAX];
int many_confirm;
sprintf(path, "%s/%s", get_primary_storage_path(), RECOVERY_NO_CONFIRM_FILE);
if (file_found(path))
return 1;
sprintf(path, "%s/%s", get_primary_storage_path(), RECOVERY_MANY_CONFIRM_FILE);
many_confirm = file_found(path);
char* confirm_str = strdup(confirm);
const char* confirm_headers[] = { title, " 这个操作不可恢复。", "", NULL };
int ret = 0;
int old_val = ui_is_showing_back_button();
ui_set_showing_back_button(0);
if (many_confirm) {
char* items[] = {
"不",
"不",
"不",
"不",
"不",
"不",
"不",
confirm_str, //" Yes -- wipe partition", // [7]
"不",
"不",
"不",
NULL
};
int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
ret = (chosen_item == 7);
} else {
char* items[] = {
"不",
confirm_str, //" Yes -- wipe partition", // [1]
"不",
NULL
};
int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
ret = (chosen_item == 1);
}
free(confirm_str);
ui_set_showing_back_button(old_val);
return ret;
}
int confirm_with_headers(const char** confirm_headers, const char* confirm) {
// check if recovery needs no confirm, many confirm or a few confirm menus
char path[PATH_MAX];
int many_confirm;
sprintf(path, "%s/%s", get_primary_storage_path(), RECOVERY_NO_CONFIRM_FILE);
if (file_found(path))
return 1;
sprintf(path, "%s/%s", get_primary_storage_path(), RECOVERY_MANY_CONFIRM_FILE);
many_confirm = file_found(path);
char* confirm_str = strdup(confirm);
int ret = 0;
int old_val = ui_is_showing_back_button();
ui_set_showing_back_button(0);
if (many_confirm) {
char* items[] = {
"不",
"不",
"不",
"不",
"不",
"不",
"不",
confirm_str, //" Yes -- wipe partition", // [7]
"不",
"不",
"不",
NULL
};
int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
ret = (chosen_item == 7);
} else {
char* items[] = {
"No",
confirm_str, //" Yes -- wipe partition", // [1]
"No",
NULL
};
int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
ret = (chosen_item == 1);
}
free(confirm_str);
ui_set_showing_back_button(old_val);
return ret;
}
void handle_failure() {
if (0 != ensure_path_mounted(get_primary_storage_path()))
return;
mkdir("/sdcard/clockworkmod", S_IRWXU | S_IRWXG | S_IRWXO);
__system("cp /tmp/recovery.log /sdcard/clockworkmod/philz_recovery.log");
ui_print("/tmp/recovery.log已经复制到/sdcard/clockworkmod/philz_recovery.log\n");
ui_print("发送文件给Phil3759 @xda\n");
}
//start print tail from custom log file
void ui_print_custom_logtail(const char* filename, int nb_lines) {
char * backup_log;
char tmp[PATH_MAX];
FILE * f;
int line=0;
sprintf(tmp, "tail -n %d %s > /tmp/custom_tail.log", nb_lines, filename);
__system(tmp);
f = fopen("/tmp/custom_tail.log", "rb");
if (f != NULL) {
while (line < nb_lines) {
backup_log = fgets(tmp, PATH_MAX, f);
if (backup_log == NULL) break;
ui_print("%s", tmp);
line++;
}
fclose(f);
} else
LOGE("Cannot open /tmp/custom_tail.log\n");
}
/**********************************/
/* Start md5sum display */
/* Original source by PhilZ */
/* MD5 code from twrpDigest */
/* by */
/* bigbiff/Dees_Troy TeamWin */
/**********************************/
// calculate md5sum of filepath
// return 0 on success, 1 if cancelled by user, -1 on failure to open source file
// fills md5sum char array with the resulting md5sum
// functions calling this should first set the progress bar:
// ui_reset_progress();
// ui_show_progress(1, 0);
// and after it is done, reset the progress bar
// ui_reset_progress();
static int cancel_md5digest = 0;
static int computeMD5(const char* filepath, char *md5sum) {
struct MD5Context md5c;
unsigned char md5sum_array[MD5LENGTH];
unsigned char buf[1024];
char hex[3];
unsigned long size_total;
unsigned long size_progress;
unsigned len;
int i;
if (!file_found(filepath)) {
LOGE("computeMD5: '%s' not found\n", filepath);
return -1;
}
FILE *file = fopen(filepath, "rb");
if (file == NULL) {
LOGE("computeMD5: can't open %s\n", filepath);
return -1;
}
size_total = Get_File_Size(filepath);
size_progress = 0;
cancel_md5digest = 0;
MD5Init(&md5c);
while (!cancel_md5digest && (len = fread(buf, 1, sizeof(buf), file)) > 0) {
MD5Update(&md5c, buf, len);
size_progress += len;
if (size_total != 0)
ui_set_progress((float)size_progress / (float)size_total);
}
if (!cancel_md5digest) {
MD5Final(md5sum_array ,&md5c);
strcpy(md5sum, "");
for (i = 0; i < 16; ++i) {
snprintf(hex, 3 ,"%02x", md5sum_array[i]);
strcat(md5sum, hex);
}
}
fclose(file);
return cancel_md5digest;
}
// write calculated md5 to file or to log/screen if md5file is NULL
// returns negative value on failure or total number of bytes written on success (or 0 if only display md5)
// if cancelled by user in thread mode, it returns 1
int write_md5digest(const char* filepath, const char* md5file, int append) {
int ret;
char md5sum[PATH_MAX];
ret = computeMD5(filepath, md5sum);
if (ret != 0)
return ret;
if (md5file == NULL) {
ui_print("%s\n", md5sum);
} else {
char* b = t_BaseName(filepath);
strcat(md5sum, " ");
strcat(md5sum, b);
strcat(md5sum, "\n");
free(b);
if (append)
ret = append_string_to_file(md5file, md5sum);
else
ret = write_string_to_file(md5file, md5sum);
}
return ret;
}
// verify md5sum of filepath if it matches content from md5file
// if md5file == NULL, we try filepath.md5
int verify_md5digest(const char* filepath, const char* md5file) {
char md5file2[PATH_MAX];
int ret = -1;
if (!file_found(filepath)) {
LOGE("verify_md5digest: '%s' not found\n", filepath);
return ret;
}
if (md5file != NULL) {
sprintf(md5file2, "%s", md5file);
} else {
sprintf(md5file2, "%s.md5", filepath);
}
// read md5 sum from md5file2
// read_file_to_buffer() will call file_found() function
unsigned long len = 0;
char* md5read = read_file_to_buffer(md5file2, &len);
if (md5read == NULL)
return ret;
md5read[len] = '\0';
// calculate md5sum of filepath and check if it matches what we read from md5file2
// computeMD5() != 0 if cancelled by user in multi-threading mode (1) or if file not found (-1)
// file.md5 is formatted like (new line at end):
// 264c7c1e6f682cb99a07c283117f7f07 test_code.c\n
char md5sum[PATH_MAX];
if (0 == (ret = computeMD5(filepath, md5sum))) {
char* b = t_BaseName(filepath);
strcat(md5sum, " ");
strcat(md5sum, b);
strcat(md5sum, "\n");
free(b);
if (strcmp(md5read, md5sum) != 0) {
LOGE("MD5 calc: %s\n", md5sum);
LOGE("Expected: %s\n", md5read);
ret = -1;
}
}
free(md5read);
return ret;
}
// thread function called when installing zip files from menu
pthread_t tmd5_display;
pthread_t tmd5_verify;
static void *md5_display_thread(void *arg) {
char filepath[PATH_MAX];
ui_reset_progress();
ui_show_progress(1, 0);
sprintf(filepath, "%s", (char*)arg);
write_md5digest(filepath, NULL, 0);
ui_reset_progress();
return NULL;
}
static void *md5_verify_thread(void *arg) {
int ret;
char filepath[PATH_MAX];
sprintf(filepath, "%s", (char*)arg);
ui_reset_progress();
ui_show_progress(1, 0);
ret = verify_md5digest(filepath, NULL);
ui_reset_progress();
// ret == 1 if cancelled by user: do not log
if (ret < 0) {
#ifdef PHILZ_TOUCH_RECOVERY
ui_print_preset_colors(1, "red");
#endif
ui_print("MD5验证:错误\n");
} else if (ret == 0) {
#ifdef PHILZ_TOUCH_RECOVERY
ui_print_preset_colors(1, "green");
#endif
ui_print("MD5验证: 成功\n");
}
return NULL;
}
void start_md5_display_thread(char* filepath) {
// ensure_path_mounted() is not thread safe, we must disable it when starting a thread for md5 checks
// to install the zip file, we must re-enable the ensure_path_mounted() function: it is done when stopping the thread
// at this point, filepath should be mounted by caller
// we ensure primary storage is also mounted as it is needed by confirm_install() function
ensure_path_mounted(get_primary_storage_path());
set_ensure_mount_always_true(1);
// show the message in color (cyan by default)
// we will reset color before exiting the thread
#ifdef PHILZ_TOUCH_RECOVERY
ui_print_preset_colors(1, NULL);
#endif
ui_print("正在计算md5值...\n");
pthread_create(&tmd5_display, NULL, &md5_display_thread, filepath);
}
void stop_md5_display_thread() {
cancel_md5digest = 1;
if (pthread_kill(tmd5_display, 0) != ESRCH)
ui_print("正在取消计算md5值...\n");
pthread_join(tmd5_display, NULL);
set_ensure_mount_always_true(0);
#ifdef PHILZ_TOUCH_RECOVERY
ui_print_preset_colors(0, NULL);
#endif
}
void start_md5_verify_thread(char* filepath) {
// ensure_path_mounted() is not thread safe, we must disable it when starting a thread for md5 checks
// to install the zip file, we must re-enable the ensure_path_mounted() function: it is done when stopping the thread
// at this point, filepath should be mounted by caller
// we ensure primary storage is also mounted as it is needed by confirm_install() function
ensure_path_mounted(get_primary_storage_path());
set_ensure_mount_always_true(1);
// show the message in color (cyan by default)
// we will reset color before exiting the thread
#ifdef PHILZ_TOUCH_RECOVERY
ui_print_preset_colors(1, NULL);
#endif
ui_print("正在验证md5值...\n");
pthread_create(&tmd5_verify, NULL, &md5_verify_thread, filepath);
}
void stop_md5_verify_thread() {
cancel_md5digest = 1;
if (pthread_kill(tmd5_verify, 0) != ESRCH)
ui_print("正在取消md5验证...\n");
pthread_join(tmd5_verify, NULL);
set_ensure_mount_always_true(0);
#ifdef PHILZ_TOUCH_RECOVERY
ui_print_preset_colors(0, NULL);
#endif
}
// ------- End md5sum display
/***********************************************/
/* start wipe data and system options and menu */
/***********************************************/
// partition sdcard menu allows use of sd-ext
// it still must be defined in recovery.fstab to be properly used in recovery
static void show_partition_sdcard_menu() {
const char* headers[] = { "Partition sdcard menu", " enables sd-ext support", "", NULL };
char** list = (char**)malloc(((MAX_NUM_MANAGED_VOLUMES) + 1) * sizeof(char*)); // + 1 for last NULL (GO_BACK menu) entry
int i = 0;
int list_index = 0;
char buf[256];
const char list_prefix[] = "Partition ";
char* primary_path = get_primary_storage_path();
char** extra_paths = get_extra_storage_paths();
int num_extra_volumes = get_num_extra_volumes();
if (can_partition(primary_path)) {
sprintf(buf, "%s%s", list_prefix, primary_path);
list[list_index] = strdup(buf);
++list_index;
}
if (extra_paths != NULL) {
for (i = 0; i < num_extra_volumes; ++i) {
if (can_partition(extra_paths[i])) {
sprintf(buf, "%s%s", list_prefix, extra_paths[i]);
list[list_index] = strdup(buf);
++list_index;
}
}
}
list[list_index] = NULL;
if (list_index == 0) {
LOGE("no volumes found to partition.\n");
free_string_array(extra_paths);
return;
}
if (volume_for_path("/sd-ext") == NULL)
ui_print("W: sd-ext missing in fstab!\n");
int chosen_item = get_filtered_menu_selection(headers, list, 0, 0, sizeof(list) / sizeof(char*));
if (chosen_item < 0) // GO_BACK || REFRESH
goto out;
char* path = list[chosen_item] + strlen(list_prefix);
if (!can_partition(path)) {
LOGE("Can't partition device: %s\n", path);
goto out;
}
char* ext_sizes[] = {
"128M",
"256M",
"512M",
"1024M",
"2048M",
"4096M",
NULL
};
char* swap_sizes[] = {
"0M",
"32M",
"64M",
"128M",
"256M",
NULL
};
char* partition_types[] = {
"ext3",
"ext4",
NULL
};
const char* ext_headers[] = { "Ext大小", "", NULL };
const char* swap_headers[] = { "Swap大小", "", NULL };
const char* fstype_headers[] = { "分区类型", "", NULL };
int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
if (ext_size < 0)
goto out;
int swap_size = get_menu_selection(swap_headers, swap_sizes, 0, 0);
if (swap_size < 0)
goto out;
int partition_type = get_menu_selection(fstype_headers, partition_types, 0, 0);
if (partition_type < 0) // GO_BACK / REFRESH
goto out;
char cmd[PATH_MAX];
char sddevice[256];
Volume *vol = volume_for_path(path);
// can_partition() ensured either blk_device or blk_device2 has /dev/block/mmcblk format
if (strstr(vol->blk_device, "/dev/block/mmcblk") != NULL)
strcpy(sddevice, vol->blk_device);
else
strcpy(sddevice, vol->blk_device2);
// we only want the mmcblk, not the partition
sddevice[strlen("/dev/block/mmcblkX")] = '\0';
setenv("SDPATH", sddevice, 1);
sprintf(cmd, "sdparted -es %s -ss %s -efs %s -s", ext_sizes[ext_size], swap_sizes[swap_size], partition_types[partition_type]);
ui_print("正在格式化SD卡... 请稍后...\n");
if (0 == __system(cmd))
ui_print("完成!\n");
else
LOGE("SD卡在分区时发生错误, 详情请查看 /tmp/recovery.log。\n");
out:
free_string_array(list);
free_string_array(extra_paths);
}
void wipe_data_menu() {
const char* headers[] = { "选择清除选项", NULL };
char* list[] = {
"恢复出厂设置",
"清空 Cache",
"清空 Dalvik/ART Cache",
"以安装新Rom的标准自动清空",
NULL,
"自定义格式选项",
NULL,
NULL
};
if (is_data_media())
list[4] = "清除用户的媒体数据";
// check if we have a volume that can be partitionned (sd-ext support)
char* primary_path = get_primary_storage_path();
char** extra_paths = get_extra_storage_paths();
int num_extra_volumes = get_num_extra_volumes();
int can_partition_volumes = 0;
if (can_partition(primary_path)) {
can_partition_volumes = 1;
} else if (extra_paths != NULL) {
int i;
for (i = 0; i < num_extra_volumes; ++i) {
if (can_partition(extra_paths[i])) {
can_partition_volumes = 1;
break;
}
}
free_string_array(extra_paths);
}
if (can_partition_volumes)
list[6] = "Partition sdcard (sd-ext support)";
int chosen_item = 0;
for (;;) {
chosen_item = get_filtered_menu_selection(headers, list, 0, 0, sizeof(list) / sizeof(char*));
if (chosen_item < 0) // GO_BACK / REFRESH
break;
switch (chosen_item) {
case 0: {
wipe_data(true);
break;
}
case 1: {
if (confirm_selection("清除cache 分区 ?", "是 - 清除cache")) {
ui_print("\n-- 清除cache...\n");
if (erase_volume("/cache") == 0)
ui_print("清除Cache完成\n");
}
break;
}
case 2: {
if (0 != ensure_path_mounted("/data"))
break;
if (volume_for_path("/sd-ext") != NULL)
ensure_path_mounted("/sd-ext");
ensure_path_mounted("/cache");
if (confirm_selection("清除dalvik cache ?", "是 - 清除dalvik cache")) {
ui_print("\n-- 清除dalvik cache...\n");
__system("rm -r /data/dalvik-cache");
__system("rm -r /cache/dalvik-cache");
__system("rm -r /sd-ext/dalvik-cache");
ui_print("清除Dalvik Cache完成.\n");
}
break;
}
case 3: {
const char* headers[] = {
"清除用户和系统数据?",
" data | cache | datadata",
" sd-ext | android_secure",
" system | preload",
"",
NULL
};
if (confirm_with_headers(headers, "是 - 清除用户和系统数据")) {
wipe_data(false);
ui_print("-- 清除system...\n");
erase_volume("/system");
if (volume_for_path("/preload") != NULL) {
ui_print("-- 清除preload...\n");
erase_volume("/preload");
}
ui_print("现在刷入新ROM\n");
}
break;
}
case 4: {
const char* headers[] = {
"清除用户的媒体数据?",
" /sdcard (/data/media)",
"",
NULL
};
if (confirm_with_headers(headers, "是 - 清除用户的媒体数据")) {
ui_print("\n-- 清除媒体数据...\n");
erase_volume("/data/media");
ui_print("清除媒体数据完成\n");
}
break;
}
case 5: {
show_partition_format_menu();
break;
}
case 6: {
show_partition_sdcard_menu();
break;
}
}
}
}
// ------ end wipe and format options
/*****************************************/
/* Start Multi-Flash Zip code */
/* Original code by PhilZ @xda */
/*****************************************/
void show_multi_flash_menu() {
const char* headers_dir[] = { "选择一个zip文件", NULL };
const char* headers[] = { "选择要安装的文件...", NULL };
char tmp[PATH_MAX];
char* zip_folder = NULL;
char* primary_path = get_primary_storage_path();
char** extra_paths = get_extra_storage_paths();
int num_extra_volumes = get_num_extra_volumes();
// Browse sdcards until a valid multi_flash folder is found
// first, look for MULTI_ZIP_FOLDER in /sdcard
struct stat st;
ensure_path_mounted(primary_path);
sprintf(tmp, "%s/%s/", primary_path, MULTI_ZIP_FOLDER);
stat(tmp, &st);
if (S_ISDIR(st.st_mode)) {
zip_folder = choose_file_menu(tmp, NULL, headers_dir);
// zip_folder = NULL if no subfolders found or user chose Go Back
if (no_files_found) {
ui_print("在 %s 下面至少要有一个有zip文件的子目录\n", tmp);
ui_print("正在在其他存储设备查找...\n");
}
} else {
LOGI("%s 未找到. 搜索其他文件夹...\n", tmp);
}
// case MULTI_ZIP_FOLDER not found, or no subfolders or user selected Go Back (zip_folder == NULL)
// search for MULTI_ZIP_FOLDER in other storage paths if they exist (extra_paths != NULL)
int i = 0;
struct stat s;
if (extra_paths != NULL) {
while (zip_folder == NULL && i < num_extra_volumes) {
ensure_path_mounted(extra_paths[i]);
sprintf(tmp, "%s/%s/", extra_paths[i], MULTI_ZIP_FOLDER);
stat(tmp, &s);
if (S_ISDIR(s.st_mode)) {
zip_folder = choose_file_menu(tmp, NULL, headers_dir);
if (no_files_found)
ui_print("在 %s 下面至少要有一个有zip文件的子目录\n", tmp);
}
i++;
}
free_string_array(extra_paths);
}
// either MULTI_ZIP_FOLDER path not found (ui_print help)
// or it was found but no subfolder (ui_print help above in no_files_found)
// or user chose Go Back every time: return silently
if (zip_folder == NULL) {
if (!(S_ISDIR(st.st_mode)) && !(S_ISDIR(s.st_mode)))
ui_print("在%s 下面创建至少一个含有zip文件的文件夹\n", MULTI_ZIP_FOLDER);
return;
}
//gather zip files list
int dir_len = strlen(zip_folder);
int numFiles = 0;
char** files = gather_files(zip_folder, ".zip", &numFiles);
if (numFiles == 0) {
ui_print("在 %s 下面没有zip文件\n", zip_folder);
} else {
// start showing multi-zip menu
char** list = (char**)malloc((numFiles + 3) * sizeof(char*));
memset(list, 0, sizeof(list));
list[1] = strdup(">> 刷入选择的文件 <<");
list[numFiles+2] = NULL; // Go Back Menu
int i;
for(i = 2; i < numFiles + 2; i++) {
list[i] = strdup(files[i - 2] + dir_len - 4);
strncpy(list[i], "(x) ", 4);
}
int select_all = 1;
int chosen_item;
for (;;) {
chosen_item = get_menu_selection(headers, list, 0, 0);
if (chosen_item == GO_BACK || chosen_item == REFRESH)
break;
if (chosen_item == 1)
break;
if (chosen_item == 0) {
// select / unselect all
select_all ^= 1;
for(i = 2; i < numFiles + 2; i++) {
if (select_all) strncpy(list[i], "(x)", 3);
else strncpy(list[i], "( )", 3);
}
} else if (strncmp(list[chosen_item], "( )", 3) == 0) {
strncpy(list[chosen_item], "(x)", 3);
} else if (strncmp(list[chosen_item], "(x)", 3) == 0) {
strncpy(list[chosen_item], "( )", 3);
}
}
//flashing selected zip files
if (chosen_item == 1) {
char confirm[PATH_MAX];
sprintf(confirm, "是的 - 从 %s 安装", BaseName(zip_folder));
if (confirm_selection("安装选定的文件?", confirm)) {
for(i = 2; i < numFiles + 2; i++) {
if (strncmp(list[i], "(x)", 3) == 0) {
#ifdef PHILZ_TOUCH_RECOVERY
force_wait = -1;
#endif
if (install_zip(files[i - 2]) != INSTALL_SUCCESS)
break;
}
}
}
}
free_string_array(list);
}
free_string_array(files);
free(zip_folder);
}
//-------- End Multi-Flash Zip code
/*****************************************/
/* DO NOT REMOVE THIS CREDITS HEARDER */
/* IF YOU MODIFY ANY PART OF THIS SOURCE */
/* YOU MUST AGREE TO SHARE THE CHANGES */
/* */
/* Start open recovery script support */
/* Original code: Dees_Troy at yahoo */
/* Original cwm port by sk8erwitskil */
/* Enhanced by PhilZ @xda */
/*****************************************/
// check ors and extendedcommand boot scripts at boot (called from recovery.c)
// verifies that the boot script exists
// if found, try to edit the boot script file to fix paths in scripts generated by GooManager, ROM Manager or user
int check_boot_script_file(const char* boot_script) {
if (!file_found(boot_script)) {
LOGI("Ignoring '%s' boot-script: file not found\n", BaseName(boot_script));
return -1;
}
LOGI("Script file found: '%s'\n", boot_script);
char tmp[PATH_MAX];
sprintf(tmp, "/sbin/bootscripts_mnt.sh %s %s", boot_script, get_primary_storage_path());
if (0 != __system(tmp)) {
// non fatal error
LOGE("failed to fix boot script (%s)\n", strerror(errno));
LOGE("run without fixing...\n");
}
return 0;
}
// run ors script on boot (called from recovery.c)
// this must be called after check_boot_script_file()
int run_ors_boot_script() {
int ret = 0;
char tmp[PATH_MAX];
if (!file_found(ORS_BOOT_SCRIPT_FILE))
return -1;
// move formatted ors boot script to /tmp and run it from there
sprintf(tmp, "cp -f %s /tmp/%s", ORS_BOOT_SCRIPT_FILE, BaseName(ORS_BOOT_SCRIPT_FILE));
__system(tmp);
remove(ORS_BOOT_SCRIPT_FILE);
sprintf(tmp, "/tmp/%s", BaseName(ORS_BOOT_SCRIPT_FILE));
return run_ors_script(tmp);
}
// sets the default backup volume for ors backup command
// default is primary storage
static void get_ors_backup_volume(char *volume) {
char value_def[PATH_MAX];
sprintf(value_def, "%s", get_primary_storage_path());
read_config_file(PHILZ_SETTINGS_FILE, ors_backup_path.key, ors_backup_path.value, value_def);
// on data media device, v == NULL if it is sdcard. But, it doesn't matter since value_def will be /sdcard in that case
Volume* v = volume_for_path(ors_backup_path.value);
if (v != NULL && ensure_path_mounted(ors_backup_path.value) == 0 && strcmp(ors_backup_path.value, v->mount_point) == 0)
strcpy(volume, ors_backup_path.value);
else strcpy(volume, value_def);
}
// choose ors backup volume and save user setting
static void choose_ors_volume() {
char* primary_path = get_primary_storage_path();
char** extra_paths = get_extra_storage_paths();
int num_extra_volumes = get_num_extra_volumes();
const char* headers[] = { "把ors备份保存到:", NULL };
char* list[MAX_NUM_MANAGED_VOLUMES + 1];
memset(list, 0, sizeof(list));
list[0] = strdup(primary_path);
char buf[80];
int i;