forked from drakaz/android_bootable_recovery_galaxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
recovery.c
2410 lines (2160 loc) · 88.4 KB
/
recovery.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) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <linux/input.h>
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <sys/reboot.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <termios.h>
#include <libgen.h>
#include "bootloader.h"
#include "commands.h"
#include "common.h"
#include "cutils/properties.h"
#include "firmware.h"
#include "install.h"
#include "minui/minui.h"
#include "minzip/DirUtil.h"
#include "roots.h"
#include "recovery_ui.h"
#include "extendedcommand.h"
#define MENU_HINT "Use up/down to highlight;", \
"OK to select", \
""
#define PSFREEDOM 1
static const struct option OPTIONS[] = {
{ "send_intent", required_argument, NULL, 's' },
{ "update_package", required_argument, NULL, 'u' },
{ "update_gapps", required_argument, NULL, 'g' },
{ "wipe_data", no_argument, NULL, 'w' },
{ "wipe_cache", no_argument, NULL, 'c' },
{ "wipe_full", no_argument, NULL, 'a' },
{ "nandroid", no_argument, NULL, 'n' },
{ "reboot", no_argument, NULL, 'r' },
{ "hello", no_argument, NULL, 'h' },
{ "migrate", no_argument, NULL, 'm' },
};
static const char *COMMAND_FILE = "CACHE:recovery/command";
static const char *INTENT_FILE = "CACHE:recovery/intent";
static const char *LOG_FILE = "CACHE:recovery/log";
static const char *SDCARD_PACKAGE_FILE = "SDCARD:update.zip";
static const char *SDCARD_PATH = "SDCARD:";
static const char *THEMES_PATH = "THEMES:";
#define SDCARD_PATH_LENGTH 20
#define THEMES_PATH_LENGTH 20
static const char *TEMPORARY_LOG_FILE = "/sdcard/recovery.log";
/*
* The recovery tool communicates with the main system through /cache files.
* /cache/recovery/command - INPUT - command line for tool, one arg per line
* /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
* /cache/recovery/intent - OUTPUT - intent that was passed in
*
* The arguments which may be supplied in the recovery.command file:
* --send_intent=anystring - write the text out to recovery.intent
* --update_package=root:path - verify install an OTA package file
* --wipe_data - erase user data (and cache), then reboot
* --wipe_cache - wipe cache (but not user data), then reboot
*
* After completing, we remove /cache/recovery/command and reboot.
* Arguments may also be supplied in the bootloader control block (BCB).
* These important scenarios must be safely restartable at any point:
*
* FACTORY RESET
* 1. user selects "factory reset"
* 2. main system writes "--wipe_data" to /cache/recovery/command
* 3. main system reboots into recovery
* 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
* -- after this, rebooting will restart the erase --
* 5. erase_root() reformats /data
* 6. erase_root() reformats /cache
* 7. finish_recovery() erases BCB
* -- after this, rebooting will restart the main system --
* 8. main() calls reboot() to boot main system
*
* OTA INSTALL
* 1. main system downloads OTA package to /cache/some-filename.zip
* 2. main system writes "--update_package=CACHE:some-filename.zip"
* 3. main system reboots into recovery
* 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
* -- after this, rebooting will attempt to reinstall the update --
* 5. install_package() attempts to install the update
* NOTE: the package install must itself be restartable from any point
* 6. finish_recovery() erases BCB
* -- after this, rebooting will (try to) restart the main system --
* 7. ** if install failed **
* 7a. prompt_and_wait() shows an error icon and waits for the user
* 7b; the user reboots (pulling the battery, etc) into the main system
* 8. main() calls maybe_install_firmware_update()
* ** if the update contained radio/hboot firmware **:
* 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
* -- after this, rebooting will reformat cache & restart main system --
* 8b. m_i_f_u() writes firmware image into raw cache partition
* 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
* -- after this, rebooting will attempt to reinstall firmware --
* 8d. bootloader tries to flash firmware
* 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
* -- after this, rebooting will reformat cache & restart main system --
* 8f. erase_root() reformats /cache
* 8g. finish_recovery() erases BCB
* -- after this, rebooting will (try to) restart the main system --
* 9. main() calls reboot() to boot main system
*/
static const int MAX_ARG_LENGTH = 4096;
static const int MAX_ARGS = 100;
static int do_reboot = 1;
// drakaz : binary location
#define STARTUP_BIN "/tmp/RECTOOLS/startup.sh"
#define NANDROID_BIN "/tmp/RECTOOLS/nandroid-mobile.sh"
#define MKE2FS_BIN "/tmp/RECTOOLS/mke2fs"
#define E2FSCK_BIN "/tmp/RECTOOLS/e2fsck"
#define SDTOOLS "/tmp/RECTOOLS/sdtools.sh"
#define FIX_PERMS_BIN "/tmp/RECTOOLS/fix_permissions.sh"
#define BACKUP_DATA_BIN "/tmp/RECTOOLS/backupdata.sh"
#define ROOTME_BIN "/tmp/RECTOOLS/rootme.sh"
#define WIPE_BIN "/tmp/RECTOOLS/wipe.sh"
#define NANDROID_BACKUP "/sdcard/nandroid/"
#define NANDROID_BACKUP "/sdcard/nandroid/"
// Pour emulateur..
//#define SYSTEME_PART "/dev/block/mtdblock0"
//#define DATA_PART "/dev/block/mtdblock1"
// drakaz : define galaxy partitions
#define SYSTEME_PART "/dev/block/mtdblock1"
#define DATA_PART "/dev/block/mmcblk0p1"
#define MAX_COMMAND_ARG 256
static char command_prompt[MAX_COMMAND_ARG];
static char command_label[MAX_COMMAND_ARG];
static char command[MAX_COMMAND_ARG];
static char command_err[MAX_COMMAND_ARG];
// open a file given in root:path format, mounting partitions as necessary
static FILE*
fopen_root_path(const char *root_path, const char *mode) {
if (ensure_root_path_mounted(root_path) != 0) {
LOGE("Can't mount %s\n", root_path);
return NULL;
}
char path[PATH_MAX] = "";
if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
LOGE("Bad path %s\n", root_path);
return NULL;
}
// When writing, try to create the containing directory, if necessary.
// Use generous permissions, the system (init.rc) will reset them.
if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
FILE *fp = fopen(path, mode);
return fp;
}
// close a file, log an error if the error indicator is set
static void
check_and_fclose(FILE *fp, const char *name) {
fflush(fp);
if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
fclose(fp);
}
// command line args come from, in decreasing precedence:
// - the actual command line
// - the bootloader control block (one per line, after "recovery")
// - the contents of COMMAND_FILE (one per line)
static void
get_args(int *argc, char ***argv) {
struct bootloader_message boot;
memset(&boot, 0, sizeof(boot));
get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
if (boot.command[0] != 0 && boot.command[0] != 255) {
LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
}
if (boot.status[0] != 0 && boot.status[0] != 255) {
LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
}
// --- if arguments weren't supplied, look in the bootloader control block
if (*argc <= 1) {
boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
const char *arg = strtok(boot.recovery, "\n");
if (arg != NULL && !strcmp(arg, "recovery")) {
*argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
(*argv)[0] = strdup(arg);
for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
if ((arg = strtok(NULL, "\n")) == NULL) break;
(*argv)[*argc] = strdup(arg);
}
LOGI("Got arguments from boot message\n");
} else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
}
}
// --- if that doesn't work, try the command file
if (*argc <= 1) {
FILE *fp = fopen_root_path(COMMAND_FILE, "r");
if (fp != NULL) {
char *argv0 = (*argv)[0];
*argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
(*argv)[0] = argv0; // use the same program name
char buf[MAX_ARG_LENGTH];
for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
if (!fgets(buf, sizeof(buf), fp)) break;
(*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
}
check_and_fclose(fp, COMMAND_FILE);
LOGI("Got arguments from %s\n", COMMAND_FILE);
}
}
// --> write the arguments we have back into the bootloader control block
// always boot into recovery after this (until finish_recovery() is called)
strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
int i;
for (i = 1; i < *argc; ++i) {
strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
strlcat(boot.recovery, "\n", sizeof(boot.recovery));
}
set_bootloader_message(&boot);
}
// clear the recovery command and prepare to boot a (hopefully working) system,
// copy our log file to cache as well (for the system to read), and
// record any intent we were asked to communicate back to the system.
// this function is idempotent: call it as many times as you like.
static void
finish_recovery(const char *send_intent)
{
// By this point, we're ready to return to the main system...
if (send_intent != NULL) {
FILE *fp = fopen_root_path(INTENT_FILE, "w");
if (fp == NULL) {
LOGE("Can't open %s\n", INTENT_FILE);
} else {
fputs(send_intent, fp);
check_and_fclose(fp, INTENT_FILE);
}
}
// Copy logs to cache so the system can find out what happened.
/*
FILE *log = fopen_root_path(LOG_FILE, "a");
if (log == NULL) {
LOGE("Can't open %s\n", LOG_FILE);
} else {
FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
if (tmplog == NULL) {
LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
} else {
static long tmplog_offset = 0;
fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
char buf[4096];
while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
tmplog_offset = ftell(tmplog);
check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
}
check_and_fclose(log, LOG_FILE);
}
*/
// Reset the bootloader message to revert to a normal main system boot.
struct bootloader_message boot;
memset(&boot, 0, sizeof(boot));
set_bootloader_message(&boot);
// Remove the command file, so recovery won't repeat indefinitely.
char path[PATH_MAX] = "";
if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
(unlink(path) && errno != ENOENT)) {
LOGW("Can't unlink %s\n", COMMAND_FILE);
}
sync(); // For good measure.
}
#define TEST_AMEND 0
#if TEST_AMEND
static void
test_amend()
{
extern int test_symtab(void);
extern int test_cmd_fn(void);
int ret;
LOGD("Testing symtab...\n");
ret = test_symtab();
LOGD(" returned %d\n", ret);
LOGD("Testing cmd_fn...\n");
ret = test_cmd_fn();
LOGD(" returned %d\n", ret);
}
#endif // TEST_AMEND
static int
erase_root(const char *root)
{
ui_set_background(BACKGROUND_ICON_INSTALLING);
ui_show_indeterminate_progress();
ui_print("Formatting %s...\n", root);
return format_root_device(root);
}
static void
run_script(char *str1,char *str2,char *str3,char *str4,char *str5,char *str6,char *str7, bool promptUser)
{
bool confirm = true;
ui_end_menu();
if (promptUser) {
ui_clear_key_queue();
ui_print("\n-- ");
ui_print(str1);
ui_print("\n-- Press HOME to confirm, or");
ui_print("\n-- any other key to abort.");
confirm = (ui_wait_key() == KEY_HOME);
}
if (confirm) {
ui_print(str2);
pid_t pid = fork();
if (pid == 0) {
char *args[] = { "/sbin/sh", "-c", str3, "1>&2", NULL };
execv("/sbin/sh", args);
fprintf(stderr, str4, strerror(errno));
_exit(-1);
}
int status;
while (waitpid(pid, &status, WNOHANG) == 0) {
ui_print(".");
sleep(1);
}
ui_print("\n");
if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
ui_print(str5);
} else {
ui_print(str6);
}
} else {
ui_print(str7);
}
if (!ui_text_visible()) return;
}
static void
run_startup_script() {
ui_print("Running startup script");
pid_t pid = fork();
if (pid == 0) {
char *args[] = { STARTUP_BIN, "1>&2", NULL };
execv(STARTUP_BIN, args);
fprintf(stderr, "\nUnable to execute startup script!\n(%s)", strerror(errno));
_exit(-1);
}
int status;
while (waitpid(pid, &status, WNOHANG) == 0) {
ui_print(".");
sleep(1);
}
if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
ui_print("\nError while executing startup script!\n");
}
}
int device_handle_key(int key_code, int visible) {
if (visible) {
switch (key_code) {
case KEY_CAPSLOCK:
case KEY_DOWN:
case KEY_VOLUMEDOWN:
return HIGHLIGHT_DOWN;
case KEY_LEFTSHIFT:
case KEY_UP:
case KEY_VOLUMEUP:
return HIGHLIGHT_UP;
case KEY_POWER:
/*if (ui_get_showing_back_button()) {
return SELECT_ITEM;
}
if (!get_allow_toggle_display())
return GO_BACK;*/
break;
case KEY_LEFTBRACE:
case KEY_ENTER:
case BTN_MOUSE:
case KEY_CENTER:
case KEY_CAMERA:
case KEY_F21:
case KEY_SEND:
return SELECT_ITEM;
case KEY_END:
case KEY_BACKSPACE:
case KEY_BACK:
//if (!get_allow_toggle_display())
return GO_BACK;
}
}
return NO_ACTION;
}
int
get_menu_selection(char** headers, char** items, int menu_only) {
// throw away keys pressed previously, so user doesn't
// accidentally trigger menu items.
ui_clear_key_queue();
int item_count = ui_start_menu(headers, items);
int selected = 0;
int chosen_item = -1;
// Some users with dead enter keys need a way to turn on power to select.
// Jiggering across the wrapping menu is one "secret" way to enable it.
// We can't rely on /cache or /sdcard since they may not be available.
int wrap_count = 0;
while (chosen_item < 0 && chosen_item != GO_BACK) {
int key = ui_wait_key();
int visible = ui_text_visible();
int action = device_handle_key(key, visible);
int old_selected = selected;
if (action < 0) {
switch (action) {
case HIGHLIGHT_UP:
--selected;
selected = ui_menu_select(selected);
break;
case HIGHLIGHT_DOWN:
++selected;
selected = ui_menu_select(selected);
break;
case SELECT_ITEM:
chosen_item = selected;
/*if (ui_get_showing_back_button()) {
if (chosen_item == item_count) {
chosen_item = GO_BACK;
}
}*/
break;
case NO_ACTION:
break;
case GO_BACK:
chosen_item = GO_BACK;
break;
}
} else if (!menu_only) {
chosen_item = action;
}
if (abs(selected - old_selected) > 1) {
wrap_count++;
if (wrap_count == 3) {
wrap_count = 0;
/*if (ui_get_showing_back_button()) {
ui_print("Back menu button disabled.\n");
ui_set_showing_back_button(0);
}
else {
ui_print("Back menu button enabled.\n");
ui_set_showing_back_button(1);
}*/
}
}
}
//ui_end_menu();
ui_clear_key_queue();
return chosen_item;
}
#if PSFREEDOM == 1
#define PSFREEDOM_PATH "/sdcard/psfreedom/"
#define PSFREEDOM_SELECTED_PAYLOAD PSFREEDOM_PATH "selected_payload.txt"
#define PSFREEDOM_MAX_PAYLOADS 32
static void show_payload_menu()
{
static char* headers[] = { "Choose PSFreedom Payload",
"",
MENU_HINT,
NULL
};
static char* list[PSFREEDOM_MAX_PAYLOADS+2];
struct dirent* dir_ent;
DIR* dir;
int len, i;
int file_nb = 0;
int exit_menu = 0;
memset(list, 0, sizeof(list));
list[0] = "<default>";
/* Create a list of the available payloads (files with .bin extension in PSFREEDOM_PATH folder) */
if ((dir = opendir (PSFREEDOM_PATH)) != NULL){
while ( (dir_ent = readdir ( dir )) != NULL ) {
len = strlen(dir_ent->d_name);
if ((len > 4) &&
(strncmp(&dir_ent->d_name[len-4],".bin",4) == 0)){
if (file_nb < PSFREEDOM_MAX_PAYLOADS) {
list[file_nb + 1] = strdup(dir_ent->d_name);
file_nb++;
}
}
}
}
while (!exit_menu) {
int chosen_item = get_menu_selection(headers, list, 0);
switch (chosen_item) {
case 0 :
/* default paylaod */
unlink(PSFREEDOM_SELECTED_PAYLOAD);
exit_menu = 1;
break;
case GO_BACK:
exit_menu = 1;
break;
default :
if ((chosen_item >= 1) &&
(chosen_item <= file_nb)){
int fd;
fd = open(PSFREEDOM_SELECTED_PAYLOAD,O_CREAT|O_WRONLY|O_TRUNC);
write(fd, PSFREEDOM_PATH, strlen(PSFREEDOM_PATH));
write(fd, list[chosen_item], strlen(list[chosen_item]));
close(fd);
exit_menu = 1;
}
break;
}
}
/* Release dynamically allocated strings */
for (i = 1; i <= file_nb; i++){
free(list[i]);
}
return;
}
#endif
// Nandroid slot support from bukington
static int choose_nandroid_slot()
{
static char* headers[] = { "Choose nandroid SLOT",
"",
MENU_HINT,
NULL };
static char* slots[] = { "Slot 1", "Slot 2", "Slot 3", "Slot 4", NULL };
return get_menu_selection(headers, slots, 0) + 1;
}
static void show_nandroid_menu()
{
#define ITEM_NANDROID_BACKUP 0
#define ITEM_NANDROID_RESTORE 1
#define ITEM_NANDROID_DELETE 2
static char* headers[] = { "Nandroid",
"",
MENU_HINT,
NULL
};
static char* list[] = { "Backup",
"Restore",
"Delete",
/*"Advanced Restore",*/
NULL
};
for (;;) {
int chosen_item = get_menu_selection(headers, list, 0);
switch (chosen_item) {
case ITEM_NANDROID_BACKUP:
{
int slota = choose_nandroid_slot();
if (slota > 0) {
char strSlot[5];
sprintf(strSlot, "SLOT%d", slota);
if (ensure_root_path_mounted("SDCARD:") != 0) {
ui_print("\nCan't mount sdcard\n");
} else {
char sdcard_backup_dir[1024];
strcpy(sdcard_backup_dir, NANDROID_BACKUP);
strcat(sdcard_backup_dir, strSlot);
snprintf(command_label, MAX_COMMAND_ARG, "\nPerforming backup in %s", strSlot);
snprintf(command, MAX_COMMAND_ARG, "%s -b -p %s", NANDROID_BIN, sdcard_backup_dir);
snprintf(command_err, MAX_COMMAND_ARG, "\nE:Can't run %s\n(%s)", NANDROID_BIN);
run_script("",
command_label,
command,
command_err,
"\nError running nandroid backup. Backup not performed.",
"\nBackup complete!",
"\nBackup aborted by user!",
false);
}
}
}
break;
case ITEM_NANDROID_RESTORE:
{
int slota;
for (;;) {
slota = choose_nandroid_slot();
if (slota < 1)
break;
char strSlot[5];
sprintf(strSlot, "SLOT%d", slota);
static const char* headers[] = { "Choose a backup to restore",
"",
MENU_HINT,
NULL
};
char sdcard_backup_dir[1024];
strcpy(sdcard_backup_dir, NANDROID_BACKUP);
strcat(sdcard_backup_dir, strSlot);
strcat(sdcard_backup_dir, "/");
char* file = choose_file_menu(sdcard_backup_dir, NULL, headers);
if (file != NULL) {
char* backup = basename(file);
snprintf(command_prompt, MAX_COMMAND_ARG, "Restore backup %s from %s", backup, strSlot);
snprintf(command_label, MAX_COMMAND_ARG, "\nRestoring backup %s from %s", backup, strSlot);
snprintf(command, MAX_COMMAND_ARG, "%s --restore --defaultinput -p %s -s %s", NANDROID_BIN, sdcard_backup_dir, backup);
snprintf(command_err, MAX_COMMAND_ARG, "\nE:Can't run %s\n(\%s)", NANDROID_BIN);
run_script(command_prompt,
command_label,
command,
command_err,
"\nError running nandroid restore! Try running 'nandroid-mobile.sh --restore' from console.",
"\nRestore complete!",
"\nRestore aborted by user!",
true);
}
}
}
break;
case ITEM_NANDROID_DELETE:
{
int slota;
for (;;) {
slota = choose_nandroid_slot();
if (slota < 1)
break;
char strSlot[5];
sprintf(strSlot, "SLOT%d", slota);
static const char* headers[] = { "Choose a backup to delete",
"",
MENU_HINT,
NULL
};
char sdcard_backup_dir[1024];
strcpy(sdcard_backup_dir, NANDROID_BACKUP);
strcat(sdcard_backup_dir, strSlot);
strcat(sdcard_backup_dir, "/");
char* file = NULL;
// Keep menu while files are deleted
for (;;) {
file = choose_file_menu(sdcard_backup_dir, NULL, headers);
if (file == NULL)
break;
char* backup = basename(file);
snprintf(command_prompt, MAX_COMMAND_ARG, "Delete backup %s from %s", backup, strSlot);
snprintf(command_label, MAX_COMMAND_ARG, "\nDeleting backup %s from %s", backup, strSlot);
snprintf(command, MAX_COMMAND_ARG, "%s -d --defaultinput -p %s -s %s", NANDROID_BIN, sdcard_backup_dir, backup);
snprintf(command_err, MAX_COMMAND_ARG, "\nE:Can't run %s\n(\%s)", NANDROID_BIN);
run_script(command_prompt,
command_label,
command,
command_err,
"\nError performing delete! Try running 'nandroid-mobile.sh -d' from console.",
"\nDelete complete!",
"\nDelete aborted by user!",
true);
}
}
}
break;
case GO_BACK:
return;
break;
}
}
}
static void
choose_update_file() {
if (ensure_root_path_mounted(SDCARD_PATH) != 0) {
LOGE("Can't mount %s\n", SDCARD_PATH);
return;
}
static const char* headers[] = { "Choose a zip to apply",
"",
MENU_HINT,
NULL
};
char* file = choose_file_menu("/sdcard/", ".zip", headers);
if (file == NULL)
return;
char sdcard_package_file[1024];
strcpy(sdcard_package_file, "SDCARD:");
strcat(sdcard_package_file, file + strlen("/sdcard/"));
ui_end_menu();
ui_print("\n-- Installing new image!");
ui_print("\n-- Press HOME to confirm, or");
ui_print("\n-- any other key to abort\n\n");
int confirm_apply = ui_wait_key();
if (confirm_apply == KEY_DREAM_HOME) {
ui_print("\nInstalling from sdcard...\n");
int status = install_package(sdcard_package_file);
if (status != INSTALL_SUCCESS) {
ui_set_background(BACKGROUND_ICON_ERROR);
ui_print("Installation failed\n");
} else if (!ui_text_visible()) {
return;//break; // reboot if logs aren't visible
} else {
if (firmware_update_pending()) {
ui_print("\nReboot\n"
"to complete installation\n");
} else {
ui_print("\nInstall from sdcard complete\n");
}
}
} else {
ui_print("\nInstallation failed");
}
}
static void
choose_theme_file()
{
static char* headers[] = { "Choose theme ZIP file",
"",
MENU_HINT,
NULL };
// Mount system partition
ui_print("\nRemounting system partition in rw..");
pid_t pidtheme1 = fork();
if (pidtheme1 == 0) {
char *argstheme1[] = { "mount", "/system", NULL };
execv("/sbin/busybox", argstheme1);
fprintf(stderr, "Can't mount %s\n(%s)\n", SYSTEME_PART, strerror(errno));
_exit(-1);
}
int statustheme1;
while (waitpid(pidtheme1, &statustheme1, WNOHANG) == 0) {
ui_print(".");
sleep(1);
}
// Remount system partition in rw
pid_t pidtheme2 = fork();
if (pidtheme2 == 0) {
char *argstheme2[] = { "mount", "-o", "remount,rw", SYSTEME_PART, "/system", NULL };
execv("/sbin/busybox", argstheme2);
fprintf(stderr, "Can't remount %s\n(%s) in rw\n", SYSTEME_PART, strerror(errno));
_exit(-1);
}
int statustheme2;
while (waitpid(pidtheme2, &statustheme2, WNOHANG) == 0) {
ui_print(".");
sleep(1);
}
ui_print("OK\n");
char path[PATH_MAX] = "";
DIR *dir;
struct dirent *de;
char **files;
int total = 0;
int i;
if (ensure_root_path_mounted(THEMES_PATH) != 0) {
LOGE("Can't mount %s\n", THEMES_PATH);
return;
}
if (translate_root_path(THEMES_PATH, path, sizeof(path)) == NULL) {
LOGE("Bad path %s", path);
return;
}
dir = opendir(path);
if (dir == NULL) {
LOGE("Couldn't open directory %s", path);
return;
}
/* count how many files we're looking at */
while ((de = readdir(dir)) != NULL) {
char *extension = strrchr(de->d_name, '.');
if (extension == NULL || de->d_name[0] == '.') {
continue;
} else if (!strcasecmp(extension, ".zip")) {
total++;
}
}
/* allocate the array for the file list menu */
files = (char **) malloc((total + 1) * sizeof(*files));
files[total] = NULL;
/* set it up for the second pass */
rewinddir(dir);
/* put the names in the array for the menu */
i = 0;
while ((de = readdir(dir)) != NULL) {
char *extension = strrchr(de->d_name, '.');
if (extension == NULL || de->d_name[0] == '.') {
continue;
} else if (!strcasecmp(extension, ".zip")) {
files[i] = (char *) malloc(THEMES_PATH_LENGTH + strlen(de->d_name) + 1);
strcpy(files[i], THEMES_PATH);
strcat(files[i], de->d_name);
i++;
}
}
/* close directory handle */
if (closedir(dir) < 0) {
LOGE("Failure closing directory %s", path);
goto out;
}
ui_start_menu(headers, files);
int selected = 0;
int chosen_item = -1;
finish_recovery(NULL);
ui_reset_progress();
for (;;) {
int key = ui_wait_key();
int visible = ui_text_visible();
if (key == KEY_DREAM_BACK) {
break;
} else if ((key == KEY_DOWN || key == KEY_VOLUMEDOWN) && visible) {
++selected;
selected = ui_menu_select(selected);
} else if ((key == KEY_UP || key == KEY_VOLUMEUP) && visible) {
--selected;
selected = ui_menu_select(selected);
} else if ((key == BTN_MOUSE || key == KEY_I7500_CENTER) && visible) {
chosen_item = selected;
}
if (chosen_item >= 0) {
// turn off the menu, letting ui_print() to scroll output
// on the screen.
ui_end_menu();
ui_print("\n-- Installing new theme!");
ui_print("\n-- Press HOME to confirm, or");
ui_print("\n-- any other key to abort..");
int confirm_apply = ui_wait_key();
if (confirm_apply == KEY_DREAM_HOME) {
ui_print("\n-- Install new theme from sdcard...\n");
int status = install_package(files[chosen_item]);
if (status != INSTALL_SUCCESS) {
ui_set_background(BACKGROUND_ICON_ERROR);
ui_print("Installation aborted.\n");
} else if (!ui_text_visible()) {
break; // reboot if logs aren't visible
} else {
if (firmware_update_pending()) {
ui_print("\nReboot via menu\n"
"to complete installation.\n");
} else {
ui_print("\nInstall new theme from sdcard complete.\n");
}
}
} else {
ui_print("\nInstallation aborted.\n");
}
if (!ui_text_visible()) break;
break;
}
}
out:
for (i = 0; i < total; i++) {
free(files[i]);
}
free(files);
}
static void
prompt_and_wait()
{
// drakaz : new headers
static char* headers[] = { "Android system recovery " EXPAND(RECOVERY_VERSION),
" --- Galaxy Version ---",
"",
NULL };
// drakaz : add news functions
// these constants correspond to elements of the items[] list.
#define ITEM_REBOOT 0
#define ITEM_REBOOT_RECOVERY 1
#if PSFREEDOM == 0
#define ITEM_APPLY_SDCARD 2
#define ITEM_APPLY_UPDATE 3
//#define ITEM_APPLY_THEME 3
//#define ITEM_GRESTORE 4
#define UMS_ON 4
#define UMS_OFF 5
//#define ITEM_BACKUP_DATA 7
//#define ITEM_RESTORE_DATA 8
#define ITEM_NANDROID 6
//#define ITEM_SU_ON 9
//#define ITEM_SU_OFF 10
#define ITEM_WIPE_DATA 7
#define ITEM_WIPE_DATAK 8
#define ITEM_FSCK 9
#define ITEM_SD_SWAP_ON 10
#define ITEM_SD_SWAP_OFF 11
#define ITEM_FORMAT_EXT3 12
#define ITEM_FORMAT_EXT4 13
#define FIX_PERMS 14
#define ITEM_ROOTME 15
//#define CONVERT_DATA_EXT4 17
#define FLASH_PSFREEDOM 16
#else
#define START_PSFREEDOM 2
#define PAYLOAD_PSFREEDOM 3
#endif
// drakaz : delete console access because of non existent keyboard on galaxy