Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 2 | /* |
Pali Rohár | 10a953d | 2020-04-01 00:35:08 +0200 | [diff] [blame] | 3 | * (C) Copyright 2011-2013 Pali Rohár <pali@kernel.org> |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Masahisa Kojima | 015405a | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 6 | #include <charset.h> |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 7 | #include <cli.h> |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <command.h> |
| 10 | #include <ansi.h> |
Masahisa Kojima | 767a9e6 | 2022-09-12 17:33:54 +0900 | [diff] [blame] | 11 | #include <efi_config.h> |
Masahisa Kojima | f90c7b2 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 12 | #include <efi_variable.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 13 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 15 | #include <menu.h> |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 16 | #include <watchdog.h> |
| 17 | #include <malloc.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 18 | #include <linux/delay.h> |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 19 | #include <linux/string.h> |
| 20 | |
| 21 | /* maximum bootmenu entries */ |
| 22 | #define MAX_COUNT 99 |
| 23 | |
| 24 | /* maximal size of bootmenu env |
| 25 | * 9 = strlen("bootmenu_") |
| 26 | * 2 = strlen(MAX_COUNT) |
| 27 | * 1 = NULL term |
| 28 | */ |
| 29 | #define MAX_ENV_SIZE (9 + 2 + 1) |
| 30 | |
Masahisa Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 31 | enum bootmenu_ret { |
| 32 | BOOTMENU_RET_SUCCESS = 0, |
| 33 | BOOTMENU_RET_FAIL, |
| 34 | BOOTMENU_RET_QUIT, |
| 35 | BOOTMENU_RET_UPDATED, |
| 36 | }; |
| 37 | |
Masahisa Kojima | 015405a | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 38 | enum boot_type { |
| 39 | BOOTMENU_TYPE_NONE = 0, |
| 40 | BOOTMENU_TYPE_BOOTMENU, |
Masahisa Kojima | f90c7b2 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 41 | BOOTMENU_TYPE_UEFI_BOOT_OPTION, |
Masahisa Kojima | 015405a | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 42 | }; |
| 43 | |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 44 | struct bootmenu_entry { |
| 45 | unsigned short int num; /* unique number 0 .. MAX_COUNT */ |
| 46 | char key[3]; /* key identifier of number */ |
Masahisa Kojima | 1baf964 | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 47 | char *title; /* title of entry */ |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 48 | char *command; /* hush command of entry */ |
Masahisa Kojima | 015405a | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 49 | enum boot_type type; /* boot type of entry */ |
| 50 | u16 bootorder; /* order for each boot type */ |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 51 | struct bootmenu_data *menu; /* this bootmenu */ |
| 52 | struct bootmenu_entry *next; /* next menu entry (num+1) */ |
| 53 | }; |
| 54 | |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 55 | static char *bootmenu_getoption(unsigned short int n) |
| 56 | { |
Lan Yixun (dlan) | 981c7e0 | 2013-06-27 18:58:53 +0800 | [diff] [blame] | 57 | char name[MAX_ENV_SIZE]; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 58 | |
| 59 | if (n > MAX_COUNT) |
| 60 | return NULL; |
| 61 | |
Lan Yixun (dlan) | 981c7e0 | 2013-06-27 18:58:53 +0800 | [diff] [blame] | 62 | sprintf(name, "bootmenu_%d", n); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 63 | return env_get(name); |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static 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 Schuchardt | d7dc877 | 2022-05-01 23:17:18 +0200 | [diff] [blame] | 75 | printf(ANSI_CURSOR_POSITION, entry->num + 4, 7); |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 76 | |
| 77 | if (reverse) |
| 78 | puts(ANSI_COLOR_REVERSE); |
| 79 | |
Masahisa Kojima | 1baf964 | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 80 | printf("%s", entry->title); |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 81 | |
| 82 | if (reverse) |
| 83 | puts(ANSI_COLOR_RESET); |
| 84 | } |
| 85 | |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 86 | static char *bootmenu_choice_entry(void *data) |
| 87 | { |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 88 | struct cli_ch_state s_cch, *cch = &s_cch; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 89 | struct bootmenu_data *menu = data; |
| 90 | struct bootmenu_entry *iter; |
Simon Glass | 05ecdf8 | 2023-01-06 08:52:22 -0600 | [diff] [blame] | 91 | enum bootmenu_key key = BKEY_NONE; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 92 | int i; |
| 93 | |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 94 | cli_ch_init(cch); |
| 95 | |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 96 | while (1) { |
| 97 | if (menu->delay >= 0) { |
| 98 | /* Autoboot was not stopped */ |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 99 | key = bootmenu_autoboot_loop(menu, cch); |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 100 | } else { |
| 101 | /* Some key was pressed, so autoboot was stopped */ |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 102 | key = bootmenu_loop(menu, cch); |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | switch (key) { |
Simon Glass | 05ecdf8 | 2023-01-06 08:52:22 -0600 | [diff] [blame] | 106 | case BKEY_UP: |
Pali Rohár | ac91b47 | 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 | 05ecdf8 | 2023-01-06 08:52:22 -0600 | [diff] [blame] | 111 | case BKEY_DOWN: |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 112 | if (menu->active < menu->count - 1) |
| 113 | ++menu->active; |
| 114 | /* no menu key selected, regenerate menu */ |
| 115 | return NULL; |
Simon Glass | 05ecdf8 | 2023-01-06 08:52:22 -0600 | [diff] [blame] | 116 | case BKEY_SELECT: |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 117 | iter = menu->first; |
| 118 | for (i = 0; i < menu->active; ++i) |
| 119 | iter = iter->next; |
| 120 | return iter->key; |
Simon Glass | 05ecdf8 | 2023-01-06 08:52:22 -0600 | [diff] [blame] | 121 | case BKEY_QUIT: |
Pali Rohár | 3c0629b | 2020-12-27 01:04:38 +0100 | [diff] [blame] | 122 | /* Quit by choosing the last entry - U-Boot console */ |
| 123 | iter = menu->first; |
| 124 | while (iter->next) |
| 125 | iter = iter->next; |
| 126 | return iter->key; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* never happens */ |
| 133 | debug("bootmenu: this should not happen"); |
| 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | static 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 Kojima | 015405a | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 152 | /** |
| 153 | * prepare_bootmenu_entry() - generate the bootmenu_xx entries |
| 154 | * |
| 155 | * This function read the "bootmenu_x" U-Boot environment variable |
| 156 | * and generate the bootmenu entries. |
| 157 | * |
| 158 | * @menu: pointer to the bootmenu structure |
| 159 | * @current: pointer to the last bootmenu entry list |
| 160 | * @index: pointer to the index of the last bootmenu entry, |
| 161 | * the number of bootmenu entry is added by this function |
| 162 | * Return: 1 on success, negative value on error |
| 163 | */ |
| 164 | static int prepare_bootmenu_entry(struct bootmenu_data *menu, |
| 165 | struct bootmenu_entry **current, |
| 166 | unsigned short int *index) |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 167 | { |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 168 | char *sep; |
Masahisa Kojima | 015405a | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 169 | const char *option; |
| 170 | unsigned short int i = *index; |
| 171 | struct bootmenu_entry *entry = NULL; |
| 172 | struct bootmenu_entry *iter = *current; |
Frank Wunderlich | 6a3578e | 2018-10-05 11:41:59 +0200 | [diff] [blame] | 173 | |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 174 | while ((option = bootmenu_getoption(i))) { |
Masahisa Kojima | 015405a | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 175 | |
Masahisa Kojima | 1baf964 | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 176 | /* bootmenu_[num] format is "[title]=[commands]" */ |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 177 | sep = strchr(option, '='); |
| 178 | if (!sep) { |
| 179 | printf("Invalid bootmenu entry: %s\n", option); |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | entry = malloc(sizeof(struct bootmenu_entry)); |
| 184 | if (!entry) |
Masahisa Kojima | 015405a | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 185 | return -ENOMEM; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 186 | |
Masahisa Kojima | 1baf964 | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 187 | entry->title = strndup(option, sep - option); |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 188 | if (!entry->title) { |
| 189 | free(entry); |
Masahisa Kojima | 015405a | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 190 | return -ENOMEM; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 191 | } |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 192 | |
Masahisa Kojima | 1baf964 | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 193 | entry->command = strdup(sep + 1); |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 194 | if (!entry->command) { |
| 195 | free(entry->title); |
| 196 | free(entry); |
Masahisa Kojima | 015405a | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 197 | return -ENOMEM; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 198 | } |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 199 | |
| 200 | sprintf(entry->key, "%d", i); |
| 201 | |
| 202 | entry->num = i; |
| 203 | entry->menu = menu; |
Masahisa Kojima | 015405a | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 204 | entry->type = BOOTMENU_TYPE_BOOTMENU; |
| 205 | entry->bootorder = i; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 206 | entry->next = NULL; |
| 207 | |
| 208 | if (!iter) |
| 209 | menu->first = entry; |
| 210 | else |
| 211 | iter->next = entry; |
| 212 | |
| 213 | iter = entry; |
| 214 | ++i; |
| 215 | |
| 216 | if (i == MAX_COUNT - 1) |
| 217 | break; |
| 218 | } |
| 219 | |
Masahisa Kojima | 015405a | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 220 | *index = i; |
| 221 | *current = iter; |
| 222 | |
| 223 | return 1; |
| 224 | } |
| 225 | |
Simon Glass | 315367a | 2023-02-05 15:36:28 -0700 | [diff] [blame] | 226 | #if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) && (IS_ENABLED(CONFIG_CMD_EFICONFIG)) |
Masahisa Kojima | f90c7b2 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 227 | /** |
| 228 | * prepare_uefi_bootorder_entry() - generate the uefi bootmenu entries |
| 229 | * |
| 230 | * This function read the "BootOrder" UEFI variable |
| 231 | * and generate the bootmenu entries in the order of "BootOrder". |
| 232 | * |
| 233 | * @menu: pointer to the bootmenu structure |
| 234 | * @current: pointer to the last bootmenu entry list |
| 235 | * @index: pointer to the index of the last bootmenu entry, |
| 236 | * the number of uefi entry is added by this function |
| 237 | * Return: 1 on success, negative value on error |
| 238 | */ |
| 239 | static 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 Kojima | 1baf964 | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 279 | char *buf; |
| 280 | |
| 281 | buf = calloc(1, utf16_utf8_strlen(lo.label) + 1); |
| 282 | if (!buf) { |
Masahisa Kojima | f90c7b2 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 283 | free(load_option); |
| 284 | free(entry); |
| 285 | free(bootorder); |
| 286 | return -ENOMEM; |
| 287 | } |
Masahisa Kojima | 1baf964 | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 288 | entry->title = buf; |
| 289 | utf16_utf8_strncpy(&buf, lo.label, u16_strlen(lo.label)); |
Masahisa Kojima | f90c7b2 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 290 | entry->command = strdup("bootefi bootmgr"); |
| 291 | sprintf(entry->key, "%d", i); |
| 292 | entry->num = i; |
| 293 | entry->menu = menu; |
| 294 | entry->type = BOOTMENU_TYPE_UEFI_BOOT_OPTION; |
| 295 | entry->bootorder = bootorder[j]; |
| 296 | entry->next = NULL; |
| 297 | |
| 298 | if (!iter) |
| 299 | menu->first = entry; |
| 300 | else |
| 301 | iter->next = entry; |
| 302 | |
| 303 | iter = entry; |
| 304 | i++; |
| 305 | } |
| 306 | |
| 307 | free(load_option); |
| 308 | |
| 309 | if (i == MAX_COUNT - 1) |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | free(bootorder); |
| 314 | *index = i; |
| 315 | *current = iter; |
| 316 | |
| 317 | return 1; |
| 318 | } |
Masahisa Kojima | 1baf964 | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 319 | #endif |
Masahisa Kojima | f90c7b2 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 320 | |
Masahisa Kojima | 015405a | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 321 | static 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 Glass | 315367a | 2023-02-05 15:36:28 -0700 | [diff] [blame] | 346 | #if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) && (IS_ENABLED(CONFIG_CMD_EFICONFIG)) |
Masahisa Kojima | 1baf964 | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 347 | if (i < MAX_COUNT - 1) { |
Masahisa Kojima | 767a9e6 | 2022-09-12 17:33:54 +0900 | [diff] [blame] | 348 | efi_status_t efi_ret; |
| 349 | |
| 350 | /* |
| 351 | * UEFI specification requires booting from removal media using |
| 352 | * a architecture-specific default image name such as BOOTAA64.EFI. |
| 353 | */ |
| 354 | efi_ret = eficonfig_generate_media_device_boot_option(); |
| 355 | if (efi_ret != EFI_SUCCESS && efi_ret != EFI_NOT_FOUND) |
| 356 | goto cleanup; |
| 357 | |
| 358 | ret = prepare_uefi_bootorder_entry(menu, &iter, &i); |
| 359 | if (ret < 0 && ret != -ENOENT) |
| 360 | goto cleanup; |
Masahisa Kojima | f90c7b2 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 361 | } |
Masahisa Kojima | 1baf964 | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 362 | #endif |
Masahisa Kojima | f90c7b2 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 363 | |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 364 | /* Add U-Boot console entry at the end */ |
| 365 | if (i <= MAX_COUNT - 1) { |
| 366 | entry = malloc(sizeof(struct bootmenu_entry)); |
| 367 | if (!entry) |
| 368 | goto cleanup; |
| 369 | |
Masahisa Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 370 | /* Add Quit entry if entering U-Boot console is disabled */ |
Masahisa Kojima | 97cbcc4 | 2022-05-26 19:09:38 +0900 | [diff] [blame] | 371 | if (!IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE)) |
Masahisa Kojima | 1baf964 | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 372 | entry->title = strdup("U-Boot console"); |
Masahisa Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 373 | else |
Masahisa Kojima | 1baf964 | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 374 | entry->title = strdup("Quit"); |
Masahisa Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 375 | |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 376 | if (!entry->title) { |
| 377 | free(entry); |
| 378 | goto cleanup; |
| 379 | } |
| 380 | |
| 381 | entry->command = strdup(""); |
| 382 | if (!entry->command) { |
| 383 | free(entry->title); |
| 384 | free(entry); |
| 385 | goto cleanup; |
| 386 | } |
| 387 | |
| 388 | sprintf(entry->key, "%d", i); |
| 389 | |
| 390 | entry->num = i; |
| 391 | entry->menu = menu; |
Masahisa Kojima | 015405a | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 392 | entry->type = BOOTMENU_TYPE_NONE; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 393 | entry->next = NULL; |
| 394 | |
| 395 | if (!iter) |
| 396 | menu->first = entry; |
| 397 | else |
| 398 | iter->next = entry; |
| 399 | |
| 400 | iter = entry; |
| 401 | ++i; |
| 402 | } |
| 403 | |
| 404 | menu->count = i; |
Frank Wunderlich | 4931d96 | 2018-12-03 11:23:41 +0100 | [diff] [blame] | 405 | |
| 406 | if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu |
| 407 | printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1); |
| 408 | menu->active=0; |
| 409 | } |
| 410 | |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 411 | return menu; |
| 412 | |
| 413 | cleanup: |
| 414 | bootmenu_destroy(menu); |
| 415 | return NULL; |
| 416 | } |
| 417 | |
Thirupathaiah Annapureddy | d6b9f6b | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 418 | static 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 Schuchardt | d7dc877 | 2022-05-01 23:17:18 +0200 | [diff] [blame] | 430 | printf(ANSI_CURSOR_POSITION, 2, 3); |
| 431 | puts("*** U-Boot Boot Menu ***"); |
Thirupathaiah Annapureddy | d6b9f6b | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 432 | puts(ANSI_CLEAR_LINE_TO_END); |
| 433 | printf(ANSI_CURSOR_POSITION, 3, 1); |
| 434 | puts(ANSI_CLEAR_LINE); |
| 435 | |
| 436 | /* First 3 lines are bootmenu header + 2 empty lines between entries */ |
| 437 | printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); |
| 438 | puts(ANSI_CLEAR_LINE); |
Heinrich Schuchardt | d7dc877 | 2022-05-01 23:17:18 +0200 | [diff] [blame] | 439 | printf(ANSI_CURSOR_POSITION, menu->count + 6, 3); |
Masahisa Kojima | 3a9eb07 | 2023-02-02 18:24:43 +0900 | [diff] [blame] | 440 | puts("Press UP/DOWN to move, ENTER to select, ESC to quit"); |
Thirupathaiah Annapureddy | d6b9f6b | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 441 | puts(ANSI_CLEAR_LINE_TO_END); |
| 442 | printf(ANSI_CURSOR_POSITION, menu->count + 7, 1); |
| 443 | puts(ANSI_CLEAR_LINE); |
| 444 | } |
| 445 | |
Masahisa Kojima | f90c7b2 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 446 | static 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 Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 471 | static enum bootmenu_ret bootmenu_show(int delay) |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 472 | { |
Masahisa Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 473 | int cmd_ret; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 474 | int init = 0; |
| 475 | void *choice = NULL; |
Masahisa Kojima | 1baf964 | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 476 | char *title = NULL; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 477 | char *command = NULL; |
| 478 | struct menu *menu; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 479 | struct bootmenu_entry *iter; |
Masahisa Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 480 | int ret = BOOTMENU_RET_SUCCESS; |
| 481 | struct bootmenu_data *bootmenu; |
Masahisa Kojima | f90c7b2 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 482 | efi_status_t efi_ret = EFI_SUCCESS; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 483 | char *option, *sep; |
| 484 | |
Masahisa Kojima | f90c7b2 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 485 | if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) |
| 486 | handle_uefi_bootnext(); |
| 487 | |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 488 | /* If delay is 0 do not create menu, just run first entry */ |
| 489 | if (delay == 0) { |
| 490 | option = bootmenu_getoption(0); |
| 491 | if (!option) { |
| 492 | puts("bootmenu option 0 was not found\n"); |
Masahisa Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 493 | return BOOTMENU_RET_FAIL; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 494 | } |
| 495 | sep = strchr(option, '='); |
| 496 | if (!sep) { |
| 497 | puts("bootmenu option 0 is invalid\n"); |
Masahisa Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 498 | return BOOTMENU_RET_FAIL; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 499 | } |
Masahisa Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 500 | cmd_ret = run_command(sep + 1, 0); |
| 501 | return (cmd_ret == CMD_RET_SUCCESS ? BOOTMENU_RET_SUCCESS : BOOTMENU_RET_FAIL); |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | bootmenu = bootmenu_create(delay); |
| 505 | if (!bootmenu) |
Masahisa Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 506 | return BOOTMENU_RET_FAIL; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 507 | |
Thirupathaiah Annapureddy | d6b9f6b | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 508 | menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline, |
| 509 | bootmenu_print_entry, bootmenu_choice_entry, |
| 510 | bootmenu); |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 511 | if (!menu) { |
| 512 | bootmenu_destroy(bootmenu); |
Masahisa Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 513 | return BOOTMENU_RET_FAIL; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | for (iter = bootmenu->first; iter; iter = iter->next) { |
Masahisa Kojima | acc969e | 2022-03-24 22:54:33 +0900 | [diff] [blame] | 517 | if (menu_item_add(menu, iter->key, iter) != 1) |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 518 | goto cleanup; |
| 519 | } |
| 520 | |
| 521 | /* Default menu entry is always first */ |
| 522 | menu_default_set(menu, "0"); |
| 523 | |
| 524 | puts(ANSI_CURSOR_HIDE); |
| 525 | puts(ANSI_CLEAR_CONSOLE); |
| 526 | printf(ANSI_CURSOR_POSITION, 1, 1); |
| 527 | |
| 528 | init = 1; |
| 529 | |
Masahisa Kojima | acc969e | 2022-03-24 22:54:33 +0900 | [diff] [blame] | 530 | if (menu_get_choice(menu, &choice) == 1) { |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 531 | iter = choice; |
Masahisa Kojima | 1baf964 | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 532 | title = strdup(iter->title); |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 533 | command = strdup(iter->command); |
Masahisa Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 534 | |
| 535 | /* last entry is U-Boot console or Quit */ |
| 536 | if (iter->num == iter->menu->count - 1) { |
| 537 | ret = BOOTMENU_RET_QUIT; |
| 538 | goto cleanup; |
| 539 | } |
| 540 | } else { |
| 541 | goto cleanup; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Masahisa Kojima | f90c7b2 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 544 | /* |
| 545 | * If the selected entry is UEFI BOOT####, set the BootNext variable. |
| 546 | * Then uefi bootmgr is invoked by the preset command in iter->command. |
| 547 | */ |
| 548 | if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) { |
| 549 | if (iter->type == BOOTMENU_TYPE_UEFI_BOOT_OPTION) { |
| 550 | /* |
| 551 | * UEFI specification requires BootNext variable needs non-volatile |
| 552 | * attribute, but this BootNext is only used inside of U-Boot and |
| 553 | * removed by efi bootmgr once BootNext is processed. |
| 554 | * So this BootNext can be volatile. |
| 555 | */ |
| 556 | efi_ret = efi_set_variable_int(u"BootNext", &efi_global_variable_guid, |
| 557 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 558 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 559 | sizeof(u16), &iter->bootorder, false); |
| 560 | if (efi_ret != EFI_SUCCESS) |
| 561 | goto cleanup; |
| 562 | } |
| 563 | } |
| 564 | |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 565 | cleanup: |
| 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 Kojima | 1baf964 | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 576 | debug("Starting entry '%s'\n", title); |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 577 | free(title); |
Masahisa Kojima | f90c7b2 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 578 | if (efi_ret == EFI_SUCCESS) |
Masahisa Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 579 | cmd_ret = run_command(command, 0); |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 580 | free(command); |
| 581 | } |
| 582 | |
Tom Rini | e510a3f | 2022-12-04 10:13:33 -0500 | [diff] [blame] | 583 | #ifdef CFG_POSTBOOTMENU |
| 584 | run_command(CFG_POSTBOOTMENU, 0); |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 585 | #endif |
Masahisa Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 586 | |
| 587 | if (efi_ret != EFI_SUCCESS || cmd_ret != CMD_RET_SUCCESS) |
| 588 | ret = BOOTMENU_RET_FAIL; |
| 589 | |
| 590 | return ret; |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Simon Glass | ec5f71a | 2019-07-20 20:51:24 -0600 | [diff] [blame] | 593 | #ifdef CONFIG_AUTOBOOT_MENU_SHOW |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 594 | int menu_show(int bootdelay) |
| 595 | { |
Masahisa Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 596 | int ret; |
| 597 | |
| 598 | while (1) { |
| 599 | ret = bootmenu_show(bootdelay); |
| 600 | bootdelay = -1; |
| 601 | if (ret == BOOTMENU_RET_UPDATED) |
| 602 | continue; |
| 603 | |
Masahisa Kojima | 97cbcc4 | 2022-05-26 19:09:38 +0900 | [diff] [blame] | 604 | if (IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE)) { |
Masahisa Kojima | 43d0ab2 | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 605 | if (ret == BOOTMENU_RET_QUIT) { |
| 606 | /* default boot process */ |
| 607 | if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) |
| 608 | run_command("bootefi bootmgr", 0); |
| 609 | |
| 610 | run_command("run bootcmd", 0); |
| 611 | } |
| 612 | } else { |
| 613 | break; |
| 614 | } |
| 615 | } |
| 616 | |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 617 | return -1; /* -1 - abort boot and run monitor code */ |
| 618 | } |
| 619 | #endif |
| 620 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 621 | int do_bootmenu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 622 | { |
| 623 | char *delay_str = NULL; |
| 624 | int delay = 10; |
| 625 | |
| 626 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) |
| 627 | delay = CONFIG_BOOTDELAY; |
| 628 | #endif |
| 629 | |
| 630 | if (argc >= 2) |
| 631 | delay_str = argv[1]; |
| 632 | |
| 633 | if (!delay_str) |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 634 | delay_str = env_get("bootmenu_delay"); |
Pali Rohár | ac91b47 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 635 | |
| 636 | if (delay_str) |
| 637 | delay = (int)simple_strtol(delay_str, NULL, 10); |
| 638 | |
| 639 | bootmenu_show(delay); |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | U_BOOT_CMD( |
| 644 | bootmenu, 2, 1, do_bootmenu, |
| 645 | "ANSI terminal bootmenu", |
| 646 | "[delay]\n" |
| 647 | " - show ANSI terminal bootmenu with autoboot delay" |
| 648 | ); |