2 * (C) Copyright 2011-2013 Pali Rohár <pali.rohar@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0+
14 #include <linux/string.h>
16 /* maximum bootmenu entries */
19 /* maximal size of bootmenu env
20 * 9 = strlen("bootmenu_")
21 * 2 = strlen(MAX_COUNT)
24 #define MAX_ENV_SIZE (9 + 2 + 1)
26 struct bootmenu_entry {
27 unsigned short int num; /* unique number 0 .. MAX_COUNT */
28 char key[3]; /* key identifier of number */
29 char *title; /* title of entry */
30 char *command; /* hush command of entry */
31 struct bootmenu_data *menu; /* this bootmenu */
32 struct bootmenu_entry *next; /* next menu entry (num+1) */
35 struct bootmenu_data {
36 int delay; /* delay for autoboot */
37 int active; /* active menu entry */
38 int count; /* total count of menu entries */
39 struct bootmenu_entry *first; /* first menu entry */
49 static char *bootmenu_getoption(unsigned short int n)
51 char name[MAX_ENV_SIZE];
56 sprintf(name, "bootmenu_%d", n);
60 static void bootmenu_print_entry(void *data)
62 struct bootmenu_entry *entry = data;
63 int reverse = (entry->menu->active == entry->num);
66 * Move cursor to line where the entry will be drown (entry->num)
67 * First 3 lines contain bootmenu header + 1 empty line
69 printf(ANSI_CURSOR_POSITION, entry->num + 4, 1);
74 puts(ANSI_COLOR_REVERSE);
79 puts(ANSI_COLOR_RESET);
82 static void bootmenu_autoboot_loop(struct bootmenu_data *menu,
83 enum bootmenu_key *key, int *esc)
87 if (menu->delay > 0) {
88 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
89 printf(" Hit any key to stop autoboot: %2d ", menu->delay);
92 while (menu->delay > 0) {
93 for (i = 0; i < 100; ++i) {
123 printf("\b\b\b%2d ", menu->delay);
126 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
127 puts(ANSI_CLEAR_LINE);
129 if (menu->delay == 0)
133 static void bootmenu_loop(struct bootmenu_data *menu,
134 enum bootmenu_key *key, int *esc)
147 /* First char of ANSI escape sequence '\e' */
154 /* Second char of ANSI '[' */
164 /* Third char of ANSI (number '1') - optional */
165 if (*esc == 2 && c == '1') {
173 /* ANSI 'A' - key up was pressed */
176 /* ANSI 'B' - key down was pressed */
179 /* other key was pressed */
186 /* enter key was pressed */
191 static char *bootmenu_choice_entry(void *data)
193 struct bootmenu_data *menu = data;
194 struct bootmenu_entry *iter;
195 enum bootmenu_key key = KEY_NONE;
200 if (menu->delay >= 0) {
201 /* Autoboot was not stopped */
202 bootmenu_autoboot_loop(menu, &key, &esc);
204 /* Some key was pressed, so autoboot was stopped */
205 bootmenu_loop(menu, &key, &esc);
210 if (menu->active > 0)
212 /* no menu key selected, regenerate menu */
215 if (menu->active < menu->count - 1)
217 /* no menu key selected, regenerate menu */
221 for (i = 0; i < menu->active; ++i)
230 debug("bootmenu: this should not happen");
234 static void bootmenu_destroy(struct bootmenu_data *menu)
236 struct bootmenu_entry *iter = menu->first;
237 struct bootmenu_entry *next;
249 static struct bootmenu_data *bootmenu_create(int delay)
251 unsigned short int i = 0;
253 struct bootmenu_data *menu;
254 struct bootmenu_entry *iter = NULL;
258 struct bootmenu_entry *entry;
260 menu = malloc(sizeof(struct bootmenu_data));
268 while ((option = bootmenu_getoption(i))) {
269 sep = strchr(option, '=');
271 printf("Invalid bootmenu entry: %s\n", option);
275 entry = malloc(sizeof(struct bootmenu_entry));
280 entry->title = malloc(len + 1);
285 memcpy(entry->title, option, len);
286 entry->title[len] = 0;
288 len = strlen(sep + 1);
289 entry->command = malloc(len + 1);
290 if (!entry->command) {
295 memcpy(entry->command, sep + 1, len);
296 entry->command[len] = 0;
298 sprintf(entry->key, "%d", i);
312 if (i == MAX_COUNT - 1)
316 /* Add U-Boot console entry at the end */
317 if (i <= MAX_COUNT - 1) {
318 entry = malloc(sizeof(struct bootmenu_entry));
322 entry->title = strdup("U-Boot console");
328 entry->command = strdup("");
329 if (!entry->command) {
335 sprintf(entry->key, "%d", i);
354 bootmenu_destroy(menu);
358 static void bootmenu_show(int delay)
363 char *command = NULL;
365 struct bootmenu_data *bootmenu;
366 struct bootmenu_entry *iter;
369 /* If delay is 0 do not create menu, just run first entry */
371 option = bootmenu_getoption(0);
373 puts("bootmenu option 0 was not found\n");
376 sep = strchr(option, '=');
378 puts("bootmenu option 0 is invalid\n");
381 run_command(sep+1, 0);
385 bootmenu = bootmenu_create(delay);
389 menu = menu_create(NULL, bootmenu->delay, 1, bootmenu_print_entry,
390 bootmenu_choice_entry, bootmenu);
392 bootmenu_destroy(bootmenu);
396 for (iter = bootmenu->first; iter; iter = iter->next) {
397 if (!menu_item_add(menu, iter->key, iter))
401 /* Default menu entry is always first */
402 menu_default_set(menu, "0");
404 puts(ANSI_CURSOR_HIDE);
405 puts(ANSI_CLEAR_CONSOLE);
406 printf(ANSI_CURSOR_POSITION, 1, 1);
410 if (menu_get_choice(menu, &choice)) {
412 title = strdup(iter->title);
413 command = strdup(iter->command);
418 bootmenu_destroy(bootmenu);
421 puts(ANSI_CURSOR_SHOW);
422 puts(ANSI_CLEAR_CONSOLE);
423 printf(ANSI_CURSOR_POSITION, 1, 1);
426 if (title && command) {
427 debug("Starting entry '%s'\n", title);
429 run_command(command, 0);
433 #ifdef CONFIG_POSTBOOTMENU
434 run_command(CONFIG_POSTBOOTMENU, 0);
438 void menu_display_statusline(struct menu *m)
440 struct bootmenu_entry *entry;
441 struct bootmenu_data *menu;
443 if (menu_default_choice(m, (void *)&entry) < 0)
448 printf(ANSI_CURSOR_POSITION, 1, 1);
449 puts(ANSI_CLEAR_LINE);
450 printf(ANSI_CURSOR_POSITION, 2, 1);
451 puts(" *** U-Boot Boot Menu ***");
452 puts(ANSI_CLEAR_LINE_TO_END);
453 printf(ANSI_CURSOR_POSITION, 3, 1);
454 puts(ANSI_CLEAR_LINE);
456 /* First 3 lines are bootmenu header + 2 empty lines between entries */
457 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
458 puts(ANSI_CLEAR_LINE);
459 printf(ANSI_CURSOR_POSITION, menu->count + 6, 1);
460 puts(" Press UP/DOWN to move, ENTER to select");
461 puts(ANSI_CLEAR_LINE_TO_END);
462 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
463 puts(ANSI_CLEAR_LINE);
466 #ifdef CONFIG_MENU_SHOW
467 int menu_show(int bootdelay)
469 bootmenu_show(bootdelay);
470 return -1; /* -1 - abort boot and run monitor code */
474 int do_bootmenu(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
476 char *delay_str = NULL;
479 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
480 delay = CONFIG_BOOTDELAY;
487 delay_str = getenv("bootmenu_delay");
490 delay = (int)simple_strtol(delay_str, NULL, 10);
492 bootmenu_show(delay);
497 bootmenu, 2, 1, do_bootmenu,
498 "ANSI terminal bootmenu",
500 " - show ANSI terminal bootmenu with autoboot delay"