]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - cmd/bootmenu.c
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
);
357 bootmenu_destroy(menu
);
361 static void bootmenu_show(int delay
)
366 char *command
= NULL
;
368 struct bootmenu_data
*bootmenu
;
369 struct bootmenu_entry
*iter
;
372 /* If delay is 0 do not create menu, just run first entry */
374 option
= bootmenu_getoption(0);
376 puts("bootmenu option 0 was not found\n");
379 sep
= strchr(option
, '=');
381 puts("bootmenu option 0 is invalid\n");
384 run_command(sep
+1, 0);
388 bootmenu
= bootmenu_create(delay
);
392 menu
= menu_create(NULL
, bootmenu
->delay
, 1, bootmenu_print_entry
,
393 bootmenu_choice_entry
, bootmenu
);
395 bootmenu_destroy(bootmenu
);
399 for (iter
= bootmenu
->first
; iter
; iter
= iter
->next
) {
400 if (!menu_item_add(menu
, iter
->key
, iter
))
404 /* Default menu entry is always first */
405 menu_default_set(menu
, "0");
407 puts(ANSI_CURSOR_HIDE
);
408 puts(ANSI_CLEAR_CONSOLE
);
409 printf(ANSI_CURSOR_POSITION
, 1, 1);
413 if (menu_get_choice(menu
, &choice
)) {
415 title
= strdup(iter
->title
);
416 command
= strdup(iter
->command
);
421 bootmenu_destroy(bootmenu
);
424 puts(ANSI_CURSOR_SHOW
);
425 puts(ANSI_CLEAR_CONSOLE
);
426 printf(ANSI_CURSOR_POSITION
, 1, 1);
429 if (title
&& command
) {
430 debug("Starting entry '%s'\n", title
);
432 run_command(command
, 0);
436 #ifdef CONFIG_POSTBOOTMENU
437 run_command(CONFIG_POSTBOOTMENU
, 0);
441 void menu_display_statusline(struct menu
*m
)
443 struct bootmenu_entry
*entry
;
444 struct bootmenu_data
*menu
;
446 if (menu_default_choice(m
, (void *)&entry
) < 0)
451 printf(ANSI_CURSOR_POSITION
, 1, 1);
452 puts(ANSI_CLEAR_LINE
);
453 printf(ANSI_CURSOR_POSITION
, 2, 1);
454 puts(" *** U-Boot Boot Menu ***");
455 puts(ANSI_CLEAR_LINE_TO_END
);
456 printf(ANSI_CURSOR_POSITION
, 3, 1);
457 puts(ANSI_CLEAR_LINE
);
459 /* First 3 lines are bootmenu header + 2 empty lines between entries */
460 printf(ANSI_CURSOR_POSITION
, menu
->count
+ 5, 1);
461 puts(ANSI_CLEAR_LINE
);
462 printf(ANSI_CURSOR_POSITION
, menu
->count
+ 6, 1);
463 puts(" Press UP/DOWN to move, ENTER to select");
464 puts(ANSI_CLEAR_LINE_TO_END
);
465 printf(ANSI_CURSOR_POSITION
, menu
->count
+ 7, 1);
466 puts(ANSI_CLEAR_LINE
);
469 #ifdef CONFIG_MENU_SHOW
470 int menu_show(int bootdelay
)
472 bootmenu_show(bootdelay
);
473 return -1; /* -1 - abort boot and run monitor code */
477 int do_bootmenu(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *const argv
[])
479 char *delay_str
= NULL
;
482 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
483 delay
= CONFIG_BOOTDELAY
;
490 delay_str
= env_get("bootmenu_delay");
493 delay
= (int)simple_strtol(delay_str
, NULL
, 10);
495 bootmenu_show(delay
);
500 bootmenu
, 2, 1, do_bootmenu
,
501 "ANSI terminal bootmenu",
503 " - show ANSI terminal bootmenu with autoboot delay"