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> |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 7 | #include <common.h> |
| 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 | { |
| 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ár | 83a287a | 2020-12-27 01:04:38 +0100 | [diff] [blame] | 118 | 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ár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 124 | default: |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /* never happens */ |
| 130 | debug("bootmenu: this should not happen"); |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | static 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 Kojima | a3d0aa8 | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 149 | /** |
| 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 | */ |
| 161 | static int prepare_bootmenu_entry(struct bootmenu_data *menu, |
| 162 | struct bootmenu_entry **current, |
| 163 | unsigned short int *index) |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 164 | { |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 165 | char *sep; |
Masahisa Kojima | a3d0aa8 | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 166 | const char *option; |
| 167 | unsigned short int i = *index; |
| 168 | struct bootmenu_entry *entry = NULL; |
| 169 | struct bootmenu_entry *iter = *current; |
Frank Wunderlich | f7bb20a | 2018-10-05 11:41:59 +0200 | [diff] [blame] | 170 | |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 171 | while ((option = bootmenu_getoption(i))) { |
Masahisa Kojima | a3d0aa8 | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 172 | |
Masahisa Kojima | e85727d | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 173 | /* bootmenu_[num] format is "[title]=[commands]" */ |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 174 | sep = strchr(option, '='); |
| 175 | if (!sep) { |
| 176 | printf("Invalid bootmenu entry: %s\n", option); |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | entry = malloc(sizeof(struct bootmenu_entry)); |
| 181 | if (!entry) |
Masahisa Kojima | a3d0aa8 | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 182 | return -ENOMEM; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 183 | |
Masahisa Kojima | e85727d | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 184 | entry->title = strndup(option, sep - option); |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 185 | if (!entry->title) { |
| 186 | free(entry); |
Masahisa Kojima | a3d0aa8 | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 187 | return -ENOMEM; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 188 | } |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 189 | |
Masahisa Kojima | e85727d | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 190 | entry->command = strdup(sep + 1); |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 191 | if (!entry->command) { |
| 192 | free(entry->title); |
| 193 | free(entry); |
Masahisa Kojima | a3d0aa8 | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 194 | return -ENOMEM; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 195 | } |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 196 | |
| 197 | sprintf(entry->key, "%d", i); |
| 198 | |
| 199 | entry->num = i; |
| 200 | entry->menu = menu; |
Masahisa Kojima | a3d0aa8 | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 201 | entry->type = BOOTMENU_TYPE_BOOTMENU; |
| 202 | entry->bootorder = i; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 203 | entry->next = NULL; |
| 204 | |
| 205 | if (!iter) |
| 206 | menu->first = entry; |
| 207 | else |
| 208 | iter->next = entry; |
| 209 | |
| 210 | iter = entry; |
| 211 | ++i; |
| 212 | |
| 213 | if (i == MAX_COUNT - 1) |
| 214 | break; |
| 215 | } |
| 216 | |
Masahisa Kojima | a3d0aa8 | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 217 | *index = i; |
| 218 | *current = iter; |
| 219 | |
| 220 | return 1; |
| 221 | } |
| 222 | |
Masahisa Kojima | c416f1c | 2022-09-12 17:33:54 +0900 | [diff] [blame^] | 223 | #if (CONFIG_IS_ENABLED(CMD_BOOTEFI_BOOTMGR)) && (CONFIG_IS_ENABLED(CMD_EFICONFIG)) |
Masahisa Kojima | c606c00 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 224 | /** |
| 225 | * prepare_uefi_bootorder_entry() - generate the uefi bootmenu entries |
| 226 | * |
| 227 | * This function read the "BootOrder" UEFI variable |
| 228 | * and generate the bootmenu entries in the order of "BootOrder". |
| 229 | * |
| 230 | * @menu: pointer to the bootmenu structure |
| 231 | * @current: pointer to the last bootmenu entry list |
| 232 | * @index: pointer to the index of the last bootmenu entry, |
| 233 | * the number of uefi entry is added by this function |
| 234 | * Return: 1 on success, negative value on error |
| 235 | */ |
| 236 | static int prepare_uefi_bootorder_entry(struct bootmenu_data *menu, |
| 237 | struct bootmenu_entry **current, |
| 238 | unsigned short int *index) |
| 239 | { |
| 240 | u16 *bootorder; |
| 241 | efi_status_t ret; |
| 242 | unsigned short j; |
| 243 | efi_uintn_t num, size; |
| 244 | void *load_option; |
| 245 | struct efi_load_option lo; |
| 246 | u16 varname[] = u"Boot####"; |
| 247 | unsigned short int i = *index; |
| 248 | struct bootmenu_entry *entry = NULL; |
| 249 | struct bootmenu_entry *iter = *current; |
| 250 | |
| 251 | bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); |
| 252 | if (!bootorder) |
| 253 | return -ENOENT; |
| 254 | |
| 255 | num = size / sizeof(u16); |
| 256 | for (j = 0; j < num; j++) { |
| 257 | entry = malloc(sizeof(struct bootmenu_entry)); |
| 258 | if (!entry) |
| 259 | return -ENOMEM; |
| 260 | |
| 261 | efi_create_indexed_name(varname, sizeof(varname), |
| 262 | "Boot", bootorder[j]); |
| 263 | load_option = efi_get_var(varname, &efi_global_variable_guid, &size); |
| 264 | if (!load_option) |
| 265 | continue; |
| 266 | |
| 267 | ret = efi_deserialize_load_option(&lo, load_option, &size); |
| 268 | if (ret != EFI_SUCCESS) { |
| 269 | log_warning("Invalid load option for %ls\n", varname); |
| 270 | free(load_option); |
| 271 | free(entry); |
| 272 | continue; |
| 273 | } |
| 274 | |
| 275 | if (lo.attributes & LOAD_OPTION_ACTIVE) { |
Masahisa Kojima | e85727d | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 276 | char *buf; |
| 277 | |
| 278 | buf = calloc(1, utf16_utf8_strlen(lo.label) + 1); |
| 279 | if (!buf) { |
Masahisa Kojima | c606c00 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 280 | free(load_option); |
| 281 | free(entry); |
| 282 | free(bootorder); |
| 283 | return -ENOMEM; |
| 284 | } |
Masahisa Kojima | e85727d | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 285 | entry->title = buf; |
| 286 | utf16_utf8_strncpy(&buf, lo.label, u16_strlen(lo.label)); |
Masahisa Kojima | c606c00 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 287 | entry->command = strdup("bootefi bootmgr"); |
| 288 | sprintf(entry->key, "%d", i); |
| 289 | entry->num = i; |
| 290 | entry->menu = menu; |
| 291 | entry->type = BOOTMENU_TYPE_UEFI_BOOT_OPTION; |
| 292 | entry->bootorder = bootorder[j]; |
| 293 | entry->next = NULL; |
| 294 | |
| 295 | if (!iter) |
| 296 | menu->first = entry; |
| 297 | else |
| 298 | iter->next = entry; |
| 299 | |
| 300 | iter = entry; |
| 301 | i++; |
| 302 | } |
| 303 | |
| 304 | free(load_option); |
| 305 | |
| 306 | if (i == MAX_COUNT - 1) |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | free(bootorder); |
| 311 | *index = i; |
| 312 | *current = iter; |
| 313 | |
| 314 | return 1; |
| 315 | } |
Masahisa Kojima | e85727d | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 316 | #endif |
Masahisa Kojima | c606c00 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 317 | |
Masahisa Kojima | a3d0aa8 | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 318 | static struct bootmenu_data *bootmenu_create(int delay) |
| 319 | { |
| 320 | int ret; |
| 321 | unsigned short int i = 0; |
| 322 | struct bootmenu_data *menu; |
| 323 | struct bootmenu_entry *iter = NULL; |
| 324 | struct bootmenu_entry *entry; |
| 325 | char *default_str; |
| 326 | |
| 327 | menu = malloc(sizeof(struct bootmenu_data)); |
| 328 | if (!menu) |
| 329 | return NULL; |
| 330 | |
| 331 | menu->delay = delay; |
| 332 | menu->active = 0; |
| 333 | menu->first = NULL; |
| 334 | |
| 335 | default_str = env_get("bootmenu_default"); |
| 336 | if (default_str) |
| 337 | menu->active = (int)simple_strtol(default_str, NULL, 10); |
| 338 | |
| 339 | ret = prepare_bootmenu_entry(menu, &iter, &i); |
| 340 | if (ret < 0) |
| 341 | goto cleanup; |
| 342 | |
Masahisa Kojima | c416f1c | 2022-09-12 17:33:54 +0900 | [diff] [blame^] | 343 | #if (CONFIG_IS_ENABLED(CMD_BOOTEFI_BOOTMGR)) && (CONFIG_IS_ENABLED(CMD_EFICONFIG)) |
Masahisa Kojima | e85727d | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 344 | if (i < MAX_COUNT - 1) { |
Masahisa Kojima | c416f1c | 2022-09-12 17:33:54 +0900 | [diff] [blame^] | 345 | efi_status_t efi_ret; |
| 346 | |
| 347 | /* |
| 348 | * UEFI specification requires booting from removal media using |
| 349 | * a architecture-specific default image name such as BOOTAA64.EFI. |
| 350 | */ |
| 351 | efi_ret = eficonfig_generate_media_device_boot_option(); |
| 352 | if (efi_ret != EFI_SUCCESS && efi_ret != EFI_NOT_FOUND) |
| 353 | goto cleanup; |
| 354 | |
| 355 | ret = prepare_uefi_bootorder_entry(menu, &iter, &i); |
| 356 | if (ret < 0 && ret != -ENOENT) |
| 357 | goto cleanup; |
Masahisa Kojima | c606c00 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 358 | } |
Masahisa Kojima | e85727d | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 359 | #endif |
Masahisa Kojima | c606c00 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 360 | |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 361 | /* Add U-Boot console entry at the end */ |
| 362 | if (i <= MAX_COUNT - 1) { |
| 363 | entry = malloc(sizeof(struct bootmenu_entry)); |
| 364 | if (!entry) |
| 365 | goto cleanup; |
| 366 | |
Masahisa Kojima | 2158b0d | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 367 | /* Add Quit entry if entering U-Boot console is disabled */ |
Masahisa Kojima | 83f7363 | 2022-05-26 19:09:38 +0900 | [diff] [blame] | 368 | if (!IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE)) |
Masahisa Kojima | e85727d | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 369 | entry->title = strdup("U-Boot console"); |
Masahisa Kojima | 2158b0d | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 370 | else |
Masahisa Kojima | e85727d | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 371 | entry->title = strdup("Quit"); |
Masahisa Kojima | 2158b0d | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 372 | |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 373 | if (!entry->title) { |
| 374 | free(entry); |
| 375 | goto cleanup; |
| 376 | } |
| 377 | |
| 378 | entry->command = strdup(""); |
| 379 | if (!entry->command) { |
| 380 | free(entry->title); |
| 381 | free(entry); |
| 382 | goto cleanup; |
| 383 | } |
| 384 | |
| 385 | sprintf(entry->key, "%d", i); |
| 386 | |
| 387 | entry->num = i; |
| 388 | entry->menu = menu; |
Masahisa Kojima | a3d0aa8 | 2022-04-28 17:09:41 +0900 | [diff] [blame] | 389 | entry->type = BOOTMENU_TYPE_NONE; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 390 | entry->next = NULL; |
| 391 | |
| 392 | if (!iter) |
| 393 | menu->first = entry; |
| 394 | else |
| 395 | iter->next = entry; |
| 396 | |
| 397 | iter = entry; |
| 398 | ++i; |
| 399 | } |
| 400 | |
| 401 | menu->count = i; |
Frank Wunderlich | bace221 | 2018-12-03 11:23:41 +0100 | [diff] [blame] | 402 | |
| 403 | if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu |
| 404 | printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1); |
| 405 | menu->active=0; |
| 406 | } |
| 407 | |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 408 | return menu; |
| 409 | |
| 410 | cleanup: |
| 411 | bootmenu_destroy(menu); |
| 412 | return NULL; |
| 413 | } |
| 414 | |
Thirupathaiah Annapureddy | 5168d7a | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 415 | static void menu_display_statusline(struct menu *m) |
| 416 | { |
| 417 | struct bootmenu_entry *entry; |
| 418 | struct bootmenu_data *menu; |
| 419 | |
| 420 | if (menu_default_choice(m, (void *)&entry) < 0) |
| 421 | return; |
| 422 | |
| 423 | menu = entry->menu; |
| 424 | |
| 425 | printf(ANSI_CURSOR_POSITION, 1, 1); |
| 426 | puts(ANSI_CLEAR_LINE); |
Heinrich Schuchardt | d1d7ed7 | 2022-05-01 23:17:18 +0200 | [diff] [blame] | 427 | printf(ANSI_CURSOR_POSITION, 2, 3); |
| 428 | puts("*** U-Boot Boot Menu ***"); |
Thirupathaiah Annapureddy | 5168d7a | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 429 | puts(ANSI_CLEAR_LINE_TO_END); |
| 430 | printf(ANSI_CURSOR_POSITION, 3, 1); |
| 431 | puts(ANSI_CLEAR_LINE); |
| 432 | |
| 433 | /* First 3 lines are bootmenu header + 2 empty lines between entries */ |
| 434 | printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); |
| 435 | puts(ANSI_CLEAR_LINE); |
Heinrich Schuchardt | d1d7ed7 | 2022-05-01 23:17:18 +0200 | [diff] [blame] | 436 | printf(ANSI_CURSOR_POSITION, menu->count + 6, 3); |
| 437 | puts("Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit"); |
Thirupathaiah Annapureddy | 5168d7a | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 438 | puts(ANSI_CLEAR_LINE_TO_END); |
| 439 | printf(ANSI_CURSOR_POSITION, menu->count + 7, 1); |
| 440 | puts(ANSI_CLEAR_LINE); |
| 441 | } |
| 442 | |
Masahisa Kojima | c606c00 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 443 | static void handle_uefi_bootnext(void) |
| 444 | { |
| 445 | u16 bootnext; |
| 446 | efi_status_t ret; |
| 447 | efi_uintn_t size; |
| 448 | |
| 449 | /* Initialize EFI drivers */ |
| 450 | ret = efi_init_obj_list(); |
| 451 | if (ret != EFI_SUCCESS) { |
| 452 | log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n", |
| 453 | ret & ~EFI_ERROR_MASK); |
| 454 | |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | /* If UEFI BootNext variable is set, boot the BootNext load option */ |
| 459 | size = sizeof(u16); |
| 460 | ret = efi_get_variable_int(u"BootNext", |
| 461 | &efi_global_variable_guid, |
| 462 | NULL, &size, &bootnext, NULL); |
| 463 | if (ret == EFI_SUCCESS) |
| 464 | /* BootNext does exist here, try to boot */ |
| 465 | run_command("bootefi bootmgr", 0); |
| 466 | } |
| 467 | |
Masahisa Kojima | 2158b0d | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 468 | static enum bootmenu_ret bootmenu_show(int delay) |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 469 | { |
Masahisa Kojima | 2158b0d | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 470 | int cmd_ret; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 471 | int init = 0; |
| 472 | void *choice = NULL; |
Masahisa Kojima | e85727d | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 473 | char *title = NULL; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 474 | char *command = NULL; |
| 475 | struct menu *menu; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 476 | struct bootmenu_entry *iter; |
Masahisa Kojima | 2158b0d | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 477 | int ret = BOOTMENU_RET_SUCCESS; |
| 478 | struct bootmenu_data *bootmenu; |
Masahisa Kojima | c606c00 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 479 | efi_status_t efi_ret = EFI_SUCCESS; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 480 | char *option, *sep; |
| 481 | |
Masahisa Kojima | c606c00 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 482 | if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) |
| 483 | handle_uefi_bootnext(); |
| 484 | |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 485 | /* If delay is 0 do not create menu, just run first entry */ |
| 486 | if (delay == 0) { |
| 487 | option = bootmenu_getoption(0); |
| 488 | if (!option) { |
| 489 | puts("bootmenu option 0 was not found\n"); |
Masahisa Kojima | 2158b0d | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 490 | return BOOTMENU_RET_FAIL; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 491 | } |
| 492 | sep = strchr(option, '='); |
| 493 | if (!sep) { |
| 494 | puts("bootmenu option 0 is invalid\n"); |
Masahisa Kojima | 2158b0d | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 495 | return BOOTMENU_RET_FAIL; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 496 | } |
Masahisa Kojima | 2158b0d | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 497 | cmd_ret = run_command(sep + 1, 0); |
| 498 | 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] | 499 | } |
| 500 | |
| 501 | bootmenu = bootmenu_create(delay); |
| 502 | if (!bootmenu) |
Masahisa Kojima | 2158b0d | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 503 | return BOOTMENU_RET_FAIL; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 504 | |
Thirupathaiah Annapureddy | 5168d7a | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 505 | menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline, |
| 506 | bootmenu_print_entry, bootmenu_choice_entry, |
| 507 | bootmenu); |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 508 | if (!menu) { |
| 509 | bootmenu_destroy(bootmenu); |
Masahisa Kojima | 2158b0d | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 510 | return BOOTMENU_RET_FAIL; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | for (iter = bootmenu->first; iter; iter = iter->next) { |
Masahisa Kojima | 990f663 | 2022-03-24 22:54:33 +0900 | [diff] [blame] | 514 | if (menu_item_add(menu, iter->key, iter) != 1) |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 515 | goto cleanup; |
| 516 | } |
| 517 | |
| 518 | /* Default menu entry is always first */ |
| 519 | menu_default_set(menu, "0"); |
| 520 | |
| 521 | puts(ANSI_CURSOR_HIDE); |
| 522 | puts(ANSI_CLEAR_CONSOLE); |
| 523 | printf(ANSI_CURSOR_POSITION, 1, 1); |
| 524 | |
| 525 | init = 1; |
| 526 | |
Masahisa Kojima | 990f663 | 2022-03-24 22:54:33 +0900 | [diff] [blame] | 527 | if (menu_get_choice(menu, &choice) == 1) { |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 528 | iter = choice; |
Masahisa Kojima | e85727d | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 529 | title = strdup(iter->title); |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 530 | command = strdup(iter->command); |
Masahisa Kojima | 2158b0d | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 531 | |
| 532 | /* last entry is U-Boot console or Quit */ |
| 533 | if (iter->num == iter->menu->count - 1) { |
| 534 | ret = BOOTMENU_RET_QUIT; |
| 535 | goto cleanup; |
| 536 | } |
| 537 | } else { |
| 538 | goto cleanup; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Masahisa Kojima | c606c00 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 541 | /* |
| 542 | * If the selected entry is UEFI BOOT####, set the BootNext variable. |
| 543 | * Then uefi bootmgr is invoked by the preset command in iter->command. |
| 544 | */ |
| 545 | if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) { |
| 546 | if (iter->type == BOOTMENU_TYPE_UEFI_BOOT_OPTION) { |
| 547 | /* |
| 548 | * UEFI specification requires BootNext variable needs non-volatile |
| 549 | * attribute, but this BootNext is only used inside of U-Boot and |
| 550 | * removed by efi bootmgr once BootNext is processed. |
| 551 | * So this BootNext can be volatile. |
| 552 | */ |
| 553 | efi_ret = efi_set_variable_int(u"BootNext", &efi_global_variable_guid, |
| 554 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 555 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 556 | sizeof(u16), &iter->bootorder, false); |
| 557 | if (efi_ret != EFI_SUCCESS) |
| 558 | goto cleanup; |
| 559 | } |
| 560 | } |
| 561 | |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 562 | cleanup: |
| 563 | menu_destroy(menu); |
| 564 | bootmenu_destroy(bootmenu); |
| 565 | |
| 566 | if (init) { |
| 567 | puts(ANSI_CURSOR_SHOW); |
| 568 | puts(ANSI_CLEAR_CONSOLE); |
| 569 | printf(ANSI_CURSOR_POSITION, 1, 1); |
| 570 | } |
| 571 | |
| 572 | if (title && command) { |
Masahisa Kojima | e85727d | 2022-05-29 10:52:43 +0900 | [diff] [blame] | 573 | debug("Starting entry '%s'\n", title); |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 574 | free(title); |
Masahisa Kojima | c606c00 | 2022-04-28 17:09:42 +0900 | [diff] [blame] | 575 | if (efi_ret == EFI_SUCCESS) |
Masahisa Kojima | 2158b0d | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 576 | cmd_ret = run_command(command, 0); |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 577 | free(command); |
| 578 | } |
| 579 | |
| 580 | #ifdef CONFIG_POSTBOOTMENU |
| 581 | run_command(CONFIG_POSTBOOTMENU, 0); |
| 582 | #endif |
Masahisa Kojima | 2158b0d | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 583 | |
| 584 | if (efi_ret != EFI_SUCCESS || cmd_ret != CMD_RET_SUCCESS) |
| 585 | ret = BOOTMENU_RET_FAIL; |
| 586 | |
| 587 | return ret; |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Simon Glass | e231306 | 2019-07-20 20:51:24 -0600 | [diff] [blame] | 590 | #ifdef CONFIG_AUTOBOOT_MENU_SHOW |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 591 | int menu_show(int bootdelay) |
| 592 | { |
Masahisa Kojima | 2158b0d | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 593 | int ret; |
| 594 | |
| 595 | while (1) { |
| 596 | ret = bootmenu_show(bootdelay); |
| 597 | bootdelay = -1; |
| 598 | if (ret == BOOTMENU_RET_UPDATED) |
| 599 | continue; |
| 600 | |
Masahisa Kojima | 83f7363 | 2022-05-26 19:09:38 +0900 | [diff] [blame] | 601 | if (IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE)) { |
Masahisa Kojima | 2158b0d | 2022-04-28 17:09:44 +0900 | [diff] [blame] | 602 | if (ret == BOOTMENU_RET_QUIT) { |
| 603 | /* default boot process */ |
| 604 | if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) |
| 605 | run_command("bootefi bootmgr", 0); |
| 606 | |
| 607 | run_command("run bootcmd", 0); |
| 608 | } |
| 609 | } else { |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 614 | return -1; /* -1 - abort boot and run monitor code */ |
| 615 | } |
| 616 | #endif |
| 617 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 618 | 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] | 619 | { |
| 620 | char *delay_str = NULL; |
| 621 | int delay = 10; |
| 622 | |
| 623 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) |
| 624 | delay = CONFIG_BOOTDELAY; |
| 625 | #endif |
| 626 | |
| 627 | if (argc >= 2) |
| 628 | delay_str = argv[1]; |
| 629 | |
| 630 | if (!delay_str) |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 631 | delay_str = env_get("bootmenu_delay"); |
Pali Rohár | e7abe91 | 2013-03-23 14:53:08 +0000 | [diff] [blame] | 632 | |
| 633 | if (delay_str) |
| 634 | delay = (int)simple_strtol(delay_str, NULL, 10); |
| 635 | |
| 636 | bootmenu_show(delay); |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | U_BOOT_CMD( |
| 641 | bootmenu, 2, 1, do_bootmenu, |
| 642 | "ANSI terminal bootmenu", |
| 643 | "[delay]\n" |
| 644 | " - show ANSI terminal bootmenu with autoboot delay" |
| 645 | ); |