Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Menu-driven UEFI Variable maintenance |
| 4 | * |
| 5 | * Copyright (c) 2022 Masahisa Kojima, Linaro Limited |
| 6 | */ |
| 7 | |
| 8 | #include <ansi.h> |
| 9 | #include <common.h> |
| 10 | #include <charset.h> |
| 11 | #include <efi_loader.h> |
| 12 | #include <efi_load_initrd.h> |
| 13 | #include <efi_config.h> |
| 14 | #include <efi_variable.h> |
| 15 | #include <log.h> |
| 16 | #include <malloc.h> |
| 17 | #include <menu.h> |
| 18 | #include <sort.h> |
| 19 | #include <watchdog.h> |
| 20 | #include <asm/unaligned.h> |
| 21 | #include <linux/delay.h> |
| 22 | |
| 23 | static struct efi_simple_text_input_protocol *cin; |
| 24 | |
| 25 | #define EFICONFIG_DESCRIPTION_MAX 32 |
| 26 | #define EFICONFIG_OPTIONAL_DATA_MAX 64 |
| 27 | |
| 28 | /** |
| 29 | * struct eficonfig_filepath_info - structure to be used to store file path |
| 30 | * |
| 31 | * @name: file or directory name |
| 32 | * @list: list structure |
| 33 | */ |
| 34 | struct eficonfig_filepath_info { |
| 35 | char *name; |
| 36 | struct list_head list; |
| 37 | }; |
| 38 | |
| 39 | /** |
| 40 | * struct eficonfig_boot_option - structure to be used for updating UEFI boot option |
| 41 | * |
| 42 | * @file_info: user selected file info |
| 43 | * @initrd_info: user selected initrd file info |
| 44 | * @boot_index: index of the boot option |
| 45 | * @description: pointer to the description string |
| 46 | * @optional_data: pointer to the optional_data |
| 47 | * @edit_completed: flag indicates edit complete |
| 48 | */ |
| 49 | struct eficonfig_boot_option { |
| 50 | struct eficonfig_select_file_info file_info; |
| 51 | struct eficonfig_select_file_info initrd_info; |
| 52 | unsigned int boot_index; |
| 53 | u16 *description; |
| 54 | u16 *optional_data; |
| 55 | bool edit_completed; |
| 56 | }; |
| 57 | |
| 58 | /** |
| 59 | * struct eficonfig_volume_entry_data - structure to be used to store volume info |
| 60 | * |
| 61 | * @file_info: pointer to file info structure |
| 62 | * @v: pointer to the protocol interface |
| 63 | * @dp: pointer to the device path |
| 64 | */ |
| 65 | struct eficonfig_volume_entry_data { |
| 66 | struct eficonfig_select_file_info *file_info; |
| 67 | struct efi_simple_file_system_protocol *v; |
| 68 | struct efi_device_path *dp; |
| 69 | }; |
| 70 | |
| 71 | /** |
| 72 | * struct eficonfig_file_entry_data - structure to be used to store file info |
| 73 | * |
| 74 | * @file_info: pointer to file info structure |
| 75 | * @is_directory: flag to identify the directory or file |
| 76 | * @file_name: name of directory or file |
| 77 | */ |
| 78 | struct eficonfig_file_entry_data { |
| 79 | struct eficonfig_select_file_info *file_info; |
| 80 | bool is_directory; |
| 81 | char *file_name; |
| 82 | }; |
| 83 | |
| 84 | /** |
Masahisa Kojima | e34158b | 2022-09-12 17:33:51 +0900 | [diff] [blame] | 85 | * struct eficonfig_boot_selection_data - structure to be used to select the boot option entry |
| 86 | * |
| 87 | * @boot_index: index of the boot option |
| 88 | * @selected: pointer to store the selected index in the BootOrder variable |
| 89 | */ |
| 90 | struct eficonfig_boot_selection_data { |
| 91 | u16 boot_index; |
| 92 | int *selected; |
| 93 | }; |
| 94 | |
| 95 | /** |
Masahisa Kojima | e5948ee | 2022-09-12 17:33:56 +0900 | [diff] [blame] | 96 | * struct eficonfig_boot_order - structure to be used to update BootOrder variable |
| 97 | * |
| 98 | * @num: index in the menu entry |
| 99 | * @description: pointer to the description string |
| 100 | * @boot_index: boot option index |
| 101 | * @active: flag to include the boot option into BootOrder variable |
| 102 | * @list: list structure |
| 103 | */ |
| 104 | struct eficonfig_boot_order { |
| 105 | u32 num; |
| 106 | u16 *description; |
| 107 | u32 boot_index; |
| 108 | bool active; |
| 109 | struct list_head list; |
| 110 | }; |
| 111 | |
| 112 | /** |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 113 | * eficonfig_print_msg() - print message |
| 114 | * |
| 115 | * display the message to the user, user proceeds the screen |
| 116 | * with any key press. |
| 117 | * |
| 118 | * @items: pointer to the structure of each menu entry |
| 119 | * @count: the number of menu entry |
| 120 | * @menu_header: pointer to the menu header string |
| 121 | * Return: status code |
| 122 | */ |
| 123 | void eficonfig_print_msg(char *msg) |
| 124 | { |
| 125 | /* Flush input */ |
| 126 | while (tstc()) |
| 127 | getchar(); |
| 128 | |
| 129 | printf(ANSI_CURSOR_HIDE |
| 130 | ANSI_CLEAR_CONSOLE |
| 131 | ANSI_CURSOR_POSITION |
| 132 | "%s\n\n Press any key to continue", 3, 4, msg); |
| 133 | |
| 134 | getchar(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * eficonfig_print_entry() - print each menu entry |
| 139 | * |
| 140 | * @data: pointer to the data associated with each menu entry |
| 141 | */ |
| 142 | static void eficonfig_print_entry(void *data) |
| 143 | { |
| 144 | struct eficonfig_entry *entry = data; |
| 145 | int reverse = (entry->efi_menu->active == entry->num); |
| 146 | |
| 147 | /* TODO: support scroll or page for many entries */ |
| 148 | |
| 149 | /* |
| 150 | * Move cursor to line where the entry will be drawn (entry->num) |
| 151 | * First 3 lines(menu header) + 1 empty line |
| 152 | */ |
| 153 | printf(ANSI_CURSOR_POSITION, entry->num + 4, 7); |
| 154 | |
| 155 | if (reverse) |
| 156 | puts(ANSI_COLOR_REVERSE); |
| 157 | |
| 158 | printf("%s", entry->title); |
| 159 | |
| 160 | if (reverse) |
| 161 | puts(ANSI_COLOR_RESET); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * eficonfig_display_statusline() - print status line |
| 166 | * |
| 167 | * @m: pointer to the menu structure |
| 168 | */ |
| 169 | static void eficonfig_display_statusline(struct menu *m) |
| 170 | { |
| 171 | struct eficonfig_entry *entry; |
| 172 | |
| 173 | if (menu_default_choice(m, (void *)&entry) < 0) |
| 174 | return; |
| 175 | |
| 176 | printf(ANSI_CURSOR_POSITION |
| 177 | "\n%s\n" |
| 178 | ANSI_CURSOR_POSITION ANSI_CLEAR_LINE ANSI_CURSOR_POSITION |
| 179 | " Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit" |
| 180 | ANSI_CLEAR_LINE_TO_END ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, |
| 181 | 1, 1, entry->efi_menu->menu_header, entry->efi_menu->count + 5, 1, |
| 182 | entry->efi_menu->count + 6, 1, entry->efi_menu->count + 7, 1); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * eficonfig_choice_entry() - user key input handler |
| 187 | * |
| 188 | * @data: pointer to the efimenu structure |
| 189 | * Return: key string to identify the selected entry |
| 190 | */ |
| 191 | static char *eficonfig_choice_entry(void *data) |
| 192 | { |
| 193 | int esc = 0; |
| 194 | struct list_head *pos, *n; |
| 195 | struct eficonfig_entry *entry; |
| 196 | enum bootmenu_key key = KEY_NONE; |
| 197 | struct efimenu *efi_menu = data; |
| 198 | |
| 199 | while (1) { |
| 200 | bootmenu_loop((struct bootmenu_data *)efi_menu, &key, &esc); |
| 201 | |
| 202 | switch (key) { |
| 203 | case KEY_UP: |
| 204 | if (efi_menu->active > 0) |
| 205 | --efi_menu->active; |
| 206 | /* no menu key selected, regenerate menu */ |
| 207 | return NULL; |
| 208 | case KEY_DOWN: |
| 209 | if (efi_menu->active < efi_menu->count - 1) |
| 210 | ++efi_menu->active; |
| 211 | /* no menu key selected, regenerate menu */ |
| 212 | return NULL; |
| 213 | case KEY_SELECT: |
| 214 | list_for_each_safe(pos, n, &efi_menu->list) { |
| 215 | entry = list_entry(pos, struct eficonfig_entry, list); |
| 216 | if (entry->num == efi_menu->active) |
| 217 | return entry->key; |
| 218 | } |
| 219 | break; |
| 220 | case KEY_QUIT: |
| 221 | /* Quit by choosing the last entry */ |
| 222 | entry = list_last_entry(&efi_menu->list, struct eficonfig_entry, list); |
| 223 | return entry->key; |
| 224 | default: |
| 225 | /* Pressed key is not valid, no need to regenerate the menu */ |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * eficonfig_destroy() - destroy efimenu |
| 233 | * |
| 234 | * @efi_menu: pointer to the efimenu structure |
| 235 | */ |
| 236 | void eficonfig_destroy(struct efimenu *efi_menu) |
| 237 | { |
| 238 | struct list_head *pos, *n; |
| 239 | struct eficonfig_entry *entry; |
| 240 | |
| 241 | if (!efi_menu) |
| 242 | return; |
| 243 | |
| 244 | list_for_each_safe(pos, n, &efi_menu->list) { |
| 245 | entry = list_entry(pos, struct eficonfig_entry, list); |
| 246 | free(entry->title); |
| 247 | list_del(&entry->list); |
| 248 | free(entry); |
| 249 | } |
| 250 | free(efi_menu->menu_header); |
| 251 | free(efi_menu); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * eficonfig_process_quit() - callback function for "Quit" entry |
| 256 | * |
| 257 | * @data: pointer to the data |
| 258 | * Return: status code |
| 259 | */ |
| 260 | efi_status_t eficonfig_process_quit(void *data) |
| 261 | { |
| 262 | return EFI_ABORTED; |
| 263 | } |
| 264 | |
| 265 | /** |
Masahisa Kojima | 8961e93 | 2022-11-20 09:21:14 +0900 | [diff] [blame^] | 266 | * eficonfig_append_menu_entry() - append menu item |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 267 | * |
| 268 | * @efi_menu: pointer to the efimenu structure |
| 269 | * @title: pointer to the entry title |
| 270 | * @func: callback of each entry |
| 271 | * @data: pointer to the data to be passed to each entry callback |
| 272 | * Return: status code |
| 273 | */ |
Masahisa Kojima | 8961e93 | 2022-11-20 09:21:14 +0900 | [diff] [blame^] | 274 | efi_status_t eficonfig_append_menu_entry(struct efimenu *efi_menu, |
| 275 | char *title, eficonfig_entry_func func, |
| 276 | void *data) |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 277 | { |
| 278 | struct eficonfig_entry *entry; |
| 279 | |
| 280 | if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX) |
| 281 | return EFI_OUT_OF_RESOURCES; |
| 282 | |
| 283 | entry = calloc(1, sizeof(struct eficonfig_entry)); |
| 284 | if (!entry) |
| 285 | return EFI_OUT_OF_RESOURCES; |
| 286 | |
| 287 | entry->title = title; |
| 288 | sprintf(entry->key, "%d", efi_menu->count); |
| 289 | entry->efi_menu = efi_menu; |
| 290 | entry->func = func; |
| 291 | entry->data = data; |
| 292 | entry->num = efi_menu->count++; |
| 293 | list_add_tail(&entry->list, &efi_menu->list); |
| 294 | |
| 295 | return EFI_SUCCESS; |
| 296 | } |
| 297 | |
| 298 | /** |
Masahisa Kojima | 8961e93 | 2022-11-20 09:21:14 +0900 | [diff] [blame^] | 299 | * eficonfig_append_quit_entry() - append quit entry |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 300 | * |
| 301 | * @efi_menu: pointer to the efimenu structure |
| 302 | * Return: status code |
| 303 | */ |
Masahisa Kojima | 8961e93 | 2022-11-20 09:21:14 +0900 | [diff] [blame^] | 304 | efi_status_t eficonfig_append_quit_entry(struct efimenu *efi_menu) |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 305 | { |
| 306 | char *title; |
| 307 | efi_status_t ret; |
| 308 | |
| 309 | title = strdup("Quit"); |
| 310 | if (!title) |
| 311 | return EFI_OUT_OF_RESOURCES; |
| 312 | |
Masahisa Kojima | 8961e93 | 2022-11-20 09:21:14 +0900 | [diff] [blame^] | 313 | ret = eficonfig_append_menu_entry(efi_menu, title, eficonfig_process_quit, NULL); |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 314 | if (ret != EFI_SUCCESS) |
| 315 | free(title); |
| 316 | |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * eficonfig_create_fixed_menu() - create fixed entry menu structure |
| 322 | * |
| 323 | * @items: pointer to the menu entry item |
| 324 | * @count: the number of menu entry |
| 325 | * Return: pointer to the efimenu structure |
| 326 | */ |
| 327 | void *eficonfig_create_fixed_menu(const struct eficonfig_item *items, int count) |
| 328 | { |
| 329 | u32 i; |
| 330 | char *title; |
| 331 | efi_status_t ret; |
| 332 | struct efimenu *efi_menu; |
| 333 | const struct eficonfig_item *iter = items; |
| 334 | |
| 335 | efi_menu = calloc(1, sizeof(struct efimenu)); |
| 336 | if (!efi_menu) |
| 337 | return NULL; |
| 338 | |
| 339 | INIT_LIST_HEAD(&efi_menu->list); |
| 340 | for (i = 0; i < count; i++, iter++) { |
| 341 | title = strdup(iter->title); |
| 342 | if (!title) |
| 343 | goto out; |
| 344 | |
Masahisa Kojima | 8961e93 | 2022-11-20 09:21:14 +0900 | [diff] [blame^] | 345 | ret = eficonfig_append_menu_entry(efi_menu, title, iter->func, iter->data); |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 346 | if (ret != EFI_SUCCESS) { |
| 347 | free(title); |
| 348 | goto out; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | return efi_menu; |
| 353 | out: |
| 354 | eficonfig_destroy(efi_menu); |
| 355 | |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * eficonfig_process_common() - main handler for UEFI menu |
| 361 | * |
| 362 | * Construct the structures required to show the menu, then handle |
| 363 | * the user input interacting with u-boot menu functions. |
| 364 | * |
| 365 | * @efi_menu: pointer to the efimenu structure |
| 366 | * @menu_header: pointer to the menu header string |
| 367 | * Return: status code |
| 368 | */ |
| 369 | efi_status_t eficonfig_process_common(struct efimenu *efi_menu, char *menu_header) |
| 370 | { |
| 371 | struct menu *menu; |
| 372 | void *choice = NULL; |
| 373 | struct list_head *pos, *n; |
| 374 | struct eficonfig_entry *entry; |
| 375 | efi_status_t ret = EFI_SUCCESS; |
| 376 | |
| 377 | if (efi_menu->count > EFICONFIG_ENTRY_NUM_MAX) |
| 378 | return EFI_OUT_OF_RESOURCES; |
| 379 | |
| 380 | efi_menu->delay = -1; |
| 381 | efi_menu->active = 0; |
| 382 | |
| 383 | if (menu_header) { |
| 384 | efi_menu->menu_header = strdup(menu_header); |
| 385 | if (!efi_menu->menu_header) |
| 386 | return EFI_OUT_OF_RESOURCES; |
| 387 | } |
| 388 | |
| 389 | menu = menu_create(NULL, 0, 1, eficonfig_display_statusline, |
| 390 | eficonfig_print_entry, eficonfig_choice_entry, |
| 391 | efi_menu); |
| 392 | if (!menu) |
| 393 | return EFI_INVALID_PARAMETER; |
| 394 | |
| 395 | list_for_each_safe(pos, n, &efi_menu->list) { |
| 396 | entry = list_entry(pos, struct eficonfig_entry, list); |
| 397 | if (!menu_item_add(menu, entry->key, entry)) { |
| 398 | ret = EFI_INVALID_PARAMETER; |
| 399 | goto out; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | entry = list_first_entry_or_null(&efi_menu->list, struct eficonfig_entry, list); |
| 404 | if (entry) |
| 405 | menu_default_set(menu, entry->key); |
| 406 | |
| 407 | printf(ANSI_CURSOR_HIDE |
| 408 | ANSI_CLEAR_CONSOLE |
| 409 | ANSI_CURSOR_POSITION, 1, 1); |
| 410 | |
| 411 | if (menu_get_choice(menu, &choice)) { |
| 412 | entry = choice; |
| 413 | if (entry->func) |
| 414 | ret = entry->func(entry->data); |
| 415 | } |
| 416 | out: |
| 417 | menu_destroy(menu); |
| 418 | |
| 419 | printf(ANSI_CLEAR_CONSOLE |
| 420 | ANSI_CURSOR_POSITION |
| 421 | ANSI_CURSOR_SHOW, 1, 1); |
| 422 | |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * eficonfig_volume_selected() - handler of volume selection |
| 428 | * |
| 429 | * @data: pointer to the data of selected entry |
| 430 | * Return: status code |
| 431 | */ |
| 432 | static efi_status_t eficonfig_volume_selected(void *data) |
| 433 | { |
| 434 | struct eficonfig_volume_entry_data *info = data; |
| 435 | |
| 436 | if (info) { |
| 437 | info->file_info->current_volume = info->v; |
| 438 | info->file_info->dp_volume = info->dp; |
| 439 | } |
| 440 | |
| 441 | return EFI_SUCCESS; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * create_selected_device_path() - create device path |
| 446 | * |
| 447 | * @file_info: pointer to the selected file information |
| 448 | * Return: |
| 449 | * device path or NULL. Caller must free the returned value |
| 450 | */ |
| 451 | static |
| 452 | struct efi_device_path *create_selected_device_path(struct eficonfig_select_file_info *file_info) |
| 453 | { |
| 454 | char *p; |
| 455 | void *buf; |
| 456 | efi_uintn_t fp_size; |
| 457 | struct efi_device_path *dp; |
| 458 | struct efi_device_path_file_path *fp; |
| 459 | |
| 460 | fp_size = sizeof(struct efi_device_path) + |
| 461 | ((u16_strlen(file_info->current_path) + 1) * sizeof(u16)); |
| 462 | buf = calloc(1, fp_size + sizeof(END)); |
| 463 | if (!buf) |
| 464 | return NULL; |
| 465 | |
| 466 | fp = buf; |
| 467 | fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE, |
| 468 | fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH, |
| 469 | fp->dp.length = (u16)fp_size; |
| 470 | u16_strcpy(fp->str, file_info->current_path); |
| 471 | |
| 472 | p = buf; |
| 473 | p += fp_size; |
| 474 | *((struct efi_device_path *)p) = END; |
| 475 | |
| 476 | dp = efi_dp_append(file_info->dp_volume, (struct efi_device_path *)buf); |
| 477 | free(buf); |
| 478 | |
| 479 | return dp; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * eficonfig_file_selected() - handler of file selection |
| 484 | * |
| 485 | * @data: pointer to the data of selected entry |
| 486 | * Return: status code |
| 487 | */ |
| 488 | static efi_status_t eficonfig_file_selected(void *data) |
| 489 | { |
| 490 | u16 *tmp; |
| 491 | struct eficonfig_file_entry_data *info = data; |
| 492 | |
| 493 | if (!info) |
| 494 | return EFI_INVALID_PARAMETER; |
| 495 | |
| 496 | if (!strcmp(info->file_name, "..")) { |
| 497 | struct eficonfig_filepath_info *iter; |
| 498 | struct list_head *pos, *n; |
| 499 | int is_last; |
| 500 | char *filepath; |
| 501 | tmp = info->file_info->current_path; |
| 502 | |
| 503 | memset(info->file_info->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE); |
| 504 | filepath = calloc(1, EFICONFIG_FILE_PATH_MAX); |
| 505 | if (!filepath) |
| 506 | return EFI_OUT_OF_RESOURCES; |
| 507 | |
| 508 | list_for_each_safe(pos, n, &info->file_info->filepath_list) { |
| 509 | iter = list_entry(pos, struct eficonfig_filepath_info, list); |
| 510 | |
| 511 | is_last = list_is_last(&iter->list, &info->file_info->filepath_list); |
| 512 | if (is_last) { |
| 513 | list_del(&iter->list); |
| 514 | free(iter->name); |
| 515 | free(iter); |
| 516 | break; |
| 517 | } |
| 518 | strlcat(filepath, iter->name, EFICONFIG_FILE_PATH_MAX); |
| 519 | } |
| 520 | utf8_utf16_strcpy(&tmp, filepath); |
| 521 | } else { |
| 522 | size_t new_len; |
| 523 | struct eficonfig_filepath_info *filepath_info; |
| 524 | |
| 525 | new_len = u16_strlen(info->file_info->current_path) + |
| 526 | strlen(info->file_name); |
| 527 | if (new_len >= EFICONFIG_FILE_PATH_MAX) { |
| 528 | eficonfig_print_msg("File path is too long!"); |
| 529 | return EFI_INVALID_PARAMETER; |
| 530 | } |
| 531 | tmp = &info->file_info->current_path[u16_strlen(info->file_info->current_path)]; |
| 532 | utf8_utf16_strcpy(&tmp, info->file_name); |
| 533 | |
| 534 | filepath_info = calloc(1, sizeof(struct eficonfig_filepath_info)); |
| 535 | if (!filepath_info) |
| 536 | return EFI_OUT_OF_RESOURCES; |
| 537 | |
| 538 | filepath_info->name = strdup(info->file_name); |
| 539 | if (!filepath_info->name) { |
| 540 | free(filepath_info); |
| 541 | return EFI_OUT_OF_RESOURCES; |
| 542 | } |
| 543 | list_add_tail(&filepath_info->list, &info->file_info->filepath_list); |
| 544 | |
| 545 | if (!info->is_directory) |
| 546 | info->file_info->file_selected = true; |
| 547 | } |
| 548 | |
| 549 | return EFI_SUCCESS; |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * eficonfig_select_volume() - construct the volume selection menu |
| 554 | * |
| 555 | * @file_info: pointer to the file selection structure |
| 556 | * Return: status code |
| 557 | */ |
| 558 | static efi_status_t eficonfig_select_volume(struct eficonfig_select_file_info *file_info) |
| 559 | { |
| 560 | u32 i; |
| 561 | efi_status_t ret; |
| 562 | efi_uintn_t count; |
| 563 | struct efimenu *efi_menu; |
| 564 | struct list_head *pos, *n; |
| 565 | struct efi_handler *handler; |
| 566 | struct eficonfig_entry *entry; |
| 567 | struct efi_device_path *device_path; |
| 568 | efi_handle_t *volume_handles = NULL; |
| 569 | struct efi_simple_file_system_protocol *v; |
| 570 | |
| 571 | ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid, |
| 572 | NULL, &count, (efi_handle_t **)&volume_handles); |
| 573 | if (ret != EFI_SUCCESS) { |
| 574 | eficonfig_print_msg("No block device found!"); |
| 575 | return ret; |
| 576 | } |
| 577 | |
| 578 | efi_menu = calloc(1, sizeof(struct efimenu)); |
| 579 | if (!efi_menu) |
| 580 | return EFI_OUT_OF_RESOURCES; |
| 581 | |
| 582 | INIT_LIST_HEAD(&efi_menu->list); |
| 583 | for (i = 0; i < count; i++) { |
| 584 | char *devname; |
| 585 | struct efi_block_io *block_io; |
| 586 | struct eficonfig_volume_entry_data *info; |
| 587 | |
| 588 | if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1) |
| 589 | break; |
| 590 | |
| 591 | ret = efi_search_protocol(volume_handles[i], |
| 592 | &efi_simple_file_system_protocol_guid, &handler); |
| 593 | if (ret != EFI_SUCCESS) |
| 594 | continue; |
| 595 | ret = efi_protocol_open(handler, (void **)&v, efi_root, NULL, |
| 596 | EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 597 | if (ret != EFI_SUCCESS) |
| 598 | continue; |
| 599 | |
| 600 | ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler); |
| 601 | if (ret != EFI_SUCCESS) |
| 602 | continue; |
| 603 | ret = efi_protocol_open(handler, (void **)&device_path, |
| 604 | efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 605 | if (ret != EFI_SUCCESS) |
| 606 | continue; |
| 607 | |
| 608 | ret = efi_search_protocol(volume_handles[i], &efi_block_io_guid, &handler); |
| 609 | if (ret != EFI_SUCCESS) |
| 610 | continue; |
| 611 | ret = efi_protocol_open(handler, (void **)&block_io, |
| 612 | efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 613 | if (ret != EFI_SUCCESS) |
| 614 | continue; |
| 615 | |
| 616 | info = calloc(1, sizeof(struct eficonfig_volume_entry_data)); |
| 617 | if (!info) { |
| 618 | ret = EFI_OUT_OF_RESOURCES; |
| 619 | goto out; |
| 620 | } |
| 621 | |
| 622 | devname = calloc(1, BOOTMENU_DEVICE_NAME_MAX); |
| 623 | if (!devname) { |
| 624 | free(info); |
| 625 | ret = EFI_OUT_OF_RESOURCES; |
| 626 | goto out; |
| 627 | } |
| 628 | ret = efi_disk_get_device_name(volume_handles[i], devname, |
| 629 | BOOTMENU_DEVICE_NAME_MAX); |
| 630 | if (ret != EFI_SUCCESS) { |
| 631 | free(info); |
| 632 | goto out; |
| 633 | } |
| 634 | |
| 635 | info->v = v; |
| 636 | info->dp = device_path; |
| 637 | info->file_info = file_info; |
Masahisa Kojima | 8961e93 | 2022-11-20 09:21:14 +0900 | [diff] [blame^] | 638 | ret = eficonfig_append_menu_entry(efi_menu, devname, eficonfig_volume_selected, |
| 639 | info); |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 640 | if (ret != EFI_SUCCESS) { |
| 641 | free(info); |
| 642 | goto out; |
| 643 | } |
| 644 | } |
| 645 | |
Masahisa Kojima | 8961e93 | 2022-11-20 09:21:14 +0900 | [diff] [blame^] | 646 | ret = eficonfig_append_quit_entry(efi_menu); |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 647 | if (ret != EFI_SUCCESS) |
| 648 | goto out; |
| 649 | |
| 650 | ret = eficonfig_process_common(efi_menu, " ** Select Volume **"); |
| 651 | out: |
| 652 | efi_free_pool(volume_handles); |
| 653 | list_for_each_safe(pos, n, &efi_menu->list) { |
| 654 | entry = list_entry(pos, struct eficonfig_entry, list); |
| 655 | free(entry->data); |
| 656 | } |
| 657 | eficonfig_destroy(efi_menu); |
| 658 | |
| 659 | return ret; |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * sort_file() - sort the file name in ascii order |
| 664 | * |
| 665 | * @data1: pointer to the file entry data |
| 666 | * @data2: pointer to the file entry data |
| 667 | * Return: -1 if the data1 file name is less than data2 file name, |
| 668 | * 0 if both file name match, |
| 669 | * 1 if the data1 file name is greater thant data2 file name. |
| 670 | */ |
| 671 | static int sort_file(const void *arg1, const void *arg2) |
| 672 | { |
| 673 | const struct eficonfig_file_entry_data *data1, *data2; |
| 674 | |
| 675 | data1 = *((const struct eficonfig_file_entry_data **)arg1); |
| 676 | data2 = *((const struct eficonfig_file_entry_data **)arg2); |
| 677 | |
| 678 | return strcasecmp(data1->file_name, data2->file_name); |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * eficonfig_create_file_entry() - construct the file menu entry |
| 683 | * |
| 684 | * @efi_menu: pointer to the efimenu structure |
| 685 | * @count: number of the directory and file |
| 686 | * @tmp_infos: pointer to the entry data array |
| 687 | * @f: pointer to the file handle |
| 688 | * @buf: pointer to the buffer to store the directory information |
| 689 | * @file_info: pointer to the file selection structure |
| 690 | * Return: status code |
| 691 | */ |
| 692 | static efi_status_t |
| 693 | eficonfig_create_file_entry(struct efimenu *efi_menu, u32 count, |
| 694 | struct eficonfig_file_entry_data **tmp_infos, |
| 695 | struct efi_file_handle *f, struct efi_file_info *buf, |
| 696 | struct eficonfig_select_file_info *file_info) |
| 697 | { |
| 698 | char *name, *p; |
| 699 | efi_uintn_t len; |
| 700 | efi_status_t ret; |
| 701 | u32 i, entry_num = 0; |
| 702 | struct eficonfig_file_entry_data *info; |
| 703 | |
| 704 | efi_file_setpos_int(f, 0); |
| 705 | /* Read directory and construct menu structure */ |
| 706 | for (i = 0; i < count; i++) { |
| 707 | if (entry_num >= EFICONFIG_ENTRY_NUM_MAX - 1) |
| 708 | break; |
| 709 | |
| 710 | len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE; |
| 711 | ret = efi_file_read_int(f, &len, buf); |
| 712 | if (ret != EFI_SUCCESS || len == 0) |
| 713 | break; |
| 714 | |
| 715 | info = calloc(1, sizeof(struct eficonfig_file_entry_data)); |
| 716 | if (!info) { |
| 717 | ret = EFI_OUT_OF_RESOURCES; |
| 718 | goto out; |
| 719 | } |
| 720 | |
| 721 | /* append '\\' at the end of directory name */ |
| 722 | name = calloc(1, utf16_utf8_strlen(buf->file_name) + 2); |
| 723 | if (!name) { |
| 724 | ret = EFI_OUT_OF_RESOURCES; |
| 725 | free(info); |
| 726 | goto out; |
| 727 | } |
| 728 | p = name; |
| 729 | utf16_utf8_strcpy(&p, buf->file_name); |
| 730 | if (buf->attribute & EFI_FILE_DIRECTORY) { |
| 731 | /* filter out u'.' */ |
| 732 | if (!u16_strcmp(buf->file_name, u".")) { |
| 733 | free(info); |
| 734 | free(name); |
| 735 | continue; |
| 736 | } |
| 737 | name[u16_strlen(buf->file_name)] = '\\'; |
| 738 | info->is_directory = true; |
| 739 | } |
| 740 | |
| 741 | info->file_name = name; |
| 742 | info->file_info = file_info; |
| 743 | tmp_infos[entry_num++] = info; |
| 744 | } |
| 745 | |
| 746 | qsort(tmp_infos, entry_num, sizeof(*tmp_infos), |
| 747 | (int (*)(const void *, const void *))sort_file); |
| 748 | |
| 749 | for (i = 0; i < entry_num; i++) { |
Masahisa Kojima | 8961e93 | 2022-11-20 09:21:14 +0900 | [diff] [blame^] | 750 | ret = eficonfig_append_menu_entry(efi_menu, tmp_infos[i]->file_name, |
| 751 | eficonfig_file_selected, tmp_infos[i]); |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 752 | if (ret != EFI_SUCCESS) |
| 753 | goto out; |
| 754 | } |
| 755 | |
| 756 | out: |
| 757 | return ret; |
| 758 | } |
| 759 | |
| 760 | /** |
Masahisa Kojima | a84040a | 2022-11-20 09:21:13 +0900 | [diff] [blame] | 761 | * eficonfig_show_file_selection() - construct the file selection menu |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 762 | * |
| 763 | * @file_info: pointer to the file selection structure |
| 764 | * @root: pointer to the file handle |
| 765 | * Return: status code |
| 766 | */ |
Masahisa Kojima | a84040a | 2022-11-20 09:21:13 +0900 | [diff] [blame] | 767 | static efi_status_t eficonfig_show_file_selection(struct eficonfig_select_file_info *file_info, |
| 768 | struct efi_file_handle *root) |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 769 | { |
| 770 | u32 count = 0, i; |
| 771 | efi_uintn_t len; |
| 772 | efi_status_t ret; |
| 773 | struct efimenu *efi_menu; |
| 774 | struct efi_file_handle *f; |
| 775 | struct efi_file_info *buf; |
| 776 | struct eficonfig_file_entry_data **tmp_infos; |
| 777 | |
| 778 | buf = calloc(1, sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE); |
| 779 | if (!buf) |
| 780 | return EFI_OUT_OF_RESOURCES; |
| 781 | |
| 782 | while (!file_info->file_selected) { |
| 783 | efi_menu = calloc(1, sizeof(struct efimenu)); |
| 784 | if (!efi_menu) { |
| 785 | ret = EFI_OUT_OF_RESOURCES; |
| 786 | goto out; |
| 787 | } |
| 788 | INIT_LIST_HEAD(&efi_menu->list); |
| 789 | |
| 790 | ret = efi_file_open_int(root, &f, file_info->current_path, EFI_FILE_MODE_READ, 0); |
| 791 | if (ret != EFI_SUCCESS) { |
| 792 | eficonfig_print_msg("Reading volume failed!"); |
| 793 | free(efi_menu); |
| 794 | ret = EFI_ABORTED; |
| 795 | goto out; |
| 796 | } |
| 797 | |
| 798 | /* Count the number of directory entries */ |
| 799 | for (;;) { |
| 800 | len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE; |
| 801 | ret = efi_file_read_int(f, &len, buf); |
| 802 | if (ret != EFI_SUCCESS || len == 0) |
| 803 | break; |
| 804 | |
| 805 | count++; |
| 806 | } |
| 807 | |
| 808 | /* allocate array to sort the entry */ |
| 809 | tmp_infos = calloc(count, sizeof(*tmp_infos)); |
| 810 | if (!tmp_infos) { |
| 811 | ret = EFI_OUT_OF_RESOURCES; |
| 812 | goto err; |
| 813 | } |
| 814 | |
| 815 | ret = eficonfig_create_file_entry(efi_menu, count, tmp_infos, |
| 816 | f, buf, file_info); |
| 817 | if (ret != EFI_SUCCESS) |
| 818 | goto err; |
| 819 | |
Masahisa Kojima | 8961e93 | 2022-11-20 09:21:14 +0900 | [diff] [blame^] | 820 | ret = eficonfig_append_quit_entry(efi_menu); |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 821 | if (ret != EFI_SUCCESS) |
| 822 | goto err; |
| 823 | |
| 824 | ret = eficonfig_process_common(efi_menu, " ** Select File **"); |
| 825 | err: |
| 826 | efi_file_close_int(f); |
| 827 | eficonfig_destroy(efi_menu); |
| 828 | |
| 829 | if (tmp_infos) { |
| 830 | for (i = 0; i < count; i++) |
| 831 | free(tmp_infos[i]); |
| 832 | } |
| 833 | |
| 834 | free(tmp_infos); |
| 835 | |
| 836 | if (ret != EFI_SUCCESS) |
| 837 | break; |
| 838 | } |
| 839 | |
| 840 | out: |
| 841 | free(buf); |
| 842 | |
| 843 | return ret; |
| 844 | } |
| 845 | |
| 846 | /** |
| 847 | * handle_user_input() - handle user input |
| 848 | * |
| 849 | * @buf: pointer to the buffer |
| 850 | * @buf_size: size of the buffer |
| 851 | * @cursor_col: cursor column for user input |
| 852 | * @msg: pointer to the string to display |
| 853 | * Return: status code |
| 854 | */ |
| 855 | static efi_status_t handle_user_input(u16 *buf, int buf_size, |
| 856 | int cursor_col, char *msg) |
| 857 | { |
| 858 | u16 *tmp; |
| 859 | efi_status_t ret; |
| 860 | |
| 861 | printf(ANSI_CLEAR_CONSOLE |
| 862 | ANSI_CURSOR_POSITION |
| 863 | "%s" |
| 864 | ANSI_CURSOR_POSITION |
| 865 | " Press ENTER to complete, ESC/CTRL+C to quit", |
| 866 | 0, 1, msg, 8, 1); |
| 867 | |
| 868 | /* tmp is used to accept user cancel */ |
| 869 | tmp = calloc(1, buf_size * sizeof(u16)); |
| 870 | if (!tmp) |
| 871 | return EFI_OUT_OF_RESOURCES; |
| 872 | |
| 873 | ret = efi_console_get_u16_string(cin, tmp, buf_size, NULL, 4, cursor_col); |
| 874 | if (ret == EFI_SUCCESS) |
| 875 | u16_strcpy(buf, tmp); |
| 876 | |
| 877 | free(tmp); |
| 878 | |
| 879 | /* to stay the parent menu */ |
| 880 | ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret; |
| 881 | |
| 882 | return ret; |
| 883 | } |
| 884 | |
| 885 | /** |
| 886 | * eficonfig_boot_add_enter_description() - handle user input for description |
| 887 | * |
| 888 | * @data: pointer to the internal boot option structure |
| 889 | * Return: status code |
| 890 | */ |
| 891 | static efi_status_t eficonfig_boot_add_enter_description(void *data) |
| 892 | { |
| 893 | struct eficonfig_boot_option *bo = data; |
| 894 | |
| 895 | return handle_user_input(bo->description, EFICONFIG_DESCRIPTION_MAX, 22, |
| 896 | "\n ** Edit Description **\n" |
| 897 | "\n" |
| 898 | " enter description: "); |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * eficonfig_boot_add_optional_data() - handle user input for optional data |
| 903 | * |
| 904 | * @data: pointer to the internal boot option structure |
| 905 | * Return: status code |
| 906 | */ |
| 907 | static efi_status_t eficonfig_boot_add_optional_data(void *data) |
| 908 | { |
| 909 | struct eficonfig_boot_option *bo = data; |
| 910 | |
| 911 | return handle_user_input(bo->optional_data, EFICONFIG_OPTIONAL_DATA_MAX, 24, |
| 912 | "\n ** Edit Optional Data **\n" |
| 913 | "\n" |
| 914 | " enter optional data:"); |
| 915 | } |
| 916 | |
| 917 | /** |
| 918 | * eficonfig_boot_edit_save() - handler to save the boot option |
| 919 | * |
| 920 | * @data: pointer to the internal boot option structure |
| 921 | * Return: status code |
| 922 | */ |
| 923 | static efi_status_t eficonfig_boot_edit_save(void *data) |
| 924 | { |
| 925 | struct eficonfig_boot_option *bo = data; |
| 926 | |
| 927 | if (u16_strlen(bo->description) == 0) { |
| 928 | eficonfig_print_msg("Boot Description is empty!"); |
| 929 | bo->edit_completed = false; |
| 930 | return EFI_NOT_READY; |
| 931 | } |
| 932 | if (u16_strlen(bo->file_info.current_path) == 0) { |
| 933 | eficonfig_print_msg("File is not selected!"); |
| 934 | bo->edit_completed = false; |
| 935 | return EFI_NOT_READY; |
| 936 | } |
| 937 | |
| 938 | bo->edit_completed = true; |
| 939 | |
| 940 | return EFI_SUCCESS; |
| 941 | } |
| 942 | |
| 943 | /** |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 944 | * eficonfig_process_clear_file_selection() - callback function for "Clear" entry |
| 945 | * |
| 946 | * @data: pointer to the data |
| 947 | * Return: status code |
| 948 | */ |
| 949 | efi_status_t eficonfig_process_clear_file_selection(void *data) |
| 950 | { |
| 951 | struct eficonfig_select_file_info *file_info = data; |
| 952 | |
| 953 | /* clear the existing file information */ |
| 954 | file_info->current_volume = NULL; |
| 955 | file_info->current_path[0] = u'\0'; |
| 956 | file_info->dp_volume = NULL; |
| 957 | |
| 958 | return EFI_ABORTED; |
| 959 | } |
| 960 | |
| 961 | static struct eficonfig_item select_file_menu_items[] = { |
| 962 | {"Select File", eficonfig_process_select_file}, |
| 963 | {"Clear", eficonfig_process_clear_file_selection}, |
| 964 | {"Quit", eficonfig_process_quit}, |
| 965 | }; |
| 966 | |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 967 | /** |
Masahisa Kojima | a84040a | 2022-11-20 09:21:13 +0900 | [diff] [blame] | 968 | * eficonfig_process_show_file_option() - display select file option |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 969 | * |
| 970 | * @file_info: pointer to the file information structure |
| 971 | * Return: status code |
| 972 | */ |
Masahisa Kojima | a84040a | 2022-11-20 09:21:13 +0900 | [diff] [blame] | 973 | efi_status_t eficonfig_process_show_file_option(void *data) |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 974 | { |
| 975 | efi_status_t ret; |
| 976 | struct efimenu *efi_menu; |
| 977 | |
Masahisa Kojima | a84040a | 2022-11-20 09:21:13 +0900 | [diff] [blame] | 978 | select_file_menu_items[0].data = data; |
| 979 | select_file_menu_items[1].data = data; |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 980 | efi_menu = eficonfig_create_fixed_menu(select_file_menu_items, |
| 981 | ARRAY_SIZE(select_file_menu_items)); |
| 982 | if (!efi_menu) |
| 983 | return EFI_OUT_OF_RESOURCES; |
| 984 | |
| 985 | ret = eficonfig_process_common(efi_menu, " ** Update File **"); |
| 986 | if (ret != EFI_SUCCESS) /* User selects "Clear" or "Quit" */ |
| 987 | ret = EFI_NOT_READY; |
| 988 | |
| 989 | eficonfig_destroy(efi_menu); |
| 990 | |
| 991 | return ret; |
| 992 | } |
| 993 | |
| 994 | /** |
Masahisa Kojima | a84040a | 2022-11-20 09:21:13 +0900 | [diff] [blame] | 995 | * eficonfig_process_select_file() - handle user file selection |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 996 | * |
| 997 | * @data: pointer to the data |
| 998 | * Return: status code |
| 999 | */ |
Masahisa Kojima | a84040a | 2022-11-20 09:21:13 +0900 | [diff] [blame] | 1000 | efi_status_t eficonfig_process_select_file(void *data) |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1001 | { |
| 1002 | size_t len; |
| 1003 | efi_status_t ret; |
| 1004 | struct list_head *pos, *n; |
| 1005 | struct efi_file_handle *root; |
| 1006 | struct eficonfig_filepath_info *item; |
| 1007 | struct eficonfig_select_file_info *tmp = NULL; |
| 1008 | struct eficonfig_select_file_info *file_info = data; |
| 1009 | |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1010 | tmp = calloc(1, sizeof(struct eficonfig_select_file_info)); |
| 1011 | if (!tmp) |
| 1012 | return EFI_OUT_OF_RESOURCES; |
| 1013 | |
| 1014 | tmp->current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE); |
| 1015 | if (!tmp->current_path) { |
| 1016 | free(tmp); |
| 1017 | return EFI_OUT_OF_RESOURCES; |
| 1018 | } |
| 1019 | INIT_LIST_HEAD(&tmp->filepath_list); |
| 1020 | |
| 1021 | while (!tmp->file_selected) { |
| 1022 | tmp->current_volume = NULL; |
| 1023 | memset(tmp->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE); |
| 1024 | |
| 1025 | ret = eficonfig_select_volume(tmp); |
| 1026 | if (ret != EFI_SUCCESS) |
| 1027 | goto out; |
| 1028 | |
| 1029 | if (!tmp->current_volume) |
| 1030 | return EFI_INVALID_PARAMETER; |
| 1031 | |
| 1032 | ret = efi_open_volume_int(tmp->current_volume, &root); |
| 1033 | if (ret != EFI_SUCCESS) |
| 1034 | goto out; |
| 1035 | |
Masahisa Kojima | a84040a | 2022-11-20 09:21:13 +0900 | [diff] [blame] | 1036 | ret = eficonfig_show_file_selection(tmp, root); |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1037 | if (ret == EFI_ABORTED) |
| 1038 | continue; |
| 1039 | if (ret != EFI_SUCCESS) |
| 1040 | goto out; |
| 1041 | } |
| 1042 | |
| 1043 | out: |
| 1044 | if (ret == EFI_SUCCESS) { |
| 1045 | len = u16_strlen(tmp->current_path); |
| 1046 | len = (len >= EFICONFIG_FILE_PATH_MAX) ? (EFICONFIG_FILE_PATH_MAX - 1) : len; |
| 1047 | memcpy(file_info->current_path, tmp->current_path, len * sizeof(u16)); |
| 1048 | file_info->current_path[len] = u'\0'; |
| 1049 | file_info->current_volume = tmp->current_volume; |
| 1050 | file_info->dp_volume = tmp->dp_volume; |
| 1051 | } |
| 1052 | |
| 1053 | list_for_each_safe(pos, n, &tmp->filepath_list) { |
| 1054 | item = list_entry(pos, struct eficonfig_filepath_info, list); |
| 1055 | list_del(&item->list); |
| 1056 | free(item->name); |
| 1057 | free(item); |
| 1058 | } |
| 1059 | free(tmp->current_path); |
| 1060 | free(tmp); |
| 1061 | |
| 1062 | /* to stay the parent menu */ |
| 1063 | ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret; |
| 1064 | |
| 1065 | return ret; |
| 1066 | } |
| 1067 | |
| 1068 | /** |
| 1069 | * eficonfig_get_unused_bootoption() - get unused "Boot####" index |
| 1070 | * |
| 1071 | * @buf: pointer to the buffer to store boot option variable name |
| 1072 | * @buf_size: buffer size |
| 1073 | * @index: pointer to store the index in the BootOrder variable |
| 1074 | * Return: status code |
| 1075 | */ |
| 1076 | efi_status_t eficonfig_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size, |
| 1077 | unsigned int *index) |
| 1078 | { |
| 1079 | u32 i; |
| 1080 | efi_status_t ret; |
| 1081 | efi_uintn_t size; |
| 1082 | |
| 1083 | if (buf_size < u16_strsize(u"Boot####")) |
| 1084 | return EFI_BUFFER_TOO_SMALL; |
| 1085 | |
| 1086 | for (i = 0; i <= 0xFFFF; i++) { |
| 1087 | size = 0; |
| 1088 | efi_create_indexed_name(buf, buf_size, "Boot", i); |
| 1089 | ret = efi_get_variable_int(buf, &efi_global_variable_guid, |
| 1090 | NULL, &size, NULL, NULL); |
| 1091 | if (ret == EFI_BUFFER_TOO_SMALL) |
| 1092 | continue; |
| 1093 | else |
| 1094 | break; |
| 1095 | } |
| 1096 | |
| 1097 | if (i > 0xFFFF) |
| 1098 | return EFI_OUT_OF_RESOURCES; |
| 1099 | |
| 1100 | *index = i; |
| 1101 | |
| 1102 | return EFI_SUCCESS; |
| 1103 | } |
| 1104 | |
| 1105 | /** |
| 1106 | * eficonfig_set_boot_option() - set boot option |
| 1107 | * |
| 1108 | * @varname: pointer to variable name |
| 1109 | * @dp: pointer to device path |
| 1110 | * @label: pointer to label string |
| 1111 | * @optional_data: pointer to optional data |
| 1112 | * Return: status code |
| 1113 | */ |
| 1114 | static efi_status_t eficonfig_set_boot_option(u16 *varname, struct efi_device_path *dp, |
| 1115 | efi_uintn_t dp_size, u16 *label, char *optional_data) |
| 1116 | { |
| 1117 | void *p = NULL; |
| 1118 | efi_status_t ret; |
| 1119 | efi_uintn_t size; |
| 1120 | struct efi_load_option lo; |
| 1121 | |
| 1122 | lo.file_path = dp; |
| 1123 | lo.file_path_length = dp_size; |
| 1124 | lo.attributes = LOAD_OPTION_ACTIVE; |
| 1125 | lo.optional_data = optional_data; |
| 1126 | lo.label = label; |
| 1127 | |
| 1128 | size = efi_serialize_load_option(&lo, (u8 **)&p); |
| 1129 | if (!size) |
| 1130 | return EFI_INVALID_PARAMETER; |
| 1131 | |
| 1132 | ret = efi_set_variable_int(varname, &efi_global_variable_guid, |
| 1133 | EFI_VARIABLE_NON_VOLATILE | |
| 1134 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1135 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1136 | size, p, false); |
| 1137 | free(p); |
| 1138 | |
| 1139 | return ret; |
| 1140 | } |
| 1141 | |
| 1142 | /** |
| 1143 | * eficonfig_append_bootorder() - append new boot option in BootOrder variable |
| 1144 | * |
| 1145 | * @index: "Boot####" index to append to BootOrder variable |
| 1146 | * Return: status code |
| 1147 | */ |
| 1148 | efi_status_t eficonfig_append_bootorder(u16 index) |
| 1149 | { |
| 1150 | u16 *bootorder; |
| 1151 | efi_status_t ret; |
| 1152 | u16 *new_bootorder = NULL; |
| 1153 | efi_uintn_t last, size, new_size; |
| 1154 | |
| 1155 | /* append new boot option */ |
| 1156 | bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); |
| 1157 | last = size / sizeof(u16); |
| 1158 | new_size = size + sizeof(u16); |
| 1159 | new_bootorder = calloc(1, new_size); |
| 1160 | if (!new_bootorder) { |
| 1161 | ret = EFI_OUT_OF_RESOURCES; |
| 1162 | goto out; |
| 1163 | } |
| 1164 | memcpy(new_bootorder, bootorder, size); |
| 1165 | new_bootorder[last] = index; |
| 1166 | |
| 1167 | ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid, |
| 1168 | EFI_VARIABLE_NON_VOLATILE | |
| 1169 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1170 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1171 | new_size, new_bootorder, false); |
| 1172 | if (ret != EFI_SUCCESS) |
| 1173 | goto out; |
| 1174 | |
| 1175 | out: |
| 1176 | free(bootorder); |
| 1177 | free(new_bootorder); |
| 1178 | |
| 1179 | return ret; |
| 1180 | } |
| 1181 | |
| 1182 | /** |
| 1183 | * create_boot_option_entry() - create boot option entry |
| 1184 | * |
| 1185 | * @efi_menu: pointer to the efimenu structure |
| 1186 | * @title: pointer to the entry title |
| 1187 | * @val: pointer to boot option label |
| 1188 | * @func: callback of each entry |
| 1189 | * @data: pointer to the data to be passed to each entry callback |
| 1190 | * Return: status code |
| 1191 | */ |
| 1192 | static efi_status_t create_boot_option_entry(struct efimenu *efi_menu, char *title, u16 *val, |
| 1193 | eficonfig_entry_func func, void *data) |
| 1194 | { |
| 1195 | u32 len; |
| 1196 | char *p, *buf; |
| 1197 | |
| 1198 | len = strlen(title) + 1; |
| 1199 | if (val) |
| 1200 | len += utf16_utf8_strlen(val); |
| 1201 | buf = calloc(1, len); |
| 1202 | if (!buf) |
| 1203 | return EFI_OUT_OF_RESOURCES; |
| 1204 | |
| 1205 | strcpy(buf, title); |
| 1206 | if (val) { |
| 1207 | p = buf + strlen(title); |
| 1208 | utf16_utf8_strcpy(&p, val); |
| 1209 | } |
| 1210 | |
Masahisa Kojima | 8961e93 | 2022-11-20 09:21:14 +0900 | [diff] [blame^] | 1211 | return eficonfig_append_menu_entry(efi_menu, buf, func, data); |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | /** |
| 1215 | * prepare_file_selection_entry() - prepare file selection entry |
| 1216 | * |
| 1217 | * @efi_menu: pointer to the efimenu structure |
| 1218 | * @title: pointer to the title string |
| 1219 | * @file_info: pointer to the file info |
| 1220 | * Return: status code |
| 1221 | */ |
| 1222 | static efi_status_t prepare_file_selection_entry(struct efimenu *efi_menu, char *title, |
| 1223 | struct eficonfig_select_file_info *file_info) |
| 1224 | { |
| 1225 | u32 len; |
| 1226 | efi_status_t ret; |
Masahisa Kojima | e34158b | 2022-09-12 17:33:51 +0900 | [diff] [blame] | 1227 | u16 *file_name = NULL, *p; |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1228 | efi_handle_t handle; |
Masahisa Kojima | e34158b | 2022-09-12 17:33:51 +0900 | [diff] [blame] | 1229 | char *devname; |
| 1230 | |
| 1231 | devname = calloc(1, EFICONFIG_VOLUME_PATH_MAX + 1); |
| 1232 | if (!devname) |
| 1233 | return EFI_OUT_OF_RESOURCES; |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1234 | |
| 1235 | /* get the device name only when the user already selected the file path */ |
| 1236 | handle = efi_dp_find_obj(file_info->dp_volume, NULL, NULL); |
| 1237 | if (handle) { |
Masahisa Kojima | e34158b | 2022-09-12 17:33:51 +0900 | [diff] [blame] | 1238 | ret = efi_disk_get_device_name(handle, devname, EFICONFIG_VOLUME_PATH_MAX); |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1239 | if (ret != EFI_SUCCESS) |
Masahisa Kojima | e34158b | 2022-09-12 17:33:51 +0900 | [diff] [blame] | 1240 | goto out; |
| 1241 | } |
| 1242 | |
| 1243 | /* |
| 1244 | * If the preconfigured volume does not exist in the system, display the text |
| 1245 | * converted volume device path instead of U-Boot friendly name(e.g. "usb 0:1"). |
| 1246 | */ |
| 1247 | if (!handle && file_info->dp_volume) { |
| 1248 | u16 *dp_str; |
| 1249 | char *q = devname; |
| 1250 | |
| 1251 | dp_str = efi_dp_str(file_info->dp_volume); |
| 1252 | if (dp_str) |
| 1253 | utf16_utf8_strncpy(&q, dp_str, EFICONFIG_VOLUME_PATH_MAX); |
| 1254 | |
| 1255 | efi_free_pool(dp_str); |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | /* append u'/' to devname, it is just for display purpose. */ |
| 1259 | if (file_info->current_path[0] != u'\0' && file_info->current_path[0] != u'/') |
Masahisa Kojima | e34158b | 2022-09-12 17:33:51 +0900 | [diff] [blame] | 1260 | strlcat(devname, "/", EFICONFIG_VOLUME_PATH_MAX + 1); |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1261 | |
| 1262 | len = strlen(devname); |
| 1263 | len += utf16_utf8_strlen(file_info->current_path) + 1; |
| 1264 | file_name = calloc(1, len * sizeof(u16)); |
Masahisa Kojima | e34158b | 2022-09-12 17:33:51 +0900 | [diff] [blame] | 1265 | if (!file_name) { |
| 1266 | ret = EFI_OUT_OF_RESOURCES; |
| 1267 | goto out; |
| 1268 | } |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1269 | |
| 1270 | p = file_name; |
| 1271 | utf8_utf16_strcpy(&p, devname); |
| 1272 | u16_strlcat(file_name, file_info->current_path, len); |
| 1273 | ret = create_boot_option_entry(efi_menu, title, file_name, |
Masahisa Kojima | a84040a | 2022-11-20 09:21:13 +0900 | [diff] [blame] | 1274 | eficonfig_process_show_file_option, file_info); |
Masahisa Kojima | e34158b | 2022-09-12 17:33:51 +0900 | [diff] [blame] | 1275 | out: |
| 1276 | free(devname); |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1277 | free(file_name); |
Masahisa Kojima | e34158b | 2022-09-12 17:33:51 +0900 | [diff] [blame] | 1278 | |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1279 | return ret; |
| 1280 | } |
| 1281 | |
| 1282 | /** |
| 1283 | * eficonfig_show_boot_option() - prepare menu entry for editing boot option |
| 1284 | * |
| 1285 | * Construct the structures to create edit boot option menu |
| 1286 | * |
| 1287 | * @bo: pointer to the boot option |
| 1288 | * @header_str: pointer to the header string |
| 1289 | * Return: status code |
| 1290 | */ |
| 1291 | static efi_status_t eficonfig_show_boot_option(struct eficonfig_boot_option *bo, |
| 1292 | char *header_str) |
| 1293 | { |
| 1294 | efi_status_t ret; |
| 1295 | struct efimenu *efi_menu; |
| 1296 | |
| 1297 | efi_menu = calloc(1, sizeof(struct efimenu)); |
| 1298 | if (!efi_menu) |
| 1299 | return EFI_OUT_OF_RESOURCES; |
| 1300 | |
| 1301 | INIT_LIST_HEAD(&efi_menu->list); |
| 1302 | |
| 1303 | ret = create_boot_option_entry(efi_menu, "Description: ", bo->description, |
| 1304 | eficonfig_boot_add_enter_description, bo); |
| 1305 | if (ret != EFI_SUCCESS) |
| 1306 | goto out; |
| 1307 | |
| 1308 | ret = prepare_file_selection_entry(efi_menu, "File: ", &bo->file_info); |
| 1309 | if (ret != EFI_SUCCESS) |
| 1310 | goto out; |
| 1311 | |
| 1312 | ret = prepare_file_selection_entry(efi_menu, "Initrd File: ", &bo->initrd_info); |
| 1313 | if (ret != EFI_SUCCESS) |
| 1314 | goto out; |
| 1315 | |
| 1316 | ret = create_boot_option_entry(efi_menu, "Optional Data: ", bo->optional_data, |
| 1317 | eficonfig_boot_add_optional_data, bo); |
| 1318 | if (ret != EFI_SUCCESS) |
| 1319 | goto out; |
| 1320 | |
| 1321 | ret = create_boot_option_entry(efi_menu, "Save", NULL, |
| 1322 | eficonfig_boot_edit_save, bo); |
| 1323 | if (ret != EFI_SUCCESS) |
| 1324 | goto out; |
| 1325 | |
| 1326 | ret = create_boot_option_entry(efi_menu, "Quit", NULL, |
| 1327 | eficonfig_process_quit, NULL); |
| 1328 | if (ret != EFI_SUCCESS) |
| 1329 | goto out; |
| 1330 | |
| 1331 | ret = eficonfig_process_common(efi_menu, header_str); |
| 1332 | out: |
| 1333 | eficonfig_destroy(efi_menu); |
| 1334 | |
| 1335 | return ret; |
| 1336 | } |
| 1337 | |
| 1338 | /** |
| 1339 | * fill_file_info() - fill the file info from efi_device_path structure |
| 1340 | * |
| 1341 | * @dp: pointer to the device path |
| 1342 | * @file_info: pointer to the file info structure |
| 1343 | * @device_dp: pointer to the volume device path |
| 1344 | */ |
| 1345 | static void fill_file_info(struct efi_device_path *dp, |
| 1346 | struct eficonfig_select_file_info *file_info, |
| 1347 | struct efi_device_path *device_dp) |
| 1348 | { |
| 1349 | u16 *file_str, *p; |
| 1350 | struct efi_device_path *file_dp = NULL; |
| 1351 | |
| 1352 | efi_dp_split_file_path(dp, &device_dp, &file_dp); |
| 1353 | file_info->dp_volume = device_dp; |
| 1354 | |
| 1355 | if (file_dp) { |
| 1356 | file_str = efi_dp_str(file_dp); |
| 1357 | /* |
| 1358 | * efi_convert_device_path_to_text() automatically adds u'/' at the |
| 1359 | * beginning of file name, remove u'/' before copying to current_path |
| 1360 | */ |
| 1361 | p = file_str; |
| 1362 | if (p[0] == u'/') |
| 1363 | p++; |
| 1364 | |
| 1365 | u16_strcpy(file_info->current_path, p); |
| 1366 | efi_free_pool(file_dp); |
| 1367 | efi_free_pool(file_str); |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | /** |
| 1372 | * eficonfig_edit_boot_option() - prepare boot option structure for editing |
| 1373 | * |
| 1374 | * Construct the boot option structure and copy the existing value |
| 1375 | * |
| 1376 | * @varname: pointer to the UEFI variable name |
| 1377 | * @bo: pointer to the boot option |
| 1378 | * @load_option: pointer to the load option |
| 1379 | * @load_option_size: size of the load option |
| 1380 | * @header_str: pointer to the header string |
| 1381 | * Return : status code |
| 1382 | */ |
| 1383 | static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_boot_option *bo, |
| 1384 | void *load_option, efi_uintn_t load_option_size, |
| 1385 | char *header_str) |
| 1386 | { |
| 1387 | size_t len; |
| 1388 | efi_status_t ret; |
| 1389 | char *tmp = NULL, *p; |
| 1390 | struct efi_load_option lo = {0}; |
| 1391 | efi_uintn_t final_dp_size; |
| 1392 | struct efi_device_path *dp = NULL; |
| 1393 | efi_uintn_t size = load_option_size; |
| 1394 | struct efi_device_path *final_dp = NULL; |
| 1395 | struct efi_device_path *device_dp = NULL; |
| 1396 | struct efi_device_path *initrd_dp = NULL; |
| 1397 | struct efi_device_path *initrd_device_dp = NULL; |
| 1398 | |
| 1399 | const struct efi_initrd_dp id_dp = { |
| 1400 | .vendor = { |
| 1401 | { |
| 1402 | DEVICE_PATH_TYPE_MEDIA_DEVICE, |
| 1403 | DEVICE_PATH_SUB_TYPE_VENDOR_PATH, |
| 1404 | sizeof(id_dp.vendor), |
| 1405 | }, |
| 1406 | EFI_INITRD_MEDIA_GUID, |
| 1407 | }, |
| 1408 | .end = { |
| 1409 | DEVICE_PATH_TYPE_END, |
| 1410 | DEVICE_PATH_SUB_TYPE_END, |
| 1411 | sizeof(id_dp.end), |
| 1412 | } |
| 1413 | }; |
| 1414 | |
| 1415 | bo->file_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE); |
| 1416 | if (!bo->file_info.current_path) { |
| 1417 | ret = EFI_OUT_OF_RESOURCES; |
| 1418 | goto out; |
| 1419 | } |
| 1420 | |
| 1421 | bo->initrd_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE); |
| 1422 | if (!bo->file_info.current_path) { |
| 1423 | ret = EFI_OUT_OF_RESOURCES; |
| 1424 | goto out; |
| 1425 | } |
| 1426 | |
| 1427 | bo->description = calloc(1, EFICONFIG_DESCRIPTION_MAX * sizeof(u16)); |
| 1428 | if (!bo->description) { |
| 1429 | ret = EFI_OUT_OF_RESOURCES; |
| 1430 | goto out; |
| 1431 | } |
| 1432 | |
| 1433 | bo->optional_data = calloc(1, EFICONFIG_OPTIONAL_DATA_MAX * sizeof(u16)); |
| 1434 | if (!bo->optional_data) { |
| 1435 | ret = EFI_OUT_OF_RESOURCES; |
| 1436 | goto out; |
| 1437 | } |
| 1438 | |
| 1439 | /* copy the preset value */ |
| 1440 | if (load_option) { |
| 1441 | ret = efi_deserialize_load_option(&lo, load_option, &size); |
| 1442 | if (ret != EFI_SUCCESS) |
| 1443 | goto out; |
| 1444 | |
Masahisa Kojima | e34158b | 2022-09-12 17:33:51 +0900 | [diff] [blame] | 1445 | if (!lo.label) { |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1446 | ret = EFI_INVALID_PARAMETER; |
| 1447 | goto out; |
| 1448 | } |
Masahisa Kojima | e34158b | 2022-09-12 17:33:51 +0900 | [diff] [blame] | 1449 | /* truncate the long label string */ |
| 1450 | if (u16_strlen(lo.label) >= EFICONFIG_DESCRIPTION_MAX) |
| 1451 | lo.label[EFICONFIG_DESCRIPTION_MAX - 1] = u'\0'; |
| 1452 | |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1453 | u16_strcpy(bo->description, lo.label); |
| 1454 | |
| 1455 | /* EFI image file path is a first instance */ |
| 1456 | if (lo.file_path) |
| 1457 | fill_file_info(lo.file_path, &bo->file_info, device_dp); |
| 1458 | |
| 1459 | /* Initrd file path(optional) is placed at second instance. */ |
| 1460 | initrd_dp = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid); |
| 1461 | if (initrd_dp) { |
| 1462 | fill_file_info(initrd_dp, &bo->initrd_info, initrd_device_dp); |
| 1463 | efi_free_pool(initrd_dp); |
| 1464 | } |
| 1465 | |
| 1466 | if (size > 0) |
| 1467 | memcpy(bo->optional_data, lo.optional_data, size); |
| 1468 | } |
| 1469 | |
| 1470 | while (1) { |
| 1471 | ret = eficonfig_show_boot_option(bo, header_str); |
| 1472 | if (ret == EFI_SUCCESS && bo->edit_completed) |
| 1473 | break; |
| 1474 | if (ret == EFI_NOT_READY) |
| 1475 | continue; |
| 1476 | if (ret != EFI_SUCCESS) |
| 1477 | goto out; |
| 1478 | } |
| 1479 | |
| 1480 | if (bo->initrd_info.dp_volume) { |
| 1481 | dp = create_selected_device_path(&bo->initrd_info); |
| 1482 | if (!dp) { |
| 1483 | ret = EFI_OUT_OF_RESOURCES; |
| 1484 | goto out; |
| 1485 | } |
| 1486 | initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp, dp); |
| 1487 | efi_free_pool(dp); |
| 1488 | } |
| 1489 | |
| 1490 | dp = create_selected_device_path(&bo->file_info); |
| 1491 | if (!dp) { |
| 1492 | ret = EFI_OUT_OF_RESOURCES; |
| 1493 | goto out; |
| 1494 | } |
| 1495 | final_dp_size = efi_dp_size(dp) + sizeof(END); |
| 1496 | if (initrd_dp) { |
| 1497 | final_dp = efi_dp_concat(dp, initrd_dp); |
| 1498 | final_dp_size += efi_dp_size(initrd_dp) + sizeof(END); |
| 1499 | } else { |
| 1500 | final_dp = efi_dp_dup(dp); |
| 1501 | } |
| 1502 | efi_free_pool(dp); |
| 1503 | |
| 1504 | if (!final_dp) |
| 1505 | goto out; |
| 1506 | |
| 1507 | if (utf16_utf8_strlen(bo->optional_data)) { |
| 1508 | len = utf16_utf8_strlen(bo->optional_data) + 1; |
| 1509 | tmp = calloc(1, len); |
| 1510 | if (!tmp) |
| 1511 | goto out; |
| 1512 | p = tmp; |
| 1513 | utf16_utf8_strncpy(&p, bo->optional_data, u16_strlen(bo->optional_data)); |
| 1514 | } |
| 1515 | |
| 1516 | ret = eficonfig_set_boot_option(varname, final_dp, final_dp_size, bo->description, tmp); |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1517 | out: |
| 1518 | free(tmp); |
| 1519 | free(bo->optional_data); |
| 1520 | free(bo->description); |
| 1521 | free(bo->file_info.current_path); |
| 1522 | free(bo->initrd_info.current_path); |
| 1523 | efi_free_pool(device_dp); |
| 1524 | efi_free_pool(initrd_device_dp); |
| 1525 | efi_free_pool(initrd_dp); |
| 1526 | efi_free_pool(final_dp); |
| 1527 | |
| 1528 | return ret; |
| 1529 | } |
| 1530 | |
| 1531 | /** |
| 1532 | * eficonfig_process_add_boot_option() - handler to add boot option |
| 1533 | * |
| 1534 | * @data: pointer to the data for each entry |
| 1535 | * Return: status code |
| 1536 | */ |
| 1537 | static efi_status_t eficonfig_process_add_boot_option(void *data) |
| 1538 | { |
| 1539 | u16 varname[9]; |
| 1540 | efi_status_t ret; |
| 1541 | struct eficonfig_boot_option *bo = NULL; |
| 1542 | |
| 1543 | bo = calloc(1, sizeof(struct eficonfig_boot_option)); |
| 1544 | if (!bo) |
| 1545 | return EFI_OUT_OF_RESOURCES; |
| 1546 | |
| 1547 | ret = eficonfig_get_unused_bootoption(varname, sizeof(varname), &bo->boot_index); |
| 1548 | if (ret != EFI_SUCCESS) |
| 1549 | return ret; |
| 1550 | |
| 1551 | ret = eficonfig_edit_boot_option(varname, bo, NULL, 0, " ** Add Boot Option ** "); |
| 1552 | if (ret != EFI_SUCCESS) |
| 1553 | goto out; |
| 1554 | |
| 1555 | ret = eficonfig_append_bootorder((u16)bo->boot_index); |
| 1556 | if (ret != EFI_SUCCESS) |
| 1557 | goto out; |
| 1558 | |
| 1559 | out: |
| 1560 | free(bo); |
| 1561 | |
| 1562 | /* to stay the parent menu */ |
| 1563 | ret = (ret == EFI_ABORTED) ? EFI_SUCCESS : ret; |
| 1564 | |
| 1565 | return ret; |
| 1566 | } |
| 1567 | |
| 1568 | /** |
Masahisa Kojima | e34158b | 2022-09-12 17:33:51 +0900 | [diff] [blame] | 1569 | * eficonfig_process_boot_selected() - handler to select boot option entry |
| 1570 | * |
| 1571 | * @data: pointer to the data for each entry |
| 1572 | * Return: status code |
| 1573 | */ |
| 1574 | static efi_status_t eficonfig_process_boot_selected(void *data) |
| 1575 | { |
| 1576 | struct eficonfig_boot_selection_data *info = data; |
| 1577 | |
| 1578 | if (info) |
| 1579 | *info->selected = info->boot_index; |
| 1580 | |
| 1581 | return EFI_SUCCESS; |
| 1582 | } |
| 1583 | |
| 1584 | /** |
| 1585 | * search_bootorder() - search the boot option index in BootOrder |
| 1586 | * |
| 1587 | * @bootorder: pointer to the BootOrder variable |
| 1588 | * @num: number of BootOrder entry |
| 1589 | * @target: target boot option index to search |
| 1590 | * @index: pointer to store the index of BootOrder variable |
| 1591 | * Return: true if exists, false otherwise |
| 1592 | */ |
| 1593 | static bool search_bootorder(u16 *bootorder, efi_uintn_t num, u32 target, u32 *index) |
| 1594 | { |
| 1595 | u32 i; |
| 1596 | |
| 1597 | for (i = 0; i < num; i++) { |
| 1598 | if (target == bootorder[i]) { |
| 1599 | if (index) |
| 1600 | *index = i; |
| 1601 | |
| 1602 | return true; |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | return false; |
| 1607 | } |
| 1608 | |
| 1609 | /** |
| 1610 | * eficonfig_add_boot_selection_entry() - add boot option menu entry |
| 1611 | * |
| 1612 | * @efi_menu: pointer to store the efimenu structure |
| 1613 | * @boot_index: boot option index to be added |
| 1614 | * @selected: pointer to store the selected boot option index |
| 1615 | * Return: status code |
| 1616 | */ |
| 1617 | static efi_status_t eficonfig_add_boot_selection_entry(struct efimenu *efi_menu, |
| 1618 | unsigned int boot_index, |
| 1619 | unsigned int *selected) |
| 1620 | { |
| 1621 | char *buf, *p; |
| 1622 | efi_status_t ret; |
| 1623 | efi_uintn_t size; |
| 1624 | void *load_option; |
| 1625 | struct efi_load_option lo; |
| 1626 | u16 varname[] = u"Boot####"; |
| 1627 | struct eficonfig_boot_selection_data *info; |
| 1628 | |
| 1629 | efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index); |
| 1630 | load_option = efi_get_var(varname, &efi_global_variable_guid, &size); |
| 1631 | if (!load_option) |
| 1632 | return EFI_SUCCESS; |
| 1633 | |
| 1634 | ret = efi_deserialize_load_option(&lo, load_option, &size); |
| 1635 | if (ret != EFI_SUCCESS) { |
| 1636 | log_warning("Invalid load option for %ls\n", varname); |
| 1637 | free(load_option); |
| 1638 | return ret; |
| 1639 | } |
| 1640 | |
| 1641 | if (size >= sizeof(efi_guid_t) && |
| 1642 | !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) { |
| 1643 | /* |
| 1644 | * auto generated entry has GUID in optional_data, |
| 1645 | * skip auto generated entry because it will be generated |
| 1646 | * again even if it is edited or deleted. |
| 1647 | */ |
| 1648 | free(load_option); |
| 1649 | return EFI_SUCCESS; |
| 1650 | } |
| 1651 | |
| 1652 | info = calloc(1, sizeof(struct eficonfig_boot_selection_data)); |
| 1653 | if (!info) { |
| 1654 | free(load_option); |
| 1655 | return EFI_OUT_OF_RESOURCES; |
| 1656 | } |
| 1657 | |
| 1658 | buf = calloc(1, utf16_utf8_strlen(lo.label) + 1); |
| 1659 | if (!buf) { |
| 1660 | free(load_option); |
| 1661 | free(info); |
| 1662 | return EFI_OUT_OF_RESOURCES; |
| 1663 | } |
| 1664 | p = buf; |
| 1665 | utf16_utf8_strcpy(&p, lo.label); |
| 1666 | info->boot_index = boot_index; |
| 1667 | info->selected = selected; |
Masahisa Kojima | 8961e93 | 2022-11-20 09:21:14 +0900 | [diff] [blame^] | 1668 | ret = eficonfig_append_menu_entry(efi_menu, buf, eficonfig_process_boot_selected, info); |
Masahisa Kojima | e34158b | 2022-09-12 17:33:51 +0900 | [diff] [blame] | 1669 | if (ret != EFI_SUCCESS) { |
| 1670 | free(load_option); |
| 1671 | free(info); |
| 1672 | return ret; |
| 1673 | } |
| 1674 | free(load_option); |
| 1675 | |
| 1676 | return EFI_SUCCESS; |
| 1677 | } |
| 1678 | |
| 1679 | /** |
| 1680 | * eficonfig_show_boot_selection() - construct boot option menu entry |
| 1681 | * |
| 1682 | * @selected: pointer to store the selected boot option index |
| 1683 | * Return: status code |
| 1684 | */ |
| 1685 | static efi_status_t eficonfig_show_boot_selection(unsigned int *selected) |
| 1686 | { |
| 1687 | u32 i; |
| 1688 | u16 *bootorder; |
| 1689 | efi_status_t ret; |
| 1690 | efi_uintn_t num, size; |
| 1691 | struct efimenu *efi_menu; |
| 1692 | struct list_head *pos, *n; |
| 1693 | struct eficonfig_entry *entry; |
| 1694 | |
| 1695 | efi_menu = calloc(1, sizeof(struct efimenu)); |
| 1696 | if (!efi_menu) |
| 1697 | return EFI_OUT_OF_RESOURCES; |
| 1698 | |
| 1699 | bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); |
| 1700 | |
| 1701 | INIT_LIST_HEAD(&efi_menu->list); |
| 1702 | num = size / sizeof(u16); |
| 1703 | /* list the load option in the order of BootOrder variable */ |
| 1704 | for (i = 0; i < num; i++) { |
| 1705 | ret = eficonfig_add_boot_selection_entry(efi_menu, bootorder[i], selected); |
| 1706 | if (ret != EFI_SUCCESS) |
| 1707 | goto out; |
| 1708 | |
| 1709 | if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1) |
| 1710 | break; |
| 1711 | } |
| 1712 | |
| 1713 | /* list the remaining load option not included in the BootOrder */ |
| 1714 | for (i = 0; i <= 0xFFFF; i++) { |
| 1715 | /* If the index is included in the BootOrder, skip it */ |
| 1716 | if (search_bootorder(bootorder, num, i, NULL)) |
| 1717 | continue; |
| 1718 | |
| 1719 | ret = eficonfig_add_boot_selection_entry(efi_menu, i, selected); |
| 1720 | if (ret != EFI_SUCCESS) |
| 1721 | goto out; |
| 1722 | |
| 1723 | if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1) |
| 1724 | break; |
| 1725 | } |
| 1726 | |
Masahisa Kojima | 8961e93 | 2022-11-20 09:21:14 +0900 | [diff] [blame^] | 1727 | ret = eficonfig_append_quit_entry(efi_menu); |
Masahisa Kojima | e34158b | 2022-09-12 17:33:51 +0900 | [diff] [blame] | 1728 | if (ret != EFI_SUCCESS) |
| 1729 | goto out; |
| 1730 | |
| 1731 | ret = eficonfig_process_common(efi_menu, " ** Select Boot Option **"); |
| 1732 | out: |
| 1733 | list_for_each_safe(pos, n, &efi_menu->list) { |
| 1734 | entry = list_entry(pos, struct eficonfig_entry, list); |
| 1735 | free(entry->data); |
| 1736 | } |
| 1737 | eficonfig_destroy(efi_menu); |
| 1738 | |
| 1739 | return ret; |
| 1740 | } |
| 1741 | |
| 1742 | /** |
| 1743 | * eficonfig_process_edit_boot_option() - handler to edit boot option |
| 1744 | * |
| 1745 | * @data: pointer to the data for each entry |
| 1746 | * Return: status code |
| 1747 | */ |
| 1748 | static efi_status_t eficonfig_process_edit_boot_option(void *data) |
| 1749 | { |
| 1750 | efi_status_t ret; |
| 1751 | efi_uintn_t size; |
| 1752 | struct eficonfig_boot_option *bo = NULL; |
| 1753 | |
| 1754 | while (1) { |
| 1755 | unsigned int selected; |
| 1756 | void *load_option; |
| 1757 | u16 varname[] = u"Boot####"; |
| 1758 | |
| 1759 | ret = eficonfig_show_boot_selection(&selected); |
| 1760 | if (ret != EFI_SUCCESS) |
| 1761 | break; |
| 1762 | |
| 1763 | bo = calloc(1, sizeof(struct eficonfig_boot_option)); |
| 1764 | if (!bo) { |
| 1765 | ret = EFI_OUT_OF_RESOURCES; |
| 1766 | goto out; |
| 1767 | } |
| 1768 | |
| 1769 | bo->boot_index = selected; |
| 1770 | efi_create_indexed_name(varname, sizeof(varname), "Boot", selected); |
| 1771 | load_option = efi_get_var(varname, &efi_global_variable_guid, &size); |
| 1772 | if (!load_option) { |
| 1773 | free(bo); |
| 1774 | ret = EFI_NOT_FOUND; |
| 1775 | goto out; |
| 1776 | } |
| 1777 | |
| 1778 | ret = eficonfig_edit_boot_option(varname, bo, load_option, size, |
| 1779 | " ** Edit Boot Option ** "); |
| 1780 | |
| 1781 | free(load_option); |
| 1782 | free(bo); |
| 1783 | if (ret != EFI_SUCCESS && ret != EFI_ABORTED) |
| 1784 | break; |
| 1785 | } |
| 1786 | out: |
| 1787 | /* to stay the parent menu */ |
| 1788 | ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret; |
| 1789 | |
| 1790 | return ret; |
| 1791 | } |
| 1792 | |
| 1793 | /** |
Masahisa Kojima | e5948ee | 2022-09-12 17:33:56 +0900 | [diff] [blame] | 1794 | * eficonfig_display_change_boot_order() - display the BootOrder list |
| 1795 | * |
| 1796 | * @efi_menu: pointer to the efimenu structure |
| 1797 | * Return: status code |
| 1798 | */ |
| 1799 | static void eficonfig_display_change_boot_order(struct efimenu *efi_menu) |
| 1800 | { |
| 1801 | bool reverse; |
| 1802 | struct list_head *pos, *n; |
| 1803 | struct eficonfig_boot_order *entry; |
| 1804 | |
| 1805 | printf(ANSI_CLEAR_CONSOLE ANSI_CURSOR_POSITION |
| 1806 | "\n ** Change Boot Order **\n" |
| 1807 | ANSI_CURSOR_POSITION |
| 1808 | " Press UP/DOWN to move, +/- to change order" |
| 1809 | ANSI_CURSOR_POSITION |
| 1810 | " Press SPACE to activate or deactivate the entry" |
| 1811 | ANSI_CURSOR_POSITION |
| 1812 | " Select [Save] to complete, ESC/CTRL+C to quit" |
| 1813 | ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, |
| 1814 | 1, 1, efi_menu->count + 5, 1, efi_menu->count + 6, 1, |
| 1815 | efi_menu->count + 7, 1, efi_menu->count + 8, 1); |
| 1816 | |
| 1817 | /* draw boot option list */ |
| 1818 | list_for_each_safe(pos, n, &efi_menu->list) { |
| 1819 | entry = list_entry(pos, struct eficonfig_boot_order, list); |
| 1820 | reverse = (entry->num == efi_menu->active); |
| 1821 | |
| 1822 | printf(ANSI_CURSOR_POSITION, entry->num + 4, 7); |
| 1823 | |
| 1824 | if (reverse) |
| 1825 | puts(ANSI_COLOR_REVERSE); |
| 1826 | |
| 1827 | if (entry->num < efi_menu->count - 2) { |
| 1828 | if (entry->active) |
| 1829 | printf("[*] "); |
| 1830 | else |
| 1831 | printf("[ ] "); |
| 1832 | } |
| 1833 | |
| 1834 | printf("%ls", entry->description); |
| 1835 | |
| 1836 | if (reverse) |
| 1837 | puts(ANSI_COLOR_RESET); |
| 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | /** |
| 1842 | * eficonfig_choice_change_boot_order() - handle the BootOrder update |
| 1843 | * |
| 1844 | * @efi_menu: pointer to the efimenu structure |
| 1845 | * Return: status code |
| 1846 | */ |
| 1847 | static efi_status_t eficonfig_choice_change_boot_order(struct efimenu *efi_menu) |
| 1848 | { |
| 1849 | int esc = 0; |
| 1850 | struct list_head *pos, *n; |
| 1851 | struct eficonfig_boot_order *tmp; |
| 1852 | enum bootmenu_key key = KEY_NONE; |
| 1853 | struct eficonfig_boot_order *entry; |
| 1854 | |
| 1855 | while (1) { |
| 1856 | bootmenu_loop(NULL, &key, &esc); |
| 1857 | |
| 1858 | switch (key) { |
| 1859 | case KEY_PLUS: |
| 1860 | if (efi_menu->active > 0) { |
| 1861 | list_for_each_safe(pos, n, &efi_menu->list) { |
| 1862 | entry = list_entry(pos, struct eficonfig_boot_order, list); |
| 1863 | if (entry->num == efi_menu->active) |
| 1864 | break; |
| 1865 | } |
| 1866 | tmp = list_entry(pos->prev, struct eficonfig_boot_order, list); |
| 1867 | entry->num--; |
| 1868 | tmp->num++; |
| 1869 | list_del(&tmp->list); |
| 1870 | list_add(&tmp->list, &entry->list); |
| 1871 | } |
| 1872 | fallthrough; |
| 1873 | case KEY_UP: |
| 1874 | if (efi_menu->active > 0) |
| 1875 | --efi_menu->active; |
| 1876 | return EFI_NOT_READY; |
| 1877 | case KEY_MINUS: |
| 1878 | if (efi_menu->active < efi_menu->count - 3) { |
| 1879 | list_for_each_safe(pos, n, &efi_menu->list) { |
| 1880 | entry = list_entry(pos, struct eficonfig_boot_order, list); |
| 1881 | if (entry->num == efi_menu->active) |
| 1882 | break; |
| 1883 | } |
| 1884 | tmp = list_entry(pos->next, struct eficonfig_boot_order, list); |
| 1885 | entry->num++; |
| 1886 | tmp->num--; |
| 1887 | list_del(&entry->list); |
| 1888 | list_add(&entry->list, &tmp->list); |
| 1889 | |
| 1890 | ++efi_menu->active; |
| 1891 | } |
| 1892 | return EFI_NOT_READY; |
| 1893 | case KEY_DOWN: |
| 1894 | if (efi_menu->active < efi_menu->count - 1) |
| 1895 | ++efi_menu->active; |
| 1896 | return EFI_NOT_READY; |
| 1897 | case KEY_SELECT: |
| 1898 | /* "Save" */ |
| 1899 | if (efi_menu->active == efi_menu->count - 2) |
| 1900 | return EFI_SUCCESS; |
| 1901 | |
| 1902 | /* "Quit" */ |
| 1903 | if (efi_menu->active == efi_menu->count - 1) |
| 1904 | return EFI_ABORTED; |
| 1905 | |
| 1906 | break; |
| 1907 | case KEY_SPACE: |
| 1908 | if (efi_menu->active < efi_menu->count - 2) { |
| 1909 | list_for_each_safe(pos, n, &efi_menu->list) { |
| 1910 | entry = list_entry(pos, struct eficonfig_boot_order, list); |
| 1911 | if (entry->num == efi_menu->active) { |
| 1912 | entry->active = entry->active ? false : true; |
| 1913 | return EFI_NOT_READY; |
| 1914 | } |
| 1915 | } |
| 1916 | } |
| 1917 | break; |
| 1918 | case KEY_QUIT: |
| 1919 | return EFI_ABORTED; |
| 1920 | default: |
| 1921 | /* Pressed key is not valid, no need to regenerate the menu */ |
| 1922 | break; |
| 1923 | } |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | /** |
| 1928 | * eficonfig_add_change_boot_order_entry() - add boot order entry |
| 1929 | * |
| 1930 | * @efi_menu: pointer to the efimenu structure |
| 1931 | * @boot_index: boot option index to be added |
| 1932 | * @active: flag to include the boot option into BootOrder |
| 1933 | * Return: status code |
| 1934 | */ |
| 1935 | static efi_status_t eficonfig_add_change_boot_order_entry(struct efimenu *efi_menu, |
| 1936 | u32 boot_index, bool active) |
| 1937 | { |
| 1938 | efi_status_t ret; |
| 1939 | efi_uintn_t size; |
| 1940 | void *load_option; |
| 1941 | struct efi_load_option lo; |
| 1942 | u16 varname[] = u"Boot####"; |
| 1943 | struct eficonfig_boot_order *entry; |
| 1944 | |
| 1945 | efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index); |
| 1946 | load_option = efi_get_var(varname, &efi_global_variable_guid, &size); |
| 1947 | if (!load_option) |
| 1948 | return EFI_SUCCESS; |
| 1949 | |
| 1950 | ret = efi_deserialize_load_option(&lo, load_option, &size); |
| 1951 | if (ret != EFI_SUCCESS) { |
| 1952 | free(load_option); |
| 1953 | return ret; |
| 1954 | } |
| 1955 | |
| 1956 | entry = calloc(1, sizeof(struct eficonfig_boot_order)); |
| 1957 | if (!entry) { |
| 1958 | free(load_option); |
| 1959 | return EFI_OUT_OF_RESOURCES; |
| 1960 | } |
| 1961 | |
| 1962 | entry->description = u16_strdup(lo.label); |
| 1963 | if (!entry->description) { |
| 1964 | free(load_option); |
| 1965 | free(entry); |
| 1966 | return EFI_OUT_OF_RESOURCES; |
| 1967 | } |
| 1968 | entry->num = efi_menu->count++; |
| 1969 | entry->boot_index = boot_index; |
| 1970 | entry->active = active; |
| 1971 | list_add_tail(&entry->list, &efi_menu->list); |
| 1972 | |
| 1973 | free(load_option); |
| 1974 | |
| 1975 | return EFI_SUCCESS; |
| 1976 | } |
| 1977 | |
| 1978 | /** |
| 1979 | * eficonfig_create_change_boot_order_entry() - create boot order entry |
| 1980 | * |
| 1981 | * @efi_menu: pointer to the efimenu structure |
| 1982 | * @bootorder: pointer to the BootOrder variable |
| 1983 | * @num: number of BootOrder entry |
| 1984 | * Return: status code |
| 1985 | */ |
| 1986 | static efi_status_t eficonfig_create_change_boot_order_entry(struct efimenu *efi_menu, |
| 1987 | u16 *bootorder, efi_uintn_t num) |
| 1988 | { |
| 1989 | u32 i; |
| 1990 | efi_status_t ret; |
| 1991 | struct eficonfig_boot_order *entry; |
| 1992 | |
| 1993 | /* list the load option in the order of BootOrder variable */ |
| 1994 | for (i = 0; i < num; i++) { |
| 1995 | if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2) |
| 1996 | break; |
| 1997 | |
| 1998 | ret = eficonfig_add_change_boot_order_entry(efi_menu, bootorder[i], true); |
| 1999 | if (ret != EFI_SUCCESS) |
| 2000 | goto out; |
| 2001 | } |
| 2002 | |
| 2003 | /* list the remaining load option not included in the BootOrder */ |
| 2004 | for (i = 0; i < 0xFFFF; i++) { |
| 2005 | if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2) |
| 2006 | break; |
| 2007 | |
| 2008 | /* If the index is included in the BootOrder, skip it */ |
| 2009 | if (search_bootorder(bootorder, num, i, NULL)) |
| 2010 | continue; |
| 2011 | |
| 2012 | ret = eficonfig_add_change_boot_order_entry(efi_menu, i, false); |
| 2013 | if (ret != EFI_SUCCESS) |
| 2014 | goto out; |
| 2015 | } |
| 2016 | |
| 2017 | /* add "Save" and "Quit" entries */ |
| 2018 | entry = calloc(1, sizeof(struct eficonfig_boot_order)); |
| 2019 | if (!entry) |
| 2020 | goto out; |
| 2021 | |
| 2022 | entry->num = efi_menu->count++; |
| 2023 | entry->description = u16_strdup(u"Save"); |
| 2024 | list_add_tail(&entry->list, &efi_menu->list); |
| 2025 | |
| 2026 | entry = calloc(1, sizeof(struct eficonfig_boot_order)); |
| 2027 | if (!entry) |
| 2028 | goto out; |
| 2029 | |
| 2030 | entry->num = efi_menu->count++; |
| 2031 | entry->description = u16_strdup(u"Quit"); |
| 2032 | list_add_tail(&entry->list, &efi_menu->list); |
| 2033 | |
| 2034 | efi_menu->active = 0; |
| 2035 | |
| 2036 | return EFI_SUCCESS; |
| 2037 | out: |
| 2038 | return EFI_OUT_OF_RESOURCES; |
| 2039 | } |
| 2040 | |
| 2041 | /** |
| 2042 | * eficonfig_process_change_boot_order() - handler to change boot order |
| 2043 | * |
| 2044 | * @data: pointer to the data for each entry |
| 2045 | * Return: status code |
| 2046 | */ |
| 2047 | static efi_status_t eficonfig_process_change_boot_order(void *data) |
| 2048 | { |
| 2049 | u32 count; |
| 2050 | u16 *bootorder; |
| 2051 | efi_status_t ret; |
| 2052 | efi_uintn_t num, size; |
| 2053 | struct list_head *pos, *n; |
| 2054 | struct eficonfig_boot_order *entry; |
| 2055 | struct efimenu *efi_menu; |
| 2056 | |
| 2057 | efi_menu = calloc(1, sizeof(struct efimenu)); |
| 2058 | if (!efi_menu) |
| 2059 | return EFI_OUT_OF_RESOURCES; |
| 2060 | |
| 2061 | bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); |
| 2062 | |
| 2063 | INIT_LIST_HEAD(&efi_menu->list); |
| 2064 | num = size / sizeof(u16); |
| 2065 | ret = eficonfig_create_change_boot_order_entry(efi_menu, bootorder, num); |
| 2066 | if (ret != EFI_SUCCESS) |
| 2067 | goto out; |
| 2068 | |
| 2069 | while (1) { |
| 2070 | eficonfig_display_change_boot_order(efi_menu); |
| 2071 | |
| 2072 | ret = eficonfig_choice_change_boot_order(efi_menu); |
| 2073 | if (ret == EFI_SUCCESS) { |
| 2074 | u16 *new_bootorder; |
| 2075 | |
| 2076 | new_bootorder = calloc(1, (efi_menu->count - 2) * sizeof(u16)); |
| 2077 | if (!new_bootorder) { |
| 2078 | ret = EFI_OUT_OF_RESOURCES; |
| 2079 | goto out; |
| 2080 | } |
| 2081 | |
| 2082 | /* create new BootOrder */ |
| 2083 | count = 0; |
| 2084 | list_for_each_safe(pos, n, &efi_menu->list) { |
| 2085 | entry = list_entry(pos, struct eficonfig_boot_order, list); |
| 2086 | if (entry->active) |
| 2087 | new_bootorder[count++] = entry->boot_index; |
| 2088 | } |
| 2089 | |
| 2090 | size = count * sizeof(u16); |
| 2091 | ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid, |
| 2092 | EFI_VARIABLE_NON_VOLATILE | |
| 2093 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 2094 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 2095 | size, new_bootorder, false); |
| 2096 | |
| 2097 | free(new_bootorder); |
| 2098 | goto out; |
| 2099 | } else if (ret == EFI_NOT_READY) { |
| 2100 | continue; |
| 2101 | } else { |
| 2102 | goto out; |
| 2103 | } |
| 2104 | } |
| 2105 | out: |
| 2106 | list_for_each_safe(pos, n, &efi_menu->list) { |
| 2107 | entry = list_entry(pos, struct eficonfig_boot_order, list); |
| 2108 | list_del(&entry->list); |
| 2109 | free(entry->description); |
| 2110 | free(entry); |
| 2111 | } |
| 2112 | |
| 2113 | free(bootorder); |
| 2114 | free(efi_menu); |
| 2115 | |
| 2116 | /* to stay the parent menu */ |
| 2117 | ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret; |
| 2118 | |
| 2119 | return ret; |
| 2120 | } |
| 2121 | |
| 2122 | /** |
Masahisa Kojima | bb8498a | 2022-09-12 17:33:53 +0900 | [diff] [blame] | 2123 | * delete_boot_option() - delete selected boot option |
| 2124 | * |
| 2125 | * @boot_index: boot option index to delete |
| 2126 | * Return: status code |
| 2127 | */ |
| 2128 | static efi_status_t delete_boot_option(u16 boot_index) |
| 2129 | { |
| 2130 | u16 *bootorder; |
| 2131 | u16 varname[9]; |
| 2132 | efi_status_t ret; |
| 2133 | unsigned int index; |
| 2134 | efi_uintn_t num, size; |
| 2135 | |
| 2136 | efi_create_indexed_name(varname, sizeof(varname), |
| 2137 | "Boot", boot_index); |
| 2138 | ret = efi_set_variable_int(varname, &efi_global_variable_guid, |
| 2139 | 0, 0, NULL, false); |
| 2140 | if (ret != EFI_SUCCESS) { |
| 2141 | log_err("delete boot option(%ls) failed\n", varname); |
| 2142 | return ret; |
| 2143 | } |
| 2144 | |
| 2145 | /* update BootOrder if necessary */ |
| 2146 | bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); |
| 2147 | if (!bootorder) |
| 2148 | return EFI_SUCCESS; |
| 2149 | |
| 2150 | num = size / sizeof(u16); |
| 2151 | if (!search_bootorder(bootorder, num, boot_index, &index)) |
| 2152 | return EFI_SUCCESS; |
| 2153 | |
| 2154 | memmove(&bootorder[index], &bootorder[index + 1], |
| 2155 | (num - index - 1) * sizeof(u16)); |
| 2156 | size -= sizeof(u16); |
| 2157 | ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid, |
| 2158 | EFI_VARIABLE_NON_VOLATILE | |
| 2159 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 2160 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 2161 | size, bootorder, false); |
| 2162 | |
| 2163 | return ret; |
| 2164 | } |
| 2165 | |
| 2166 | /** |
| 2167 | * eficonfig_process_delete_boot_option() - handler to delete boot option |
| 2168 | * |
| 2169 | * @data: pointer to the data for each entry |
| 2170 | * Return: status code |
| 2171 | */ |
| 2172 | static efi_status_t eficonfig_process_delete_boot_option(void *data) |
| 2173 | { |
| 2174 | efi_status_t ret; |
| 2175 | unsigned int selected; |
| 2176 | |
| 2177 | while (1) { |
| 2178 | ret = eficonfig_show_boot_selection(&selected); |
| 2179 | if (ret == EFI_SUCCESS) |
| 2180 | ret = delete_boot_option(selected); |
| 2181 | |
| 2182 | if (ret != EFI_SUCCESS) |
| 2183 | break; |
| 2184 | } |
| 2185 | |
| 2186 | /* to stay the parent menu */ |
| 2187 | ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret; |
| 2188 | |
| 2189 | return ret; |
| 2190 | } |
| 2191 | |
| 2192 | /** |
Masahisa Kojima | c416f1c | 2022-09-12 17:33:54 +0900 | [diff] [blame] | 2193 | * eficonfig_enumerate_boot_option() - enumerate the possible bootable media |
| 2194 | * |
| 2195 | * @opt: pointer to the media boot option structure |
| 2196 | * @volume_handles: pointer to the efi handles |
| 2197 | * @count: number of efi handle |
| 2198 | * Return: status code |
| 2199 | */ |
| 2200 | efi_status_t eficonfig_enumerate_boot_option(struct eficonfig_media_boot_option *opt, |
| 2201 | efi_handle_t *volume_handles, efi_status_t count) |
| 2202 | { |
| 2203 | u32 i; |
| 2204 | struct efi_handler *handler; |
| 2205 | efi_status_t ret = EFI_SUCCESS; |
| 2206 | |
| 2207 | for (i = 0; i < count; i++) { |
| 2208 | u16 *p; |
| 2209 | u16 dev_name[BOOTMENU_DEVICE_NAME_MAX]; |
| 2210 | char *optional_data; |
| 2211 | struct efi_load_option lo; |
| 2212 | char buf[BOOTMENU_DEVICE_NAME_MAX]; |
| 2213 | struct efi_device_path *device_path; |
| 2214 | |
| 2215 | ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler); |
| 2216 | if (ret != EFI_SUCCESS) |
| 2217 | continue; |
| 2218 | ret = efi_protocol_open(handler, (void **)&device_path, |
| 2219 | efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 2220 | if (ret != EFI_SUCCESS) |
| 2221 | continue; |
| 2222 | |
| 2223 | ret = efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEVICE_NAME_MAX); |
| 2224 | if (ret != EFI_SUCCESS) |
| 2225 | continue; |
| 2226 | |
| 2227 | p = dev_name; |
| 2228 | utf8_utf16_strncpy(&p, buf, strlen(buf)); |
| 2229 | |
| 2230 | lo.label = dev_name; |
| 2231 | lo.attributes = LOAD_OPTION_ACTIVE; |
| 2232 | lo.file_path = device_path; |
| 2233 | lo.file_path_length = efi_dp_size(device_path) + sizeof(END); |
| 2234 | /* |
| 2235 | * Set the dedicated guid to optional_data, it is used to identify |
| 2236 | * the boot option that automatically generated by the bootmenu. |
| 2237 | * efi_serialize_load_option() expects optional_data is null-terminated |
| 2238 | * utf8 string, so set the "1234567" string to allocate enough space |
| 2239 | * to store guid, instead of realloc the load_option. |
| 2240 | */ |
| 2241 | lo.optional_data = "1234567"; |
| 2242 | opt[i].size = efi_serialize_load_option(&lo, (u8 **)&opt[i].lo); |
| 2243 | if (!opt[i].size) { |
| 2244 | ret = EFI_OUT_OF_RESOURCES; |
| 2245 | goto out; |
| 2246 | } |
| 2247 | /* set the guid */ |
| 2248 | optional_data = (char *)opt[i].lo + (opt[i].size - u16_strsize(u"1234567")); |
| 2249 | memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t)); |
| 2250 | } |
| 2251 | |
| 2252 | out: |
| 2253 | return ret; |
| 2254 | } |
| 2255 | |
| 2256 | /** |
| 2257 | * eficonfig_delete_invalid_boot_option() - delete non-existing boot option |
| 2258 | * |
| 2259 | * @opt: pointer to the media boot option structure |
| 2260 | * @count: number of media boot option structure |
| 2261 | * Return: status code |
| 2262 | */ |
| 2263 | efi_status_t eficonfig_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt, |
| 2264 | efi_status_t count) |
| 2265 | { |
| 2266 | u32 i, j; |
| 2267 | efi_uintn_t size; |
Masahisa Kojima | c416f1c | 2022-09-12 17:33:54 +0900 | [diff] [blame] | 2268 | void *load_option; |
| 2269 | struct efi_load_option lo; |
| 2270 | u16 varname[] = u"Boot####"; |
Masahisa Kojima | 1167e88 | 2022-11-14 19:00:47 +0900 | [diff] [blame] | 2271 | efi_status_t ret = EFI_SUCCESS; |
Masahisa Kojima | c416f1c | 2022-09-12 17:33:54 +0900 | [diff] [blame] | 2272 | |
| 2273 | for (i = 0; i <= 0xFFFF; i++) { |
| 2274 | efi_uintn_t tmp; |
| 2275 | |
| 2276 | efi_create_indexed_name(varname, sizeof(varname), "Boot", i); |
| 2277 | load_option = efi_get_var(varname, &efi_global_variable_guid, &size); |
| 2278 | if (!load_option) |
| 2279 | continue; |
| 2280 | |
| 2281 | tmp = size; |
| 2282 | ret = efi_deserialize_load_option(&lo, load_option, &size); |
| 2283 | if (ret != EFI_SUCCESS) |
| 2284 | goto next; |
| 2285 | |
| 2286 | if (size >= sizeof(efi_guid_bootmenu_auto_generated)) { |
| 2287 | if (guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated) == 0) { |
| 2288 | for (j = 0; j < count; j++) { |
| 2289 | if (opt[j].size == tmp && |
| 2290 | memcmp(opt[j].lo, load_option, tmp) == 0) { |
| 2291 | opt[j].exist = true; |
| 2292 | break; |
| 2293 | } |
| 2294 | } |
| 2295 | |
| 2296 | if (j == count) { |
| 2297 | ret = delete_boot_option(i); |
| 2298 | if (ret != EFI_SUCCESS) { |
| 2299 | free(load_option); |
| 2300 | goto out; |
| 2301 | } |
| 2302 | } |
| 2303 | } |
| 2304 | } |
| 2305 | next: |
| 2306 | free(load_option); |
| 2307 | } |
| 2308 | |
| 2309 | out: |
| 2310 | return ret; |
| 2311 | } |
| 2312 | |
| 2313 | /** |
| 2314 | * eficonfig_generate_media_device_boot_option() - generate the media device boot option |
| 2315 | * |
| 2316 | * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL |
| 2317 | * and generate the bootmenu entries. |
| 2318 | * This function also provide the BOOT#### variable maintenance for |
| 2319 | * the media device entries. |
| 2320 | * - Automatically create the BOOT#### variable for the newly detected device, |
| 2321 | * this BOOT#### variable is distinguished by the special GUID |
| 2322 | * stored in the EFI_LOAD_OPTION.optional_data |
| 2323 | * - If the device is not attached to the system, the associated BOOT#### variable |
| 2324 | * is automatically deleted. |
| 2325 | * |
| 2326 | * Return: status code |
| 2327 | */ |
| 2328 | efi_status_t eficonfig_generate_media_device_boot_option(void) |
| 2329 | { |
| 2330 | u32 i; |
| 2331 | efi_status_t ret; |
| 2332 | efi_uintn_t count; |
| 2333 | efi_handle_t *volume_handles = NULL; |
| 2334 | struct eficonfig_media_boot_option *opt = NULL; |
| 2335 | |
| 2336 | ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid, |
| 2337 | NULL, &count, (efi_handle_t **)&volume_handles); |
| 2338 | if (ret != EFI_SUCCESS) |
| 2339 | return ret; |
| 2340 | |
| 2341 | opt = calloc(count, sizeof(struct eficonfig_media_boot_option)); |
| 2342 | if (!opt) |
| 2343 | goto out; |
| 2344 | |
| 2345 | /* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */ |
| 2346 | ret = eficonfig_enumerate_boot_option(opt, volume_handles, count); |
| 2347 | if (ret != EFI_SUCCESS) |
| 2348 | goto out; |
| 2349 | |
| 2350 | /* |
| 2351 | * System hardware configuration may vary depending on the user setup. |
| 2352 | * The boot option is automatically added by the bootmenu. |
| 2353 | * If the device is not attached to the system, the boot option needs |
| 2354 | * to be deleted. |
| 2355 | */ |
| 2356 | ret = eficonfig_delete_invalid_boot_option(opt, count); |
| 2357 | if (ret != EFI_SUCCESS) |
| 2358 | goto out; |
| 2359 | |
| 2360 | /* add non-existent boot option */ |
| 2361 | for (i = 0; i < count; i++) { |
| 2362 | u32 boot_index; |
| 2363 | u16 var_name[9]; |
| 2364 | |
| 2365 | if (!opt[i].exist) { |
| 2366 | ret = eficonfig_get_unused_bootoption(var_name, sizeof(var_name), |
| 2367 | &boot_index); |
| 2368 | if (ret != EFI_SUCCESS) |
| 2369 | goto out; |
| 2370 | |
| 2371 | ret = efi_set_variable_int(var_name, &efi_global_variable_guid, |
| 2372 | EFI_VARIABLE_NON_VOLATILE | |
| 2373 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 2374 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 2375 | opt[i].size, opt[i].lo, false); |
| 2376 | if (ret != EFI_SUCCESS) |
| 2377 | goto out; |
| 2378 | |
| 2379 | ret = eficonfig_append_bootorder(boot_index); |
| 2380 | if (ret != EFI_SUCCESS) { |
| 2381 | efi_set_variable_int(var_name, &efi_global_variable_guid, |
| 2382 | 0, 0, NULL, false); |
| 2383 | goto out; |
| 2384 | } |
| 2385 | } |
| 2386 | } |
| 2387 | |
| 2388 | out: |
| 2389 | if (opt) { |
| 2390 | for (i = 0; i < count; i++) |
| 2391 | free(opt[i].lo); |
| 2392 | } |
| 2393 | free(opt); |
| 2394 | efi_free_pool(volume_handles); |
| 2395 | |
| 2396 | return ret; |
| 2397 | } |
| 2398 | |
Masahisa Kojima | c416f1c | 2022-09-12 17:33:54 +0900 | [diff] [blame] | 2399 | /** |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 2400 | * eficonfig_init() - do required initialization for eficonfig command |
| 2401 | * |
| 2402 | * Return: status code |
| 2403 | */ |
| 2404 | static efi_status_t eficonfig_init(void) |
| 2405 | { |
| 2406 | efi_status_t ret = EFI_SUCCESS; |
| 2407 | static bool init; |
| 2408 | struct efi_handler *handler; |
| 2409 | |
| 2410 | if (!init) { |
| 2411 | ret = efi_search_protocol(efi_root, &efi_guid_text_input_protocol, &handler); |
| 2412 | if (ret != EFI_SUCCESS) |
| 2413 | return ret; |
| 2414 | |
| 2415 | ret = efi_protocol_open(handler, (void **)&cin, efi_root, NULL, |
| 2416 | EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 2417 | if (ret != EFI_SUCCESS) |
| 2418 | return ret; |
| 2419 | } |
| 2420 | |
| 2421 | init = true; |
| 2422 | |
| 2423 | return ret; |
| 2424 | } |
| 2425 | |
| 2426 | static const struct eficonfig_item maintenance_menu_items[] = { |
| 2427 | {"Add Boot Option", eficonfig_process_add_boot_option}, |
Masahisa Kojima | e34158b | 2022-09-12 17:33:51 +0900 | [diff] [blame] | 2428 | {"Edit Boot Option", eficonfig_process_edit_boot_option}, |
Masahisa Kojima | e5948ee | 2022-09-12 17:33:56 +0900 | [diff] [blame] | 2429 | {"Change Boot Order", eficonfig_process_change_boot_order}, |
Masahisa Kojima | bb8498a | 2022-09-12 17:33:53 +0900 | [diff] [blame] | 2430 | {"Delete Boot Option", eficonfig_process_delete_boot_option}, |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 2431 | {"Quit", eficonfig_process_quit}, |
| 2432 | }; |
| 2433 | |
| 2434 | /** |
| 2435 | * do_eficonfig() - execute `eficonfig` command |
| 2436 | * |
| 2437 | * @cmdtp: table entry describing command |
| 2438 | * @flag: bitmap indicating how the command was invoked |
| 2439 | * @argc: number of arguments |
| 2440 | * @argv: command line arguments |
| 2441 | * Return: status code |
| 2442 | */ |
| 2443 | static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 2444 | { |
| 2445 | efi_status_t ret; |
| 2446 | struct efimenu *efi_menu; |
| 2447 | |
| 2448 | if (argc > 1) |
| 2449 | return CMD_RET_USAGE; |
| 2450 | |
| 2451 | ret = efi_init_obj_list(); |
| 2452 | if (ret != EFI_SUCCESS) { |
| 2453 | log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n", |
| 2454 | ret & ~EFI_ERROR_MASK); |
| 2455 | |
| 2456 | return CMD_RET_FAILURE; |
| 2457 | } |
| 2458 | |
| 2459 | ret = eficonfig_init(); |
| 2460 | if (ret != EFI_SUCCESS) |
| 2461 | return CMD_RET_FAILURE; |
| 2462 | |
Masahisa Kojima | b5135a1 | 2022-09-12 17:33:55 +0900 | [diff] [blame] | 2463 | ret = eficonfig_generate_media_device_boot_option(); |
| 2464 | if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND) |
| 2465 | return ret; |
| 2466 | |
Masahisa Kojima | 87d7914 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 2467 | while (1) { |
| 2468 | efi_menu = eficonfig_create_fixed_menu(maintenance_menu_items, |
| 2469 | ARRAY_SIZE(maintenance_menu_items)); |
| 2470 | if (!efi_menu) |
| 2471 | return CMD_RET_FAILURE; |
| 2472 | |
| 2473 | ret = eficonfig_process_common(efi_menu, " ** UEFI Maintenance Menu **"); |
| 2474 | eficonfig_destroy(efi_menu); |
| 2475 | |
| 2476 | if (ret == EFI_ABORTED) |
| 2477 | break; |
| 2478 | } |
| 2479 | |
| 2480 | return CMD_RET_SUCCESS; |
| 2481 | } |
| 2482 | |
| 2483 | U_BOOT_CMD( |
| 2484 | eficonfig, 1, 0, do_eficonfig, |
| 2485 | "provide menu-driven UEFI variable maintenance interface", |
| 2486 | "" |
| 2487 | ); |