Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2011-2013 Pali Rohár <pali.rohar@gmail.com> |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
| 8 | #include <ansi.h> |
Simon Glass | 7b51b57 | 2019-08-01 09:46:52 -0600 | [diff] [blame^] | 9 | #include <env.h> |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 10 | #include <menu.h> |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 11 | #include <watchdog.h> |
| 12 | #include <malloc.h> |
| 13 | #include <linux/string.h> |
| 14 | |
| 15 | /* maximum bootmenu entries */ |
| 16 | #define MAX_COUNT 99 |
| 17 | |
| 18 | /* maximal size of bootmenu env |
| 19 | * 9 = strlen("bootmenu_") |
| 20 | * 2 = strlen(MAX_COUNT) |
| 21 | * 1 = NULL term |
| 22 | */ |
| 23 | #define MAX_ENV_SIZE (9 + 2 + 1) |
| 24 | |
| 25 | struct bootmenu_entry { |
| 26 | unsigned short int num; /* unique number 0 .. MAX_COUNT */ |
| 27 | char key[3]; /* key identifier of number */ |
| 28 | char *title; /* title of entry */ |
| 29 | char *command; /* hush command of entry */ |
| 30 | struct bootmenu_data *menu; /* this bootmenu */ |
| 31 | struct bootmenu_entry *next; /* next menu entry (num+1) */ |
| 32 | }; |
| 33 | |
| 34 | struct bootmenu_data { |
| 35 | int delay; /* delay for autoboot */ |
| 36 | int active; /* active menu entry */ |
| 37 | int count; /* total count of menu entries */ |
| 38 | struct bootmenu_entry *first; /* first menu entry */ |
| 39 | }; |
| 40 | |
| 41 | enum bootmenu_key { |
| 42 | KEY_NONE = 0, |
| 43 | KEY_UP, |
| 44 | KEY_DOWN, |
| 45 | KEY_SELECT, |
| 46 | }; |
| 47 | |
| 48 | static char *bootmenu_getoption(unsigned short int n) |
| 49 | { |
Lan Yixun (dlan) | 0eb33ad | 2013-06-27 18:58:53 +0800 | [diff] [blame] | 50 | char name[MAX_ENV_SIZE]; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 51 | |
| 52 | if (n > MAX_COUNT) |
| 53 | return NULL; |
| 54 | |
Lan Yixun (dlan) | 0eb33ad | 2013-06-27 18:58:53 +0800 | [diff] [blame] | 55 | sprintf(name, "bootmenu_%d", n); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 56 | return env_get(name); |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static void bootmenu_print_entry(void *data) |
| 60 | { |
| 61 | struct bootmenu_entry *entry = data; |
| 62 | int reverse = (entry->menu->active == entry->num); |
| 63 | |
| 64 | /* |
| 65 | * Move cursor to line where the entry will be drown (entry->num) |
| 66 | * First 3 lines contain bootmenu header + 1 empty line |
| 67 | */ |
| 68 | printf(ANSI_CURSOR_POSITION, entry->num + 4, 1); |
| 69 | |
| 70 | puts(" "); |
| 71 | |
| 72 | if (reverse) |
| 73 | puts(ANSI_COLOR_REVERSE); |
| 74 | |
| 75 | puts(entry->title); |
| 76 | |
| 77 | if (reverse) |
| 78 | puts(ANSI_COLOR_RESET); |
| 79 | } |
| 80 | |
| 81 | static void bootmenu_autoboot_loop(struct bootmenu_data *menu, |
| 82 | enum bootmenu_key *key, int *esc) |
| 83 | { |
| 84 | int i, c; |
| 85 | |
| 86 | if (menu->delay > 0) { |
| 87 | printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); |
| 88 | printf(" Hit any key to stop autoboot: %2d ", menu->delay); |
| 89 | } |
| 90 | |
| 91 | while (menu->delay > 0) { |
| 92 | for (i = 0; i < 100; ++i) { |
| 93 | if (!tstc()) { |
| 94 | WATCHDOG_RESET(); |
| 95 | mdelay(10); |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | menu->delay = -1; |
| 100 | c = getc(); |
| 101 | |
| 102 | switch (c) { |
| 103 | case '\e': |
| 104 | *esc = 1; |
| 105 | *key = KEY_NONE; |
| 106 | break; |
| 107 | case '\r': |
| 108 | *key = KEY_SELECT; |
| 109 | break; |
| 110 | default: |
| 111 | *key = KEY_NONE; |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | if (menu->delay < 0) |
| 119 | break; |
| 120 | |
| 121 | --menu->delay; |
| 122 | printf("\b\b\b%2d ", menu->delay); |
| 123 | } |
| 124 | |
| 125 | printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); |
| 126 | puts(ANSI_CLEAR_LINE); |
| 127 | |
| 128 | if (menu->delay == 0) |
| 129 | *key = KEY_SELECT; |
| 130 | } |
| 131 | |
| 132 | static void bootmenu_loop(struct bootmenu_data *menu, |
| 133 | enum bootmenu_key *key, int *esc) |
| 134 | { |
| 135 | int c; |
| 136 | |
| 137 | while (!tstc()) { |
| 138 | WATCHDOG_RESET(); |
| 139 | mdelay(10); |
| 140 | } |
| 141 | |
| 142 | c = getc(); |
| 143 | |
| 144 | switch (*esc) { |
| 145 | case 0: |
| 146 | /* First char of ANSI escape sequence '\e' */ |
| 147 | if (c == '\e') { |
| 148 | *esc = 1; |
| 149 | *key = KEY_NONE; |
| 150 | } |
| 151 | break; |
| 152 | case 1: |
| 153 | /* Second char of ANSI '[' */ |
| 154 | if (c == '[') { |
| 155 | *esc = 2; |
| 156 | *key = KEY_NONE; |
| 157 | } else { |
| 158 | *esc = 0; |
| 159 | } |
| 160 | break; |
| 161 | case 2: |
| 162 | case 3: |
| 163 | /* Third char of ANSI (number '1') - optional */ |
| 164 | if (*esc == 2 && c == '1') { |
| 165 | *esc = 3; |
| 166 | *key = KEY_NONE; |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | *esc = 0; |
| 171 | |
| 172 | /* ANSI 'A' - key up was pressed */ |
| 173 | if (c == 'A') |
| 174 | *key = KEY_UP; |
| 175 | /* ANSI 'B' - key down was pressed */ |
| 176 | else if (c == 'B') |
| 177 | *key = KEY_DOWN; |
| 178 | /* other key was pressed */ |
| 179 | else |
| 180 | *key = KEY_NONE; |
| 181 | |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | /* enter key was pressed */ |
| 186 | if (c == '\r') |
| 187 | *key = KEY_SELECT; |
| 188 | } |
| 189 | |
| 190 | static char *bootmenu_choice_entry(void *data) |
| 191 | { |
| 192 | struct bootmenu_data *menu = data; |
| 193 | struct bootmenu_entry *iter; |
| 194 | enum bootmenu_key key = KEY_NONE; |
| 195 | int esc = 0; |
| 196 | int i; |
| 197 | |
| 198 | while (1) { |
| 199 | if (menu->delay >= 0) { |
| 200 | /* Autoboot was not stopped */ |
| 201 | bootmenu_autoboot_loop(menu, &key, &esc); |
| 202 | } else { |
| 203 | /* Some key was pressed, so autoboot was stopped */ |
| 204 | bootmenu_loop(menu, &key, &esc); |
| 205 | } |
| 206 | |
| 207 | switch (key) { |
| 208 | case KEY_UP: |
| 209 | if (menu->active > 0) |
| 210 | --menu->active; |
| 211 | /* no menu key selected, regenerate menu */ |
| 212 | return NULL; |
| 213 | case KEY_DOWN: |
| 214 | if (menu->active < menu->count - 1) |
| 215 | ++menu->active; |
| 216 | /* no menu key selected, regenerate menu */ |
| 217 | return NULL; |
| 218 | case KEY_SELECT: |
| 219 | iter = menu->first; |
| 220 | for (i = 0; i < menu->active; ++i) |
| 221 | iter = iter->next; |
| 222 | return iter->key; |
| 223 | default: |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /* never happens */ |
| 229 | debug("bootmenu: this should not happen"); |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | static void bootmenu_destroy(struct bootmenu_data *menu) |
| 234 | { |
| 235 | struct bootmenu_entry *iter = menu->first; |
| 236 | struct bootmenu_entry *next; |
| 237 | |
| 238 | while (iter) { |
| 239 | next = iter->next; |
| 240 | free(iter->title); |
| 241 | free(iter->command); |
| 242 | free(iter); |
| 243 | iter = next; |
| 244 | } |
| 245 | free(menu); |
| 246 | } |
| 247 | |
| 248 | static struct bootmenu_data *bootmenu_create(int delay) |
| 249 | { |
| 250 | unsigned short int i = 0; |
| 251 | const char *option; |
| 252 | struct bootmenu_data *menu; |
| 253 | struct bootmenu_entry *iter = NULL; |
| 254 | |
| 255 | int len; |
| 256 | char *sep; |
Frank Wunderlich | f7bb20a | 2018-10-05 11:41:59 +0200 | [diff] [blame] | 257 | char *default_str; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 258 | struct bootmenu_entry *entry; |
| 259 | |
| 260 | menu = malloc(sizeof(struct bootmenu_data)); |
| 261 | if (!menu) |
| 262 | return NULL; |
| 263 | |
| 264 | menu->delay = delay; |
| 265 | menu->active = 0; |
| 266 | menu->first = NULL; |
| 267 | |
Frank Wunderlich | f7bb20a | 2018-10-05 11:41:59 +0200 | [diff] [blame] | 268 | default_str = env_get("bootmenu_default"); |
| 269 | if (default_str) |
| 270 | menu->active = (int)simple_strtol(default_str, NULL, 10); |
| 271 | |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 272 | while ((option = bootmenu_getoption(i))) { |
| 273 | sep = strchr(option, '='); |
| 274 | if (!sep) { |
| 275 | printf("Invalid bootmenu entry: %s\n", option); |
| 276 | break; |
| 277 | } |
| 278 | |
| 279 | entry = malloc(sizeof(struct bootmenu_entry)); |
| 280 | if (!entry) |
| 281 | goto cleanup; |
| 282 | |
| 283 | len = sep-option; |
| 284 | entry->title = malloc(len + 1); |
| 285 | if (!entry->title) { |
| 286 | free(entry); |
| 287 | goto cleanup; |
| 288 | } |
| 289 | memcpy(entry->title, option, len); |
| 290 | entry->title[len] = 0; |
| 291 | |
| 292 | len = strlen(sep + 1); |
| 293 | entry->command = malloc(len + 1); |
| 294 | if (!entry->command) { |
| 295 | free(entry->title); |
| 296 | free(entry); |
| 297 | goto cleanup; |
| 298 | } |
| 299 | memcpy(entry->command, sep + 1, len); |
| 300 | entry->command[len] = 0; |
| 301 | |
| 302 | sprintf(entry->key, "%d", i); |
| 303 | |
| 304 | entry->num = i; |
| 305 | entry->menu = menu; |
| 306 | entry->next = NULL; |
| 307 | |
| 308 | if (!iter) |
| 309 | menu->first = entry; |
| 310 | else |
| 311 | iter->next = entry; |
| 312 | |
| 313 | iter = entry; |
| 314 | ++i; |
| 315 | |
| 316 | if (i == MAX_COUNT - 1) |
| 317 | break; |
| 318 | } |
| 319 | |
| 320 | /* Add U-Boot console entry at the end */ |
| 321 | if (i <= MAX_COUNT - 1) { |
| 322 | entry = malloc(sizeof(struct bootmenu_entry)); |
| 323 | if (!entry) |
| 324 | goto cleanup; |
| 325 | |
| 326 | entry->title = strdup("U-Boot console"); |
| 327 | if (!entry->title) { |
| 328 | free(entry); |
| 329 | goto cleanup; |
| 330 | } |
| 331 | |
| 332 | entry->command = strdup(""); |
| 333 | if (!entry->command) { |
| 334 | free(entry->title); |
| 335 | free(entry); |
| 336 | goto cleanup; |
| 337 | } |
| 338 | |
| 339 | sprintf(entry->key, "%d", i); |
| 340 | |
| 341 | entry->num = i; |
| 342 | entry->menu = menu; |
| 343 | entry->next = NULL; |
| 344 | |
| 345 | if (!iter) |
| 346 | menu->first = entry; |
| 347 | else |
| 348 | iter->next = entry; |
| 349 | |
| 350 | iter = entry; |
| 351 | ++i; |
| 352 | } |
| 353 | |
| 354 | menu->count = i; |
Frank Wunderlich | bace221 | 2018-12-03 11:23:41 +0100 | [diff] [blame] | 355 | |
| 356 | if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu |
| 357 | printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1); |
| 358 | menu->active=0; |
| 359 | } |
| 360 | |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 361 | return menu; |
| 362 | |
| 363 | cleanup: |
| 364 | bootmenu_destroy(menu); |
| 365 | return NULL; |
| 366 | } |
| 367 | |
| 368 | static void bootmenu_show(int delay) |
| 369 | { |
| 370 | int init = 0; |
| 371 | void *choice = NULL; |
| 372 | char *title = NULL; |
| 373 | char *command = NULL; |
| 374 | struct menu *menu; |
| 375 | struct bootmenu_data *bootmenu; |
| 376 | struct bootmenu_entry *iter; |
| 377 | char *option, *sep; |
| 378 | |
| 379 | /* If delay is 0 do not create menu, just run first entry */ |
| 380 | if (delay == 0) { |
| 381 | option = bootmenu_getoption(0); |
| 382 | if (!option) { |
| 383 | puts("bootmenu option 0 was not found\n"); |
| 384 | return; |
| 385 | } |
| 386 | sep = strchr(option, '='); |
| 387 | if (!sep) { |
| 388 | puts("bootmenu option 0 is invalid\n"); |
| 389 | return; |
| 390 | } |
| 391 | run_command(sep+1, 0); |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | bootmenu = bootmenu_create(delay); |
| 396 | if (!bootmenu) |
| 397 | return; |
| 398 | |
| 399 | menu = menu_create(NULL, bootmenu->delay, 1, bootmenu_print_entry, |
| 400 | bootmenu_choice_entry, bootmenu); |
| 401 | if (!menu) { |
| 402 | bootmenu_destroy(bootmenu); |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | for (iter = bootmenu->first; iter; iter = iter->next) { |
| 407 | if (!menu_item_add(menu, iter->key, iter)) |
| 408 | goto cleanup; |
| 409 | } |
| 410 | |
| 411 | /* Default menu entry is always first */ |
| 412 | menu_default_set(menu, "0"); |
| 413 | |
| 414 | puts(ANSI_CURSOR_HIDE); |
| 415 | puts(ANSI_CLEAR_CONSOLE); |
| 416 | printf(ANSI_CURSOR_POSITION, 1, 1); |
| 417 | |
| 418 | init = 1; |
| 419 | |
| 420 | if (menu_get_choice(menu, &choice)) { |
| 421 | iter = choice; |
| 422 | title = strdup(iter->title); |
| 423 | command = strdup(iter->command); |
| 424 | } |
| 425 | |
| 426 | cleanup: |
| 427 | menu_destroy(menu); |
| 428 | bootmenu_destroy(bootmenu); |
| 429 | |
| 430 | if (init) { |
| 431 | puts(ANSI_CURSOR_SHOW); |
| 432 | puts(ANSI_CLEAR_CONSOLE); |
| 433 | printf(ANSI_CURSOR_POSITION, 1, 1); |
| 434 | } |
| 435 | |
| 436 | if (title && command) { |
| 437 | debug("Starting entry '%s'\n", title); |
| 438 | free(title); |
| 439 | run_command(command, 0); |
| 440 | free(command); |
| 441 | } |
| 442 | |
| 443 | #ifdef CONFIG_POSTBOOTMENU |
| 444 | run_command(CONFIG_POSTBOOTMENU, 0); |
| 445 | #endif |
| 446 | } |
| 447 | |
| 448 | void menu_display_statusline(struct menu *m) |
| 449 | { |
| 450 | struct bootmenu_entry *entry; |
| 451 | struct bootmenu_data *menu; |
| 452 | |
| 453 | if (menu_default_choice(m, (void *)&entry) < 0) |
| 454 | return; |
| 455 | |
| 456 | menu = entry->menu; |
| 457 | |
| 458 | printf(ANSI_CURSOR_POSITION, 1, 1); |
| 459 | puts(ANSI_CLEAR_LINE); |
| 460 | printf(ANSI_CURSOR_POSITION, 2, 1); |
| 461 | puts(" *** U-Boot Boot Menu ***"); |
| 462 | puts(ANSI_CLEAR_LINE_TO_END); |
| 463 | printf(ANSI_CURSOR_POSITION, 3, 1); |
| 464 | puts(ANSI_CLEAR_LINE); |
| 465 | |
| 466 | /* First 3 lines are bootmenu header + 2 empty lines between entries */ |
| 467 | printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); |
| 468 | puts(ANSI_CLEAR_LINE); |
| 469 | printf(ANSI_CURSOR_POSITION, menu->count + 6, 1); |
| 470 | puts(" Press UP/DOWN to move, ENTER to select"); |
| 471 | puts(ANSI_CLEAR_LINE_TO_END); |
| 472 | printf(ANSI_CURSOR_POSITION, menu->count + 7, 1); |
| 473 | puts(ANSI_CLEAR_LINE); |
| 474 | } |
| 475 | |
Simon Glass | e231306 | 2019-07-20 20:51:24 -0600 | [diff] [blame] | 476 | #ifdef CONFIG_AUTOBOOT_MENU_SHOW |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 477 | int menu_show(int bootdelay) |
| 478 | { |
| 479 | bootmenu_show(bootdelay); |
| 480 | return -1; /* -1 - abort boot and run monitor code */ |
| 481 | } |
| 482 | #endif |
| 483 | |
| 484 | int do_bootmenu(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
| 485 | { |
| 486 | char *delay_str = NULL; |
| 487 | int delay = 10; |
| 488 | |
| 489 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) |
| 490 | delay = CONFIG_BOOTDELAY; |
| 491 | #endif |
| 492 | |
| 493 | if (argc >= 2) |
| 494 | delay_str = argv[1]; |
| 495 | |
| 496 | if (!delay_str) |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 497 | delay_str = env_get("bootmenu_delay"); |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 498 | |
| 499 | if (delay_str) |
| 500 | delay = (int)simple_strtol(delay_str, NULL, 10); |
| 501 | |
| 502 | bootmenu_show(delay); |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | U_BOOT_CMD( |
| 507 | bootmenu, 2, 1, do_bootmenu, |
| 508 | "ANSI terminal bootmenu", |
| 509 | "[delay]\n" |
| 510 | " - show ANSI terminal bootmenu with autoboot delay" |
| 511 | ); |