1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2011-2013 Pali Rohár <pali.rohar@gmail.com>
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 bootmenu_show(int delay)
373 char *command = NULL;
375 struct bootmenu_data *bootmenu;
376 struct bootmenu_entry *iter;
379 /* If delay is 0 do not create menu, just run first entry */
381 option = bootmenu_getoption(0);
383 puts("bootmenu option 0 was not found\n");
386 sep = strchr(option, '=');
388 puts("bootmenu option 0 is invalid\n");
391 run_command(sep+1, 0);
395 bootmenu = bootmenu_create(delay);
399 menu = menu_create(NULL, bootmenu->delay, 1, bootmenu_print_entry,
400 bootmenu_choice_entry, bootmenu);
402 bootmenu_destroy(bootmenu);
406 for (iter = bootmenu->first; iter; iter = iter->next) {
407 if (!menu_item_add(menu, iter->key, iter))
411 /* Default menu entry is always first */
412 menu_default_set(menu, "0");
414 puts(ANSI_CURSOR_HIDE);
415 puts(ANSI_CLEAR_CONSOLE);
416 printf(ANSI_CURSOR_POSITION, 1, 1);
420 if (menu_get_choice(menu, &choice)) {
422 title = strdup(iter->title);
423 command = strdup(iter->command);
428 bootmenu_destroy(bootmenu);
431 puts(ANSI_CURSOR_SHOW);
432 puts(ANSI_CLEAR_CONSOLE);
433 printf(ANSI_CURSOR_POSITION, 1, 1);
436 if (title && command) {
437 debug("Starting entry '%s'\n", title);
439 run_command(command, 0);
443 #ifdef CONFIG_POSTBOOTMENU
444 run_command(CONFIG_POSTBOOTMENU, 0);
448 void menu_display_statusline(struct menu *m)
450 struct bootmenu_entry *entry;
451 struct bootmenu_data *menu;
453 if (menu_default_choice(m, (void *)&entry) < 0)
458 printf(ANSI_CURSOR_POSITION, 1, 1);
459 puts(ANSI_CLEAR_LINE);
460 printf(ANSI_CURSOR_POSITION, 2, 1);
461 puts(" *** U-Boot Boot Menu ***");
462 puts(ANSI_CLEAR_LINE_TO_END);
463 printf(ANSI_CURSOR_POSITION, 3, 1);
464 puts(ANSI_CLEAR_LINE);
466 /* First 3 lines are bootmenu header + 2 empty lines between entries */
467 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
468 puts(ANSI_CLEAR_LINE);
469 printf(ANSI_CURSOR_POSITION, menu->count + 6, 1);
470 puts(" Press UP/DOWN to move, ENTER to select");
471 puts(ANSI_CLEAR_LINE_TO_END);
472 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
473 puts(ANSI_CLEAR_LINE);
476 #ifdef CONFIG_AUTOBOOT_MENU_SHOW
477 int menu_show(int bootdelay)
479 bootmenu_show(bootdelay);
480 return -1; /* -1 - abort boot and run monitor code */
484 int do_bootmenu(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
486 char *delay_str = NULL;
489 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
490 delay = CONFIG_BOOTDELAY;
497 delay_str = env_get("bootmenu_delay");
500 delay = (int)simple_strtol(delay_str, NULL, 10);
502 bootmenu_show(delay);
507 bootmenu, 2, 1, do_bootmenu,
508 "ANSI terminal bootmenu",
510 " - show ANSI terminal bootmenu with autoboot delay"