blob: 8859eebea503c3e006c17dce64b79d512e72d9d2 [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 Kojima2158b0d2022-04-28 17:09:44 +090030enum bootmenu_ret {
31 BOOTMENU_RET_SUCCESS = 0,
32 BOOTMENU_RET_FAIL,
33 BOOTMENU_RET_QUIT,
34 BOOTMENU_RET_UPDATED,
35};
36
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +090037enum boot_type {
38 BOOTMENU_TYPE_NONE = 0,
39 BOOTMENU_TYPE_BOOTMENU,
Masahisa Kojimac606c002022-04-28 17:09:42 +090040 BOOTMENU_TYPE_UEFI_BOOT_OPTION,
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +090041};
42
Pali Roháre7abe912013-03-23 14:53:08 +000043struct bootmenu_entry {
44 unsigned short int num; /* unique number 0 .. MAX_COUNT */
45 char key[3]; /* key identifier of number */
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +090046 u16 *title; /* title of entry */
Pali Roháre7abe912013-03-23 14:53:08 +000047 char *command; /* hush command of entry */
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +090048 enum boot_type type; /* boot type of entry */
49 u16 bootorder; /* order for each boot type */
Pali Roháre7abe912013-03-23 14:53:08 +000050 struct bootmenu_data *menu; /* this bootmenu */
51 struct bootmenu_entry *next; /* next menu entry (num+1) */
52};
53
Pali Roháre7abe912013-03-23 14:53:08 +000054static char *bootmenu_getoption(unsigned short int n)
55{
Lan Yixun (dlan)0eb33ad2013-06-27 18:58:53 +080056 char name[MAX_ENV_SIZE];
Pali Roháre7abe912013-03-23 14:53:08 +000057
58 if (n > MAX_COUNT)
59 return NULL;
60
Lan Yixun (dlan)0eb33ad2013-06-27 18:58:53 +080061 sprintf(name, "bootmenu_%d", n);
Simon Glass00caae62017-08-03 12:22:12 -060062 return env_get(name);
Pali Roháre7abe912013-03-23 14:53:08 +000063}
64
65static void bootmenu_print_entry(void *data)
66{
67 struct bootmenu_entry *entry = data;
68 int reverse = (entry->menu->active == entry->num);
69
70 /*
71 * Move cursor to line where the entry will be drown (entry->num)
72 * First 3 lines contain bootmenu header + 1 empty line
73 */
Heinrich Schuchardtd1d7ed72022-05-01 23:17:18 +020074 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
Pali Roháre7abe912013-03-23 14:53:08 +000075
76 if (reverse)
77 puts(ANSI_COLOR_REVERSE);
78
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +090079 printf("%ls", entry->title);
Pali Roháre7abe912013-03-23 14:53:08 +000080
81 if (reverse)
82 puts(ANSI_COLOR_RESET);
83}
84
Pali Roháre7abe912013-03-23 14:53:08 +000085static char *bootmenu_choice_entry(void *data)
86{
87 struct bootmenu_data *menu = data;
88 struct bootmenu_entry *iter;
89 enum bootmenu_key key = KEY_NONE;
90 int esc = 0;
91 int i;
92
93 while (1) {
94 if (menu->delay >= 0) {
95 /* Autoboot was not stopped */
96 bootmenu_autoboot_loop(menu, &key, &esc);
97 } else {
98 /* Some key was pressed, so autoboot was stopped */
99 bootmenu_loop(menu, &key, &esc);
100 }
101
102 switch (key) {
103 case KEY_UP:
104 if (menu->active > 0)
105 --menu->active;
106 /* no menu key selected, regenerate menu */
107 return NULL;
108 case KEY_DOWN:
109 if (menu->active < menu->count - 1)
110 ++menu->active;
111 /* no menu key selected, regenerate menu */
112 return NULL;
113 case KEY_SELECT:
114 iter = menu->first;
115 for (i = 0; i < menu->active; ++i)
116 iter = iter->next;
117 return iter->key;
Pali Rohár83a287a2020-12-27 01:04:38 +0100118 case KEY_QUIT:
119 /* Quit by choosing the last entry - U-Boot console */
120 iter = menu->first;
121 while (iter->next)
122 iter = iter->next;
123 return iter->key;
Pali Roháre7abe912013-03-23 14:53:08 +0000124 default:
125 break;
126 }
127 }
128
129 /* never happens */
130 debug("bootmenu: this should not happen");
131 return NULL;
132}
133
134static void bootmenu_destroy(struct bootmenu_data *menu)
135{
136 struct bootmenu_entry *iter = menu->first;
137 struct bootmenu_entry *next;
138
139 while (iter) {
140 next = iter->next;
141 free(iter->title);
142 free(iter->command);
143 free(iter);
144 iter = next;
145 }
146 free(menu);
147}
148
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900149/**
150 * prepare_bootmenu_entry() - generate the bootmenu_xx entries
151 *
152 * This function read the "bootmenu_x" U-Boot environment variable
153 * and generate the bootmenu entries.
154 *
155 * @menu: pointer to the bootmenu structure
156 * @current: pointer to the last bootmenu entry list
157 * @index: pointer to the index of the last bootmenu entry,
158 * the number of bootmenu entry is added by this function
159 * Return: 1 on success, negative value on error
160 */
161static int prepare_bootmenu_entry(struct bootmenu_data *menu,
162 struct bootmenu_entry **current,
163 unsigned short int *index)
Pali Roháre7abe912013-03-23 14:53:08 +0000164{
Pali Roháre7abe912013-03-23 14:53:08 +0000165 int len;
166 char *sep;
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900167 const char *option;
168 unsigned short int i = *index;
169 struct bootmenu_entry *entry = NULL;
170 struct bootmenu_entry *iter = *current;
Frank Wunderlichf7bb20a2018-10-05 11:41:59 +0200171
Pali Roháre7abe912013-03-23 14:53:08 +0000172 while ((option = bootmenu_getoption(i))) {
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900173 u16 *buf;
174
Pali Roháre7abe912013-03-23 14:53:08 +0000175 sep = strchr(option, '=');
176 if (!sep) {
177 printf("Invalid bootmenu entry: %s\n", option);
178 break;
179 }
180
181 entry = malloc(sizeof(struct bootmenu_entry));
182 if (!entry)
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900183 return -ENOMEM;
Pali Roháre7abe912013-03-23 14:53:08 +0000184
185 len = sep-option;
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900186 buf = calloc(1, (len + 1) * sizeof(u16));
187 entry->title = buf;
Pali Roháre7abe912013-03-23 14:53:08 +0000188 if (!entry->title) {
189 free(entry);
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900190 return -ENOMEM;
Pali Roháre7abe912013-03-23 14:53:08 +0000191 }
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900192 utf8_utf16_strncpy(&buf, option, len);
Pali Roháre7abe912013-03-23 14:53:08 +0000193
194 len = strlen(sep + 1);
195 entry->command = malloc(len + 1);
196 if (!entry->command) {
197 free(entry->title);
198 free(entry);
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900199 return -ENOMEM;
Pali Roháre7abe912013-03-23 14:53:08 +0000200 }
201 memcpy(entry->command, sep + 1, len);
202 entry->command[len] = 0;
203
204 sprintf(entry->key, "%d", i);
205
206 entry->num = i;
207 entry->menu = menu;
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900208 entry->type = BOOTMENU_TYPE_BOOTMENU;
209 entry->bootorder = i;
Pali Roháre7abe912013-03-23 14:53:08 +0000210 entry->next = NULL;
211
212 if (!iter)
213 menu->first = entry;
214 else
215 iter->next = entry;
216
217 iter = entry;
218 ++i;
219
220 if (i == MAX_COUNT - 1)
221 break;
222 }
223
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900224 *index = i;
225 *current = iter;
226
227 return 1;
228}
229
Masahisa Kojimac606c002022-04-28 17:09:42 +0900230/**
231 * prepare_uefi_bootorder_entry() - generate the uefi bootmenu entries
232 *
233 * This function read the "BootOrder" UEFI variable
234 * and generate the bootmenu entries in the order of "BootOrder".
235 *
236 * @menu: pointer to the bootmenu structure
237 * @current: pointer to the last bootmenu entry list
238 * @index: pointer to the index of the last bootmenu entry,
239 * the number of uefi entry is added by this function
240 * Return: 1 on success, negative value on error
241 */
242static int prepare_uefi_bootorder_entry(struct bootmenu_data *menu,
243 struct bootmenu_entry **current,
244 unsigned short int *index)
245{
246 u16 *bootorder;
247 efi_status_t ret;
248 unsigned short j;
249 efi_uintn_t num, size;
250 void *load_option;
251 struct efi_load_option lo;
252 u16 varname[] = u"Boot####";
253 unsigned short int i = *index;
254 struct bootmenu_entry *entry = NULL;
255 struct bootmenu_entry *iter = *current;
256
257 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
258 if (!bootorder)
259 return -ENOENT;
260
261 num = size / sizeof(u16);
262 for (j = 0; j < num; j++) {
263 entry = malloc(sizeof(struct bootmenu_entry));
264 if (!entry)
265 return -ENOMEM;
266
267 efi_create_indexed_name(varname, sizeof(varname),
268 "Boot", bootorder[j]);
269 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
270 if (!load_option)
271 continue;
272
273 ret = efi_deserialize_load_option(&lo, load_option, &size);
274 if (ret != EFI_SUCCESS) {
275 log_warning("Invalid load option for %ls\n", varname);
276 free(load_option);
277 free(entry);
278 continue;
279 }
280
281 if (lo.attributes & LOAD_OPTION_ACTIVE) {
282 entry->title = u16_strdup(lo.label);
283 if (!entry->title) {
284 free(load_option);
285 free(entry);
286 free(bootorder);
287 return -ENOMEM;
288 }
289 entry->command = strdup("bootefi bootmgr");
290 sprintf(entry->key, "%d", i);
291 entry->num = i;
292 entry->menu = menu;
293 entry->type = BOOTMENU_TYPE_UEFI_BOOT_OPTION;
294 entry->bootorder = bootorder[j];
295 entry->next = NULL;
296
297 if (!iter)
298 menu->first = entry;
299 else
300 iter->next = entry;
301
302 iter = entry;
303 i++;
304 }
305
306 free(load_option);
307
308 if (i == MAX_COUNT - 1)
309 break;
310 }
311
312 free(bootorder);
313 *index = i;
314 *current = iter;
315
316 return 1;
317}
318
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900319static struct bootmenu_data *bootmenu_create(int delay)
320{
321 int ret;
322 unsigned short int i = 0;
323 struct bootmenu_data *menu;
324 struct bootmenu_entry *iter = NULL;
325 struct bootmenu_entry *entry;
326 char *default_str;
327
328 menu = malloc(sizeof(struct bootmenu_data));
329 if (!menu)
330 return NULL;
331
332 menu->delay = delay;
333 menu->active = 0;
334 menu->first = NULL;
335
336 default_str = env_get("bootmenu_default");
337 if (default_str)
338 menu->active = (int)simple_strtol(default_str, NULL, 10);
339
340 ret = prepare_bootmenu_entry(menu, &iter, &i);
341 if (ret < 0)
342 goto cleanup;
343
Masahisa Kojimac606c002022-04-28 17:09:42 +0900344 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
345 if (i < MAX_COUNT - 1) {
346 ret = prepare_uefi_bootorder_entry(menu, &iter, &i);
347 if (ret < 0 && ret != -ENOENT)
348 goto cleanup;
349 }
350 }
351
Pali Roháre7abe912013-03-23 14:53:08 +0000352 /* Add U-Boot console entry at the end */
353 if (i <= MAX_COUNT - 1) {
354 entry = malloc(sizeof(struct bootmenu_entry));
355 if (!entry)
356 goto cleanup;
357
Masahisa Kojima2158b0d2022-04-28 17:09:44 +0900358 /* Add Quit entry if entering U-Boot console is disabled */
359 if (IS_ENABLED(CONFIG_CMD_BOOTMENU_ENTER_UBOOT_CONSOLE))
360 entry->title = u16_strdup(u"U-Boot console");
361 else
362 entry->title = u16_strdup(u"Quit");
363
Pali Roháre7abe912013-03-23 14:53:08 +0000364 if (!entry->title) {
365 free(entry);
366 goto cleanup;
367 }
368
369 entry->command = strdup("");
370 if (!entry->command) {
371 free(entry->title);
372 free(entry);
373 goto cleanup;
374 }
375
376 sprintf(entry->key, "%d", i);
377
378 entry->num = i;
379 entry->menu = menu;
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900380 entry->type = BOOTMENU_TYPE_NONE;
Pali Roháre7abe912013-03-23 14:53:08 +0000381 entry->next = NULL;
382
383 if (!iter)
384 menu->first = entry;
385 else
386 iter->next = entry;
387
388 iter = entry;
389 ++i;
390 }
391
392 menu->count = i;
Frank Wunderlichbace2212018-12-03 11:23:41 +0100393
394 if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu
395 printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1);
396 menu->active=0;
397 }
398
Pali Roháre7abe912013-03-23 14:53:08 +0000399 return menu;
400
401cleanup:
402 bootmenu_destroy(menu);
403 return NULL;
404}
405
Thirupathaiah Annapureddy5168d7a2020-03-18 11:38:42 -0700406static void menu_display_statusline(struct menu *m)
407{
408 struct bootmenu_entry *entry;
409 struct bootmenu_data *menu;
410
411 if (menu_default_choice(m, (void *)&entry) < 0)
412 return;
413
414 menu = entry->menu;
415
416 printf(ANSI_CURSOR_POSITION, 1, 1);
417 puts(ANSI_CLEAR_LINE);
Heinrich Schuchardtd1d7ed72022-05-01 23:17:18 +0200418 printf(ANSI_CURSOR_POSITION, 2, 3);
419 puts("*** U-Boot Boot Menu ***");
Thirupathaiah Annapureddy5168d7a2020-03-18 11:38:42 -0700420 puts(ANSI_CLEAR_LINE_TO_END);
421 printf(ANSI_CURSOR_POSITION, 3, 1);
422 puts(ANSI_CLEAR_LINE);
423
424 /* First 3 lines are bootmenu header + 2 empty lines between entries */
425 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
426 puts(ANSI_CLEAR_LINE);
Heinrich Schuchardtd1d7ed72022-05-01 23:17:18 +0200427 printf(ANSI_CURSOR_POSITION, menu->count + 6, 3);
428 puts("Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit");
Thirupathaiah Annapureddy5168d7a2020-03-18 11:38:42 -0700429 puts(ANSI_CLEAR_LINE_TO_END);
430 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
431 puts(ANSI_CLEAR_LINE);
432}
433
Masahisa Kojimac606c002022-04-28 17:09:42 +0900434static void handle_uefi_bootnext(void)
435{
436 u16 bootnext;
437 efi_status_t ret;
438 efi_uintn_t size;
439
440 /* Initialize EFI drivers */
441 ret = efi_init_obj_list();
442 if (ret != EFI_SUCCESS) {
443 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
444 ret & ~EFI_ERROR_MASK);
445
446 return;
447 }
448
449 /* If UEFI BootNext variable is set, boot the BootNext load option */
450 size = sizeof(u16);
451 ret = efi_get_variable_int(u"BootNext",
452 &efi_global_variable_guid,
453 NULL, &size, &bootnext, NULL);
454 if (ret == EFI_SUCCESS)
455 /* BootNext does exist here, try to boot */
456 run_command("bootefi bootmgr", 0);
457}
458
Masahisa Kojima2158b0d2022-04-28 17:09:44 +0900459static enum bootmenu_ret bootmenu_show(int delay)
Pali Roháre7abe912013-03-23 14:53:08 +0000460{
Masahisa Kojima2158b0d2022-04-28 17:09:44 +0900461 int cmd_ret;
Pali Roháre7abe912013-03-23 14:53:08 +0000462 int init = 0;
463 void *choice = NULL;
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900464 u16 *title = NULL;
Pali Roháre7abe912013-03-23 14:53:08 +0000465 char *command = NULL;
466 struct menu *menu;
Pali Roháre7abe912013-03-23 14:53:08 +0000467 struct bootmenu_entry *iter;
Masahisa Kojima2158b0d2022-04-28 17:09:44 +0900468 int ret = BOOTMENU_RET_SUCCESS;
469 struct bootmenu_data *bootmenu;
Masahisa Kojimac606c002022-04-28 17:09:42 +0900470 efi_status_t efi_ret = EFI_SUCCESS;
Pali Roháre7abe912013-03-23 14:53:08 +0000471 char *option, *sep;
472
Masahisa Kojimac606c002022-04-28 17:09:42 +0900473 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
474 handle_uefi_bootnext();
475
Pali Roháre7abe912013-03-23 14:53:08 +0000476 /* If delay is 0 do not create menu, just run first entry */
477 if (delay == 0) {
478 option = bootmenu_getoption(0);
479 if (!option) {
480 puts("bootmenu option 0 was not found\n");
Masahisa Kojima2158b0d2022-04-28 17:09:44 +0900481 return BOOTMENU_RET_FAIL;
Pali Roháre7abe912013-03-23 14:53:08 +0000482 }
483 sep = strchr(option, '=');
484 if (!sep) {
485 puts("bootmenu option 0 is invalid\n");
Masahisa Kojima2158b0d2022-04-28 17:09:44 +0900486 return BOOTMENU_RET_FAIL;
Pali Roháre7abe912013-03-23 14:53:08 +0000487 }
Masahisa Kojima2158b0d2022-04-28 17:09:44 +0900488 cmd_ret = run_command(sep + 1, 0);
489 return (cmd_ret == CMD_RET_SUCCESS ? BOOTMENU_RET_SUCCESS : BOOTMENU_RET_FAIL);
Pali Roháre7abe912013-03-23 14:53:08 +0000490 }
491
492 bootmenu = bootmenu_create(delay);
493 if (!bootmenu)
Masahisa Kojima2158b0d2022-04-28 17:09:44 +0900494 return BOOTMENU_RET_FAIL;
Pali Roháre7abe912013-03-23 14:53:08 +0000495
Thirupathaiah Annapureddy5168d7a2020-03-18 11:38:42 -0700496 menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline,
497 bootmenu_print_entry, bootmenu_choice_entry,
498 bootmenu);
Pali Roháre7abe912013-03-23 14:53:08 +0000499 if (!menu) {
500 bootmenu_destroy(bootmenu);
Masahisa Kojima2158b0d2022-04-28 17:09:44 +0900501 return BOOTMENU_RET_FAIL;
Pali Roháre7abe912013-03-23 14:53:08 +0000502 }
503
504 for (iter = bootmenu->first; iter; iter = iter->next) {
Masahisa Kojima990f6632022-03-24 22:54:33 +0900505 if (menu_item_add(menu, iter->key, iter) != 1)
Pali Roháre7abe912013-03-23 14:53:08 +0000506 goto cleanup;
507 }
508
509 /* Default menu entry is always first */
510 menu_default_set(menu, "0");
511
512 puts(ANSI_CURSOR_HIDE);
513 puts(ANSI_CLEAR_CONSOLE);
514 printf(ANSI_CURSOR_POSITION, 1, 1);
515
516 init = 1;
517
Masahisa Kojima990f6632022-03-24 22:54:33 +0900518 if (menu_get_choice(menu, &choice) == 1) {
Pali Roháre7abe912013-03-23 14:53:08 +0000519 iter = choice;
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900520 title = u16_strdup(iter->title);
Pali Roháre7abe912013-03-23 14:53:08 +0000521 command = strdup(iter->command);
Masahisa Kojima2158b0d2022-04-28 17:09:44 +0900522
523 /* last entry is U-Boot console or Quit */
524 if (iter->num == iter->menu->count - 1) {
525 ret = BOOTMENU_RET_QUIT;
526 goto cleanup;
527 }
528 } else {
529 goto cleanup;
Pali Roháre7abe912013-03-23 14:53:08 +0000530 }
531
Masahisa Kojimac606c002022-04-28 17:09:42 +0900532 /*
533 * If the selected entry is UEFI BOOT####, set the BootNext variable.
534 * Then uefi bootmgr is invoked by the preset command in iter->command.
535 */
536 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
537 if (iter->type == BOOTMENU_TYPE_UEFI_BOOT_OPTION) {
538 /*
539 * UEFI specification requires BootNext variable needs non-volatile
540 * attribute, but this BootNext is only used inside of U-Boot and
541 * removed by efi bootmgr once BootNext is processed.
542 * So this BootNext can be volatile.
543 */
544 efi_ret = efi_set_variable_int(u"BootNext", &efi_global_variable_guid,
545 EFI_VARIABLE_BOOTSERVICE_ACCESS |
546 EFI_VARIABLE_RUNTIME_ACCESS,
547 sizeof(u16), &iter->bootorder, false);
548 if (efi_ret != EFI_SUCCESS)
549 goto cleanup;
550 }
551 }
552
Pali Roháre7abe912013-03-23 14:53:08 +0000553cleanup:
554 menu_destroy(menu);
555 bootmenu_destroy(bootmenu);
556
557 if (init) {
558 puts(ANSI_CURSOR_SHOW);
559 puts(ANSI_CLEAR_CONSOLE);
560 printf(ANSI_CURSOR_POSITION, 1, 1);
561 }
562
563 if (title && command) {
Masahisa Kojimaa3d0aa82022-04-28 17:09:41 +0900564 debug("Starting entry '%ls'\n", title);
Pali Roháre7abe912013-03-23 14:53:08 +0000565 free(title);
Masahisa Kojimac606c002022-04-28 17:09:42 +0900566 if (efi_ret == EFI_SUCCESS)
Masahisa Kojima2158b0d2022-04-28 17:09:44 +0900567 cmd_ret = run_command(command, 0);
Pali Roháre7abe912013-03-23 14:53:08 +0000568 free(command);
569 }
570
571#ifdef CONFIG_POSTBOOTMENU
572 run_command(CONFIG_POSTBOOTMENU, 0);
573#endif
Masahisa Kojima2158b0d2022-04-28 17:09:44 +0900574
575 if (efi_ret != EFI_SUCCESS || cmd_ret != CMD_RET_SUCCESS)
576 ret = BOOTMENU_RET_FAIL;
577
578 return ret;
Pali Roháre7abe912013-03-23 14:53:08 +0000579}
580
Simon Glasse2313062019-07-20 20:51:24 -0600581#ifdef CONFIG_AUTOBOOT_MENU_SHOW
Pali Roháre7abe912013-03-23 14:53:08 +0000582int menu_show(int bootdelay)
583{
Masahisa Kojima2158b0d2022-04-28 17:09:44 +0900584 int ret;
585
586 while (1) {
587 ret = bootmenu_show(bootdelay);
588 bootdelay = -1;
589 if (ret == BOOTMENU_RET_UPDATED)
590 continue;
591
592 if (!IS_ENABLED(CONFIG_CMD_BOOTMENU_ENTER_UBOOT_CONSOLE)) {
593 if (ret == BOOTMENU_RET_QUIT) {
594 /* default boot process */
595 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
596 run_command("bootefi bootmgr", 0);
597
598 run_command("run bootcmd", 0);
599 }
600 } else {
601 break;
602 }
603 }
604
Pali Roháre7abe912013-03-23 14:53:08 +0000605 return -1; /* -1 - abort boot and run monitor code */
606}
607#endif
608
Simon Glass09140112020-05-10 11:40:03 -0600609int do_bootmenu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Pali Roháre7abe912013-03-23 14:53:08 +0000610{
611 char *delay_str = NULL;
612 int delay = 10;
613
614#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
615 delay = CONFIG_BOOTDELAY;
616#endif
617
618 if (argc >= 2)
619 delay_str = argv[1];
620
621 if (!delay_str)
Simon Glass00caae62017-08-03 12:22:12 -0600622 delay_str = env_get("bootmenu_delay");
Pali Roháre7abe912013-03-23 14:53:08 +0000623
624 if (delay_str)
625 delay = (int)simple_strtol(delay_str, NULL, 10);
626
627 bootmenu_show(delay);
628 return 0;
629}
630
631U_BOOT_CMD(
632 bootmenu, 2, 1, do_bootmenu,
633 "ANSI terminal bootmenu",
634 "[delay]\n"
635 " - show ANSI terminal bootmenu with autoboot delay"
636);