forked from fedya/bootmenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootmenu.c
251 lines (214 loc) · 5.85 KB
/
bootmenu.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
/*
* 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 <errno.h>
#include <linux/input.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/reboot.h>
//#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "common.h"
#include "extendedcommands.h"
#include "minui/minui.h"
#include "bootmenu_ui.h"
enum {
INSTALL_SUCCESS,
INSTALL_ERROR,
INSTALL_CORRUPT
};
static const char *FILE_PRE_MENU = "/system/bootmenu/script/pre_bootmenu.sh";
static const char *FILE_POST_MENU = "/system/bootmenu/script/post_bootmenu.sh";
char**
prepend_title(const char** headers) {
char* title[] = { "Android BootMenu <v"
EXPAND(BOOTMENU_VERSION) ">",
"",
NULL };
// count the number of lines in our title, plus the
// caller-provided headers.
int count = 0;
char** p;
for (p = title; *p; ++p, ++count);
for (p = headers; *p; ++p, ++count);
char** new_headers = malloc((count+1) * sizeof(char*));
char** h = new_headers;
for (p = title; *p; ++p, ++h) *h = *p;
for (p = headers; *p; ++p, ++h) *h = *p;
*h = NULL;
return new_headers;
}
int
get_menu_selection(char** headers, char** items, int menu_only,
int initial_selection) {
// throw away keys pressed previously, so user doesn't
// accidentally trigger menu items.
ui_clear_key_queue();
ui_start_menu(headers, items, initial_selection);
int selected = initial_selection;
int chosen_item = -1;
while (chosen_item < 0) {
int key = ui_wait_key();
int visible = ui_text_visible();
int action = device_handle_key(key, visible);
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;
break;
case NO_ACTION:
break;
}
} else if (!menu_only) {
chosen_item = action;
}
}
ui_end_menu();
return chosen_item;
}
static int compare_string(const void* a, const void* b) {
return strcmp(*(const char**)a, *(const char**)b);
}
static void
prompt_and_wait() {
char** headers = prepend_title((const char**)MENU_HEADERS);
int select = 0;
for (;;) {
ui_reset_progress();
int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, select);
// device-specific code may take some action here. It may
// return one of the core actions handled in the switch
// statement below.
chosen_item = device_perform_action(chosen_item);
switch (chosen_item) {
case ITEM_BOOT:
if (show_menu_boot()) return; else break;
case ITEM_SYSTEM:
if (show_menu_system()) return; else break;
case ITEM_RECOVERY:
if (show_menu_recovery()) return; else break;
case ITEM_TOOLS:
if (show_menu_tools()) return; else break;
case ITEM_REBOOT:
ui_print("Reboot now....\n");
sync();
reboot(RB_AUTOBOOT);
return;
}
select = chosen_item;
}
}
static void
ui_finish(void) {
LOGI("Exiting....\n");
ui_final();
}
static int
wait_key(int key) {
int i;
int result = INSTALL_SUCCESS;
evt_init();
ui_clear_key_queue();
for(i=0; i < 300; i++) {
if(ui_key_pressed(key)) {
result = INSTALL_ERROR;
}
else {
usleep(5000);
}
}
evt_exit();
return result;
}
static int
run_bootmenu(void) {
time_t start = time(NULL);
LOGI("Starting bootmenu on %s", ctime(&start));
if (bypass_check()) {
int status = INSTALL_SUCCESS;
exec_script(FILE_PRE_MENU, DISABLE);
led_alert("blue", ENABLE);
status = wait_key(KEY_VOLUMEDOWN);
if (status != INSTALL_ERROR) {
int mode = get_bootmode();
switch (mode) {
case MODE_2NDINIT:
led_alert("blue", DISABLE);
led_alert("green", ENABLE);
snd_init(DISABLE);
led_alert("green", DISABLE);
break;
case MODE_2NDBOOT:
led_alert("blue", DISABLE);
led_alert("red", ENABLE);
snd_boot(DISABLE);
led_alert("green", DISABLE);
break;
case MODE_BOOTMENU:
status = INSTALL_ERROR;
break;
}
}
if (status != INSTALL_SUCCESS) {
ui_init();
ui_set_background(BACKGROUND_ICON_ERROR);
ui_show_text(ENABLE);
LOGI("Start Android BootMenu....\n");
prompt_and_wait();
ui_finish();
}
led_alert("blue", DISABLE);
return EXIT_SUCCESS;
}
return EXIT_SUCCESS;
}
int
main(int argc, char **argv) {
char* hijacked_executable = argv[0];
int result;
if (NULL != strstr(hijacked_executable, "bootmenu")) {
fprintf(stdout, "Run BootMenu..\n");
result = run_bootmenu();
sync();
return result;
}
else if (argc >= 3 && 0 == strcmp(argv[2], "userdata")) {
real_execute(argc, argv);
result = run_bootmenu();
bypass_sign("no");
sync();
return result;
}
else if (argc >= 3 && 0 == strcmp(argv[2], "pds")) {
real_execute(argc, argv);
exec_script(FILE_OVERCLOCK, DISABLE);
result = exec_script(FILE_POST_MENU, DISABLE);
bypass_sign("no");
sync();
return result;
}
else {
return real_execute(argc, argv);
}
}