2 * (C) Copyright 2011-2013 Pali Rohár <pali.rohar@gmail.com>
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #include <linux/string.h>
32 /* maximum bootmenu entries */
35 /* maximal size of bootmenu env
36 * 9 = strlen("bootmenu_")
37 * 2 = strlen(MAX_COUNT)
40 #define MAX_ENV_SIZE (9 + 2 + 1)
42 struct bootmenu_entry {
43 unsigned short int num; /* unique number 0 .. MAX_COUNT */
44 char key[3]; /* key identifier of number */
45 char *title; /* title of entry */
46 char *command; /* hush command of entry */
47 struct bootmenu_data *menu; /* this bootmenu */
48 struct bootmenu_entry *next; /* next menu entry (num+1) */
51 struct bootmenu_data {
52 int delay; /* delay for autoboot */
53 int active; /* active menu entry */
54 int count; /* total count of menu entries */
55 struct bootmenu_entry *first; /* first menu entry */
65 static char *bootmenu_getoption(unsigned short int n)
67 char name[MAX_ENV_SIZE] = "bootmenu_";
72 sprintf(name + 9, "%d", n);
76 static void bootmenu_print_entry(void *data)
78 struct bootmenu_entry *entry = data;
79 int reverse = (entry->menu->active == entry->num);
82 * Move cursor to line where the entry will be drown (entry->num)
83 * First 3 lines contain bootmenu header + 1 empty line
85 printf(ANSI_CURSOR_POSITION, entry->num + 4, 1);
90 puts(ANSI_COLOR_REVERSE);
95 puts(ANSI_COLOR_RESET);
98 static void bootmenu_autoboot_loop(struct bootmenu_data *menu,
99 enum bootmenu_key *key, int *esc)
103 if (menu->delay > 0) {
104 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
105 printf(" Hit any key to stop autoboot: %2d ", menu->delay);
108 while (menu->delay > 0) {
109 for (i = 0; i < 100; ++i) {
139 printf("\b\b\b%2d ", menu->delay);
142 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
143 puts(ANSI_CLEAR_LINE);
145 if (menu->delay == 0)
149 static void bootmenu_loop(struct bootmenu_data *menu,
150 enum bootmenu_key *key, int *esc)
163 /* First char of ANSI escape sequence '\e' */
170 /* Second char of ANSI '[' */
180 /* Third char of ANSI (number '1') - optional */
181 if (*esc == 2 && c == '1') {
189 /* ANSI 'A' - key up was pressed */
192 /* ANSI 'B' - key down was pressed */
195 /* other key was pressed */
202 /* enter key was pressed */
207 static char *bootmenu_choice_entry(void *data)
209 struct bootmenu_data *menu = data;
210 struct bootmenu_entry *iter;
211 enum bootmenu_key key = KEY_NONE;
216 if (menu->delay >= 0) {
217 /* Autoboot was not stopped */
218 bootmenu_autoboot_loop(menu, &key, &esc);
220 /* Some key was pressed, so autoboot was stopped */
221 bootmenu_loop(menu, &key, &esc);
226 if (menu->active > 0)
228 /* no menu key selected, regenerate menu */
231 if (menu->active < menu->count - 1)
233 /* no menu key selected, regenerate menu */
237 for (i = 0; i < menu->active; ++i)
246 debug("bootmenu: this should not happen");
250 static void bootmenu_destroy(struct bootmenu_data *menu)
252 struct bootmenu_entry *iter = menu->first;
253 struct bootmenu_entry *next;
265 static struct bootmenu_data *bootmenu_create(int delay)
267 unsigned short int i = 0;
269 struct bootmenu_data *menu;
270 struct bootmenu_entry *iter = NULL;
274 struct bootmenu_entry *entry;
276 menu = malloc(sizeof(struct bootmenu_data));
284 while ((option = bootmenu_getoption(i))) {
285 sep = strchr(option, '=');
287 printf("Invalid bootmenu entry: %s\n", option);
291 entry = malloc(sizeof(struct bootmenu_entry));
296 entry->title = malloc(len + 1);
301 memcpy(entry->title, option, len);
302 entry->title[len] = 0;
304 len = strlen(sep + 1);
305 entry->command = malloc(len + 1);
306 if (!entry->command) {
311 memcpy(entry->command, sep + 1, len);
312 entry->command[len] = 0;
314 sprintf(entry->key, "%d", i);
328 if (i == MAX_COUNT - 1)
332 /* Add U-Boot console entry at the end */
333 if (i <= MAX_COUNT - 1) {
334 entry = malloc(sizeof(struct bootmenu_entry));
338 entry->title = strdup("U-Boot console");
344 entry->command = strdup("");
345 if (!entry->command) {
351 sprintf(entry->key, "%d", i);
370 bootmenu_destroy(menu);
374 static void bootmenu_show(int delay)
379 char *command = NULL;
381 struct bootmenu_data *bootmenu;
382 struct bootmenu_entry *iter;
385 /* If delay is 0 do not create menu, just run first entry */
387 option = bootmenu_getoption(0);
389 puts("bootmenu option 0 was not found\n");
392 sep = strchr(option, '=');
394 puts("bootmenu option 0 is invalid\n");
397 run_command(sep+1, 0);
401 bootmenu = bootmenu_create(delay);
405 menu = menu_create(NULL, bootmenu->delay, 1, bootmenu_print_entry,
406 bootmenu_choice_entry, bootmenu);
408 bootmenu_destroy(bootmenu);
412 for (iter = bootmenu->first; iter; iter = iter->next) {
413 if (!menu_item_add(menu, iter->key, iter))
417 /* Default menu entry is always first */
418 menu_default_set(menu, "0");
420 puts(ANSI_CURSOR_HIDE);
421 puts(ANSI_CLEAR_CONSOLE);
422 printf(ANSI_CURSOR_POSITION, 1, 1);
426 if (menu_get_choice(menu, &choice)) {
428 title = strdup(iter->title);
429 command = strdup(iter->command);
434 bootmenu_destroy(bootmenu);
437 puts(ANSI_CURSOR_SHOW);
438 puts(ANSI_CLEAR_CONSOLE);
439 printf(ANSI_CURSOR_POSITION, 1, 1);
442 if (title && command) {
443 debug("Starting entry '%s'\n", title);
445 run_command(command, 0);
449 #ifdef CONFIG_POSTBOOTMENU
450 run_command(CONFIG_POSTBOOTMENU, 0);
454 void menu_display_statusline(struct menu *m)
456 struct bootmenu_entry *entry;
457 struct bootmenu_data *menu;
459 if (menu_default_choice(m, (void *)&entry) < 0)
464 printf(ANSI_CURSOR_POSITION, 1, 1);
465 puts(ANSI_CLEAR_LINE);
466 printf(ANSI_CURSOR_POSITION, 2, 1);
467 puts(" *** U-Boot Boot Menu ***");
468 puts(ANSI_CLEAR_LINE_TO_END);
469 printf(ANSI_CURSOR_POSITION, 3, 1);
470 puts(ANSI_CLEAR_LINE);
472 /* First 3 lines are bootmenu header + 2 empty lines between entries */
473 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
474 puts(ANSI_CLEAR_LINE);
475 printf(ANSI_CURSOR_POSITION, menu->count + 6, 1);
476 puts(" Press UP/DOWN to move, ENTER to select");
477 puts(ANSI_CLEAR_LINE_TO_END);
478 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
479 puts(ANSI_CLEAR_LINE);
482 #ifdef CONFIG_MENU_SHOW
483 int menu_show(int bootdelay)
485 bootmenu_show(bootdelay);
486 return -1; /* -1 - abort boot and run monitor code */
490 int do_bootmenu(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
492 char *delay_str = NULL;
495 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
496 delay = CONFIG_BOOTDELAY;
503 delay_str = getenv("bootmenu_delay");
506 delay = (int)simple_strtol(delay_str, NULL, 10);
508 bootmenu_show(delay);
513 bootmenu, 2, 1, do_bootmenu,
514 "ANSI terminal bootmenu",
516 " - show ANSI terminal bootmenu with autoboot delay"