]> git.ipfire.org Git - thirdparty/u-boot.git/blame - cmd/bootmenu.c
Merge tag 'u-boot-dfu-20240606' of https://source.denx.de/u-boot/custodians/u-boot-dfu
[thirdparty/u-boot.git] / cmd / bootmenu.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
e7abe919 2/*
53086653 3 * (C) Copyright 2011-2013 Pali Rohár <pali@kernel.org>
e7abe919
T
4 */
5
a3d0aa87 6#include <charset.h>
32bab0ea 7#include <cli.h>
d678a59d 8#include <common.h>
e7abe919
T
9#include <command.h>
10#include <ansi.h>
c416f1c0 11#include <efi_config.h>
c606c005 12#include <efi_variable.h>
7b51b576 13#include <env.h>
f7ae49fc 14#include <log.h>
e7abe919 15#include <menu.h>
e7abe919
T
16#include <watchdog.h>
17#include <malloc.h>
c05ed00a 18#include <linux/delay.h>
e7abe919
T
19#include <linux/string.h>
20
21/* maximum bootmenu entries */
22#define MAX_COUNT 99
23
24/* maximal size of bootmenu env
25 * 9 = strlen("bootmenu_")
26 * 2 = strlen(MAX_COUNT)
27 * 1 = NULL term
28 */
29#define MAX_ENV_SIZE (9 + 2 + 1)
30
2158b0da
MK
31enum bootmenu_ret {
32 BOOTMENU_RET_SUCCESS = 0,
33 BOOTMENU_RET_FAIL,
34 BOOTMENU_RET_QUIT,
35 BOOTMENU_RET_UPDATED,
36};
37
a3d0aa87
MK
38enum boot_type {
39 BOOTMENU_TYPE_NONE = 0,
40 BOOTMENU_TYPE_BOOTMENU,
c606c005 41 BOOTMENU_TYPE_UEFI_BOOT_OPTION,
a3d0aa87
MK
42};
43
e7abe919
T
44struct bootmenu_entry {
45 unsigned short int num; /* unique number 0 .. MAX_COUNT */
46 char key[3]; /* key identifier of number */
e85727dd 47 char *title; /* title of entry */
e7abe919 48 char *command; /* hush command of entry */
a3d0aa87
MK
49 enum boot_type type; /* boot type of entry */
50 u16 bootorder; /* order for each boot type */
e7abe919
T
51 struct bootmenu_data *menu; /* this bootmenu */
52 struct bootmenu_entry *next; /* next menu entry (num+1) */
53};
54
e7abe919
T
55static char *bootmenu_getoption(unsigned short int n)
56{
0eb33ad2 57 char name[MAX_ENV_SIZE];
e7abe919
T
58
59 if (n > MAX_COUNT)
60 return NULL;
61
0eb33ad2 62 sprintf(name, "bootmenu_%d", n);
00caae6d 63 return env_get(name);
e7abe919
T
64}
65
66static void bootmenu_print_entry(void *data)
67{
68 struct bootmenu_entry *entry = data;
69 int reverse = (entry->menu->active == entry->num);
70
71 /*
72 * Move cursor to line where the entry will be drown (entry->num)
73 * First 3 lines contain bootmenu header + 1 empty line
74 */
d1d7ed7b 75 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
e7abe919
T
76
77 if (reverse)
78 puts(ANSI_COLOR_REVERSE);
79
e85727dd 80 printf("%s", entry->title);
e7abe919
T
81
82 if (reverse)
83 puts(ANSI_COLOR_RESET);
84}
85
e7abe919
T
86static char *bootmenu_choice_entry(void *data)
87{
32bab0ea 88 struct cli_ch_state s_cch, *cch = &s_cch;
e7abe919
T
89 struct bootmenu_data *menu = data;
90 struct bootmenu_entry *iter;
2da4a15e 91 enum bootmenu_key key = BKEY_NONE;
e7abe919
T
92 int i;
93
32bab0ea
SG
94 cli_ch_init(cch);
95
e7abe919
T
96 while (1) {
97 if (menu->delay >= 0) {
98 /* Autoboot was not stopped */
32bab0ea 99 key = bootmenu_autoboot_loop(menu, cch);
e7abe919
T
100 } else {
101 /* Some key was pressed, so autoboot was stopped */
32bab0ea 102 key = bootmenu_loop(menu, cch);
e7abe919
T
103 }
104
105 switch (key) {
2da4a15e 106 case BKEY_UP:
e7abe919
T
107 if (menu->active > 0)
108 --menu->active;
109 /* no menu key selected, regenerate menu */
110 return NULL;
2da4a15e 111 case BKEY_DOWN:
e7abe919
T
112 if (menu->active < menu->count - 1)
113 ++menu->active;
114 /* no menu key selected, regenerate menu */
115 return NULL;
2da4a15e 116 case BKEY_SELECT:
e7abe919
T
117 iter = menu->first;
118 for (i = 0; i < menu->active; ++i)
119 iter = iter->next;
120 return iter->key;
2da4a15e 121 case BKEY_QUIT:
983d6e5b 122 /* Quit by choosing the last entry */
83a287a6
T
123 iter = menu->first;
124 while (iter->next)
125 iter = iter->next;
126 return iter->key;
e7abe919
T
127 default:
128 break;
129 }
130 }
131
132 /* never happens */
133 debug("bootmenu: this should not happen");
134 return NULL;
135}
136
137static void bootmenu_destroy(struct bootmenu_data *menu)
138{
139 struct bootmenu_entry *iter = menu->first;
140 struct bootmenu_entry *next;
141
142 while (iter) {
143 next = iter->next;
144 free(iter->title);
145 free(iter->command);
146 free(iter);
147 iter = next;
148 }
149 free(menu);
150}
151
a3d0aa87
MK
152/**
153 * prepare_bootmenu_entry() - generate the bootmenu_xx entries
154 *
155 * This function read the "bootmenu_x" U-Boot environment variable
156 * and generate the bootmenu entries.
157 *
158 * @menu: pointer to the bootmenu structure
159 * @current: pointer to the last bootmenu entry list
160 * @index: pointer to the index of the last bootmenu entry,
161 * the number of bootmenu entry is added by this function
162 * Return: 1 on success, negative value on error
163 */
164static int prepare_bootmenu_entry(struct bootmenu_data *menu,
165 struct bootmenu_entry **current,
166 unsigned short int *index)
e7abe919 167{
e7abe919 168 char *sep;
a3d0aa87
MK
169 const char *option;
170 unsigned short int i = *index;
171 struct bootmenu_entry *entry = NULL;
172 struct bootmenu_entry *iter = *current;
f7bb20a5 173
e7abe919 174 while ((option = bootmenu_getoption(i))) {
a3d0aa87 175
e85727dd 176 /* bootmenu_[num] format is "[title]=[commands]" */
e7abe919
T
177 sep = strchr(option, '=');
178 if (!sep) {
179 printf("Invalid bootmenu entry: %s\n", option);
180 break;
181 }
182
183 entry = malloc(sizeof(struct bootmenu_entry));
184 if (!entry)
a3d0aa87 185 return -ENOMEM;
e7abe919 186
e85727dd 187 entry->title = strndup(option, sep - option);
e7abe919
T
188 if (!entry->title) {
189 free(entry);
a3d0aa87 190 return -ENOMEM;
e7abe919 191 }
e7abe919 192
e85727dd 193 entry->command = strdup(sep + 1);
e7abe919
T
194 if (!entry->command) {
195 free(entry->title);
196 free(entry);
a3d0aa87 197 return -ENOMEM;
e7abe919 198 }
e7abe919
T
199
200 sprintf(entry->key, "%d", i);
201
202 entry->num = i;
203 entry->menu = menu;
a3d0aa87
MK
204 entry->type = BOOTMENU_TYPE_BOOTMENU;
205 entry->bootorder = i;
e7abe919
T
206 entry->next = NULL;
207
208 if (!iter)
209 menu->first = entry;
210 else
211 iter->next = entry;
212
213 iter = entry;
214 ++i;
215
216 if (i == MAX_COUNT - 1)
217 break;
218 }
219
a3d0aa87
MK
220 *index = i;
221 *current = iter;
222
223 return 1;
224}
225
6cd3a797 226#if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) && (IS_ENABLED(CONFIG_CMD_EFICONFIG))
c606c005
MK
227/**
228 * prepare_uefi_bootorder_entry() - generate the uefi bootmenu entries
229 *
230 * This function read the "BootOrder" UEFI variable
231 * and generate the bootmenu entries in the order of "BootOrder".
232 *
233 * @menu: pointer to the bootmenu structure
234 * @current: pointer to the last bootmenu entry list
235 * @index: pointer to the index of the last bootmenu entry,
236 * the number of uefi entry is added by this function
237 * Return: 1 on success, negative value on error
238 */
239static int prepare_uefi_bootorder_entry(struct bootmenu_data *menu,
240 struct bootmenu_entry **current,
241 unsigned short int *index)
242{
243 u16 *bootorder;
244 efi_status_t ret;
245 unsigned short j;
246 efi_uintn_t num, size;
247 void *load_option;
248 struct efi_load_option lo;
249 u16 varname[] = u"Boot####";
250 unsigned short int i = *index;
251 struct bootmenu_entry *entry = NULL;
252 struct bootmenu_entry *iter = *current;
253
254 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
255 if (!bootorder)
256 return -ENOENT;
257
258 num = size / sizeof(u16);
259 for (j = 0; j < num; j++) {
260 entry = malloc(sizeof(struct bootmenu_entry));
261 if (!entry)
262 return -ENOMEM;
263
264 efi_create_indexed_name(varname, sizeof(varname),
265 "Boot", bootorder[j]);
266 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
267 if (!load_option)
268 continue;
269
270 ret = efi_deserialize_load_option(&lo, load_option, &size);
271 if (ret != EFI_SUCCESS) {
272 log_warning("Invalid load option for %ls\n", varname);
273 free(load_option);
274 free(entry);
275 continue;
276 }
277
278 if (lo.attributes & LOAD_OPTION_ACTIVE) {
e85727dd
MK
279 char *buf;
280
281 buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
282 if (!buf) {
c606c005
MK
283 free(load_option);
284 free(entry);
285 free(bootorder);
286 return -ENOMEM;
287 }
e85727dd
MK
288 entry->title = buf;
289 utf16_utf8_strncpy(&buf, lo.label, u16_strlen(lo.label));
c606c005
MK
290 entry->command = strdup("bootefi bootmgr");
291 sprintf(entry->key, "%d", i);
292 entry->num = i;
293 entry->menu = menu;
294 entry->type = BOOTMENU_TYPE_UEFI_BOOT_OPTION;
295 entry->bootorder = bootorder[j];
296 entry->next = NULL;
297
298 if (!iter)
299 menu->first = entry;
300 else
301 iter->next = entry;
302
303 iter = entry;
304 i++;
305 }
306
307 free(load_option);
308
309 if (i == MAX_COUNT - 1)
310 break;
311 }
312
313 free(bootorder);
314 *index = i;
315 *current = iter;
316
317 return 1;
318}
e85727dd 319#endif
c606c005 320
a3d0aa87
MK
321static struct bootmenu_data *bootmenu_create(int delay)
322{
323 int ret;
324 unsigned short int i = 0;
325 struct bootmenu_data *menu;
326 struct bootmenu_entry *iter = NULL;
327 struct bootmenu_entry *entry;
328 char *default_str;
329
330 menu = malloc(sizeof(struct bootmenu_data));
331 if (!menu)
332 return NULL;
333
334 menu->delay = delay;
335 menu->active = 0;
336 menu->first = NULL;
337
338 default_str = env_get("bootmenu_default");
339 if (default_str)
340 menu->active = (int)simple_strtol(default_str, NULL, 10);
341
342 ret = prepare_bootmenu_entry(menu, &iter, &i);
343 if (ret < 0)
344 goto cleanup;
345
6cd3a797 346#if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) && (IS_ENABLED(CONFIG_CMD_EFICONFIG))
e85727dd 347 if (i < MAX_COUNT - 1) {
c416f1c0
MK
348 efi_status_t efi_ret;
349
350 /*
351 * UEFI specification requires booting from removal media using
352 * a architecture-specific default image name such as BOOTAA64.EFI.
353 */
339b527b 354 efi_ret = efi_bootmgr_update_media_device_boot_option();
9945bc4f 355 if (efi_ret != EFI_SUCCESS)
c416f1c0
MK
356 goto cleanup;
357
358 ret = prepare_uefi_bootorder_entry(menu, &iter, &i);
359 if (ret < 0 && ret != -ENOENT)
360 goto cleanup;
c606c005 361 }
e85727dd 362#endif
c606c005 363
983d6e5b 364 /* Add Exit entry at the end */
e7abe919
T
365 if (i <= MAX_COUNT - 1) {
366 entry = malloc(sizeof(struct bootmenu_entry));
367 if (!entry)
368 goto cleanup;
369
983d6e5b 370 /* Add Quit entry if exiting bootmenu is disabled */
83f73632 371 if (!IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE))
983d6e5b 372 entry->title = strdup("Exit");
2158b0da 373 else
e85727dd 374 entry->title = strdup("Quit");
2158b0da 375
e7abe919
T
376 if (!entry->title) {
377 free(entry);
378 goto cleanup;
379 }
380
381 entry->command = strdup("");
382 if (!entry->command) {
383 free(entry->title);
384 free(entry);
385 goto cleanup;
386 }
387
388 sprintf(entry->key, "%d", i);
389
390 entry->num = i;
391 entry->menu = menu;
a3d0aa87 392 entry->type = BOOTMENU_TYPE_NONE;
e7abe919
T
393 entry->next = NULL;
394
395 if (!iter)
396 menu->first = entry;
397 else
398 iter->next = entry;
399
400 iter = entry;
401 ++i;
402 }
403
404 menu->count = i;
bace2217
FW
405
406 if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu
407 printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1);
408 menu->active=0;
409 }
410
e7abe919
T
411 return menu;
412
413cleanup:
414 bootmenu_destroy(menu);
415 return NULL;
416}
417
5168d7a6
TA
418static void menu_display_statusline(struct menu *m)
419{
420 struct bootmenu_entry *entry;
421 struct bootmenu_data *menu;
422
423 if (menu_default_choice(m, (void *)&entry) < 0)
424 return;
425
426 menu = entry->menu;
427
428 printf(ANSI_CURSOR_POSITION, 1, 1);
429 puts(ANSI_CLEAR_LINE);
d1d7ed7b
HS
430 printf(ANSI_CURSOR_POSITION, 2, 3);
431 puts("*** U-Boot Boot Menu ***");
5168d7a6
TA
432 puts(ANSI_CLEAR_LINE_TO_END);
433 printf(ANSI_CURSOR_POSITION, 3, 1);
434 puts(ANSI_CLEAR_LINE);
435
436 /* First 3 lines are bootmenu header + 2 empty lines between entries */
437 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
438 puts(ANSI_CLEAR_LINE);
d1d7ed7b 439 printf(ANSI_CURSOR_POSITION, menu->count + 6, 3);
45f5319f 440 puts("Press UP/DOWN to move, ENTER to select, ESC to quit");
5168d7a6
TA
441 puts(ANSI_CLEAR_LINE_TO_END);
442 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
443 puts(ANSI_CLEAR_LINE);
444}
445
c606c005
MK
446static void handle_uefi_bootnext(void)
447{
448 u16 bootnext;
449 efi_status_t ret;
450 efi_uintn_t size;
451
452 /* Initialize EFI drivers */
453 ret = efi_init_obj_list();
454 if (ret != EFI_SUCCESS) {
455 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
456 ret & ~EFI_ERROR_MASK);
457
458 return;
459 }
460
461 /* If UEFI BootNext variable is set, boot the BootNext load option */
462 size = sizeof(u16);
463 ret = efi_get_variable_int(u"BootNext",
464 &efi_global_variable_guid,
465 NULL, &size, &bootnext, NULL);
466 if (ret == EFI_SUCCESS)
467 /* BootNext does exist here, try to boot */
468 run_command("bootefi bootmgr", 0);
469}
470
2158b0da 471static enum bootmenu_ret bootmenu_show(int delay)
e7abe919 472{
2158b0da 473 int cmd_ret;
e7abe919
T
474 int init = 0;
475 void *choice = NULL;
e85727dd 476 char *title = NULL;
e7abe919
T
477 char *command = NULL;
478 struct menu *menu;
e7abe919 479 struct bootmenu_entry *iter;
2158b0da
MK
480 int ret = BOOTMENU_RET_SUCCESS;
481 struct bootmenu_data *bootmenu;
c606c005 482 efi_status_t efi_ret = EFI_SUCCESS;
e7abe919
T
483 char *option, *sep;
484
c606c005
MK
485 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
486 handle_uefi_bootnext();
487
e7abe919
T
488 /* If delay is 0 do not create menu, just run first entry */
489 if (delay == 0) {
490 option = bootmenu_getoption(0);
491 if (!option) {
492 puts("bootmenu option 0 was not found\n");
2158b0da 493 return BOOTMENU_RET_FAIL;
e7abe919
T
494 }
495 sep = strchr(option, '=');
496 if (!sep) {
497 puts("bootmenu option 0 is invalid\n");
2158b0da 498 return BOOTMENU_RET_FAIL;
e7abe919 499 }
2158b0da
MK
500 cmd_ret = run_command(sep + 1, 0);
501 return (cmd_ret == CMD_RET_SUCCESS ? BOOTMENU_RET_SUCCESS : BOOTMENU_RET_FAIL);
e7abe919
T
502 }
503
504 bootmenu = bootmenu_create(delay);
505 if (!bootmenu)
2158b0da 506 return BOOTMENU_RET_FAIL;
e7abe919 507
5168d7a6
TA
508 menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline,
509 bootmenu_print_entry, bootmenu_choice_entry,
510 bootmenu);
e7abe919
T
511 if (!menu) {
512 bootmenu_destroy(bootmenu);
2158b0da 513 return BOOTMENU_RET_FAIL;
e7abe919
T
514 }
515
516 for (iter = bootmenu->first; iter; iter = iter->next) {
990f6636 517 if (menu_item_add(menu, iter->key, iter) != 1)
e7abe919
T
518 goto cleanup;
519 }
520
521 /* Default menu entry is always first */
522 menu_default_set(menu, "0");
523
524 puts(ANSI_CURSOR_HIDE);
525 puts(ANSI_CLEAR_CONSOLE);
526 printf(ANSI_CURSOR_POSITION, 1, 1);
527
528 init = 1;
529
990f6636 530 if (menu_get_choice(menu, &choice) == 1) {
e7abe919 531 iter = choice;
e85727dd 532 title = strdup(iter->title);
e7abe919 533 command = strdup(iter->command);
2158b0da 534
983d6e5b 535 /* last entry exits bootmenu */
2158b0da
MK
536 if (iter->num == iter->menu->count - 1) {
537 ret = BOOTMENU_RET_QUIT;
538 goto cleanup;
539 }
540 } else {
541 goto cleanup;
e7abe919
T
542 }
543
c606c005
MK
544 /*
545 * If the selected entry is UEFI BOOT####, set the BootNext variable.
546 * Then uefi bootmgr is invoked by the preset command in iter->command.
547 */
548 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
549 if (iter->type == BOOTMENU_TYPE_UEFI_BOOT_OPTION) {
550 /*
551 * UEFI specification requires BootNext variable needs non-volatile
552 * attribute, but this BootNext is only used inside of U-Boot and
553 * removed by efi bootmgr once BootNext is processed.
554 * So this BootNext can be volatile.
555 */
556 efi_ret = efi_set_variable_int(u"BootNext", &efi_global_variable_guid,
557 EFI_VARIABLE_BOOTSERVICE_ACCESS |
558 EFI_VARIABLE_RUNTIME_ACCESS,
559 sizeof(u16), &iter->bootorder, false);
560 if (efi_ret != EFI_SUCCESS)
561 goto cleanup;
562 }
563 }
564
e7abe919
T
565cleanup:
566 menu_destroy(menu);
567 bootmenu_destroy(bootmenu);
568
569 if (init) {
570 puts(ANSI_CURSOR_SHOW);
571 puts(ANSI_CLEAR_CONSOLE);
572 printf(ANSI_CURSOR_POSITION, 1, 1);
573 }
574
575 if (title && command) {
e85727dd 576 debug("Starting entry '%s'\n", title);
e7abe919 577 free(title);
c606c005 578 if (efi_ret == EFI_SUCCESS)
2158b0da 579 cmd_ret = run_command(command, 0);
e7abe919
T
580 free(command);
581 }
582
2adbb297
TR
583#ifdef CFG_POSTBOOTMENU
584 run_command(CFG_POSTBOOTMENU, 0);
e7abe919 585#endif
2158b0da
MK
586
587 if (efi_ret != EFI_SUCCESS || cmd_ret != CMD_RET_SUCCESS)
588 ret = BOOTMENU_RET_FAIL;
589
590 return ret;
e7abe919
T
591}
592
e231306e 593#ifdef CONFIG_AUTOBOOT_MENU_SHOW
e7abe919
T
594int menu_show(int bootdelay)
595{
2158b0da
MK
596 int ret;
597
598 while (1) {
599 ret = bootmenu_show(bootdelay);
600 bootdelay = -1;
601 if (ret == BOOTMENU_RET_UPDATED)
602 continue;
603
83f73632 604 if (IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE)) {
2158b0da
MK
605 if (ret == BOOTMENU_RET_QUIT) {
606 /* default boot process */
607 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
608 run_command("bootefi bootmgr", 0);
609
610 run_command("run bootcmd", 0);
611 }
612 } else {
613 break;
614 }
615 }
616
e7abe919
T
617 return -1; /* -1 - abort boot and run monitor code */
618}
619#endif
620
09140113 621int do_bootmenu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
e7abe919
T
622{
623 char *delay_str = NULL;
624 int delay = 10;
625
626#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
627 delay = CONFIG_BOOTDELAY;
628#endif
629
630 if (argc >= 2)
631 delay_str = argv[1];
632
633 if (!delay_str)
00caae6d 634 delay_str = env_get("bootmenu_delay");
e7abe919
T
635
636 if (delay_str)
637 delay = (int)simple_strtol(delay_str, NULL, 10);
638
639 bootmenu_show(delay);
640 return 0;
641}
642
643U_BOOT_CMD(
644 bootmenu, 2, 1, do_bootmenu,
645 "ANSI terminal bootmenu",
646 "[delay]\n"
647 " - show ANSI terminal bootmenu with autoboot delay"
648);