-
Notifications
You must be signed in to change notification settings - Fork 3
/
advanced_functions.c
3086 lines (2805 loc) · 108 KB
/
advanced_functions.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
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <linux/input.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/reboot.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/limits.h>
#include <dirent.h>
#include <sys/stat.h>
// statfs
#include <sys/vfs.h>
#include <signal.h>
#include <sys/wait.h>
#include "bootloader.h"
#include "common.h"
#include "cutils/properties.h"
#include "firmware.h"
#include "install.h"
#include "make_ext4fs.h"
#include "minui/minui.h"
#include "minzip/DirUtil.h"
#include "roots.h"
#include "recovery_ui.h"
#include "extendedcommands.h"
#include "advanced_functions.h"
#include "nandroid.h"
#include "mounts.h"
#include "flashutils/flashutils.h"
#include "edify/expr.h"
#include <libgen.h>
#include "mtdutils/mtdutils.h"
#include "bmlutils/bmlutils.h"
#include "cutils/android_reboot.h"
/*****************************************/
/* DO NOT REMOVE THIS CREDITS HEARDER */
/* IF YOU MODIFY ANY PART OF THIS SOURCE */
/* YOU MUST AGREE TO SHARE THE CHANGES */
/* */
/* Start PhilZ Menu settings */
/* Code written by PhilZ@xda */
/* Part of PhilZ Touch Recovery */
/*****************************************/
// redefined MENU_MAX_COLS from ui.c - Keep same value as ui.c until a better implementation.
// used to format toggle menus to device screen width (only touch build)
#define MENU_MAX_COLS 64
// Returns the current time in msec:
unsigned long gettime_now_msec(void) {
struct timeval now;
long mseconds;
gettimeofday(&now, NULL);
mseconds = now.tv_sec * 1000;
mseconds += now.tv_usec / 1000;
return mseconds;
}
//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");
}
// is there a second storage (/sdcard is always present in fstab)
int has_second_storage() {
return (volume_for_path("/external_sd") != NULL
|| volume_for_path("/emmc") != NULL);
}
// delete a file
void delete_a_file(const char* filename) {
ensure_path_mounted(filename);
remove(filename);
}
// check if file or folder exists
int file_found(const char* filename) {
struct stat s;
if (strncmp(filename, "/sbin/", 6) != 0 && strncmp(filename, "/res/", 5) != 0 &&
strncmp(filename, "/tmp/", 5) != 0) {
// do not try to mount ramdisk, else it will error "unknown volume for path..."
ensure_path_mounted(filename);
}
if (0 == stat(filename, &s))
return 1;
return 0;
}
// check directory exists
int directory_found(const char* dir) {
struct stat s;
ensure_path_mounted(dir);
lstat(dir, &s);
if (S_ISDIR(s.st_mode))
return 1;
return 0;
}
//check if path is in ramdisk since volume_for_path() will be NULL on these
int is_path_ramdisk(const char* path) {
const char *ramdisk_dirs[] = { "/sbin/", "/res/", "/tmp/", NULL };
int i = 0;
while (ramdisk_dirs[i] != NULL) {
if (strncmp(path, ramdisk_dirs[i], strlen(ramdisk_dirs[i])) == 0)
return 1;
i++;
}
return 0;
}
//copy file (ramdisk check compatible)
int copy_a_file(const char* file_in, const char* file_out) {
if (strcmp(file_in, file_out) == 0) {
LOGI("source and destination files are same, skipping copy.\n");
return 0;
}
if (!is_path_ramdisk(file_in) && ensure_path_mounted(file_in) != 0) {
ui_print("cannot mount volume for %s\n", file_in);
return -1;
}
if (!is_path_ramdisk(file_out) && ensure_path_mounted(file_out) != 0) {
ui_print("cannot mount volume for %s\n", file_out);
return -1;
}
//this will chmod folder to 775
char tmp[PATH_MAX];
strcpy(tmp, file_out);
ensure_directory(dirname(tmp));
FILE *fp = fopen(file_in, "rb");
if (fp == NULL) {
ui_print("Source file not found (%s)\n", file_in);
return -1;
}
FILE *fp_out = fopen(file_out, "wb");
if (fp_out == NULL) {
ui_print("Failed to create destination file %s\n", file_out);
return -1;
}
//start copy
char buf[PATH_MAX];
size_t size;
//unsigned int size;
while (size = fread(buf, 1, sizeof(buf), fp)) {
fwrite(buf, 1, size, fp_out);
}
fclose(fp);
fclose(fp_out);
return 0;
}
// get file size (by Dees_Troy - TWRP)
unsigned long Get_File_Size(const char* Path) {
struct stat st;
if (stat(Path, &st) != 0)
return 0;
return st.st_size;
}
// get partition size info (adapted from Dees_Troy - TWRP)
unsigned long long Total_Size = 0; // Overall size of the partition
unsigned long long Used_Size = 0; // Overall used space
unsigned long long Free_Size = 0; // Overall free space
int Get_Size_Via_statfs(const char* Path) {
struct statfs st;
Volume* volume = volume_for_path(Path);
if (NULL == volume) {
LOGE("Cannot get size of null volume '%s'\n", Path);
return -1;
}
if (is_data_media_volume_path(volume->mount_point))
volume = volume_for_path("/data");
if (volume == NULL || volume->mount_point == NULL || statfs(volume->mount_point, &st) != 0) {
LOGE("Unable to statfs for size '%s'\n", Path);
return -1;
}
Total_Size = (unsigned long long)(st.f_blocks) * (unsigned long long)(st.f_bsize);
Free_Size = (unsigned long long)(st.f_bfree) * (unsigned long long)(st.f_bsize);
Used_Size = Total_Size - Free_Size;
// LOGI("%s: tot=%llu, use=%llu, free=%llu\n", volume->mount_point, Total_Size, Used_Size, Free_Size); // debug
return 0;
}
// alternate method for statfs (emmc, mtd...)
int Find_Partition_Size(const char* Path) {
FILE* fp;
char line[512];
char tmpdevice[1024];
Volume* volume = volume_for_path(Path);
if (volume != NULL) {
// In this case, we'll first get the partitions we care about (with labels)
fp = fopen("/proc/partitions", "rt");
if (fp != NULL) {
while (fgets(line, sizeof(line), fp) != NULL)
{
unsigned long major, minor, blocks;
char device[512];
char tmpString[64];
if (strlen(line) < 7 || line[0] == 'm')
continue;
sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
sprintf(tmpdevice, "/dev/block/");
strcat(tmpdevice, device);
if (volume->device != NULL && strcmp(tmpdevice, volume->device) == 0) {
Total_Size = blocks * 1024ULL;
//LOGI("%s(%s)=%llu\n", Path, volume->device, Total_Size); // debug
fclose(fp);
return 0;
}
if (volume->device2 != NULL && strcmp(tmpdevice, volume->device2) == 0) {
Total_Size = blocks * 1024ULL;
fclose(fp);
return 0;
}
}
fclose(fp);
}
}
LOGE("Failed to find partition size '%s'\n", Path);
return -1;
}
//----- End partition size
// get folder size (by Dees_Troy - TWRP)
unsigned long long Get_Folder_Size(const char* Path) {
DIR* d;
struct dirent* de;
struct stat st;
char path2[PATH_MAX]; char filename[PATH_MAX];
unsigned long long dusize = 0;
unsigned long long dutemp = 0;
// Make a copy of path in case the data in the pointer gets overwritten later
strcpy(path2, Path);
d = opendir(path2);
if (d == NULL)
{
LOGE("error opening '%s'\n", path2);
LOGE("error: %s\n", strerror(errno));
return 0;
}
while ((de = readdir(d)) != NULL)
{
if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
{
strcpy(filename, path2);
strcat(filename, "/");
strcat(filename, de->d_name);
dutemp = Get_Folder_Size(filename);
dusize += dutemp;
dutemp = 0;
}
else if (de->d_type == DT_REG)
{
strcpy(filename, path2);
strcat(filename, "/");
strcat(filename, de->d_name);
stat(filename, &st);
dusize += (unsigned long long)(st.st_size);
}
}
closedir(d);
return dusize;
}
/**********************************/
/* Start file parser */
/* Original source by PhilZ */
/**********************************/
// todo: parse settings file in one pass and make pairs of key:value
// get value of key from a given config file
static int read_config_file(const char* config_file, const char *key, char *value, const char *value_def) {
int ret = 0;
char line[PROPERTY_VALUE_MAX];
ensure_path_mounted(config_file);
FILE *fp = fopen(config_file, "rb");
if (fp != NULL) {
while(fgets(line, sizeof(line), fp) != NULL) {
if (strstr(line, key) != NULL && strncmp(line, key, strlen(key)) == 0 && line[strlen(key)] == '=') {
strcpy(value, strstr(line, "=") + 1);
//remove trailing \n char
if (value[strlen(value)-1] == '\n')
value[strlen(value)-1] = '\0';
if (value[0] != '\0') {
fclose(fp);
LOGI("%s=%s\n", key, value);
return ret;
}
}
}
ret = 1;
fclose(fp);
} else {
LOGI("Cannot open %s\n", config_file);
ret = -1;
}
strcpy(value, value_def);
LOGI("%s set to default (%s)\n", key, value_def);
return ret;
}
// set value of key in config file
static int write_config_file(const char* config_file, const char* key, const char* value) {
if (ensure_path_mounted(config_file) != 0) {
LOGE("Cannot mount path for settings file: %s\n", config_file);
return -1;
}
char config_file_tmp[PATH_MAX];
strcpy(config_file_tmp, config_file);
ensure_directory(dirname(config_file_tmp));
strcpy(config_file_tmp, config_file);
strcat(config_file_tmp, ".tmp");
delete_a_file(config_file_tmp);
FILE *fp = fopen(config_file, "rb");
FILE *f_tmp = fopen(config_file_tmp, "wb");
if (f_tmp == NULL) {
LOGE("failed to create temporary settings file!\n");
return -1;
}
// if a new settings file needs to be created, we write a user info header
if (fp == NULL) {
const char* header[] = {
"#PhilZ Touch Settings File\n",
"#Edit only in appropriate UNIX format (Notepad+++...)\n",
"#Entries are in the form of:\n",
"#key=value\n",
"#Do not add spaces in between!\n",
"\n",
NULL
};
int i;
for(i=0; header[i] != NULL; i++) {
fwrite(header[i], 1, strlen(header[i]), f_tmp);
}
}
// parsing existing config file and writing new temporary file.
if (fp != NULL) {
char line[PROPERTY_VALUE_MAX];
while(fgets(line, sizeof(line), fp) != NULL) {
// ignore any existing line with key we want to set
if (strstr(line, key) != NULL && strncmp(line, key, strlen(key)) == 0 && line[strlen(key)] == '=')
continue;
// ensure trailing \n, in case some one got a bad editor...
if (line[strlen(line)-1] != '\n')
strcat(line, "\n");
fwrite(line, 1, strlen(line), f_tmp);
}
fclose(fp);
}
// write new key=value entry
char new_entry[PROPERTY_VALUE_MAX];
sprintf(new_entry, "%s=%s\n", key, value);
fwrite(new_entry, 1, strlen(new_entry), f_tmp);
fclose(f_tmp);
if (rename(config_file_tmp, config_file) !=0) {
LOGE("failed renaming temporary settings file!\n");
return -1;
}
LOGI("%s was set to %s\n", key, value);
return 0;
}
//----- end file settings parser
// start wipe data and system options and menu
void wipe_data_menu() {
static char* headers[] = { "Choose wipe option",
NULL
};
char* list[] = { "Wipe Data/Factory Reset",
"Clean to Install a New ROM",
NULL
};
int chosen_item = get_menu_selection(headers, list, 0, 0);
switch (chosen_item)
{
case 0:
wipe_data(1);
break;
case 1:
//clean for new ROM: formats /data, /datadata, /cache, /system, /preload, /sd-ext, .android_secure
if (confirm_selection("Wipe data, system +/- preload?", "Yes, I will install a new ROM!")) {
wipe_data(0);
ui_print("-- Wiping system...\n");
erase_volume("/system");
if (volume_for_path("/preload") != NULL) {
ui_print("-- Wiping preload...\n");
erase_volume("/preload");
}
ui_print("Now flash a new ROM!\n");
}
break;
}
}
/*****************************************/
/* Start Multi-Flash Zip code */
/* Original code by PhilZ @xda */
/*****************************************/
#define MULTI_ZIP_FOLDER "clockworkmod/multi_flash"
void show_multi_flash_menu() {
static char* headers_dir[] = { "Choose a set of zip files",
NULL
};
static char* headers[] = { "Select files to install...",
NULL
};
//browse sdcards until a valid multi_flash folder is found
char *other_sd = NULL;
if (volume_for_path("/emmc") != NULL)
other_sd = "/emmc";
else if (volume_for_path("/external_sd") != NULL)
other_sd = "/external_sd";
char tmp[PATH_MAX];
char* zip_folder = NULL;
//look for MULTI_ZIP_FOLDER in /sdcard
struct stat st;
ensure_path_mounted("/sdcard");
sprintf(tmp, "/sdcard/%s/", 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("At least one subfolder with zip files must be created under %s\n", tmp);
ui_print("Looking in other sd...\n");
}
} else
LOGI("%s not found. Searching other sd...\n", tmp);
// case MULTI_ZIP_FOLDER not found, or no subfolders or user selected Go Back:
// search for MULTI_ZIP_FOLDER in other_sd
struct stat s;
if (other_sd != NULL) {
ensure_path_mounted(other_sd);
sprintf(tmp, "%s/%s/", other_sd, MULTI_ZIP_FOLDER);
stat(tmp, &s);
if (zip_folder == NULL && S_ISDIR(s.st_mode)) {
zip_folder = choose_file_menu(tmp, NULL, headers_dir);
if (no_files_found)
ui_print("At least one subfolder with zip files must be created under %s\n", tmp);
}
}
// 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("Create at least 1 folder with your zip files under %s\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("No zip files found under %s\n", zip_folder);
} else {
// start showing multi-zip menu
char** list = (char**) malloc((numFiles + 3) * sizeof(char*));
list[0] = strdup("Select/Unselect All");
list[1] = strdup(">> Flash Selected Files <<");
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)
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) {
static char confirm[PATH_MAX];
sprintf(confirm, "Yes - Install from %s", basename(zip_folder));
if (confirm_selection("Install selected files?", 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]) != 0)
break;
}
}
}
}
free_string_array(list);
}
free_string_array(files);
}
//-------- 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 */
/*****************************************/
#define SCRIPT_COMMAND_SIZE 512
static int ignore_android_secure = 0;
int check_for_script_file(const char* ors_boot_script) {
ensure_path_mounted(ors_boot_script);
struct stat s;
if (0 != stat(ors_boot_script, &s))
return -1;
char tmp[PATH_MAX];
LOGI("Script file found: '%s'\n", ors_boot_script);
__system("/sbin/ors-mount.sh");
// move script file to /tmp
sprintf(tmp, "mv %s /tmp", ors_boot_script);
__system(tmp);
return 0;
}
// sets the default backup volume for ors backup command
static void get_ors_backup_volume(char *other_sd) {
char value[PROPERTY_VALUE_MAX];
char value_def[15];
// favor external storage as default
if (volume_for_path("/external_sd") != NULL && ensure_path_mounted("/external_sd") == 0)
sprintf(value_def, "/external_sd");
else if (volume_for_path("/sdcard") != NULL && ensure_path_mounted("/sdcard") == 0)
sprintf(value_def, "/sdcard");
else if (volume_for_path("/emmc") != NULL && ensure_path_mounted("/emmc") == 0)
sprintf(value_def, "/emmc");
else
return;
read_config_file(PHILZ_SETTINGS_FILE, "ors_backup_path", value, value_def);
if (strcmp(value, "/external_sd") == 0 || strcmp(value, "/sdcard") == 0 || strcmp(value, "/emmc") == 0) {
if (volume_for_path(value) != NULL && ensure_path_mounted(value) == 0)
strcpy(other_sd, value);
}
}
// checks if ors backup should be done in cwm (ret=0) or twrp (ret=1) format
static int twrp_ors_backup_format() {
char value[PROPERTY_VALUE_MAX];
int ret = 0;
read_config_file(PHILZ_SETTINGS_FILE, "ors_backup_format", value, "cwm");
if (strcmp(value, "twrp") == 0)
ret = 1;
return ret;
}
// Parse backup options in ors
// Stock CWM as of v6.x, doesn't support backup options
static int ors_backup_command(const char* backup_path, const char* options) {
is_custom_backup = 1;
int old_enable_md5sum = enable_md5sum;
enable_md5sum = 1;
backup_boot = 0, backup_recovery = 0, backup_wimax = 0, backup_system = 0;
backup_preload = 0, backup_data = 0, backup_cache = 0, backup_sdext = 0;
ignore_android_secure = 1;
set_override_yaffs2_wrapper(0);
nandroid_force_backup_format("tar");
ui_print("Setting backup options:\n");
char value1[SCRIPT_COMMAND_SIZE];
int line_len, i;
strcpy(value1, options);
line_len = strlen(options);
for (i=0; i<line_len; i++) {
if (value1[i] == 'S' || value1[i] == 's') {
backup_system = 1;
ui_print("System\n");
if (nandroid_add_preload) {
backup_preload = 1;
ui_print("Preload enabled in nandroid settings.\n");
ui_print("It will be Processed with /system\n");
}
} else if (value1[i] == 'D' || value1[i] == 'd') {
backup_data = 1;
ui_print("Data\n");
} else if (value1[i] == 'C' || value1[i] == 'c') {
backup_cache = 1;
ui_print("Cache\n");
} else if (value1[i] == 'R' || value1[i] == 'r') {
backup_recovery = 1;
ui_print("Recovery\n");
} else if (value1[i] == '1') {
ui_print("%s\n", "Option for special1 ignored in CWMR");
} else if (value1[i] == '2') {
ui_print("%s\n", "Option for special2 ignored in CWMR");
} else if (value1[i] == '3') {
ui_print("%s\n", "Option for special3 ignored in CWMR");
} else if (value1[i] == 'B' || value1[i] == 'b') {
backup_boot = 1;
ui_print("Boot\n");
} else if (value1[i] == 'A' || value1[i] == 'a') {
ignore_android_secure = 0;
ui_print("Android secure\n");
} else if (value1[i] == 'E' || value1[i] == 'e') {
backup_sdext = 1;
ui_print("SD-Ext\n");
} else if (value1[i] == 'O' || value1[i] == 'o') {
nandroid_force_backup_format("tgz");
ui_print("Compression is on\n");
} else if (value1[i] == 'M' || value1[i] == 'm') {
enable_md5sum = 0;
ui_print("MD5 Generation is off\n");
}
}
int ret = -1;
if (file_found(backup_path)) {
LOGE("Specified ors backup target '%s' already exists!\n", backup_path);
} else if (twrp_backup_mode) {
ret = twrp_backup(backup_path);
} else {
ret = nandroid_backup(backup_path);
}
is_custom_backup = 0;
twrp_backup_mode = 0;
nandroid_force_backup_format("");
set_override_yaffs2_wrapper(1);
reset_custom_job_settings(0);
enable_md5sum = old_enable_md5sum;
return ret;
}
// run ors script code
// this can be started on boot or manually for custom ors
int run_ors_script(const char* ors_script) {
FILE *fp = fopen(ors_script, "r");
int ret_val = 0, cindex, line_len, i, remove_nl;
char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE],
value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE],
value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE];
char *val_start, *tok;
if (fp != NULL) {
while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) {
cindex = 0;
line_len = strlen(script_line);
if (line_len < 2)
continue; // there's a blank line or line is too short to contain a command
LOGI("script line: '%s'\n", script_line); // debug code
for (i=0; i<line_len; i++) {
if ((int)script_line[i] == 32) {
cindex = i;
i = line_len;
}
}
memset(command, 0, sizeof(command));
memset(value, 0, sizeof(value));
if ((int)script_line[line_len - 1] == 10)
remove_nl = 2;
else
remove_nl = 1;
if (cindex != 0) {
strncpy(command, script_line, cindex);
LOGI("command is: '%s' and ", command);
val_start = script_line;
val_start += cindex + 1;
if ((int) *val_start == 32)
val_start++; //get rid of space
if ((int) *val_start == 51)
val_start++; //get rid of = at the beginning
strncpy(value, val_start, line_len - cindex - remove_nl);
LOGI("value is: '%s'\n", value);
} else {
strncpy(command, script_line, line_len - remove_nl + 1);
ui_print("command is: '%s' and there is no value\n", command);
}
if (strcmp(command, "install") == 0) {
// Install zip
ui_print("Installing zip file '%s'\n", value);
ensure_path_mounted(value);
ret_val = install_zip(value);
if (ret_val != INSTALL_SUCCESS) {
LOGE("Error installing zip file '%s'\n", value);
ret_val = 1;
}
} else if (strcmp(command, "wipe") == 0) {
// Wipe
if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) {
ui_print("-- Wiping Cache Partition...\n");
erase_volume("/cache");
ui_print("-- Cache Partition Wipe Complete!\n");
} else if (strcmp(value, "dalvik") == 0 || strcmp(value, "dalvick") == 0 || strcmp(value, "dalvikcache") == 0 || strcmp(value, "dalvickcache") == 0) {
ui_print("-- Wiping Dalvik Cache...\n");
if (0 != ensure_path_mounted("/data")) {
ret_val = 1;
break;
}
ensure_path_mounted("/sd-ext");
ensure_path_mounted("/cache");
if (no_wipe_confirm) {
//do not confirm before wipe for scripts started at boot
__system("rm -r /data/dalvik-cache");
__system("rm -r /cache/dalvik-cache");
__system("rm -r /sd-ext/dalvik-cache");
ui_print("Dalvik Cache wiped.\n");
} else {
if (confirm_selection( "Confirm wipe?", "Yes - Wipe Dalvik Cache")) {
__system("rm -r /data/dalvik-cache");
__system("rm -r /cache/dalvik-cache");
__system("rm -r /sd-ext/dalvik-cache");
ui_print("Dalvik Cache wiped.\n");
}
}
ensure_path_unmounted("/data");
ui_print("-- Dalvik Cache Wipe Complete!\n");
} else if (strcmp(value, "data") == 0 || strcmp(value, "/data") == 0 || strcmp(value, "factory") == 0 || strcmp(value, "factoryreset") == 0) {
ui_print("-- Wiping Data Partition...\n");
wipe_data(0);
ui_print("-- Data Partition Wipe Complete!\n");
} else {
LOGE("Error with wipe command value: '%s'\n", value);
ret_val = 1;
}
} else if (strcmp(command, "backup") == 0) {
char backup_volume[PATH_MAX] = "";
// read user set volume target
get_ors_backup_volume(backup_volume);
if (strcmp(backup_volume, "") == 0) {
ret_val = 1;
LOGE("No valid volume found for ors backup target!\n");
continue;
}
char backup_path[PATH_MAX];
// Check if ors backup is set by user to twrp mode
if (twrp_ors_backup_format())
twrp_backup_mode = 1;
tok = strtok(value, " ");
strcpy(value1, tok);
tok = strtok(NULL, " ");
if (tok != NULL) {
// command line has a name for backup folder
memset(value2, 0, sizeof(value2));
strcpy(value2, tok);
line_len = strlen(tok);
if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) {
if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13)
remove_nl = 2;
else
remove_nl = 1;
} else
remove_nl = 0;
strncpy(value2, tok, line_len - remove_nl);
ui_print("Backup folder set to '%s'\n", value2);
if (twrp_backup_mode) {
char device_id[PROPERTY_VALUE_MAX];
get_device_id(device_id);
sprintf(backup_path, "%s/%s/%s/%s", backup_volume, TWRP_BACKUP_PATH, device_id, value2);
} else {
sprintf(backup_path, "%s/clockworkmod/backup/%s", backup_volume, value2);
}
} else if (twrp_backup_mode) {
get_twrp_backup_path(backup_volume, backup_path);
} else {
get_custom_backup_path(backup_volume, backup_path);
}
if (0 != (ret_val = ors_backup_command(backup_path, value1)))
ui_print("Backup failed !!\n");
} else if (strcmp(command, "restore") == 0) {
// Restore
tok = strtok(value, " ");
strcpy(value1, tok);
ui_print("Restoring '%s'\n", value1);
// custom restore settings
is_custom_backup = 1;
int old_enable_md5sum = enable_md5sum;
enable_md5sum = 1;
backup_boot = 0, backup_recovery = 0, backup_system = 0;
backup_preload = 0, backup_data = 0, backup_cache = 0, backup_sdext = 0;
ignore_android_secure = 1; //disable
// check what type of restore we need
if (strstr(value1, TWRP_BACKUP_PATH) != NULL)
twrp_backup_mode = 1;
tok = strtok(NULL, " ");
if (tok != NULL) {
memset(value2, 0, sizeof(value2));
strcpy(value2, tok);
ui_print("Setting restore options:\n");
line_len = strlen(value2);
for (i=0; i<line_len; i++) {
if (value2[i] == 'S' || value2[i] == 's') {
backup_system = 1;
ui_print("System\n");
if (nandroid_add_preload) {
backup_preload = 1;
ui_print("Preload enabled in nandroid settings.\n");
ui_print("It will be Processed with /system\n");
}
} else if (value2[i] == 'D' || value2[i] == 'd') {
backup_data = 1;
ui_print("Data\n");
} else if (value2[i] == 'C' || value2[i] == 'c') {
backup_cache = 1;
ui_print("Cache\n");
} else if (value2[i] == 'R' || value2[i] == 'r') {
backup_recovery = 1;
ui_print("Recovery\n");
} else if (value2[i] == '1') {
ui_print("%s\n", "Option for special1 ignored in CWMR");
} else if (value2[i] == '2') {
ui_print("%s\n", "Option for special2 ignored in CWMR");
} else if (value2[i] == '3') {
ui_print("%s\n", "Option for special3 ignored in CWMR");
} else if (value2[i] == 'B' || value2[i] == 'b') {
backup_boot = 1;
ui_print("Boot\n");
} else if (value2[i] == 'A' || value2[i] == 'a') {
ignore_android_secure = 0;
ui_print("Android secure\n");
} else if (value2[i] == 'E' || value2[i] == 'e') {
backup_sdext = 1;
ui_print("SD-Ext\n");
} else if (value2[i] == 'M' || value2[i] == 'm') {
enable_md5sum = 0;
ui_print("MD5 Check is off\n");
}
}
} else {
LOGI("No restore options set\n");
LOGI("Restoring default partitions");
backup_boot = 1, backup_system = 1;
backup_data = 1, backup_cache = 1, backup_sdext = 1;
ignore_android_secure = 0;
backup_preload = nandroid_add_preload;
}
if (twrp_backup_mode)
ret_val = twrp_restore(value1);
else
ret_val = nandroid_restore(value1, backup_boot, backup_system, backup_data, backup_cache, backup_sdext, 0);
if (ret_val != 0)
ui_print("Restore failed!\n");
is_custom_backup = 0, twrp_backup_mode = 0;
reset_custom_job_settings(0);
enable_md5sum = old_enable_md5sum;
} else if (strcmp(command, "mount") == 0) {
// Mount
if (value[0] != '/') {
strcpy(mount, "/");
strcat(mount, value);
} else
strcpy(mount, value);
ensure_path_mounted(mount);
ui_print("Mounted '%s'\n", mount);
} else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) {
// Unmount
if (value[0] != '/') {
strcpy(mount, "/");
strcat(mount, value);
} else
strcpy(mount, value);
ensure_path_unmounted(mount);
ui_print("Unmounted '%s'\n", mount);
} else if (strcmp(command, "set") == 0) {
// Set value
tok = strtok(value, " ");
strcpy(value1, tok);
tok = strtok(NULL, " ");
strcpy(value2, tok);
ui_print("Setting function disabled in CWMR: '%s' to '%s'\n", value1, value2);
} else if (strcmp(command, "mkdir") == 0) {
// Make directory (recursive)
ui_print("Recursive mkdir disabled in CWMR: '%s'\n", value);
} else if (strcmp(command, "reboot") == 0) {
// Reboot
} else if (strcmp(command, "cmd") == 0) {
if (cindex != 0) {
__system(value);
} else {
LOGE("No value given for cmd\n");
}
} else if (strcmp(command, "print") == 0) {
ui_print("%s\n", value);
} else if (strcmp(command, "sideload") == 0) {
// Install zip from sideload
ui_print("Waiting for sideload...\n");
if (0 != (ret_val = apply_from_adb()))
LOGE("Error installing from sideload\n");
} else {
LOGE("Unrecognized script command: '%s'\n", command);
ret_val = 1;
}
}
fclose(fp);
ui_print("Done processing script file\n");
} else {
LOGE("Error opening script file '%s'\n", ors_script);
return 1;
}
return ret_val;
}
//show menu: select ors from default path
static int browse_for_file = 1;
static void choose_default_ors_menu(const char* ors_path)
{
if (ensure_path_mounted(ors_path) != 0) {
LOGE("Can't mount %s\n", ors_path);
browse_for_file = 1;
return;
}