forked from CyanogenDefy/android_external_bootmenu
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bootmenu.c
411 lines (353 loc) · 9.29 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
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
/*
* Copyright (C) 2007-2011 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 "overclock.h"
#include "minui/minui.h"
#include "bootmenu_ui.h"
enum {
BUTTON_ERROR,
BUTTON_PRESSED,
BUTTON_TIMEOUT,
};
/* Main menu items */
#define ITEM_REBOOT 0
#define ITEM_BOOT 1
#define ITEM_SYSTEM 2
#define ITEM_OVERCLOCK 2
#define ITEM_RECOVERY 3
#define ITEM_TOOLS 4
#define ITEM_POWEROFF 5
#define ITEM_LAST 5
char* MENU_ITEMS[] = {
" [Reboot]",
" +Boot -->",
#if STOCK_VERSION
" +System -->",
#else
" +CPU Settings -->",
#endif
" +Recovery -->",
" +Tools -->",
" [Shutdown]",
NULL
};
static char** main_headers = NULL;
/**
* prepend_title()
*
*/
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 = (char**) 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 = (char**) headers; *p; ++p, ++h) *h = *p;
*h = NULL;
return new_headers;
}
void free_menu_headers(char **headers) {
char** p = headers;
for (p = headers; *p; ++p) *p = NULL;
if (headers != NULL) {
free(headers);
headers = NULL;
}
}
/**
* get_menu_selection()
*
*/
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 ACTION_CANCEL:
chosen_item = GO_BACK;
break;
case NO_ACTION:
break;
}
} else if (!menu_only) {
chosen_item = action;
}
}
ui_end_menu();
return chosen_item;
}
/**
* compare_string()
*
*/
static int compare_string(const void* a, const void* b) {
return strcmp(*(const char**)a, *(const char**)b);
}
/**
* prompt_and_wait()
*
*/
static void prompt_and_wait() {
int select = 0;
for (;;) {
ui_reset_progress();
int chosen_item = get_menu_selection(main_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.
if (chosen_item >= 0 && chosen_item <= ITEM_LAST) {
switch (chosen_item) {
case ITEM_REBOOT:
sync();
reboot(RB_AUTOBOOT);
return;
case ITEM_BOOT:
if (show_menu_boot()) return;
break;
#if STOCK_VERSION
case ITEM_SYSTEM:
if (show_menu_system()) return;
break;
#else
case ITEM_OVERCLOCK:
if (show_menu_overclock()) return;
break;
#endif
case ITEM_RECOVERY:
if (show_menu_recovery()) return;
break;
case ITEM_TOOLS:
if (show_menu_tools()) return;
break;
case ITEM_POWEROFF:
sync();
__reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_POWER_OFF, NULL);
return;
}
select = chosen_item;
}
}
}
/**
* ui_finish()
*
*/
static void ui_finish(void) {
LOGI("Exiting....\n");
ui_final();
}
/**
* wait_key()
*
*/
static int wait_key(int key) {
int i;
int result = 0;
evt_init();
//ui_clear_key_queue();
for(i=0; i < 100; i++) {
if(ui_key_pressed(key)) {
led_alert("blue", DISABLE);
result = 1;
break;
}
else {
usleep(15000); //15ms * 100
}
}
evt_exit();
return result;
}
/**
* run_bootmenu()
*
*/
static int run_bootmenu(void) {
int defmode, mode, status = BUTTON_ERROR;
int adb_started = 0;
time_t start = time(NULL);
LOGI("Starting bootmenu on %s", ctime(&start));
if (bypass_check()) {
// init rootfs and mount cache
exec_script(FILE_PRE_MENU, DISABLE);
led_alert("blue", ENABLE);
defmode = get_default_bootmode();
// get and clean one shot bootmode (or default)
mode = get_bootmode(1);
if (mode == int_mode("bootmenu")
|| mode == int_mode("recovery")
|| mode == int_mode("shell")) {
// dont wait if these modes are asked
} else {
status = (wait_key(KEY_VOLUMEDOWN) ? BUTTON_PRESSED : BUTTON_TIMEOUT);
}
// only start adb if usb is connected
if (usb_connected()) {
if (mode == int_mode("2nd-init-adb") || mode == int_mode("2nd-boot-adb")) {
exec_script(FILE_ADBD, DISABLE);
adb_started = 1;
}
}
// on timeout
if (status != BUTTON_PRESSED) {
if (mode == int_mode("bootmenu")) {
led_alert("blue", DISABLE);
status = BUTTON_PRESSED;
}
else if (mode == int_mode("2nd-init") || mode == int_mode("2nd-init-adb")) {
led_alert("blue", DISABLE);
led_alert("green", ENABLE);
snd_init(DISABLE);
led_alert("green", DISABLE);
status = BUTTON_TIMEOUT;
}
else if (mode == int_mode("2nd-boot") || mode == int_mode("2nd-boot-adb")) {
led_alert("blue", DISABLE);
led_alert("red", ENABLE);
snd_boot(DISABLE);
led_alert("red", DISABLE);
status = BUTTON_TIMEOUT;
}
else if (mode == int_mode("recovery")) {
led_alert("blue", DISABLE);
exec_script(FILE_STABLERECOVERY, DISABLE);
status = BUTTON_TIMEOUT;
}
else if (mode == int_mode("shell")) {
led_alert("blue", DISABLE);
exec_script(FILE_ADBD, DISABLE);
status = BUTTON_PRESSED;
}
#if STOCK_VERSION
else if (mode == int_mode("normal") || mode == int_mode("normal-adb")) {
led_alert("blue", DISABLE);
stk_boot(DISABLE);
status = BUTTON_TIMEOUT;
}
#endif
}
if (status == BUTTON_PRESSED ) {
ui_init();
ui_set_background(BACKGROUND_DEFAULT);
ui_show_text(ENABLE);
LOGI("Start Android BootMenu....\n");
main_headers = prepend_title((const char**)MENU_HEADERS);
/* can be buggy
if (!adb_started && usb_connected()) {
ui_print("Usb connected, starting adb...\n\n");
//ui_print("Battery level: %d %%\n", battery_level());
exec_script(FILE_ADBD, DISABLE);
}
*/
ui_print("Default mode: %s\n", str_mode(defmode));
if (mode == int_mode("shell")) {
ui_print("\n");
ui_print("Current mode: %s\n", str_mode(mode));
if (!usb_connected()) {
ui_print(" But USB is not connected !\n");
}
}
checkup_report();
prompt_and_wait();
free_menu_headers(main_headers);
ui_finish();
}
}
return EXIT_SUCCESS;
}
/**
* main()
*
* Here is the hijack part, logwrapper is linked to bootmenu
* we trap some of logged commands from init.rc
*
*/
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();
real_execute(argc, argv);
bypass_sign("no");
sync();
return result;
}
else if (argc >= 3 && 0 == strcmp(argv[2], "pds")) {
//kept for stock rom compatibility, please use postbootmenu
real_execute(argc, argv);
exec_script(FILE_OVERCLOCK, DISABLE);
result = exec_script(FILE_POST_MENU, DISABLE);
bypass_sign("no");
sync();
return result;
}
else if (argc == 2 && 0 == strcmp(argv[1], "postbootmenu")) {
exec_script(FILE_OVERCLOCK, DISABLE);
result = exec_script(FILE_POST_MENU, DISABLE);
bypass_sign("no");
sync();
return result;
}
else {
return real_execute(argc, argv);
}
}