blob: 2244a0faf2adaec6d6e736e6c303ad3e1e2dea59 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Pali Roháre7abe912013-03-23 14:53:08 +00002/*
Pali Rohár53086652020-04-01 00:35:08 +02003 * (C) Copyright 2011-2013 Pali Rohár <pali@kernel.org>
Pali Roháre7abe912013-03-23 14:53:08 +00004 */
5
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +09006#include <charset.h>
Pali Roháre7abe912013-03-23 14:53:08 +00007#include <common.h>
8#include <command.h>
9#include <ansi.h>
Masahisa Kojimac606c002022-04-28 17:09:42 +090010#include <efi_loader.h>
11#include <efi_variable.h>
Simon Glass7b51b572019-08-01 09:46:52 -060012#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Pali Roháre7abe912013-03-23 14:53:08 +000014#include <menu.h>
Pali Roháre7abe912013-03-23 14:53:08 +000015#include <watchdog.h>
16#include <malloc.h>
Simon Glassc05ed002020-05-10 11:40:11 -060017#include <linux/delay.h>
Pali Roháre7abe912013-03-23 14:53:08 +000018#include <linux/string.h>
19
20/* maximum bootmenu entries */
21#define MAX_COUNT 99
22
23/* maximal size of bootmenu env
24 * 9 = strlen("bootmenu_")
25 * 2 = strlen(MAX_COUNT)
26 * 1 = NULL term
27 */
28#define MAX_ENV_SIZE (9 + 2 + 1)
29
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +090030enum boot_type {
31 BOOTMENU_TYPE_NONE = 0,
32 BOOTMENU_TYPE_BOOTMENU,
Masahisa Kojimac606c002022-04-28 17:09:42 +090033 BOOTMENU_TYPE_UEFI_BOOT_OPTION,
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +090034};
35
Pali Roháre7abe912013-03-23 14:53:08 +000036struct bootmenu_entry {
37 unsigned short int num; /* unique number 0 .. MAX_COUNT */
38 char key[3]; /* key identifier of number */
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +090039 u16 *title; /* title of entry */
Pali Roháre7abe912013-03-23 14:53:08 +000040 char *command; /* hush command of entry */
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +090041 enum boot_type type; /* boot type of entry */
42 u16 bootorder; /* order for each boot type */
Pali Roháre7abe912013-03-23 14:53:08 +000043 struct bootmenu_data *menu; /* this bootmenu */
44 struct bootmenu_entry *next; /* next menu entry (num+1) */
45};
46
47struct bootmenu_data {
48 int delay; /* delay for autoboot */
49 int active; /* active menu entry */
50 int count; /* total count of menu entries */
51 struct bootmenu_entry *first; /* first menu entry */
52};
53
54enum bootmenu_key {
55 KEY_NONE = 0,
56 KEY_UP,
57 KEY_DOWN,
58 KEY_SELECT,
Pali Rohár83a287a2020-12-27 01:04:38 +010059 KEY_QUIT,
Pali Roháre7abe912013-03-23 14:53:08 +000060};
61
62static char *bootmenu_getoption(unsigned short int n)
63{
Lan Yixun (dlan)0eb33ad2013-06-27 18:58:53 +080064 char name[MAX_ENV_SIZE];
Pali Roháre7abe912013-03-23 14:53:08 +000065
66 if (n > MAX_COUNT)
67 return NULL;
68
Lan Yixun (dlan)0eb33ad2013-06-27 18:58:53 +080069 sprintf(name, "bootmenu_%d", n);
Simon Glass00caae62017-08-03 12:22:12 -060070 return env_get(name);
Pali Roháre7abe912013-03-23 14:53:08 +000071}
72
73static void bootmenu_print_entry(void *data)
74{
75 struct bootmenu_entry *entry = data;
76 int reverse = (entry->menu->active == entry->num);
77
78 /*
79 * Move cursor to line where the entry will be drown (entry->num)
80 * First 3 lines contain bootmenu header + 1 empty line
81 */
Heinrich Schuchardtd1d7ed72022-05-01 23:17:18 +020082 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
Pali Roháre7abe912013-03-23 14:53:08 +000083
84 if (reverse)
85 puts(ANSI_COLOR_REVERSE);
86
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +090087 printf("%ls", entry->title);
Pali Roháre7abe912013-03-23 14:53:08 +000088
89 if (reverse)
90 puts(ANSI_COLOR_RESET);
91}
92
93static void bootmenu_autoboot_loop(struct bootmenu_data *menu,
94 enum bootmenu_key *key, int *esc)
95{
96 int i, c;
97
Pali Roháre7abe912013-03-23 14:53:08 +000098 while (menu->delay > 0) {
Heinrich Schuchardtd1d7ed72022-05-01 23:17:18 +020099 printf(ANSI_CURSOR_POSITION, menu->count + 5, 3);
100 printf("Hit any key to stop autoboot: %d ", menu->delay);
Pali Roháre7abe912013-03-23 14:53:08 +0000101 for (i = 0; i < 100; ++i) {
102 if (!tstc()) {
103 WATCHDOG_RESET();
104 mdelay(10);
105 continue;
106 }
107
108 menu->delay = -1;
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200109 c = getchar();
Pali Roháre7abe912013-03-23 14:53:08 +0000110
111 switch (c) {
112 case '\e':
113 *esc = 1;
114 *key = KEY_NONE;
115 break;
116 case '\r':
117 *key = KEY_SELECT;
118 break;
Pali Rohár83a287a2020-12-27 01:04:38 +0100119 case 0x3: /* ^C */
120 *key = KEY_QUIT;
121 break;
Pali Roháre7abe912013-03-23 14:53:08 +0000122 default:
123 *key = KEY_NONE;
124 break;
125 }
126
127 break;
128 }
129
130 if (menu->delay < 0)
131 break;
132
133 --menu->delay;
Pali Roháre7abe912013-03-23 14:53:08 +0000134 }
135
136 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
137 puts(ANSI_CLEAR_LINE);
138
139 if (menu->delay == 0)
140 *key = KEY_SELECT;
141}
142
143static void bootmenu_loop(struct bootmenu_data *menu,
144 enum bootmenu_key *key, int *esc)
145{
146 int c;
147
Pali Rohár83a287a2020-12-27 01:04:38 +0100148 if (*esc == 1) {
149 if (tstc()) {
150 c = getchar();
151 } else {
152 WATCHDOG_RESET();
153 mdelay(10);
154 if (tstc())
155 c = getchar();
156 else
157 c = '\e';
158 }
159 } else {
160 while (!tstc()) {
161 WATCHDOG_RESET();
162 mdelay(10);
163 }
164 c = getchar();
Pali Roháre7abe912013-03-23 14:53:08 +0000165 }
166
Pali Roháre7abe912013-03-23 14:53:08 +0000167 switch (*esc) {
168 case 0:
169 /* First char of ANSI escape sequence '\e' */
170 if (c == '\e') {
171 *esc = 1;
172 *key = KEY_NONE;
173 }
174 break;
175 case 1:
176 /* Second char of ANSI '[' */
177 if (c == '[') {
178 *esc = 2;
179 *key = KEY_NONE;
180 } else {
Pali Rohár83a287a2020-12-27 01:04:38 +0100181 /* Alone ESC key was pressed */
182 *key = KEY_QUIT;
183 *esc = (c == '\e') ? 1 : 0;
Pali Roháre7abe912013-03-23 14:53:08 +0000184 }
185 break;
186 case 2:
187 case 3:
188 /* Third char of ANSI (number '1') - optional */
189 if (*esc == 2 && c == '1') {
190 *esc = 3;
191 *key = KEY_NONE;
192 break;
193 }
194
195 *esc = 0;
196
197 /* ANSI 'A' - key up was pressed */
198 if (c == 'A')
199 *key = KEY_UP;
200 /* ANSI 'B' - key down was pressed */
201 else if (c == 'B')
202 *key = KEY_DOWN;
203 /* other key was pressed */
204 else
205 *key = KEY_NONE;
206
207 break;
208 }
209
210 /* enter key was pressed */
211 if (c == '\r')
212 *key = KEY_SELECT;
Pali Rohár83a287a2020-12-27 01:04:38 +0100213
214 /* ^C was pressed */
215 if (c == 0x3)
216 *key = KEY_QUIT;
Pali Roháre7abe912013-03-23 14:53:08 +0000217}
218
219static char *bootmenu_choice_entry(void *data)
220{
221 struct bootmenu_data *menu = data;
222 struct bootmenu_entry *iter;
223 enum bootmenu_key key = KEY_NONE;
224 int esc = 0;
225 int i;
226
227 while (1) {
228 if (menu->delay >= 0) {
229 /* Autoboot was not stopped */
230 bootmenu_autoboot_loop(menu, &key, &esc);
231 } else {
232 /* Some key was pressed, so autoboot was stopped */
233 bootmenu_loop(menu, &key, &esc);
234 }
235
236 switch (key) {
237 case KEY_UP:
238 if (menu->active > 0)
239 --menu->active;
240 /* no menu key selected, regenerate menu */
241 return NULL;
242 case KEY_DOWN:
243 if (menu->active < menu->count - 1)
244 ++menu->active;
245 /* no menu key selected, regenerate menu */
246 return NULL;
247 case KEY_SELECT:
248 iter = menu->first;
249 for (i = 0; i < menu->active; ++i)
250 iter = iter->next;
251 return iter->key;
Pali Rohár83a287a2020-12-27 01:04:38 +0100252 case KEY_QUIT:
253 /* Quit by choosing the last entry - U-Boot console */
254 iter = menu->first;
255 while (iter->next)
256 iter = iter->next;
257 return iter->key;
Pali Roháre7abe912013-03-23 14:53:08 +0000258 default:
259 break;
260 }
261 }
262
263 /* never happens */
264 debug("bootmenu: this should not happen");
265 return NULL;
266}
267
268static void bootmenu_destroy(struct bootmenu_data *menu)
269{
270 struct bootmenu_entry *iter = menu->first;
271 struct bootmenu_entry *next;
272
273 while (iter) {
274 next = iter->next;
275 free(iter->title);
276 free(iter->command);
277 free(iter);
278 iter = next;
279 }
280 free(menu);
281}
282
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900283/**
284 * prepare_bootmenu_entry() - generate the bootmenu_xx entries
285 *
286 * This function read the "bootmenu_x" U-Boot environment variable
287 * and generate the bootmenu entries.
288 *
289 * @menu: pointer to the bootmenu structure
290 * @current: pointer to the last bootmenu entry list
291 * @index: pointer to the index of the last bootmenu entry,
292 * the number of bootmenu entry is added by this function
293 * Return: 1 on success, negative value on error
294 */
295static int prepare_bootmenu_entry(struct bootmenu_data *menu,
296 struct bootmenu_entry **current,
297 unsigned short int *index)
Pali Roháre7abe912013-03-23 14:53:08 +0000298{
Pali Roháre7abe912013-03-23 14:53:08 +0000299 int len;
300 char *sep;
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900301 const char *option;
302 unsigned short int i = *index;
303 struct bootmenu_entry *entry = NULL;
304 struct bootmenu_entry *iter = *current;
Frank Wunderlichf7bb20a2018-10-05 11:41:59 +0200305
Pali Roháre7abe912013-03-23 14:53:08 +0000306 while ((option = bootmenu_getoption(i))) {
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900307 u16 *buf;
308
Pali Roháre7abe912013-03-23 14:53:08 +0000309 sep = strchr(option, '=');
310 if (!sep) {
311 printf("Invalid bootmenu entry: %s\n", option);
312 break;
313 }
314
315 entry = malloc(sizeof(struct bootmenu_entry));
316 if (!entry)
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900317 return -ENOMEM;
Pali Roháre7abe912013-03-23 14:53:08 +0000318
319 len = sep-option;
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900320 buf = calloc(1, (len + 1) * sizeof(u16));
321 entry->title = buf;
Pali Roháre7abe912013-03-23 14:53:08 +0000322 if (!entry->title) {
323 free(entry);
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900324 return -ENOMEM;
Pali Roháre7abe912013-03-23 14:53:08 +0000325 }
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900326 utf8_utf16_strncpy(&buf, option, len);
Pali Roháre7abe912013-03-23 14:53:08 +0000327
328 len = strlen(sep + 1);
329 entry->command = malloc(len + 1);
330 if (!entry->command) {
331 free(entry->title);
332 free(entry);
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900333 return -ENOMEM;
Pali Roháre7abe912013-03-23 14:53:08 +0000334 }
335 memcpy(entry->command, sep + 1, len);
336 entry->command[len] = 0;
337
338 sprintf(entry->key, "%d", i);
339
340 entry->num = i;
341 entry->menu = menu;
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900342 entry->type = BOOTMENU_TYPE_BOOTMENU;
343 entry->bootorder = i;
Pali Roháre7abe912013-03-23 14:53:08 +0000344 entry->next = NULL;
345
346 if (!iter)
347 menu->first = entry;
348 else
349 iter->next = entry;
350
351 iter = entry;
352 ++i;
353
354 if (i == MAX_COUNT - 1)
355 break;
356 }
357
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900358 *index = i;
359 *current = iter;
360
361 return 1;
362}
363
Masahisa Kojimac606c002022-04-28 17:09:42 +0900364/**
365 * prepare_uefi_bootorder_entry() - generate the uefi bootmenu entries
366 *
367 * This function read the "BootOrder" UEFI variable
368 * and generate the bootmenu entries in the order of "BootOrder".
369 *
370 * @menu: pointer to the bootmenu structure
371 * @current: pointer to the last bootmenu entry list
372 * @index: pointer to the index of the last bootmenu entry,
373 * the number of uefi entry is added by this function
374 * Return: 1 on success, negative value on error
375 */
376static int prepare_uefi_bootorder_entry(struct bootmenu_data *menu,
377 struct bootmenu_entry **current,
378 unsigned short int *index)
379{
380 u16 *bootorder;
381 efi_status_t ret;
382 unsigned short j;
383 efi_uintn_t num, size;
384 void *load_option;
385 struct efi_load_option lo;
386 u16 varname[] = u"Boot####";
387 unsigned short int i = *index;
388 struct bootmenu_entry *entry = NULL;
389 struct bootmenu_entry *iter = *current;
390
391 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
392 if (!bootorder)
393 return -ENOENT;
394
395 num = size / sizeof(u16);
396 for (j = 0; j < num; j++) {
397 entry = malloc(sizeof(struct bootmenu_entry));
398 if (!entry)
399 return -ENOMEM;
400
401 efi_create_indexed_name(varname, sizeof(varname),
402 "Boot", bootorder[j]);
403 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
404 if (!load_option)
405 continue;
406
407 ret = efi_deserialize_load_option(&lo, load_option, &size);
408 if (ret != EFI_SUCCESS) {
409 log_warning("Invalid load option for %ls\n", varname);
410 free(load_option);
411 free(entry);
412 continue;
413 }
414
415 if (lo.attributes & LOAD_OPTION_ACTIVE) {
416 entry->title = u16_strdup(lo.label);
417 if (!entry->title) {
418 free(load_option);
419 free(entry);
420 free(bootorder);
421 return -ENOMEM;
422 }
423 entry->command = strdup("bootefi bootmgr");
424 sprintf(entry->key, "%d", i);
425 entry->num = i;
426 entry->menu = menu;
427 entry->type = BOOTMENU_TYPE_UEFI_BOOT_OPTION;
428 entry->bootorder = bootorder[j];
429 entry->next = NULL;
430
431 if (!iter)
432 menu->first = entry;
433 else
434 iter->next = entry;
435
436 iter = entry;
437 i++;
438 }
439
440 free(load_option);
441
442 if (i == MAX_COUNT - 1)
443 break;
444 }
445
446 free(bootorder);
447 *index = i;
448 *current = iter;
449
450 return 1;
451}
452
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900453static struct bootmenu_data *bootmenu_create(int delay)
454{
455 int ret;
456 unsigned short int i = 0;
457 struct bootmenu_data *menu;
458 struct bootmenu_entry *iter = NULL;
459 struct bootmenu_entry *entry;
460 char *default_str;
461
462 menu = malloc(sizeof(struct bootmenu_data));
463 if (!menu)
464 return NULL;
465
466 menu->delay = delay;
467 menu->active = 0;
468 menu->first = NULL;
469
470 default_str = env_get("bootmenu_default");
471 if (default_str)
472 menu->active = (int)simple_strtol(default_str, NULL, 10);
473
474 ret = prepare_bootmenu_entry(menu, &iter, &i);
475 if (ret < 0)
476 goto cleanup;
477
Masahisa Kojimac606c002022-04-28 17:09:42 +0900478 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
479 if (i < MAX_COUNT - 1) {
480 ret = prepare_uefi_bootorder_entry(menu, &iter, &i);
481 if (ret < 0 && ret != -ENOENT)
482 goto cleanup;
483 }
484 }
485
Pali Roháre7abe912013-03-23 14:53:08 +0000486 /* Add U-Boot console entry at the end */
487 if (i <= MAX_COUNT - 1) {
488 entry = malloc(sizeof(struct bootmenu_entry));
489 if (!entry)
490 goto cleanup;
491
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900492 entry->title = u16_strdup(u"U-Boot console");
Pali Roháre7abe912013-03-23 14:53:08 +0000493 if (!entry->title) {
494 free(entry);
495 goto cleanup;
496 }
497
498 entry->command = strdup("");
499 if (!entry->command) {
500 free(entry->title);
501 free(entry);
502 goto cleanup;
503 }
504
505 sprintf(entry->key, "%d", i);
506
507 entry->num = i;
508 entry->menu = menu;
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900509 entry->type = BOOTMENU_TYPE_NONE;
Pali Roháre7abe912013-03-23 14:53:08 +0000510 entry->next = NULL;
511
512 if (!iter)
513 menu->first = entry;
514 else
515 iter->next = entry;
516
517 iter = entry;
518 ++i;
519 }
520
521 menu->count = i;
Frank Wunderlichbace2212018-12-03 11:23:41 +0100522
523 if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu
524 printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1);
525 menu->active=0;
526 }
527
Pali Roháre7abe912013-03-23 14:53:08 +0000528 return menu;
529
530cleanup:
531 bootmenu_destroy(menu);
532 return NULL;
533}
534
Thirupathaiah Annapureddy5168d7a2020-03-18 11:38:42 -0700535static void menu_display_statusline(struct menu *m)
536{
537 struct bootmenu_entry *entry;
538 struct bootmenu_data *menu;
539
540 if (menu_default_choice(m, (void *)&entry) < 0)
541 return;
542
543 menu = entry->menu;
544
545 printf(ANSI_CURSOR_POSITION, 1, 1);
546 puts(ANSI_CLEAR_LINE);
Heinrich Schuchardtd1d7ed72022-05-01 23:17:18 +0200547 printf(ANSI_CURSOR_POSITION, 2, 3);
548 puts("*** U-Boot Boot Menu ***");
Thirupathaiah Annapureddy5168d7a2020-03-18 11:38:42 -0700549 puts(ANSI_CLEAR_LINE_TO_END);
550 printf(ANSI_CURSOR_POSITION, 3, 1);
551 puts(ANSI_CLEAR_LINE);
552
553 /* First 3 lines are bootmenu header + 2 empty lines between entries */
554 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
555 puts(ANSI_CLEAR_LINE);
Heinrich Schuchardtd1d7ed72022-05-01 23:17:18 +0200556 printf(ANSI_CURSOR_POSITION, menu->count + 6, 3);
557 puts("Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit");
Thirupathaiah Annapureddy5168d7a2020-03-18 11:38:42 -0700558 puts(ANSI_CLEAR_LINE_TO_END);
559 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
560 puts(ANSI_CLEAR_LINE);
561}
562
Masahisa Kojimac606c002022-04-28 17:09:42 +0900563static void handle_uefi_bootnext(void)
564{
565 u16 bootnext;
566 efi_status_t ret;
567 efi_uintn_t size;
568
569 /* Initialize EFI drivers */
570 ret = efi_init_obj_list();
571 if (ret != EFI_SUCCESS) {
572 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
573 ret & ~EFI_ERROR_MASK);
574
575 return;
576 }
577
578 /* If UEFI BootNext variable is set, boot the BootNext load option */
579 size = sizeof(u16);
580 ret = efi_get_variable_int(u"BootNext",
581 &efi_global_variable_guid,
582 NULL, &size, &bootnext, NULL);
583 if (ret == EFI_SUCCESS)
584 /* BootNext does exist here, try to boot */
585 run_command("bootefi bootmgr", 0);
586}
587
Pali Roháre7abe912013-03-23 14:53:08 +0000588static void bootmenu_show(int delay)
589{
590 int init = 0;
591 void *choice = NULL;
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900592 u16 *title = NULL;
Pali Roháre7abe912013-03-23 14:53:08 +0000593 char *command = NULL;
594 struct menu *menu;
595 struct bootmenu_data *bootmenu;
596 struct bootmenu_entry *iter;
Masahisa Kojimac606c002022-04-28 17:09:42 +0900597 efi_status_t efi_ret = EFI_SUCCESS;
Pali Roháre7abe912013-03-23 14:53:08 +0000598 char *option, *sep;
599
Masahisa Kojimac606c002022-04-28 17:09:42 +0900600 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
601 handle_uefi_bootnext();
602
Pali Roháre7abe912013-03-23 14:53:08 +0000603 /* If delay is 0 do not create menu, just run first entry */
604 if (delay == 0) {
605 option = bootmenu_getoption(0);
606 if (!option) {
607 puts("bootmenu option 0 was not found\n");
608 return;
609 }
610 sep = strchr(option, '=');
611 if (!sep) {
612 puts("bootmenu option 0 is invalid\n");
613 return;
614 }
615 run_command(sep+1, 0);
616 return;
617 }
618
619 bootmenu = bootmenu_create(delay);
620 if (!bootmenu)
621 return;
622
Thirupathaiah Annapureddy5168d7a2020-03-18 11:38:42 -0700623 menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline,
624 bootmenu_print_entry, bootmenu_choice_entry,
625 bootmenu);
Pali Roháre7abe912013-03-23 14:53:08 +0000626 if (!menu) {
627 bootmenu_destroy(bootmenu);
628 return;
629 }
630
631 for (iter = bootmenu->first; iter; iter = iter->next) {
Masahisa Kojima990f6632022-03-24 22:54:33 +0900632 if (menu_item_add(menu, iter->key, iter) != 1)
Pali Roháre7abe912013-03-23 14:53:08 +0000633 goto cleanup;
634 }
635
636 /* Default menu entry is always first */
637 menu_default_set(menu, "0");
638
639 puts(ANSI_CURSOR_HIDE);
640 puts(ANSI_CLEAR_CONSOLE);
641 printf(ANSI_CURSOR_POSITION, 1, 1);
642
643 init = 1;
644
Masahisa Kojima990f6632022-03-24 22:54:33 +0900645 if (menu_get_choice(menu, &choice) == 1) {
Pali Roháre7abe912013-03-23 14:53:08 +0000646 iter = choice;
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900647 title = u16_strdup(iter->title);
Pali Roháre7abe912013-03-23 14:53:08 +0000648 command = strdup(iter->command);
649 }
650
Masahisa Kojimac606c002022-04-28 17:09:42 +0900651 /*
652 * If the selected entry is UEFI BOOT####, set the BootNext variable.
653 * Then uefi bootmgr is invoked by the preset command in iter->command.
654 */
655 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
656 if (iter->type == BOOTMENU_TYPE_UEFI_BOOT_OPTION) {
657 /*
658 * UEFI specification requires BootNext variable needs non-volatile
659 * attribute, but this BootNext is only used inside of U-Boot and
660 * removed by efi bootmgr once BootNext is processed.
661 * So this BootNext can be volatile.
662 */
663 efi_ret = efi_set_variable_int(u"BootNext", &efi_global_variable_guid,
664 EFI_VARIABLE_BOOTSERVICE_ACCESS |
665 EFI_VARIABLE_RUNTIME_ACCESS,
666 sizeof(u16), &iter->bootorder, false);
667 if (efi_ret != EFI_SUCCESS)
668 goto cleanup;
669 }
670 }
671
Pali Roháre7abe912013-03-23 14:53:08 +0000672cleanup:
673 menu_destroy(menu);
674 bootmenu_destroy(bootmenu);
675
676 if (init) {
677 puts(ANSI_CURSOR_SHOW);
678 puts(ANSI_CLEAR_CONSOLE);
679 printf(ANSI_CURSOR_POSITION, 1, 1);
680 }
681
682 if (title && command) {
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900683 debug("Starting entry '%ls'\n", title);
Pali Roháre7abe912013-03-23 14:53:08 +0000684 free(title);
Masahisa Kojimac606c002022-04-28 17:09:42 +0900685 if (efi_ret == EFI_SUCCESS)
686 run_command(command, 0);
Pali Roháre7abe912013-03-23 14:53:08 +0000687 free(command);
688 }
689
690#ifdef CONFIG_POSTBOOTMENU
691 run_command(CONFIG_POSTBOOTMENU, 0);
692#endif
693}
694
Simon Glasse2313062019-07-20 20:51:24 -0600695#ifdef CONFIG_AUTOBOOT_MENU_SHOW
Pali Roháre7abe912013-03-23 14:53:08 +0000696int menu_show(int bootdelay)
697{
698 bootmenu_show(bootdelay);
699 return -1; /* -1 - abort boot and run monitor code */
700}
701#endif
702
Simon Glass09140112020-05-10 11:40:03 -0600703int do_bootmenu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Pali Roháre7abe912013-03-23 14:53:08 +0000704{
705 char *delay_str = NULL;
706 int delay = 10;
707
708#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
709 delay = CONFIG_BOOTDELAY;
710#endif
711
712 if (argc >= 2)
713 delay_str = argv[1];
714
715 if (!delay_str)
Simon Glass00caae62017-08-03 12:22:12 -0600716 delay_str = env_get("bootmenu_delay");
Pali Roháre7abe912013-03-23 14:53:08 +0000717
718 if (delay_str)
719 delay = (int)simple_strtol(delay_str, NULL, 10);
720
721 bootmenu_show(delay);
722 return 0;
723}
724
725U_BOOT_CMD(
726 bootmenu, 2, 1, do_bootmenu,
727 "ANSI terminal bootmenu",
728 "[delay]\n"
729 " - show ANSI terminal bootmenu with autoboot delay"
730);