]>
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@kernel.org>
14 #include <linux/delay.h>
15 #include <linux/string.h>
17 /* maximum bootmenu entries */
20 /* maximal size of bootmenu env
21 * 9 = strlen("bootmenu_")
22 * 2 = strlen(MAX_COUNT)
25 #define MAX_ENV_SIZE (9 + 2 + 1)
27 struct bootmenu_entry
{
28 unsigned short int num
; /* unique number 0 .. MAX_COUNT */
29 char key
[3]; /* key identifier of number */
30 char *title
; /* title of entry */
31 char *command
; /* hush command of entry */
32 struct bootmenu_data
*menu
; /* this bootmenu */
33 struct bootmenu_entry
*next
; /* next menu entry (num+1) */
36 struct bootmenu_data
{
37 int delay
; /* delay for autoboot */
38 int active
; /* active menu entry */
39 int count
; /* total count of menu entries */
40 struct bootmenu_entry
*first
; /* first menu entry */
50 static char *bootmenu_getoption(unsigned short int n
)
52 char name
[MAX_ENV_SIZE
];
57 sprintf(name
, "bootmenu_%d", n
);
61 static void bootmenu_print_entry(void *data
)
63 struct bootmenu_entry
*entry
= data
;
64 int reverse
= (entry
->menu
->active
== entry
->num
);
67 * Move cursor to line where the entry will be drown (entry->num)
68 * First 3 lines contain bootmenu header + 1 empty line
70 printf(ANSI_CURSOR_POSITION
, entry
->num
+ 4, 1);
75 puts(ANSI_COLOR_REVERSE
);
80 puts(ANSI_COLOR_RESET
);
83 static void bootmenu_autoboot_loop(struct bootmenu_data
*menu
,
84 enum bootmenu_key
*key
, int *esc
)
88 if (menu
->delay
> 0) {
89 printf(ANSI_CURSOR_POSITION
, menu
->count
+ 5, 1);
90 printf(" Hit any key to stop autoboot: %2d ", menu
->delay
);
93 while (menu
->delay
> 0) {
94 for (i
= 0; i
< 100; ++i
) {
124 printf("\b\b\b%2d ", menu
->delay
);
127 printf(ANSI_CURSOR_POSITION
, menu
->count
+ 5, 1);
128 puts(ANSI_CLEAR_LINE
);
130 if (menu
->delay
== 0)
134 static void bootmenu_loop(struct bootmenu_data
*menu
,
135 enum bootmenu_key
*key
, int *esc
)
148 /* First char of ANSI escape sequence '\e' */
155 /* Second char of ANSI '[' */
165 /* Third char of ANSI (number '1') - optional */
166 if (*esc
== 2 && c
== '1') {
174 /* ANSI 'A' - key up was pressed */
177 /* ANSI 'B' - key down was pressed */
180 /* other key was pressed */
187 /* enter key was pressed */
192 static char *bootmenu_choice_entry(void *data
)
194 struct bootmenu_data
*menu
= data
;
195 struct bootmenu_entry
*iter
;
196 enum bootmenu_key key
= KEY_NONE
;
201 if (menu
->delay
>= 0) {
202 /* Autoboot was not stopped */
203 bootmenu_autoboot_loop(menu
, &key
, &esc
);
205 /* Some key was pressed, so autoboot was stopped */
206 bootmenu_loop(menu
, &key
, &esc
);
211 if (menu
->active
> 0)
213 /* no menu key selected, regenerate menu */
216 if (menu
->active
< menu
->count
- 1)
218 /* no menu key selected, regenerate menu */
222 for (i
= 0; i
< menu
->active
; ++i
)
231 debug("bootmenu: this should not happen");
235 static void bootmenu_destroy(struct bootmenu_data
*menu
)
237 struct bootmenu_entry
*iter
= menu
->first
;
238 struct bootmenu_entry
*next
;
250 static struct bootmenu_data
*bootmenu_create(int delay
)
252 unsigned short int i
= 0;
254 struct bootmenu_data
*menu
;
255 struct bootmenu_entry
*iter
= NULL
;
260 struct bootmenu_entry
*entry
;
262 menu
= malloc(sizeof(struct bootmenu_data
));
270 default_str
= env_get("bootmenu_default");
272 menu
->active
= (int)simple_strtol(default_str
, NULL
, 10);
274 while ((option
= bootmenu_getoption(i
))) {
275 sep
= strchr(option
, '=');
277 printf("Invalid bootmenu entry: %s\n", option
);
281 entry
= malloc(sizeof(struct bootmenu_entry
));
286 entry
->title
= malloc(len
+ 1);
291 memcpy(entry
->title
, option
, len
);
292 entry
->title
[len
] = 0;
294 len
= strlen(sep
+ 1);
295 entry
->command
= malloc(len
+ 1);
296 if (!entry
->command
) {
301 memcpy(entry
->command
, sep
+ 1, len
);
302 entry
->command
[len
] = 0;
304 sprintf(entry
->key
, "%d", i
);
318 if (i
== MAX_COUNT
- 1)
322 /* Add U-Boot console entry at the end */
323 if (i
<= MAX_COUNT
- 1) {
324 entry
= malloc(sizeof(struct bootmenu_entry
));
328 entry
->title
= strdup("U-Boot console");
334 entry
->command
= strdup("");
335 if (!entry
->command
) {
341 sprintf(entry
->key
, "%d", i
);
358 if ((menu
->active
>= menu
->count
)||(menu
->active
< 0)) { //ensure active menuitem is inside menu
359 printf("active menuitem (%d) is outside menu (0..%d)\n",menu
->active
,menu
->count
-1);
366 bootmenu_destroy(menu
);
370 static void menu_display_statusline(struct menu
*m
)
372 struct bootmenu_entry
*entry
;
373 struct bootmenu_data
*menu
;
375 if (menu_default_choice(m
, (void *)&entry
) < 0)
380 printf(ANSI_CURSOR_POSITION
, 1, 1);
381 puts(ANSI_CLEAR_LINE
);
382 printf(ANSI_CURSOR_POSITION
, 2, 1);
383 puts(" *** U-Boot Boot Menu ***");
384 puts(ANSI_CLEAR_LINE_TO_END
);
385 printf(ANSI_CURSOR_POSITION
, 3, 1);
386 puts(ANSI_CLEAR_LINE
);
388 /* First 3 lines are bootmenu header + 2 empty lines between entries */
389 printf(ANSI_CURSOR_POSITION
, menu
->count
+ 5, 1);
390 puts(ANSI_CLEAR_LINE
);
391 printf(ANSI_CURSOR_POSITION
, menu
->count
+ 6, 1);
392 puts(" Press UP/DOWN to move, ENTER to select");
393 puts(ANSI_CLEAR_LINE_TO_END
);
394 printf(ANSI_CURSOR_POSITION
, menu
->count
+ 7, 1);
395 puts(ANSI_CLEAR_LINE
);
398 static void bootmenu_show(int delay
)
403 char *command
= NULL
;
405 struct bootmenu_data
*bootmenu
;
406 struct bootmenu_entry
*iter
;
409 /* If delay is 0 do not create menu, just run first entry */
411 option
= bootmenu_getoption(0);
413 puts("bootmenu option 0 was not found\n");
416 sep
= strchr(option
, '=');
418 puts("bootmenu option 0 is invalid\n");
421 run_command(sep
+1, 0);
425 bootmenu
= bootmenu_create(delay
);
429 menu
= menu_create(NULL
, bootmenu
->delay
, 1, menu_display_statusline
,
430 bootmenu_print_entry
, bootmenu_choice_entry
,
433 bootmenu_destroy(bootmenu
);
437 for (iter
= bootmenu
->first
; iter
; iter
= iter
->next
) {
438 if (!menu_item_add(menu
, iter
->key
, iter
))
442 /* Default menu entry is always first */
443 menu_default_set(menu
, "0");
445 puts(ANSI_CURSOR_HIDE
);
446 puts(ANSI_CLEAR_CONSOLE
);
447 printf(ANSI_CURSOR_POSITION
, 1, 1);
451 if (menu_get_choice(menu
, &choice
)) {
453 title
= strdup(iter
->title
);
454 command
= strdup(iter
->command
);
459 bootmenu_destroy(bootmenu
);
462 puts(ANSI_CURSOR_SHOW
);
463 puts(ANSI_CLEAR_CONSOLE
);
464 printf(ANSI_CURSOR_POSITION
, 1, 1);
467 if (title
&& command
) {
468 debug("Starting entry '%s'\n", title
);
470 run_command(command
, 0);
474 #ifdef CONFIG_POSTBOOTMENU
475 run_command(CONFIG_POSTBOOTMENU
, 0);
479 #ifdef CONFIG_AUTOBOOT_MENU_SHOW
480 int menu_show(int bootdelay
)
482 bootmenu_show(bootdelay
);
483 return -1; /* -1 - abort boot and run monitor code */
487 int do_bootmenu(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[])
489 char *delay_str
= NULL
;
492 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
493 delay
= CONFIG_BOOTDELAY
;
500 delay_str
= env_get("bootmenu_delay");
503 delay
= (int)simple_strtol(delay_str
, NULL
, 10);
505 bootmenu_show(delay
);
510 bootmenu
, 2, 1, do_bootmenu
,
511 "ANSI terminal bootmenu",
513 " - show ANSI terminal bootmenu with autoboot delay"