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