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