1 // SPDX-License-Identifier: GPL-2.0+
13 #include <linux/string.h>
15 /* maximum bootmenu entries */
18 /* maximal size of bootmenu env
19 * 9 = strlen("bootmenu_")
20 * 2 = strlen(MAX_COUNT)
23 #define MAX_ENV_SIZE (9 + 2 + 1)
25 struct bootmenu_entry {
26 unsigned short int num; /* unique number 0 .. MAX_COUNT */
27 char key[3]; /* key identifier of number */
28 char *title; /* title of entry */
29 char *command; /* hush command of entry */
30 struct bootmenu_data *menu; /* this bootmenu */
31 struct bootmenu_entry *next; /* next menu entry (num+1) */
34 struct bootmenu_data {
35 int delay; /* delay for autoboot */
36 int active; /* active menu entry */
37 int count; /* total count of menu entries */
38 struct bootmenu_entry *first; /* first menu entry */
48 static char *bootmenu_getoption(unsigned short int n)
50 char name[MAX_ENV_SIZE];
55 sprintf(name, "bootmenu_%d", n);
59 static void bootmenu_print_entry(void *data)
61 struct bootmenu_entry *entry = data;
62 int reverse = (entry->menu->active == entry->num);
65 * Move cursor to line where the entry will be drown (entry->num)
66 * First 3 lines contain bootmenu header + 1 empty line
68 printf(ANSI_CURSOR_POSITION, entry->num + 4, 1);
73 puts(ANSI_COLOR_REVERSE);
78 puts(ANSI_COLOR_RESET);
81 static void bootmenu_autoboot_loop(struct bootmenu_data *menu,
82 enum bootmenu_key *key, int *esc)
86 if (menu->delay > 0) {
87 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
88 printf(" Hit any key to stop autoboot: %2d ", menu->delay);
91 while (menu->delay > 0) {
92 for (i = 0; i < 100; ++i) {
122 printf("\b\b\b%2d ", menu->delay);
125 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
126 puts(ANSI_CLEAR_LINE);
128 if (menu->delay == 0)
132 static void bootmenu_loop(struct bootmenu_data *menu,
133 enum bootmenu_key *key, int *esc)
146 /* First char of ANSI escape sequence '\e' */
153 /* Second char of ANSI '[' */
163 /* Third char of ANSI (number '1') - optional */
164 if (*esc == 2 && c == '1') {
172 /* ANSI 'A' - key up was pressed */
175 /* ANSI 'B' - key down was pressed */
178 /* other key was pressed */
185 /* enter key was pressed */
190 static char *bootmenu_choice_entry(void *data)
192 struct bootmenu_data *menu = data;
193 struct bootmenu_entry *iter;
194 enum bootmenu_key key = KEY_NONE;
199 if (menu->delay >= 0) {
200 /* Autoboot was not stopped */
201 bootmenu_autoboot_loop(menu, &key, &esc);
203 /* Some key was pressed, so autoboot was stopped */
204 bootmenu_loop(menu, &key, &esc);
209 if (menu->active > 0)
211 /* no menu key selected, regenerate menu */
214 if (menu->active < menu->count - 1)
216 /* no menu key selected, regenerate menu */
220 for (i = 0; i < menu->active; ++i)
229 debug("bootmenu: this should not happen");
233 static void bootmenu_destroy(struct bootmenu_data *menu)
235 struct bootmenu_entry *iter = menu->first;
236 struct bootmenu_entry *next;
248 static struct bootmenu_data *bootmenu_create(int delay)
250 unsigned short int i = 0;
252 struct bootmenu_data *menu;
253 struct bootmenu_entry *iter = NULL;
258 struct bootmenu_entry *entry;
260 menu = malloc(sizeof(struct bootmenu_data));
268 default_str = env_get("bootmenu_default");
270 menu->active = (int)simple_strtol(default_str, NULL, 10);
272 while ((option = bootmenu_getoption(i))) {
273 sep = strchr(option, '=');
275 printf("Invalid bootmenu entry: %s\n", option);
279 entry = malloc(sizeof(struct bootmenu_entry));
284 entry->title = malloc(len + 1);
289 memcpy(entry->title, option, len);
290 entry->title[len] = 0;
292 len = strlen(sep + 1);
293 entry->command = malloc(len + 1);
294 if (!entry->command) {
299 memcpy(entry->command, sep + 1, len);
300 entry->command[len] = 0;
302 sprintf(entry->key, "%d", i);
316 if (i == MAX_COUNT - 1)
320 /* Add U-Boot console entry at the end */
321 if (i <= MAX_COUNT - 1) {
322 entry = malloc(sizeof(struct bootmenu_entry));
326 entry->title = strdup("U-Boot console");
332 entry->command = strdup("");
333 if (!entry->command) {
339 sprintf(entry->key, "%d", i);
356 if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu
357 printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1);
364 bootmenu_destroy(menu);
368 static void menu_display_statusline(struct menu *m)
370 struct bootmenu_entry *entry;
371 struct bootmenu_data *menu;
373 if (menu_default_choice(m, (void *)&entry) < 0)
378 printf(ANSI_CURSOR_POSITION, 1, 1);
379 puts(ANSI_CLEAR_LINE);
380 printf(ANSI_CURSOR_POSITION, 2, 1);
381 puts(" *** U-Boot Boot Menu ***");
382 puts(ANSI_CLEAR_LINE_TO_END);
383 printf(ANSI_CURSOR_POSITION, 3, 1);
384 puts(ANSI_CLEAR_LINE);
386 /* First 3 lines are bootmenu header + 2 empty lines between entries */
387 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
388 puts(ANSI_CLEAR_LINE);
389 printf(ANSI_CURSOR_POSITION, menu->count + 6, 1);
390 puts(" Press UP/DOWN to move, ENTER to select");
391 puts(ANSI_CLEAR_LINE_TO_END);
392 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
393 puts(ANSI_CLEAR_LINE);
396 static void bootmenu_show(int delay)
401 char *command = NULL;
403 struct bootmenu_data *bootmenu;
404 struct bootmenu_entry *iter;
407 /* If delay is 0 do not create menu, just run first entry */
409 option = bootmenu_getoption(0);
411 puts("bootmenu option 0 was not found\n");
414 sep = strchr(option, '=');
416 puts("bootmenu option 0 is invalid\n");
419 run_command(sep+1, 0);
423 bootmenu = bootmenu_create(delay);
427 menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline,
428 bootmenu_print_entry, bootmenu_choice_entry,
431 bootmenu_destroy(bootmenu);
435 for (iter = bootmenu->first; iter; iter = iter->next) {
436 if (!menu_item_add(menu, iter->key, iter))
440 /* Default menu entry is always first */
441 menu_default_set(menu, "0");
443 puts(ANSI_CURSOR_HIDE);
444 puts(ANSI_CLEAR_CONSOLE);
445 printf(ANSI_CURSOR_POSITION, 1, 1);
449 if (menu_get_choice(menu, &choice)) {
451 title = strdup(iter->title);
452 command = strdup(iter->command);
457 bootmenu_destroy(bootmenu);
460 puts(ANSI_CURSOR_SHOW);
461 puts(ANSI_CLEAR_CONSOLE);
462 printf(ANSI_CURSOR_POSITION, 1, 1);
465 if (title && command) {
466 debug("Starting entry '%s'\n", title);
468 run_command(command, 0);
472 #ifdef CONFIG_POSTBOOTMENU
473 run_command(CONFIG_POSTBOOTMENU, 0);
477 #ifdef CONFIG_AUTOBOOT_MENU_SHOW
478 int menu_show(int bootdelay)
480 bootmenu_show(bootdelay);
481 return -1; /* -1 - abort boot and run monitor code */
485 int do_bootmenu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
487 char *delay_str = NULL;
490 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
491 delay = CONFIG_BOOTDELAY;
498 delay_str = env_get("bootmenu_delay");
501 delay = (int)simple_strtol(delay_str, NULL, 10);
503 bootmenu_show(delay);
508 bootmenu, 2, 1, do_bootmenu,
509 "ANSI terminal bootmenu",
511 " - show ANSI terminal bootmenu with autoboot delay"