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;
257 struct bootmenu_entry *entry;
259 menu = malloc(sizeof(struct bootmenu_data));
267 default_str = env_get("bootmenu_default");
269 menu->active = (int)simple_strtol(default_str, NULL, 10);
271 while ((option = bootmenu_getoption(i))) {
272 sep = strchr(option, '=');
274 printf("Invalid bootmenu entry: %s\n", option);
278 entry = malloc(sizeof(struct bootmenu_entry));
283 entry->title = malloc(len + 1);
288 memcpy(entry->title, option, len);
289 entry->title[len] = 0;
291 len = strlen(sep + 1);
292 entry->command = malloc(len + 1);
293 if (!entry->command) {
298 memcpy(entry->command, sep + 1, len);
299 entry->command[len] = 0;
301 sprintf(entry->key, "%d", i);
315 if (i == MAX_COUNT - 1)
319 /* Add U-Boot console entry at the end */
320 if (i <= MAX_COUNT - 1) {
321 entry = malloc(sizeof(struct bootmenu_entry));
325 entry->title = strdup("U-Boot console");
331 entry->command = strdup("");
332 if (!entry->command) {
338 sprintf(entry->key, "%d", i);
355 if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu
356 printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1);
363 bootmenu_destroy(menu);
367 static void bootmenu_show(int delay)
372 char *command = NULL;
374 struct bootmenu_data *bootmenu;
375 struct bootmenu_entry *iter;
378 /* If delay is 0 do not create menu, just run first entry */
380 option = bootmenu_getoption(0);
382 puts("bootmenu option 0 was not found\n");
385 sep = strchr(option, '=');
387 puts("bootmenu option 0 is invalid\n");
390 run_command(sep+1, 0);
394 bootmenu = bootmenu_create(delay);
398 menu = menu_create(NULL, bootmenu->delay, 1, bootmenu_print_entry,
399 bootmenu_choice_entry, bootmenu);
401 bootmenu_destroy(bootmenu);
405 for (iter = bootmenu->first; iter; iter = iter->next) {
406 if (!menu_item_add(menu, iter->key, iter))
410 /* Default menu entry is always first */
411 menu_default_set(menu, "0");
413 puts(ANSI_CURSOR_HIDE);
414 puts(ANSI_CLEAR_CONSOLE);
415 printf(ANSI_CURSOR_POSITION, 1, 1);
419 if (menu_get_choice(menu, &choice)) {
421 title = strdup(iter->title);
422 command = strdup(iter->command);
427 bootmenu_destroy(bootmenu);
430 puts(ANSI_CURSOR_SHOW);
431 puts(ANSI_CLEAR_CONSOLE);
432 printf(ANSI_CURSOR_POSITION, 1, 1);
435 if (title && command) {
436 debug("Starting entry '%s'\n", title);
438 run_command(command, 0);
442 #ifdef CONFIG_POSTBOOTMENU
443 run_command(CONFIG_POSTBOOTMENU, 0);
447 void menu_display_statusline(struct menu *m)
449 struct bootmenu_entry *entry;
450 struct bootmenu_data *menu;
452 if (menu_default_choice(m, (void *)&entry) < 0)
457 printf(ANSI_CURSOR_POSITION, 1, 1);
458 puts(ANSI_CLEAR_LINE);
459 printf(ANSI_CURSOR_POSITION, 2, 1);
460 puts(" *** U-Boot Boot Menu ***");
461 puts(ANSI_CLEAR_LINE_TO_END);
462 printf(ANSI_CURSOR_POSITION, 3, 1);
463 puts(ANSI_CLEAR_LINE);
465 /* First 3 lines are bootmenu header + 2 empty lines between entries */
466 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
467 puts(ANSI_CLEAR_LINE);
468 printf(ANSI_CURSOR_POSITION, menu->count + 6, 1);
469 puts(" Press UP/DOWN to move, ENTER to select");
470 puts(ANSI_CLEAR_LINE_TO_END);
471 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
472 puts(ANSI_CLEAR_LINE);
475 #ifdef CONFIG_MENU_SHOW
476 int menu_show(int bootdelay)
478 bootmenu_show(bootdelay);
479 return -1; /* -1 - abort boot and run monitor code */
483 int do_bootmenu(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
485 char *delay_str = NULL;
488 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
489 delay = CONFIG_BOOTDELAY;
496 delay_str = env_get("bootmenu_delay");
499 delay = (int)simple_strtol(delay_str, NULL, 10);
501 bootmenu_show(delay);
506 bootmenu, 2, 1, do_bootmenu,
507 "ANSI terminal bootmenu",
509 " - show ANSI terminal bootmenu with autoboot delay"