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