2 * (C) Copyright 2011-2013 Pali Rohár <pali.rohar@gmail.com>
4 * 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;
257 struct bootmenu_entry *entry;
259 menu = malloc(sizeof(struct bootmenu_data));
267 while ((option = bootmenu_getoption(i))) {
268 sep = strchr(option, '=');
270 printf("Invalid bootmenu entry: %s\n", option);
274 entry = malloc(sizeof(struct bootmenu_entry));
279 entry->title = malloc(len + 1);
284 memcpy(entry->title, option, len);
285 entry->title[len] = 0;
287 len = strlen(sep + 1);
288 entry->command = malloc(len + 1);
289 if (!entry->command) {
294 memcpy(entry->command, sep + 1, len);
295 entry->command[len] = 0;
297 sprintf(entry->key, "%d", i);
311 if (i == MAX_COUNT - 1)
315 /* Add U-Boot console entry at the end */
316 if (i <= MAX_COUNT - 1) {
317 entry = malloc(sizeof(struct bootmenu_entry));
321 entry->title = strdup("U-Boot console");
327 entry->command = strdup("");
328 if (!entry->command) {
334 sprintf(entry->key, "%d", i);
353 bootmenu_destroy(menu);
357 static void bootmenu_show(int delay)
362 char *command = NULL;
364 struct bootmenu_data *bootmenu;
365 struct bootmenu_entry *iter;
368 /* If delay is 0 do not create menu, just run first entry */
370 option = bootmenu_getoption(0);
372 puts("bootmenu option 0 was not found\n");
375 sep = strchr(option, '=');
377 puts("bootmenu option 0 is invalid\n");
380 run_command(sep+1, 0);
384 bootmenu = bootmenu_create(delay);
388 menu = menu_create(NULL, bootmenu->delay, 1, bootmenu_print_entry,
389 bootmenu_choice_entry, bootmenu);
391 bootmenu_destroy(bootmenu);
395 for (iter = bootmenu->first; iter; iter = iter->next) {
396 if (!menu_item_add(menu, iter->key, iter))
400 /* Default menu entry is always first */
401 menu_default_set(menu, "0");
403 puts(ANSI_CURSOR_HIDE);
404 puts(ANSI_CLEAR_CONSOLE);
405 printf(ANSI_CURSOR_POSITION, 1, 1);
409 if (menu_get_choice(menu, &choice)) {
411 title = strdup(iter->title);
412 command = strdup(iter->command);
417 bootmenu_destroy(bootmenu);
420 puts(ANSI_CURSOR_SHOW);
421 puts(ANSI_CLEAR_CONSOLE);
422 printf(ANSI_CURSOR_POSITION, 1, 1);
425 if (title && command) {
426 debug("Starting entry '%s'\n", title);
428 run_command(command, 0);
432 #ifdef CONFIG_POSTBOOTMENU
433 run_command(CONFIG_POSTBOOTMENU, 0);
437 void menu_display_statusline(struct menu *m)
439 struct bootmenu_entry *entry;
440 struct bootmenu_data *menu;
442 if (menu_default_choice(m, (void *)&entry) < 0)
447 printf(ANSI_CURSOR_POSITION, 1, 1);
448 puts(ANSI_CLEAR_LINE);
449 printf(ANSI_CURSOR_POSITION, 2, 1);
450 puts(" *** U-Boot Boot Menu ***");
451 puts(ANSI_CLEAR_LINE_TO_END);
452 printf(ANSI_CURSOR_POSITION, 3, 1);
453 puts(ANSI_CLEAR_LINE);
455 /* First 3 lines are bootmenu header + 2 empty lines between entries */
456 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
457 puts(ANSI_CLEAR_LINE);
458 printf(ANSI_CURSOR_POSITION, menu->count + 6, 1);
459 puts(" Press UP/DOWN to move, ENTER to select");
460 puts(ANSI_CLEAR_LINE_TO_END);
461 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
462 puts(ANSI_CLEAR_LINE);
465 #ifdef CONFIG_MENU_SHOW
466 int menu_show(int bootdelay)
468 bootmenu_show(bootdelay);
469 return -1; /* -1 - abort boot and run monitor code */
473 int do_bootmenu(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
475 char *delay_str = NULL;
478 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
479 delay = CONFIG_BOOTDELAY;
486 delay_str = env_get("bootmenu_delay");
489 delay = (int)simple_strtol(delay_str, NULL, 10);
491 bootmenu_show(delay);
496 bootmenu, 2, 1, do_bootmenu,
497 "ANSI terminal bootmenu",
499 " - show ANSI terminal bootmenu with autoboot delay"