1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2011-2013 Pali Rohár <pali@kernel.org>
14 #include <linux/delay.h>
15 #include <linux/string.h>
17 /* maximum bootmenu entries */
20 /* maximal size of bootmenu env
21 * 9 = strlen("bootmenu_")
22 * 2 = strlen(MAX_COUNT)
25 #define MAX_ENV_SIZE (9 + 2 + 1)
27 struct bootmenu_entry {
28 unsigned short int num; /* unique number 0 .. MAX_COUNT */
29 char key[3]; /* key identifier of number */
30 char *title; /* title of entry */
31 char *command; /* hush command of entry */
32 struct bootmenu_data *menu; /* this bootmenu */
33 struct bootmenu_entry *next; /* next menu entry (num+1) */
36 struct bootmenu_data {
37 int delay; /* delay for autoboot */
38 int active; /* active menu entry */
39 int count; /* total count of menu entries */
40 struct bootmenu_entry *first; /* first menu entry */
51 static char *bootmenu_getoption(unsigned short int n)
53 char name[MAX_ENV_SIZE];
58 sprintf(name, "bootmenu_%d", n);
62 static void bootmenu_print_entry(void *data)
64 struct bootmenu_entry *entry = data;
65 int reverse = (entry->menu->active == entry->num);
68 * Move cursor to line where the entry will be drown (entry->num)
69 * First 3 lines contain bootmenu header + 1 empty line
71 printf(ANSI_CURSOR_POSITION, entry->num + 4, 1);
76 puts(ANSI_COLOR_REVERSE);
81 puts(ANSI_COLOR_RESET);
84 static void bootmenu_autoboot_loop(struct bootmenu_data *menu,
85 enum bootmenu_key *key, int *esc)
89 if (menu->delay > 0) {
90 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
91 printf(" Hit any key to stop autoboot: %2d ", menu->delay);
94 while (menu->delay > 0) {
95 for (i = 0; i < 100; ++i) {
128 printf("\b\b\b%2d ", menu->delay);
131 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
132 puts(ANSI_CLEAR_LINE);
134 if (menu->delay == 0)
138 static void bootmenu_loop(struct bootmenu_data *menu,
139 enum bootmenu_key *key, int *esc)
164 /* First char of ANSI escape sequence '\e' */
171 /* Second char of ANSI '[' */
176 /* Alone ESC key was pressed */
178 *esc = (c == '\e') ? 1 : 0;
183 /* Third char of ANSI (number '1') - optional */
184 if (*esc == 2 && c == '1') {
192 /* ANSI 'A' - key up was pressed */
195 /* ANSI 'B' - key down was pressed */
198 /* other key was pressed */
205 /* enter key was pressed */
214 static char *bootmenu_choice_entry(void *data)
216 struct bootmenu_data *menu = data;
217 struct bootmenu_entry *iter;
218 enum bootmenu_key key = KEY_NONE;
223 if (menu->delay >= 0) {
224 /* Autoboot was not stopped */
225 bootmenu_autoboot_loop(menu, &key, &esc);
227 /* Some key was pressed, so autoboot was stopped */
228 bootmenu_loop(menu, &key, &esc);
233 if (menu->active > 0)
235 /* no menu key selected, regenerate menu */
238 if (menu->active < menu->count - 1)
240 /* no menu key selected, regenerate menu */
244 for (i = 0; i < menu->active; ++i)
248 /* Quit by choosing the last entry - U-Boot console */
259 debug("bootmenu: this should not happen");
263 static void bootmenu_destroy(struct bootmenu_data *menu)
265 struct bootmenu_entry *iter = menu->first;
266 struct bootmenu_entry *next;
278 static struct bootmenu_data *bootmenu_create(int delay)
280 unsigned short int i = 0;
282 struct bootmenu_data *menu;
283 struct bootmenu_entry *iter = NULL;
288 struct bootmenu_entry *entry;
290 menu = malloc(sizeof(struct bootmenu_data));
298 default_str = env_get("bootmenu_default");
300 menu->active = (int)simple_strtol(default_str, NULL, 10);
302 while ((option = bootmenu_getoption(i))) {
303 sep = strchr(option, '=');
305 printf("Invalid bootmenu entry: %s\n", option);
309 entry = malloc(sizeof(struct bootmenu_entry));
314 entry->title = malloc(len + 1);
319 memcpy(entry->title, option, len);
320 entry->title[len] = 0;
322 len = strlen(sep + 1);
323 entry->command = malloc(len + 1);
324 if (!entry->command) {
329 memcpy(entry->command, sep + 1, len);
330 entry->command[len] = 0;
332 sprintf(entry->key, "%d", i);
346 if (i == MAX_COUNT - 1)
350 /* Add U-Boot console entry at the end */
351 if (i <= MAX_COUNT - 1) {
352 entry = malloc(sizeof(struct bootmenu_entry));
356 entry->title = strdup("U-Boot console");
362 entry->command = strdup("");
363 if (!entry->command) {
369 sprintf(entry->key, "%d", i);
386 if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu
387 printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1);
394 bootmenu_destroy(menu);
398 static void menu_display_statusline(struct menu *m)
400 struct bootmenu_entry *entry;
401 struct bootmenu_data *menu;
403 if (menu_default_choice(m, (void *)&entry) < 0)
408 printf(ANSI_CURSOR_POSITION, 1, 1);
409 puts(ANSI_CLEAR_LINE);
410 printf(ANSI_CURSOR_POSITION, 2, 1);
411 puts(" *** U-Boot Boot Menu ***");
412 puts(ANSI_CLEAR_LINE_TO_END);
413 printf(ANSI_CURSOR_POSITION, 3, 1);
414 puts(ANSI_CLEAR_LINE);
416 /* First 3 lines are bootmenu header + 2 empty lines between entries */
417 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
418 puts(ANSI_CLEAR_LINE);
419 printf(ANSI_CURSOR_POSITION, menu->count + 6, 1);
420 puts(" Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit");
421 puts(ANSI_CLEAR_LINE_TO_END);
422 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
423 puts(ANSI_CLEAR_LINE);
426 static void bootmenu_show(int delay)
431 char *command = NULL;
433 struct bootmenu_data *bootmenu;
434 struct bootmenu_entry *iter;
437 /* If delay is 0 do not create menu, just run first entry */
439 option = bootmenu_getoption(0);
441 puts("bootmenu option 0 was not found\n");
444 sep = strchr(option, '=');
446 puts("bootmenu option 0 is invalid\n");
449 run_command(sep+1, 0);
453 bootmenu = bootmenu_create(delay);
457 menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline,
458 bootmenu_print_entry, bootmenu_choice_entry,
461 bootmenu_destroy(bootmenu);
465 for (iter = bootmenu->first; iter; iter = iter->next) {
466 if (!menu_item_add(menu, iter->key, iter))
470 /* Default menu entry is always first */
471 menu_default_set(menu, "0");
473 puts(ANSI_CURSOR_HIDE);
474 puts(ANSI_CLEAR_CONSOLE);
475 printf(ANSI_CURSOR_POSITION, 1, 1);
479 if (menu_get_choice(menu, &choice)) {
481 title = strdup(iter->title);
482 command = strdup(iter->command);
487 bootmenu_destroy(bootmenu);
490 puts(ANSI_CURSOR_SHOW);
491 puts(ANSI_CLEAR_CONSOLE);
492 printf(ANSI_CURSOR_POSITION, 1, 1);
495 if (title && command) {
496 debug("Starting entry '%s'\n", title);
498 run_command(command, 0);
502 #ifdef CONFIG_POSTBOOTMENU
503 run_command(CONFIG_POSTBOOTMENU, 0);
507 #ifdef CONFIG_AUTOBOOT_MENU_SHOW
508 int menu_show(int bootdelay)
510 bootmenu_show(bootdelay);
511 return -1; /* -1 - abort boot and run monitor code */
515 int do_bootmenu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
517 char *delay_str = NULL;
520 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
521 delay = CONFIG_BOOTDELAY;
528 delay_str = env_get("bootmenu_delay");
531 delay = (int)simple_strtol(delay_str, NULL, 10);
533 bootmenu_show(delay);
538 bootmenu, 2, 1, do_bootmenu,
539 "ANSI terminal bootmenu",
541 " - show ANSI terminal bootmenu with autoboot delay"