blob: 78184fccab25d34bc2de50302485afe197e5b4b4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Pali Rohárac91b472013-03-23 14:53:08 +00002/*
Pali Rohár10a953d2020-04-01 00:35:08 +02003 * (C) Copyright 2011-2013 Pali Rohár <pali@kernel.org>
Pali Rohárac91b472013-03-23 14:53:08 +00004 */
5
Masahisa Kojima015405a2022-04-28 17:09:41 +09006#include <charset.h>
Simon Glass9d8d3872023-01-06 08:52:26 -06007#include <cli.h>
Pali Rohárac91b472013-03-23 14:53:08 +00008#include <common.h>
9#include <command.h>
10#include <ansi.h>
Masahisa Kojima767a9e62022-09-12 17:33:54 +090011#include <efi_config.h>
Masahisa Kojimaf90c7b22022-04-28 17:09:42 +090012#include <efi_variable.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060013#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Pali Rohárac91b472013-03-23 14:53:08 +000015#include <menu.h>
Pali Rohárac91b472013-03-23 14:53:08 +000016#include <watchdog.h>
17#include <malloc.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Pali Rohárac91b472013-03-23 14:53:08 +000019#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
Masahisa Kojima43d0ab22022-04-28 17:09:44 +090031enum bootmenu_ret {
32 BOOTMENU_RET_SUCCESS = 0,
33 BOOTMENU_RET_FAIL,
34 BOOTMENU_RET_QUIT,
35 BOOTMENU_RET_UPDATED,
36};
37
Masahisa Kojima015405a2022-04-28 17:09:41 +090038enum boot_type {
39 BOOTMENU_TYPE_NONE = 0,
40 BOOTMENU_TYPE_BOOTMENU,
Masahisa Kojimaf90c7b22022-04-28 17:09:42 +090041 BOOTMENU_TYPE_UEFI_BOOT_OPTION,
Masahisa Kojima015405a2022-04-28 17:09:41 +090042};
43
Pali Rohárac91b472013-03-23 14:53:08 +000044struct bootmenu_entry {
45 unsigned short int num; /* unique number 0 .. MAX_COUNT */
46 char key[3]; /* key identifier of number */
Masahisa Kojima1baf9642022-05-29 10:52:43 +090047 char *title; /* title of entry */
Pali Rohárac91b472013-03-23 14:53:08 +000048 char *command; /* hush command of entry */
Masahisa Kojima015405a2022-04-28 17:09:41 +090049 enum boot_type type; /* boot type of entry */
50 u16 bootorder; /* order for each boot type */
Pali Rohárac91b472013-03-23 14:53:08 +000051 struct bootmenu_data *menu; /* this bootmenu */
52 struct bootmenu_entry *next; /* next menu entry (num+1) */
53};
54
Pali Rohárac91b472013-03-23 14:53:08 +000055static char *bootmenu_getoption(unsigned short int n)
56{
Lan Yixun (dlan)981c7e02013-06-27 18:58:53 +080057 char name[MAX_ENV_SIZE];
Pali Rohárac91b472013-03-23 14:53:08 +000058
59 if (n > MAX_COUNT)
60 return NULL;
61
Lan Yixun (dlan)981c7e02013-06-27 18:58:53 +080062 sprintf(name, "bootmenu_%d", n);
Simon Glass64b723f2017-08-03 12:22:12 -060063 return env_get(name);
Pali Rohárac91b472013-03-23 14:53:08 +000064}
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 */
Heinrich Schuchardtd7dc8772022-05-01 23:17:18 +020075 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
Pali Rohárac91b472013-03-23 14:53:08 +000076
77 if (reverse)
78 puts(ANSI_COLOR_REVERSE);
79
Masahisa Kojima1baf9642022-05-29 10:52:43 +090080 printf("%s", entry->title);
Pali Rohárac91b472013-03-23 14:53:08 +000081
82 if (reverse)
83 puts(ANSI_COLOR_RESET);
84}
85
Pali Rohárac91b472013-03-23 14:53:08 +000086static char *bootmenu_choice_entry(void *data)
87{
Simon Glass9d8d3872023-01-06 08:52:26 -060088 struct cli_ch_state s_cch, *cch = &s_cch;
Pali Rohárac91b472013-03-23 14:53:08 +000089 struct bootmenu_data *menu = data;
90 struct bootmenu_entry *iter;
Simon Glass05ecdf82023-01-06 08:52:22 -060091 enum bootmenu_key key = BKEY_NONE;
Pali Rohárac91b472013-03-23 14:53:08 +000092 int i;
93
Simon Glass9d8d3872023-01-06 08:52:26 -060094 cli_ch_init(cch);
95
Pali Rohárac91b472013-03-23 14:53:08 +000096 while (1) {
97 if (menu->delay >= 0) {
98 /* Autoboot was not stopped */
Simon Glass9d8d3872023-01-06 08:52:26 -060099 key = bootmenu_autoboot_loop(menu, cch);
Pali Rohárac91b472013-03-23 14:53:08 +0000100 } else {
101 /* Some key was pressed, so autoboot was stopped */
Simon Glass9d8d3872023-01-06 08:52:26 -0600102 key = bootmenu_loop(menu, cch);
Pali Rohárac91b472013-03-23 14:53:08 +0000103 }
104
105 switch (key) {
Simon Glass05ecdf82023-01-06 08:52:22 -0600106 case BKEY_UP:
Pali Rohárac91b472013-03-23 14:53:08 +0000107 if (menu->active > 0)
108 --menu->active;
109 /* no menu key selected, regenerate menu */
110 return NULL;
Simon Glass05ecdf82023-01-06 08:52:22 -0600111 case BKEY_DOWN:
Pali Rohárac91b472013-03-23 14:53:08 +0000112 if (menu->active < menu->count - 1)
113 ++menu->active;
114 /* no menu key selected, regenerate menu */
115 return NULL;
Simon Glass05ecdf82023-01-06 08:52:22 -0600116 case BKEY_SELECT:
Pali Rohárac91b472013-03-23 14:53:08 +0000117 iter = menu->first;
118 for (i = 0; i < menu->active; ++i)
119 iter = iter->next;
120 return iter->key;
Simon Glass05ecdf82023-01-06 08:52:22 -0600121 case BKEY_QUIT:
Svyatoslav Ryhel08f67c42024-01-17 12:55:46 +0200122 /* Quit by choosing the last entry */
Pali Rohár3c0629b2020-12-27 01:04:38 +0100123 iter = menu->first;
124 while (iter->next)
125 iter = iter->next;
126 return iter->key;
Pali Rohárac91b472013-03-23 14:53:08 +0000127 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
Masahisa Kojima015405a2022-04-28 17:09:41 +0900152/**
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)
Pali Rohárac91b472013-03-23 14:53:08 +0000167{
Pali Rohárac91b472013-03-23 14:53:08 +0000168 char *sep;
Masahisa Kojima015405a2022-04-28 17:09:41 +0900169 const char *option;
170 unsigned short int i = *index;
171 struct bootmenu_entry *entry = NULL;
172 struct bootmenu_entry *iter = *current;
Frank Wunderlich6a3578e2018-10-05 11:41:59 +0200173
Pali Rohárac91b472013-03-23 14:53:08 +0000174 while ((option = bootmenu_getoption(i))) {
Masahisa Kojima015405a2022-04-28 17:09:41 +0900175
Masahisa Kojima1baf9642022-05-29 10:52:43 +0900176 /* bootmenu_[num] format is "[title]=[commands]" */
Pali Rohárac91b472013-03-23 14:53:08 +0000177 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)
Masahisa Kojima015405a2022-04-28 17:09:41 +0900185 return -ENOMEM;
Pali Rohárac91b472013-03-23 14:53:08 +0000186
Masahisa Kojima1baf9642022-05-29 10:52:43 +0900187 entry->title = strndup(option, sep - option);
Pali Rohárac91b472013-03-23 14:53:08 +0000188 if (!entry->title) {
189 free(entry);
Masahisa Kojima015405a2022-04-28 17:09:41 +0900190 return -ENOMEM;
Pali Rohárac91b472013-03-23 14:53:08 +0000191 }
Pali Rohárac91b472013-03-23 14:53:08 +0000192
Masahisa Kojima1baf9642022-05-29 10:52:43 +0900193 entry->command = strdup(sep + 1);
Pali Rohárac91b472013-03-23 14:53:08 +0000194 if (!entry->command) {
195 free(entry->title);
196 free(entry);
Masahisa Kojima015405a2022-04-28 17:09:41 +0900197 return -ENOMEM;
Pali Rohárac91b472013-03-23 14:53:08 +0000198 }
Pali Rohárac91b472013-03-23 14:53:08 +0000199
200 sprintf(entry->key, "%d", i);
201
202 entry->num = i;
203 entry->menu = menu;
Masahisa Kojima015405a2022-04-28 17:09:41 +0900204 entry->type = BOOTMENU_TYPE_BOOTMENU;
205 entry->bootorder = i;
Pali Rohárac91b472013-03-23 14:53:08 +0000206 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
Masahisa Kojima015405a2022-04-28 17:09:41 +0900220 *index = i;
221 *current = iter;
222
223 return 1;
224}
225
Simon Glass315367a2023-02-05 15:36:28 -0700226#if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) && (IS_ENABLED(CONFIG_CMD_EFICONFIG))
Masahisa Kojimaf90c7b22022-04-28 17:09:42 +0900227/**
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) {
Masahisa Kojima1baf9642022-05-29 10:52:43 +0900279 char *buf;
280
281 buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
282 if (!buf) {
Masahisa Kojimaf90c7b22022-04-28 17:09:42 +0900283 free(load_option);
284 free(entry);
285 free(bootorder);
286 return -ENOMEM;
287 }
Masahisa Kojima1baf9642022-05-29 10:52:43 +0900288 entry->title = buf;
289 utf16_utf8_strncpy(&buf, lo.label, u16_strlen(lo.label));
Masahisa Kojimaf90c7b22022-04-28 17:09:42 +0900290 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}
Masahisa Kojima1baf9642022-05-29 10:52:43 +0900319#endif
Masahisa Kojimaf90c7b22022-04-28 17:09:42 +0900320
Masahisa Kojima015405a2022-04-28 17:09:41 +0900321static 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
Simon Glass315367a2023-02-05 15:36:28 -0700346#if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) && (IS_ENABLED(CONFIG_CMD_EFICONFIG))
Masahisa Kojima1baf9642022-05-29 10:52:43 +0900347 if (i < MAX_COUNT - 1) {
Masahisa Kojima767a9e62022-09-12 17:33:54 +0900348 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 */
Raymond Mao70a76c52023-06-19 14:22:58 -0700354 efi_ret = efi_bootmgr_update_media_device_boot_option();
Raymond Maoa35784d2023-06-19 14:22:59 -0700355 if (efi_ret != EFI_SUCCESS)
Masahisa Kojima767a9e62022-09-12 17:33:54 +0900356 goto cleanup;
357
358 ret = prepare_uefi_bootorder_entry(menu, &iter, &i);
359 if (ret < 0 && ret != -ENOENT)
360 goto cleanup;
Masahisa Kojimaf90c7b22022-04-28 17:09:42 +0900361 }
Masahisa Kojima1baf9642022-05-29 10:52:43 +0900362#endif
Masahisa Kojimaf90c7b22022-04-28 17:09:42 +0900363
Svyatoslav Ryhel08f67c42024-01-17 12:55:46 +0200364 /* Add Exit entry at the end */
Pali Rohárac91b472013-03-23 14:53:08 +0000365 if (i <= MAX_COUNT - 1) {
366 entry = malloc(sizeof(struct bootmenu_entry));
367 if (!entry)
368 goto cleanup;
369
Svyatoslav Ryhel08f67c42024-01-17 12:55:46 +0200370 /* Add Quit entry if exiting bootmenu is disabled */
Masahisa Kojima97cbcc42022-05-26 19:09:38 +0900371 if (!IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE))
Svyatoslav Ryhel08f67c42024-01-17 12:55:46 +0200372 entry->title = strdup("Exit");
Masahisa Kojima43d0ab22022-04-28 17:09:44 +0900373 else
Masahisa Kojima1baf9642022-05-29 10:52:43 +0900374 entry->title = strdup("Quit");
Masahisa Kojima43d0ab22022-04-28 17:09:44 +0900375
Pali Rohárac91b472013-03-23 14:53:08 +0000376 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;
Masahisa Kojima015405a2022-04-28 17:09:41 +0900392 entry->type = BOOTMENU_TYPE_NONE;
Pali Rohárac91b472013-03-23 14:53:08 +0000393 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;
Frank Wunderlich4931d962018-12-03 11:23:41 +0100405
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
Pali Rohárac91b472013-03-23 14:53:08 +0000411 return menu;
412
413cleanup:
414 bootmenu_destroy(menu);
415 return NULL;
416}
417
Thirupathaiah Annapureddyd6b9f6b2020-03-18 11:38:42 -0700418static 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);
Heinrich Schuchardtd7dc8772022-05-01 23:17:18 +0200430 printf(ANSI_CURSOR_POSITION, 2, 3);
431 puts("*** U-Boot Boot Menu ***");
Thirupathaiah Annapureddyd6b9f6b2020-03-18 11:38:42 -0700432 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);
Heinrich Schuchardtd7dc8772022-05-01 23:17:18 +0200439 printf(ANSI_CURSOR_POSITION, menu->count + 6, 3);
Masahisa Kojima3a9eb072023-02-02 18:24:43 +0900440 puts("Press UP/DOWN to move, ENTER to select, ESC to quit");
Thirupathaiah Annapureddyd6b9f6b2020-03-18 11:38:42 -0700441 puts(ANSI_CLEAR_LINE_TO_END);
442 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
443 puts(ANSI_CLEAR_LINE);
444}
445
Masahisa Kojimaf90c7b22022-04-28 17:09:42 +0900446static 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
Masahisa Kojima43d0ab22022-04-28 17:09:44 +0900471static enum bootmenu_ret bootmenu_show(int delay)
Pali Rohárac91b472013-03-23 14:53:08 +0000472{
Masahisa Kojima43d0ab22022-04-28 17:09:44 +0900473 int cmd_ret;
Pali Rohárac91b472013-03-23 14:53:08 +0000474 int init = 0;
475 void *choice = NULL;
Masahisa Kojima1baf9642022-05-29 10:52:43 +0900476 char *title = NULL;
Pali Rohárac91b472013-03-23 14:53:08 +0000477 char *command = NULL;
478 struct menu *menu;
Pali Rohárac91b472013-03-23 14:53:08 +0000479 struct bootmenu_entry *iter;
Masahisa Kojima43d0ab22022-04-28 17:09:44 +0900480 int ret = BOOTMENU_RET_SUCCESS;
481 struct bootmenu_data *bootmenu;
Masahisa Kojimaf90c7b22022-04-28 17:09:42 +0900482 efi_status_t efi_ret = EFI_SUCCESS;
Pali Rohárac91b472013-03-23 14:53:08 +0000483 char *option, *sep;
484
Masahisa Kojimaf90c7b22022-04-28 17:09:42 +0900485 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
486 handle_uefi_bootnext();
487
Pali Rohárac91b472013-03-23 14:53:08 +0000488 /* 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");
Masahisa Kojima43d0ab22022-04-28 17:09:44 +0900493 return BOOTMENU_RET_FAIL;
Pali Rohárac91b472013-03-23 14:53:08 +0000494 }
495 sep = strchr(option, '=');
496 if (!sep) {
497 puts("bootmenu option 0 is invalid\n");
Masahisa Kojima43d0ab22022-04-28 17:09:44 +0900498 return BOOTMENU_RET_FAIL;
Pali Rohárac91b472013-03-23 14:53:08 +0000499 }
Masahisa Kojima43d0ab22022-04-28 17:09:44 +0900500 cmd_ret = run_command(sep + 1, 0);
501 return (cmd_ret == CMD_RET_SUCCESS ? BOOTMENU_RET_SUCCESS : BOOTMENU_RET_FAIL);
Pali Rohárac91b472013-03-23 14:53:08 +0000502 }
503
504 bootmenu = bootmenu_create(delay);
505 if (!bootmenu)
Masahisa Kojima43d0ab22022-04-28 17:09:44 +0900506 return BOOTMENU_RET_FAIL;
Pali Rohárac91b472013-03-23 14:53:08 +0000507
Thirupathaiah Annapureddyd6b9f6b2020-03-18 11:38:42 -0700508 menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline,
509 bootmenu_print_entry, bootmenu_choice_entry,
510 bootmenu);
Pali Rohárac91b472013-03-23 14:53:08 +0000511 if (!menu) {
512 bootmenu_destroy(bootmenu);
Masahisa Kojima43d0ab22022-04-28 17:09:44 +0900513 return BOOTMENU_RET_FAIL;
Pali Rohárac91b472013-03-23 14:53:08 +0000514 }
515
516 for (iter = bootmenu->first; iter; iter = iter->next) {
Masahisa Kojimaacc969e2022-03-24 22:54:33 +0900517 if (menu_item_add(menu, iter->key, iter) != 1)
Pali Rohárac91b472013-03-23 14:53:08 +0000518 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
Masahisa Kojimaacc969e2022-03-24 22:54:33 +0900530 if (menu_get_choice(menu, &choice) == 1) {
Pali Rohárac91b472013-03-23 14:53:08 +0000531 iter = choice;
Masahisa Kojima1baf9642022-05-29 10:52:43 +0900532 title = strdup(iter->title);
Pali Rohárac91b472013-03-23 14:53:08 +0000533 command = strdup(iter->command);
Masahisa Kojima43d0ab22022-04-28 17:09:44 +0900534
Svyatoslav Ryhel08f67c42024-01-17 12:55:46 +0200535 /* last entry exits bootmenu */
Masahisa Kojima43d0ab22022-04-28 17:09:44 +0900536 if (iter->num == iter->menu->count - 1) {
537 ret = BOOTMENU_RET_QUIT;
538 goto cleanup;
539 }
540 } else {
541 goto cleanup;
Pali Rohárac91b472013-03-23 14:53:08 +0000542 }
543
Masahisa Kojimaf90c7b22022-04-28 17:09:42 +0900544 /*
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
Pali Rohárac91b472013-03-23 14:53:08 +0000565cleanup:
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) {
Masahisa Kojima1baf9642022-05-29 10:52:43 +0900576 debug("Starting entry '%s'\n", title);
Pali Rohárac91b472013-03-23 14:53:08 +0000577 free(title);
Masahisa Kojimaf90c7b22022-04-28 17:09:42 +0900578 if (efi_ret == EFI_SUCCESS)
Masahisa Kojima43d0ab22022-04-28 17:09:44 +0900579 cmd_ret = run_command(command, 0);
Pali Rohárac91b472013-03-23 14:53:08 +0000580 free(command);
581 }
582
Tom Rinie510a3f2022-12-04 10:13:33 -0500583#ifdef CFG_POSTBOOTMENU
584 run_command(CFG_POSTBOOTMENU, 0);
Pali Rohárac91b472013-03-23 14:53:08 +0000585#endif
Masahisa Kojima43d0ab22022-04-28 17:09:44 +0900586
587 if (efi_ret != EFI_SUCCESS || cmd_ret != CMD_RET_SUCCESS)
588 ret = BOOTMENU_RET_FAIL;
589
590 return ret;
Pali Rohárac91b472013-03-23 14:53:08 +0000591}
592
Simon Glassec5f71a2019-07-20 20:51:24 -0600593#ifdef CONFIG_AUTOBOOT_MENU_SHOW
Pali Rohárac91b472013-03-23 14:53:08 +0000594int menu_show(int bootdelay)
595{
Masahisa Kojima43d0ab22022-04-28 17:09:44 +0900596 int ret;
597
598 while (1) {
599 ret = bootmenu_show(bootdelay);
600 bootdelay = -1;
601 if (ret == BOOTMENU_RET_UPDATED)
602 continue;
603
Masahisa Kojima97cbcc42022-05-26 19:09:38 +0900604 if (IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE)) {
Masahisa Kojima43d0ab22022-04-28 17:09:44 +0900605 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
Pali Rohárac91b472013-03-23 14:53:08 +0000617 return -1; /* -1 - abort boot and run monitor code */
618}
619#endif
620
Simon Glassed38aef2020-05-10 11:40:03 -0600621int do_bootmenu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Pali Rohárac91b472013-03-23 14:53:08 +0000622{
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)
Simon Glass64b723f2017-08-03 12:22:12 -0600634 delay_str = env_get("bootmenu_delay");
Pali Rohárac91b472013-03-23 14:53:08 +0000635
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);