1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2011-2013 Pali Rohár <pali.rohar@gmail.com>
12 #include <linux/string.h>
14 /* maximum bootmenu entries */
17 /* maximal size of bootmenu env
18 * 9 = strlen("bootmenu_")
19 * 2 = strlen(MAX_COUNT)
22 #define MAX_ENV_SIZE (9 + 2 + 1)
24 struct bootmenu_entry {
25 unsigned short int num; /* unique number 0 .. MAX_COUNT */
26 char key[3]; /* key identifier of number */
27 char *title; /* title of entry */
28 char *command; /* hush command of entry */
29 struct bootmenu_data *menu; /* this bootmenu */
30 struct bootmenu_entry *next; /* next menu entry (num+1) */
33 struct bootmenu_data {
34 int delay; /* delay for autoboot */
35 int active; /* active menu entry */
36 int count; /* total count of menu entries */
37 struct bootmenu_entry *first; /* first menu entry */
47 static char *bootmenu_getoption(unsigned short int n)
49 char name[MAX_ENV_SIZE];
54 sprintf(name, "bootmenu_%d", n);
58 static void bootmenu_print_entry(void *data)
60 struct bootmenu_entry *entry = data;
61 int reverse = (entry->menu->active == entry->num);
64 * Move cursor to line where the entry will be drown (entry->num)
65 * First 3 lines contain bootmenu header + 1 empty line
67 printf(ANSI_CURSOR_POSITION, entry->num + 4, 1);
72 puts(ANSI_COLOR_REVERSE);
77 puts(ANSI_COLOR_RESET);
80 static void bootmenu_autoboot_loop(struct bootmenu_data *menu,
81 enum bootmenu_key *key, int *esc)
85 if (menu->delay > 0) {
86 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
87 printf(" Hit any key to stop autoboot: %2d ", menu->delay);
90 while (menu->delay > 0) {
91 for (i = 0; i < 100; ++i) {
121 printf("\b\b\b%2d ", menu->delay);
124 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
125 puts(ANSI_CLEAR_LINE);
127 if (menu->delay == 0)
131 static void bootmenu_loop(struct bootmenu_data *menu,
132 enum bootmenu_key *key, int *esc)
145 /* First char of ANSI escape sequence '\e' */
152 /* Second char of ANSI '[' */
162 /* Third char of ANSI (number '1') - optional */
163 if (*esc == 2 && c == '1') {
171 /* ANSI 'A' - key up was pressed */
174 /* ANSI 'B' - key down was pressed */
177 /* other key was pressed */
184 /* enter key was pressed */
189 static char *bootmenu_choice_entry(void *data)
191 struct bootmenu_data *menu = data;
192 struct bootmenu_entry *iter;
193 enum bootmenu_key key = KEY_NONE;
198 if (menu->delay >= 0) {
199 /* Autoboot was not stopped */
200 bootmenu_autoboot_loop(menu, &key, &esc);
202 /* Some key was pressed, so autoboot was stopped */
203 bootmenu_loop(menu, &key, &esc);
208 if (menu->active > 0)
210 /* no menu key selected, regenerate menu */
213 if (menu->active < menu->count - 1)
215 /* no menu key selected, regenerate menu */
219 for (i = 0; i < menu->active; ++i)
228 debug("bootmenu: this should not happen");
232 static void bootmenu_destroy(struct bootmenu_data *menu)
234 struct bootmenu_entry *iter = menu->first;
235 struct bootmenu_entry *next;
247 static struct bootmenu_data *bootmenu_create(int delay)
249 unsigned short int i = 0;
251 struct bootmenu_data *menu;
252 struct bootmenu_entry *iter = NULL;
256 struct bootmenu_entry *entry;
258 menu = malloc(sizeof(struct bootmenu_data));
266 while ((option = bootmenu_getoption(i))) {
267 sep = strchr(option, '=');
269 printf("Invalid bootmenu entry: %s\n", option);
273 entry = malloc(sizeof(struct bootmenu_entry));
278 entry->title = malloc(len + 1);
283 memcpy(entry->title, option, len);
284 entry->title[len] = 0;
286 len = strlen(sep + 1);
287 entry->command = malloc(len + 1);
288 if (!entry->command) {
293 memcpy(entry->command, sep + 1, len);
294 entry->command[len] = 0;
296 sprintf(entry->key, "%d", i);
310 if (i == MAX_COUNT - 1)
314 /* Add U-Boot console entry at the end */
315 if (i <= MAX_COUNT - 1) {
316 entry = malloc(sizeof(struct bootmenu_entry));
320 entry->title = strdup("U-Boot console");
326 entry->command = strdup("");
327 if (!entry->command) {
333 sprintf(entry->key, "%d", i);
352 bootmenu_destroy(menu);
356 static void bootmenu_show(int delay)
361 char *command = NULL;
363 struct bootmenu_data *bootmenu;
364 struct bootmenu_entry *iter;
367 /* If delay is 0 do not create menu, just run first entry */
369 option = bootmenu_getoption(0);
371 puts("bootmenu option 0 was not found\n");
374 sep = strchr(option, '=');
376 puts("bootmenu option 0 is invalid\n");
379 run_command(sep+1, 0);
383 bootmenu = bootmenu_create(delay);
387 menu = menu_create(NULL, bootmenu->delay, 1, bootmenu_print_entry,
388 bootmenu_choice_entry, bootmenu);
390 bootmenu_destroy(bootmenu);
394 for (iter = bootmenu->first; iter; iter = iter->next) {
395 if (!menu_item_add(menu, iter->key, iter))
399 /* Default menu entry is always first */
400 menu_default_set(menu, "0");
402 puts(ANSI_CURSOR_HIDE);
403 puts(ANSI_CLEAR_CONSOLE);
404 printf(ANSI_CURSOR_POSITION, 1, 1);
408 if (menu_get_choice(menu, &choice)) {
410 title = strdup(iter->title);
411 command = strdup(iter->command);
416 bootmenu_destroy(bootmenu);
419 puts(ANSI_CURSOR_SHOW);
420 puts(ANSI_CLEAR_CONSOLE);
421 printf(ANSI_CURSOR_POSITION, 1, 1);
424 if (title && command) {
425 debug("Starting entry '%s'\n", title);
427 run_command(command, 0);
431 #ifdef CONFIG_POSTBOOTMENU
432 run_command(CONFIG_POSTBOOTMENU, 0);
436 void menu_display_statusline(struct menu *m)
438 struct bootmenu_entry *entry;
439 struct bootmenu_data *menu;
441 if (menu_default_choice(m, (void *)&entry) < 0)
446 printf(ANSI_CURSOR_POSITION, 1, 1);
447 puts(ANSI_CLEAR_LINE);
448 printf(ANSI_CURSOR_POSITION, 2, 1);
449 puts(" *** U-Boot Boot Menu ***");
450 puts(ANSI_CLEAR_LINE_TO_END);
451 printf(ANSI_CURSOR_POSITION, 3, 1);
452 puts(ANSI_CLEAR_LINE);
454 /* First 3 lines are bootmenu header + 2 empty lines between entries */
455 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
456 puts(ANSI_CLEAR_LINE);
457 printf(ANSI_CURSOR_POSITION, menu->count + 6, 1);
458 puts(" Press UP/DOWN to move, ENTER to select");
459 puts(ANSI_CLEAR_LINE_TO_END);
460 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
461 puts(ANSI_CLEAR_LINE);
464 #ifdef CONFIG_MENU_SHOW
465 int menu_show(int bootdelay)
467 bootmenu_show(bootdelay);
468 return -1; /* -1 - abort boot and run monitor code */
472 int do_bootmenu(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
474 char *delay_str = NULL;
477 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
478 delay = CONFIG_BOOTDELAY;
485 delay_str = env_get("bootmenu_delay");
488 delay = (int)simple_strtol(delay_str, NULL, 10);
490 bootmenu_show(delay);
495 bootmenu, 2, 1, do_bootmenu,
496 "ANSI terminal bootmenu",
498 " - show ANSI terminal bootmenu with autoboot delay"