1 // SPDX-License-Identifier: GPL-2.0+
3 * Menu-driven UEFI Variable maintenance
5 * Copyright (c) 2022 Masahisa Kojima, Linaro Limited
11 #include <efi_loader.h>
12 #include <efi_load_initrd.h>
13 #include <efi_config.h>
14 #include <efi_variable.h>
20 #include <asm/unaligned.h>
21 #include <linux/delay.h>
23 static struct efi_simple_text_input_protocol *cin;
25 #define EFICONFIG_DESCRIPTION_MAX 32
26 #define EFICONFIG_OPTIONAL_DATA_MAX 64
29 * struct eficonfig_filepath_info - structure to be used to store file path
31 * @name: file or directory name
32 * @list: list structure
34 struct eficonfig_filepath_info {
36 struct list_head list;
40 * struct eficonfig_boot_option - structure to be used for updating UEFI boot option
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
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;
59 * struct eficonfig_volume_entry_data - structure to be used to store volume info
61 * @file_info: pointer to file info structure
62 * @v: pointer to the protocol interface
63 * @dp: pointer to the device path
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;
72 * struct eficonfig_file_entry_data - structure to be used to store file info
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
78 struct eficonfig_file_entry_data {
79 struct eficonfig_select_file_info *file_info;
85 * struct eficonfig_boot_selection_data - structure to be used to select the boot option entry
87 * @boot_index: index of the boot option
88 * @selected: pointer to store the selected index in the BootOrder variable
90 struct eficonfig_boot_selection_data {
96 * struct eficonfig_boot_order - structure to be used to update BootOrder variable
98 * @num: index in the menu entry
99 * @description: pointer to the description string
100 * @boot_index: boot option index
101 * @active: flag to include the boot option into BootOrder variable
102 * @list: list structure
104 struct eficonfig_boot_order {
109 struct list_head list;
113 * eficonfig_print_msg() - print message
115 * display the message to the user, user proceeds the screen
116 * with any key press.
118 * @items: pointer to the structure of each menu entry
119 * @count: the number of menu entry
120 * @menu_header: pointer to the menu header string
121 * Return: status code
123 void eficonfig_print_msg(char *msg)
129 printf(ANSI_CURSOR_HIDE
132 "%s\n\n Press any key to continue", 3, 4, msg);
138 * eficonfig_print_entry() - print each menu entry
140 * @data: pointer to the data associated with each menu entry
142 static void eficonfig_print_entry(void *data)
144 struct eficonfig_entry *entry = data;
145 int reverse = (entry->efi_menu->active == entry->num);
147 /* TODO: support scroll or page for many entries */
150 * Move cursor to line where the entry will be drawn (entry->num)
151 * First 3 lines(menu header) + 1 empty line
153 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
156 puts(ANSI_COLOR_REVERSE);
158 printf("%s", entry->title);
161 puts(ANSI_COLOR_RESET);
165 * eficonfig_display_statusline() - print status line
167 * @m: pointer to the menu structure
169 static void eficonfig_display_statusline(struct menu *m)
171 struct eficonfig_entry *entry;
173 if (menu_default_choice(m, (void *)&entry) < 0)
176 printf(ANSI_CURSOR_POSITION
178 ANSI_CURSOR_POSITION ANSI_CLEAR_LINE ANSI_CURSOR_POSITION
179 " Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit"
180 ANSI_CLEAR_LINE_TO_END ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
181 1, 1, entry->efi_menu->menu_header, entry->efi_menu->count + 5, 1,
182 entry->efi_menu->count + 6, 1, entry->efi_menu->count + 7, 1);
186 * eficonfig_choice_entry() - user key input handler
188 * @data: pointer to the efimenu structure
189 * Return: key string to identify the selected entry
191 static char *eficonfig_choice_entry(void *data)
194 struct list_head *pos, *n;
195 struct eficonfig_entry *entry;
196 enum bootmenu_key key = KEY_NONE;
197 struct efimenu *efi_menu = data;
200 bootmenu_loop((struct bootmenu_data *)efi_menu, &key, &esc);
204 if (efi_menu->active > 0)
206 /* no menu key selected, regenerate menu */
209 if (efi_menu->active < efi_menu->count - 1)
211 /* no menu key selected, regenerate menu */
214 list_for_each_safe(pos, n, &efi_menu->list) {
215 entry = list_entry(pos, struct eficonfig_entry, list);
216 if (entry->num == efi_menu->active)
221 /* Quit by choosing the last entry */
222 entry = list_last_entry(&efi_menu->list, struct eficonfig_entry, list);
225 /* Pressed key is not valid, no need to regenerate the menu */
232 * eficonfig_destroy() - destroy efimenu
234 * @efi_menu: pointer to the efimenu structure
236 void eficonfig_destroy(struct efimenu *efi_menu)
238 struct list_head *pos, *n;
239 struct eficonfig_entry *entry;
244 list_for_each_safe(pos, n, &efi_menu->list) {
245 entry = list_entry(pos, struct eficonfig_entry, list);
247 list_del(&entry->list);
250 free(efi_menu->menu_header);
255 * eficonfig_process_quit() - callback function for "Quit" entry
257 * @data: pointer to the data
258 * Return: status code
260 efi_status_t eficonfig_process_quit(void *data)
266 * eficonfig_append_menu_entry() - append menu item
268 * @efi_menu: pointer to the efimenu structure
269 * @title: pointer to the entry title
270 * @func: callback of each entry
271 * @data: pointer to the data to be passed to each entry callback
272 * Return: status code
274 efi_status_t eficonfig_append_menu_entry(struct efimenu *efi_menu,
275 char *title, eficonfig_entry_func func,
278 struct eficonfig_entry *entry;
280 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX)
281 return EFI_OUT_OF_RESOURCES;
283 entry = calloc(1, sizeof(struct eficonfig_entry));
285 return EFI_OUT_OF_RESOURCES;
287 entry->title = title;
288 sprintf(entry->key, "%d", efi_menu->count);
289 entry->efi_menu = efi_menu;
292 entry->num = efi_menu->count++;
293 list_add_tail(&entry->list, &efi_menu->list);
299 * eficonfig_append_quit_entry() - append quit entry
301 * @efi_menu: pointer to the efimenu structure
302 * Return: status code
304 efi_status_t eficonfig_append_quit_entry(struct efimenu *efi_menu)
309 title = strdup("Quit");
311 return EFI_OUT_OF_RESOURCES;
313 ret = eficonfig_append_menu_entry(efi_menu, title, eficonfig_process_quit, NULL);
314 if (ret != EFI_SUCCESS)
321 * eficonfig_create_fixed_menu() - create fixed entry menu structure
323 * @items: pointer to the menu entry item
324 * @count: the number of menu entry
325 * Return: pointer to the efimenu structure
327 void *eficonfig_create_fixed_menu(const struct eficonfig_item *items, int count)
332 struct efimenu *efi_menu;
333 const struct eficonfig_item *iter = items;
335 efi_menu = calloc(1, sizeof(struct efimenu));
339 INIT_LIST_HEAD(&efi_menu->list);
340 for (i = 0; i < count; i++, iter++) {
341 title = strdup(iter->title);
345 ret = eficonfig_append_menu_entry(efi_menu, title, iter->func, iter->data);
346 if (ret != EFI_SUCCESS) {
354 eficonfig_destroy(efi_menu);
360 * eficonfig_process_common() - main handler for UEFI menu
362 * Construct the structures required to show the menu, then handle
363 * the user input interacting with u-boot menu functions.
365 * @efi_menu: pointer to the efimenu structure
366 * @menu_header: pointer to the menu header string
367 * Return: status code
369 efi_status_t eficonfig_process_common(struct efimenu *efi_menu, char *menu_header)
373 struct list_head *pos, *n;
374 struct eficonfig_entry *entry;
375 efi_status_t ret = EFI_SUCCESS;
377 if (efi_menu->count > EFICONFIG_ENTRY_NUM_MAX)
378 return EFI_OUT_OF_RESOURCES;
380 efi_menu->delay = -1;
381 efi_menu->active = 0;
384 efi_menu->menu_header = strdup(menu_header);
385 if (!efi_menu->menu_header)
386 return EFI_OUT_OF_RESOURCES;
389 menu = menu_create(NULL, 0, 1, eficonfig_display_statusline,
390 eficonfig_print_entry, eficonfig_choice_entry,
393 return EFI_INVALID_PARAMETER;
395 list_for_each_safe(pos, n, &efi_menu->list) {
396 entry = list_entry(pos, struct eficonfig_entry, list);
397 if (!menu_item_add(menu, entry->key, entry)) {
398 ret = EFI_INVALID_PARAMETER;
403 entry = list_first_entry_or_null(&efi_menu->list, struct eficonfig_entry, list);
405 menu_default_set(menu, entry->key);
407 printf(ANSI_CURSOR_HIDE
409 ANSI_CURSOR_POSITION, 1, 1);
411 if (menu_get_choice(menu, &choice)) {
414 ret = entry->func(entry->data);
419 printf(ANSI_CLEAR_CONSOLE
421 ANSI_CURSOR_SHOW, 1, 1);
427 * eficonfig_volume_selected() - handler of volume selection
429 * @data: pointer to the data of selected entry
430 * Return: status code
432 static efi_status_t eficonfig_volume_selected(void *data)
434 struct eficonfig_volume_entry_data *info = data;
437 info->file_info->current_volume = info->v;
438 info->file_info->dp_volume = info->dp;
445 * create_selected_device_path() - create device path
447 * @file_info: pointer to the selected file information
449 * device path or NULL. Caller must free the returned value
452 struct efi_device_path *create_selected_device_path(struct eficonfig_select_file_info *file_info)
457 struct efi_device_path *dp;
458 struct efi_device_path_file_path *fp;
460 fp_size = sizeof(struct efi_device_path) +
461 ((u16_strlen(file_info->current_path) + 1) * sizeof(u16));
462 buf = calloc(1, fp_size + sizeof(END));
467 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
468 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
469 fp->dp.length = (u16)fp_size;
470 u16_strcpy(fp->str, file_info->current_path);
474 *((struct efi_device_path *)p) = END;
476 dp = efi_dp_append(file_info->dp_volume, (struct efi_device_path *)buf);
483 * eficonfig_file_selected() - handler of file selection
485 * @data: pointer to the data of selected entry
486 * Return: status code
488 static efi_status_t eficonfig_file_selected(void *data)
491 struct eficonfig_file_entry_data *info = data;
494 return EFI_INVALID_PARAMETER;
496 if (!strcmp(info->file_name, "..")) {
497 struct eficonfig_filepath_info *iter;
498 struct list_head *pos, *n;
501 tmp = info->file_info->current_path;
503 memset(info->file_info->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
504 filepath = calloc(1, EFICONFIG_FILE_PATH_MAX);
506 return EFI_OUT_OF_RESOURCES;
508 list_for_each_safe(pos, n, &info->file_info->filepath_list) {
509 iter = list_entry(pos, struct eficonfig_filepath_info, list);
511 is_last = list_is_last(&iter->list, &info->file_info->filepath_list);
513 list_del(&iter->list);
518 strlcat(filepath, iter->name, EFICONFIG_FILE_PATH_MAX);
520 utf8_utf16_strcpy(&tmp, filepath);
523 struct eficonfig_filepath_info *filepath_info;
525 new_len = u16_strlen(info->file_info->current_path) +
526 strlen(info->file_name);
527 if (new_len >= EFICONFIG_FILE_PATH_MAX) {
528 eficonfig_print_msg("File path is too long!");
529 return EFI_INVALID_PARAMETER;
531 tmp = &info->file_info->current_path[u16_strlen(info->file_info->current_path)];
532 utf8_utf16_strcpy(&tmp, info->file_name);
534 filepath_info = calloc(1, sizeof(struct eficonfig_filepath_info));
536 return EFI_OUT_OF_RESOURCES;
538 filepath_info->name = strdup(info->file_name);
539 if (!filepath_info->name) {
541 return EFI_OUT_OF_RESOURCES;
543 list_add_tail(&filepath_info->list, &info->file_info->filepath_list);
545 if (!info->is_directory)
546 info->file_info->file_selected = true;
553 * eficonfig_select_volume() - construct the volume selection menu
555 * @file_info: pointer to the file selection structure
556 * Return: status code
558 static efi_status_t eficonfig_select_volume(struct eficonfig_select_file_info *file_info)
563 struct efimenu *efi_menu;
564 struct list_head *pos, *n;
565 struct efi_handler *handler;
566 struct eficonfig_entry *entry;
567 struct efi_device_path *device_path;
568 efi_handle_t *volume_handles = NULL;
569 struct efi_simple_file_system_protocol *v;
571 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
572 NULL, &count, (efi_handle_t **)&volume_handles);
573 if (ret != EFI_SUCCESS) {
574 eficonfig_print_msg("No block device found!");
578 efi_menu = calloc(1, sizeof(struct efimenu));
580 return EFI_OUT_OF_RESOURCES;
582 INIT_LIST_HEAD(&efi_menu->list);
583 for (i = 0; i < count; i++) {
585 struct efi_block_io *block_io;
586 struct eficonfig_volume_entry_data *info;
588 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
591 ret = efi_search_protocol(volume_handles[i],
592 &efi_simple_file_system_protocol_guid, &handler);
593 if (ret != EFI_SUCCESS)
595 ret = efi_protocol_open(handler, (void **)&v, efi_root, NULL,
596 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
597 if (ret != EFI_SUCCESS)
600 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
601 if (ret != EFI_SUCCESS)
603 ret = efi_protocol_open(handler, (void **)&device_path,
604 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
605 if (ret != EFI_SUCCESS)
608 ret = efi_search_protocol(volume_handles[i], &efi_block_io_guid, &handler);
609 if (ret != EFI_SUCCESS)
611 ret = efi_protocol_open(handler, (void **)&block_io,
612 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
613 if (ret != EFI_SUCCESS)
616 info = calloc(1, sizeof(struct eficonfig_volume_entry_data));
618 ret = EFI_OUT_OF_RESOURCES;
622 devname = calloc(1, BOOTMENU_DEVICE_NAME_MAX);
625 ret = EFI_OUT_OF_RESOURCES;
628 ret = efi_disk_get_device_name(volume_handles[i], devname,
629 BOOTMENU_DEVICE_NAME_MAX);
630 if (ret != EFI_SUCCESS) {
636 info->dp = device_path;
637 info->file_info = file_info;
638 ret = eficonfig_append_menu_entry(efi_menu, devname, eficonfig_volume_selected,
640 if (ret != EFI_SUCCESS) {
646 ret = eficonfig_append_quit_entry(efi_menu);
647 if (ret != EFI_SUCCESS)
650 ret = eficonfig_process_common(efi_menu, " ** Select Volume **");
652 efi_free_pool(volume_handles);
653 list_for_each_safe(pos, n, &efi_menu->list) {
654 entry = list_entry(pos, struct eficonfig_entry, list);
657 eficonfig_destroy(efi_menu);
663 * sort_file() - sort the file name in ascii order
665 * @data1: pointer to the file entry data
666 * @data2: pointer to the file entry data
667 * Return: -1 if the data1 file name is less than data2 file name,
668 * 0 if both file name match,
669 * 1 if the data1 file name is greater thant data2 file name.
671 static int sort_file(const void *arg1, const void *arg2)
673 const struct eficonfig_file_entry_data *data1, *data2;
675 data1 = *((const struct eficonfig_file_entry_data **)arg1);
676 data2 = *((const struct eficonfig_file_entry_data **)arg2);
678 return strcasecmp(data1->file_name, data2->file_name);
682 * eficonfig_create_file_entry() - construct the file menu entry
684 * @efi_menu: pointer to the efimenu structure
685 * @count: number of the directory and file
686 * @tmp_infos: pointer to the entry data array
687 * @f: pointer to the file handle
688 * @buf: pointer to the buffer to store the directory information
689 * @file_info: pointer to the file selection structure
690 * Return: status code
693 eficonfig_create_file_entry(struct efimenu *efi_menu, u32 count,
694 struct eficonfig_file_entry_data **tmp_infos,
695 struct efi_file_handle *f, struct efi_file_info *buf,
696 struct eficonfig_select_file_info *file_info)
701 u32 i, entry_num = 0;
702 struct eficonfig_file_entry_data *info;
704 efi_file_setpos_int(f, 0);
705 /* Read directory and construct menu structure */
706 for (i = 0; i < count; i++) {
707 if (entry_num >= EFICONFIG_ENTRY_NUM_MAX - 1)
710 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
711 ret = efi_file_read_int(f, &len, buf);
712 if (ret != EFI_SUCCESS || len == 0)
715 info = calloc(1, sizeof(struct eficonfig_file_entry_data));
717 ret = EFI_OUT_OF_RESOURCES;
721 /* append '\\' at the end of directory name */
722 name = calloc(1, utf16_utf8_strlen(buf->file_name) + 2);
724 ret = EFI_OUT_OF_RESOURCES;
729 utf16_utf8_strcpy(&p, buf->file_name);
730 if (buf->attribute & EFI_FILE_DIRECTORY) {
731 /* filter out u'.' */
732 if (!u16_strcmp(buf->file_name, u".")) {
737 name[u16_strlen(buf->file_name)] = '\\';
738 info->is_directory = true;
741 info->file_name = name;
742 info->file_info = file_info;
743 tmp_infos[entry_num++] = info;
746 qsort(tmp_infos, entry_num, sizeof(*tmp_infos),
747 (int (*)(const void *, const void *))sort_file);
749 for (i = 0; i < entry_num; i++) {
750 ret = eficonfig_append_menu_entry(efi_menu, tmp_infos[i]->file_name,
751 eficonfig_file_selected, tmp_infos[i]);
752 if (ret != EFI_SUCCESS)
761 * eficonfig_show_file_selection() - construct the file selection menu
763 * @file_info: pointer to the file selection structure
764 * @root: pointer to the file handle
765 * Return: status code
767 static efi_status_t eficonfig_show_file_selection(struct eficonfig_select_file_info *file_info,
768 struct efi_file_handle *root)
773 struct efimenu *efi_menu;
774 struct efi_file_handle *f;
775 struct efi_file_info *buf;
776 struct eficonfig_file_entry_data **tmp_infos;
778 buf = calloc(1, sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE);
780 return EFI_OUT_OF_RESOURCES;
782 while (!file_info->file_selected) {
783 efi_menu = calloc(1, sizeof(struct efimenu));
785 ret = EFI_OUT_OF_RESOURCES;
788 INIT_LIST_HEAD(&efi_menu->list);
790 ret = efi_file_open_int(root, &f, file_info->current_path, EFI_FILE_MODE_READ, 0);
791 if (ret != EFI_SUCCESS) {
792 eficonfig_print_msg("Reading volume failed!");
798 /* Count the number of directory entries */
800 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
801 ret = efi_file_read_int(f, &len, buf);
802 if (ret != EFI_SUCCESS || len == 0)
808 /* allocate array to sort the entry */
809 tmp_infos = calloc(count, sizeof(*tmp_infos));
811 ret = EFI_OUT_OF_RESOURCES;
815 ret = eficonfig_create_file_entry(efi_menu, count, tmp_infos,
817 if (ret != EFI_SUCCESS)
820 ret = eficonfig_append_quit_entry(efi_menu);
821 if (ret != EFI_SUCCESS)
824 ret = eficonfig_process_common(efi_menu, " ** Select File **");
826 efi_file_close_int(f);
827 eficonfig_destroy(efi_menu);
830 for (i = 0; i < count; i++)
836 if (ret != EFI_SUCCESS)
847 * handle_user_input() - handle user input
849 * @buf: pointer to the buffer
850 * @buf_size: size of the buffer
851 * @cursor_col: cursor column for user input
852 * @msg: pointer to the string to display
853 * Return: status code
855 static efi_status_t handle_user_input(u16 *buf, int buf_size,
856 int cursor_col, char *msg)
861 printf(ANSI_CLEAR_CONSOLE
865 " Press ENTER to complete, ESC/CTRL+C to quit",
868 /* tmp is used to accept user cancel */
869 tmp = calloc(1, buf_size * sizeof(u16));
871 return EFI_OUT_OF_RESOURCES;
873 ret = efi_console_get_u16_string(cin, tmp, buf_size, NULL, 4, cursor_col);
874 if (ret == EFI_SUCCESS)
875 u16_strcpy(buf, tmp);
879 /* to stay the parent menu */
880 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
886 * eficonfig_boot_add_enter_description() - handle user input for description
888 * @data: pointer to the internal boot option structure
889 * Return: status code
891 static efi_status_t eficonfig_boot_add_enter_description(void *data)
893 struct eficonfig_boot_option *bo = data;
895 return handle_user_input(bo->description, EFICONFIG_DESCRIPTION_MAX, 22,
896 "\n ** Edit Description **\n"
898 " enter description: ");
902 * eficonfig_boot_add_optional_data() - handle user input for optional data
904 * @data: pointer to the internal boot option structure
905 * Return: status code
907 static efi_status_t eficonfig_boot_add_optional_data(void *data)
909 struct eficonfig_boot_option *bo = data;
911 return handle_user_input(bo->optional_data, EFICONFIG_OPTIONAL_DATA_MAX, 24,
912 "\n ** Edit Optional Data **\n"
914 " enter optional data:");
918 * eficonfig_boot_edit_save() - handler to save the boot option
920 * @data: pointer to the internal boot option structure
921 * Return: status code
923 static efi_status_t eficonfig_boot_edit_save(void *data)
925 struct eficonfig_boot_option *bo = data;
927 if (u16_strlen(bo->description) == 0) {
928 eficonfig_print_msg("Boot Description is empty!");
929 bo->edit_completed = false;
930 return EFI_NOT_READY;
932 if (u16_strlen(bo->file_info.current_path) == 0) {
933 eficonfig_print_msg("File is not selected!");
934 bo->edit_completed = false;
935 return EFI_NOT_READY;
938 bo->edit_completed = true;
944 * eficonfig_process_clear_file_selection() - callback function for "Clear" entry
946 * @data: pointer to the data
947 * Return: status code
949 efi_status_t eficonfig_process_clear_file_selection(void *data)
951 struct eficonfig_select_file_info *file_info = data;
953 /* clear the existing file information */
954 file_info->current_volume = NULL;
955 file_info->current_path[0] = u'\0';
956 file_info->dp_volume = NULL;
961 static struct eficonfig_item select_file_menu_items[] = {
962 {"Select File", eficonfig_process_select_file},
963 {"Clear", eficonfig_process_clear_file_selection},
964 {"Quit", eficonfig_process_quit},
968 * eficonfig_process_show_file_option() - display select file option
970 * @file_info: pointer to the file information structure
971 * Return: status code
973 efi_status_t eficonfig_process_show_file_option(void *data)
976 struct efimenu *efi_menu;
978 select_file_menu_items[0].data = data;
979 select_file_menu_items[1].data = data;
980 efi_menu = eficonfig_create_fixed_menu(select_file_menu_items,
981 ARRAY_SIZE(select_file_menu_items));
983 return EFI_OUT_OF_RESOURCES;
985 ret = eficonfig_process_common(efi_menu, " ** Update File **");
986 if (ret != EFI_SUCCESS) /* User selects "Clear" or "Quit" */
989 eficonfig_destroy(efi_menu);
995 * eficonfig_process_select_file() - handle user file selection
997 * @data: pointer to the data
998 * Return: status code
1000 efi_status_t eficonfig_process_select_file(void *data)
1004 struct list_head *pos, *n;
1005 struct efi_file_handle *root;
1006 struct eficonfig_filepath_info *item;
1007 struct eficonfig_select_file_info *tmp = NULL;
1008 struct eficonfig_select_file_info *file_info = data;
1010 tmp = calloc(1, sizeof(struct eficonfig_select_file_info));
1012 return EFI_OUT_OF_RESOURCES;
1014 tmp->current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1015 if (!tmp->current_path) {
1017 return EFI_OUT_OF_RESOURCES;
1019 INIT_LIST_HEAD(&tmp->filepath_list);
1021 while (!tmp->file_selected) {
1022 tmp->current_volume = NULL;
1023 memset(tmp->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
1025 ret = eficonfig_select_volume(tmp);
1026 if (ret != EFI_SUCCESS)
1029 if (!tmp->current_volume)
1030 return EFI_INVALID_PARAMETER;
1032 ret = efi_open_volume_int(tmp->current_volume, &root);
1033 if (ret != EFI_SUCCESS)
1036 ret = eficonfig_show_file_selection(tmp, root);
1037 if (ret == EFI_ABORTED)
1039 if (ret != EFI_SUCCESS)
1044 if (ret == EFI_SUCCESS) {
1045 len = u16_strlen(tmp->current_path);
1046 len = (len >= EFICONFIG_FILE_PATH_MAX) ? (EFICONFIG_FILE_PATH_MAX - 1) : len;
1047 memcpy(file_info->current_path, tmp->current_path, len * sizeof(u16));
1048 file_info->current_path[len] = u'\0';
1049 file_info->current_volume = tmp->current_volume;
1050 file_info->dp_volume = tmp->dp_volume;
1053 list_for_each_safe(pos, n, &tmp->filepath_list) {
1054 item = list_entry(pos, struct eficonfig_filepath_info, list);
1055 list_del(&item->list);
1059 free(tmp->current_path);
1062 /* to stay the parent menu */
1063 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1069 * eficonfig_get_unused_bootoption() - get unused "Boot####" index
1071 * @buf: pointer to the buffer to store boot option variable name
1072 * @buf_size: buffer size
1073 * @index: pointer to store the index in the BootOrder variable
1074 * Return: status code
1076 efi_status_t eficonfig_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
1077 unsigned int *index)
1083 if (buf_size < u16_strsize(u"Boot####"))
1084 return EFI_BUFFER_TOO_SMALL;
1086 for (i = 0; i <= 0xFFFF; i++) {
1088 efi_create_indexed_name(buf, buf_size, "Boot", i);
1089 ret = efi_get_variable_int(buf, &efi_global_variable_guid,
1090 NULL, &size, NULL, NULL);
1091 if (ret == EFI_BUFFER_TOO_SMALL)
1098 return EFI_OUT_OF_RESOURCES;
1106 * eficonfig_set_boot_option() - set boot option
1108 * @varname: pointer to variable name
1109 * @dp: pointer to device path
1110 * @label: pointer to label string
1111 * @optional_data: pointer to optional data
1112 * Return: status code
1114 static efi_status_t eficonfig_set_boot_option(u16 *varname, struct efi_device_path *dp,
1115 efi_uintn_t dp_size, u16 *label, char *optional_data)
1120 struct efi_load_option lo;
1123 lo.file_path_length = dp_size;
1124 lo.attributes = LOAD_OPTION_ACTIVE;
1125 lo.optional_data = optional_data;
1128 size = efi_serialize_load_option(&lo, (u8 **)&p);
1130 return EFI_INVALID_PARAMETER;
1132 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1133 EFI_VARIABLE_NON_VOLATILE |
1134 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1135 EFI_VARIABLE_RUNTIME_ACCESS,
1143 * eficonfig_append_bootorder() - append new boot option in BootOrder variable
1145 * @index: "Boot####" index to append to BootOrder variable
1146 * Return: status code
1148 efi_status_t eficonfig_append_bootorder(u16 index)
1152 u16 *new_bootorder = NULL;
1153 efi_uintn_t last, size, new_size;
1155 /* append new boot option */
1156 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1157 last = size / sizeof(u16);
1158 new_size = size + sizeof(u16);
1159 new_bootorder = calloc(1, new_size);
1160 if (!new_bootorder) {
1161 ret = EFI_OUT_OF_RESOURCES;
1164 memcpy(new_bootorder, bootorder, size);
1165 new_bootorder[last] = index;
1167 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1168 EFI_VARIABLE_NON_VOLATILE |
1169 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1170 EFI_VARIABLE_RUNTIME_ACCESS,
1171 new_size, new_bootorder, false);
1172 if (ret != EFI_SUCCESS)
1177 free(new_bootorder);
1183 * create_boot_option_entry() - create boot option entry
1185 * @efi_menu: pointer to the efimenu structure
1186 * @title: pointer to the entry title
1187 * @val: pointer to boot option label
1188 * @func: callback of each entry
1189 * @data: pointer to the data to be passed to each entry callback
1190 * Return: status code
1192 static efi_status_t create_boot_option_entry(struct efimenu *efi_menu, char *title, u16 *val,
1193 eficonfig_entry_func func, void *data)
1198 len = strlen(title) + 1;
1200 len += utf16_utf8_strlen(val);
1201 buf = calloc(1, len);
1203 return EFI_OUT_OF_RESOURCES;
1207 p = buf + strlen(title);
1208 utf16_utf8_strcpy(&p, val);
1211 return eficonfig_append_menu_entry(efi_menu, buf, func, data);
1215 * prepare_file_selection_entry() - prepare file selection entry
1217 * @efi_menu: pointer to the efimenu structure
1218 * @title: pointer to the title string
1219 * @file_info: pointer to the file info
1220 * Return: status code
1222 static efi_status_t prepare_file_selection_entry(struct efimenu *efi_menu, char *title,
1223 struct eficonfig_select_file_info *file_info)
1227 u16 *file_name = NULL, *p;
1228 efi_handle_t handle;
1231 devname = calloc(1, EFICONFIG_VOLUME_PATH_MAX + 1);
1233 return EFI_OUT_OF_RESOURCES;
1235 /* get the device name only when the user already selected the file path */
1236 handle = efi_dp_find_obj(file_info->dp_volume, NULL, NULL);
1238 ret = efi_disk_get_device_name(handle, devname, EFICONFIG_VOLUME_PATH_MAX);
1239 if (ret != EFI_SUCCESS)
1244 * If the preconfigured volume does not exist in the system, display the text
1245 * converted volume device path instead of U-Boot friendly name(e.g. "usb 0:1").
1247 if (!handle && file_info->dp_volume) {
1251 dp_str = efi_dp_str(file_info->dp_volume);
1253 utf16_utf8_strncpy(&q, dp_str, EFICONFIG_VOLUME_PATH_MAX);
1255 efi_free_pool(dp_str);
1258 /* append u'/' to devname, it is just for display purpose. */
1259 if (file_info->current_path[0] != u'\0' && file_info->current_path[0] != u'/')
1260 strlcat(devname, "/", EFICONFIG_VOLUME_PATH_MAX + 1);
1262 len = strlen(devname);
1263 len += utf16_utf8_strlen(file_info->current_path) + 1;
1264 file_name = calloc(1, len * sizeof(u16));
1266 ret = EFI_OUT_OF_RESOURCES;
1271 utf8_utf16_strcpy(&p, devname);
1272 u16_strlcat(file_name, file_info->current_path, len);
1273 ret = create_boot_option_entry(efi_menu, title, file_name,
1274 eficonfig_process_show_file_option, file_info);
1283 * eficonfig_show_boot_option() - prepare menu entry for editing boot option
1285 * Construct the structures to create edit boot option menu
1287 * @bo: pointer to the boot option
1288 * @header_str: pointer to the header string
1289 * Return: status code
1291 static efi_status_t eficonfig_show_boot_option(struct eficonfig_boot_option *bo,
1295 struct efimenu *efi_menu;
1297 efi_menu = calloc(1, sizeof(struct efimenu));
1299 return EFI_OUT_OF_RESOURCES;
1301 INIT_LIST_HEAD(&efi_menu->list);
1303 ret = create_boot_option_entry(efi_menu, "Description: ", bo->description,
1304 eficonfig_boot_add_enter_description, bo);
1305 if (ret != EFI_SUCCESS)
1308 ret = prepare_file_selection_entry(efi_menu, "File: ", &bo->file_info);
1309 if (ret != EFI_SUCCESS)
1312 ret = prepare_file_selection_entry(efi_menu, "Initrd File: ", &bo->initrd_info);
1313 if (ret != EFI_SUCCESS)
1316 ret = create_boot_option_entry(efi_menu, "Optional Data: ", bo->optional_data,
1317 eficonfig_boot_add_optional_data, bo);
1318 if (ret != EFI_SUCCESS)
1321 ret = create_boot_option_entry(efi_menu, "Save", NULL,
1322 eficonfig_boot_edit_save, bo);
1323 if (ret != EFI_SUCCESS)
1326 ret = create_boot_option_entry(efi_menu, "Quit", NULL,
1327 eficonfig_process_quit, NULL);
1328 if (ret != EFI_SUCCESS)
1331 ret = eficonfig_process_common(efi_menu, header_str);
1333 eficonfig_destroy(efi_menu);
1339 * fill_file_info() - fill the file info from efi_device_path structure
1341 * @dp: pointer to the device path
1342 * @file_info: pointer to the file info structure
1343 * @device_dp: pointer to the volume device path
1345 static void fill_file_info(struct efi_device_path *dp,
1346 struct eficonfig_select_file_info *file_info,
1347 struct efi_device_path *device_dp)
1350 struct efi_device_path *file_dp = NULL;
1352 efi_dp_split_file_path(dp, &device_dp, &file_dp);
1353 file_info->dp_volume = device_dp;
1356 file_str = efi_dp_str(file_dp);
1358 * efi_convert_device_path_to_text() automatically adds u'/' at the
1359 * beginning of file name, remove u'/' before copying to current_path
1365 u16_strcpy(file_info->current_path, p);
1366 efi_free_pool(file_dp);
1367 efi_free_pool(file_str);
1372 * eficonfig_edit_boot_option() - prepare boot option structure for editing
1374 * Construct the boot option structure and copy the existing value
1376 * @varname: pointer to the UEFI variable name
1377 * @bo: pointer to the boot option
1378 * @load_option: pointer to the load option
1379 * @load_option_size: size of the load option
1380 * @header_str: pointer to the header string
1381 * Return : status code
1383 static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_boot_option *bo,
1384 void *load_option, efi_uintn_t load_option_size,
1389 char *tmp = NULL, *p;
1390 struct efi_load_option lo = {0};
1391 efi_uintn_t final_dp_size;
1392 struct efi_device_path *dp = NULL;
1393 efi_uintn_t size = load_option_size;
1394 struct efi_device_path *final_dp = NULL;
1395 struct efi_device_path *device_dp = NULL;
1396 struct efi_device_path *initrd_dp = NULL;
1397 struct efi_device_path *initrd_device_dp = NULL;
1399 const struct efi_initrd_dp id_dp = {
1402 DEVICE_PATH_TYPE_MEDIA_DEVICE,
1403 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
1404 sizeof(id_dp.vendor),
1406 EFI_INITRD_MEDIA_GUID,
1409 DEVICE_PATH_TYPE_END,
1410 DEVICE_PATH_SUB_TYPE_END,
1415 bo->file_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1416 if (!bo->file_info.current_path) {
1417 ret = EFI_OUT_OF_RESOURCES;
1421 bo->initrd_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1422 if (!bo->file_info.current_path) {
1423 ret = EFI_OUT_OF_RESOURCES;
1427 bo->description = calloc(1, EFICONFIG_DESCRIPTION_MAX * sizeof(u16));
1428 if (!bo->description) {
1429 ret = EFI_OUT_OF_RESOURCES;
1433 bo->optional_data = calloc(1, EFICONFIG_OPTIONAL_DATA_MAX * sizeof(u16));
1434 if (!bo->optional_data) {
1435 ret = EFI_OUT_OF_RESOURCES;
1439 /* copy the preset value */
1441 ret = efi_deserialize_load_option(&lo, load_option, &size);
1442 if (ret != EFI_SUCCESS)
1446 ret = EFI_INVALID_PARAMETER;
1449 /* truncate the long label string */
1450 if (u16_strlen(lo.label) >= EFICONFIG_DESCRIPTION_MAX)
1451 lo.label[EFICONFIG_DESCRIPTION_MAX - 1] = u'\0';
1453 u16_strcpy(bo->description, lo.label);
1455 /* EFI image file path is a first instance */
1457 fill_file_info(lo.file_path, &bo->file_info, device_dp);
1459 /* Initrd file path(optional) is placed at second instance. */
1460 initrd_dp = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
1462 fill_file_info(initrd_dp, &bo->initrd_info, initrd_device_dp);
1463 efi_free_pool(initrd_dp);
1467 memcpy(bo->optional_data, lo.optional_data, size);
1471 ret = eficonfig_show_boot_option(bo, header_str);
1472 if (ret == EFI_SUCCESS && bo->edit_completed)
1474 if (ret == EFI_NOT_READY)
1476 if (ret != EFI_SUCCESS)
1480 if (bo->initrd_info.dp_volume) {
1481 dp = create_selected_device_path(&bo->initrd_info);
1483 ret = EFI_OUT_OF_RESOURCES;
1486 initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp, dp);
1490 dp = create_selected_device_path(&bo->file_info);
1492 ret = EFI_OUT_OF_RESOURCES;
1495 final_dp_size = efi_dp_size(dp) + sizeof(END);
1497 final_dp = efi_dp_concat(dp, initrd_dp);
1498 final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
1500 final_dp = efi_dp_dup(dp);
1507 if (utf16_utf8_strlen(bo->optional_data)) {
1508 len = utf16_utf8_strlen(bo->optional_data) + 1;
1509 tmp = calloc(1, len);
1513 utf16_utf8_strncpy(&p, bo->optional_data, u16_strlen(bo->optional_data));
1516 ret = eficonfig_set_boot_option(varname, final_dp, final_dp_size, bo->description, tmp);
1519 free(bo->optional_data);
1520 free(bo->description);
1521 free(bo->file_info.current_path);
1522 free(bo->initrd_info.current_path);
1523 efi_free_pool(device_dp);
1524 efi_free_pool(initrd_device_dp);
1525 efi_free_pool(initrd_dp);
1526 efi_free_pool(final_dp);
1532 * eficonfig_process_add_boot_option() - handler to add boot option
1534 * @data: pointer to the data for each entry
1535 * Return: status code
1537 static efi_status_t eficonfig_process_add_boot_option(void *data)
1541 struct eficonfig_boot_option *bo = NULL;
1543 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1545 return EFI_OUT_OF_RESOURCES;
1547 ret = eficonfig_get_unused_bootoption(varname, sizeof(varname), &bo->boot_index);
1548 if (ret != EFI_SUCCESS)
1551 ret = eficonfig_edit_boot_option(varname, bo, NULL, 0, " ** Add Boot Option ** ");
1552 if (ret != EFI_SUCCESS)
1555 ret = eficonfig_append_bootorder((u16)bo->boot_index);
1556 if (ret != EFI_SUCCESS)
1562 /* to stay the parent menu */
1563 ret = (ret == EFI_ABORTED) ? EFI_SUCCESS : ret;
1569 * eficonfig_process_boot_selected() - handler to select boot option entry
1571 * @data: pointer to the data for each entry
1572 * Return: status code
1574 static efi_status_t eficonfig_process_boot_selected(void *data)
1576 struct eficonfig_boot_selection_data *info = data;
1579 *info->selected = info->boot_index;
1585 * search_bootorder() - search the boot option index in BootOrder
1587 * @bootorder: pointer to the BootOrder variable
1588 * @num: number of BootOrder entry
1589 * @target: target boot option index to search
1590 * @index: pointer to store the index of BootOrder variable
1591 * Return: true if exists, false otherwise
1593 static bool search_bootorder(u16 *bootorder, efi_uintn_t num, u32 target, u32 *index)
1597 for (i = 0; i < num; i++) {
1598 if (target == bootorder[i]) {
1610 * eficonfig_add_boot_selection_entry() - add boot option menu entry
1612 * @efi_menu: pointer to store the efimenu structure
1613 * @boot_index: boot option index to be added
1614 * @selected: pointer to store the selected boot option index
1615 * Return: status code
1617 static efi_status_t eficonfig_add_boot_selection_entry(struct efimenu *efi_menu,
1618 unsigned int boot_index,
1619 unsigned int *selected)
1625 struct efi_load_option lo;
1626 u16 varname[] = u"Boot####";
1627 struct eficonfig_boot_selection_data *info;
1629 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
1630 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1634 ret = efi_deserialize_load_option(&lo, load_option, &size);
1635 if (ret != EFI_SUCCESS) {
1636 log_warning("Invalid load option for %ls\n", varname);
1641 if (size >= sizeof(efi_guid_t) &&
1642 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
1644 * auto generated entry has GUID in optional_data,
1645 * skip auto generated entry because it will be generated
1646 * again even if it is edited or deleted.
1652 info = calloc(1, sizeof(struct eficonfig_boot_selection_data));
1655 return EFI_OUT_OF_RESOURCES;
1658 buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
1662 return EFI_OUT_OF_RESOURCES;
1665 utf16_utf8_strcpy(&p, lo.label);
1666 info->boot_index = boot_index;
1667 info->selected = selected;
1668 ret = eficonfig_append_menu_entry(efi_menu, buf, eficonfig_process_boot_selected, info);
1669 if (ret != EFI_SUCCESS) {
1680 * eficonfig_show_boot_selection() - construct boot option menu entry
1682 * @selected: pointer to store the selected boot option index
1683 * Return: status code
1685 static efi_status_t eficonfig_show_boot_selection(unsigned int *selected)
1690 efi_uintn_t num, size;
1691 struct efimenu *efi_menu;
1692 struct list_head *pos, *n;
1693 struct eficonfig_entry *entry;
1695 efi_menu = calloc(1, sizeof(struct efimenu));
1697 return EFI_OUT_OF_RESOURCES;
1699 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1701 INIT_LIST_HEAD(&efi_menu->list);
1702 num = size / sizeof(u16);
1703 /* list the load option in the order of BootOrder variable */
1704 for (i = 0; i < num; i++) {
1705 ret = eficonfig_add_boot_selection_entry(efi_menu, bootorder[i], selected);
1706 if (ret != EFI_SUCCESS)
1709 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1713 /* list the remaining load option not included in the BootOrder */
1714 for (i = 0; i <= 0xFFFF; i++) {
1715 /* If the index is included in the BootOrder, skip it */
1716 if (search_bootorder(bootorder, num, i, NULL))
1719 ret = eficonfig_add_boot_selection_entry(efi_menu, i, selected);
1720 if (ret != EFI_SUCCESS)
1723 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1727 ret = eficonfig_append_quit_entry(efi_menu);
1728 if (ret != EFI_SUCCESS)
1731 ret = eficonfig_process_common(efi_menu, " ** Select Boot Option **");
1733 list_for_each_safe(pos, n, &efi_menu->list) {
1734 entry = list_entry(pos, struct eficonfig_entry, list);
1737 eficonfig_destroy(efi_menu);
1743 * eficonfig_process_edit_boot_option() - handler to edit boot option
1745 * @data: pointer to the data for each entry
1746 * Return: status code
1748 static efi_status_t eficonfig_process_edit_boot_option(void *data)
1752 struct eficonfig_boot_option *bo = NULL;
1755 unsigned int selected;
1757 u16 varname[] = u"Boot####";
1759 ret = eficonfig_show_boot_selection(&selected);
1760 if (ret != EFI_SUCCESS)
1763 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1765 ret = EFI_OUT_OF_RESOURCES;
1769 bo->boot_index = selected;
1770 efi_create_indexed_name(varname, sizeof(varname), "Boot", selected);
1771 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1774 ret = EFI_NOT_FOUND;
1778 ret = eficonfig_edit_boot_option(varname, bo, load_option, size,
1779 " ** Edit Boot Option ** ");
1783 if (ret != EFI_SUCCESS && ret != EFI_ABORTED)
1787 /* to stay the parent menu */
1788 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1794 * eficonfig_display_change_boot_order() - display the BootOrder list
1796 * @efi_menu: pointer to the efimenu structure
1797 * Return: status code
1799 static void eficonfig_display_change_boot_order(struct efimenu *efi_menu)
1802 struct list_head *pos, *n;
1803 struct eficonfig_boot_order *entry;
1805 printf(ANSI_CLEAR_CONSOLE ANSI_CURSOR_POSITION
1806 "\n ** Change Boot Order **\n"
1807 ANSI_CURSOR_POSITION
1808 " Press UP/DOWN to move, +/- to change order"
1809 ANSI_CURSOR_POSITION
1810 " Press SPACE to activate or deactivate the entry"
1811 ANSI_CURSOR_POSITION
1812 " Select [Save] to complete, ESC/CTRL+C to quit"
1813 ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
1814 1, 1, efi_menu->count + 5, 1, efi_menu->count + 6, 1,
1815 efi_menu->count + 7, 1, efi_menu->count + 8, 1);
1817 /* draw boot option list */
1818 list_for_each_safe(pos, n, &efi_menu->list) {
1819 entry = list_entry(pos, struct eficonfig_boot_order, list);
1820 reverse = (entry->num == efi_menu->active);
1822 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
1825 puts(ANSI_COLOR_REVERSE);
1827 if (entry->num < efi_menu->count - 2) {
1834 printf("%ls", entry->description);
1837 puts(ANSI_COLOR_RESET);
1842 * eficonfig_choice_change_boot_order() - handle the BootOrder update
1844 * @efi_menu: pointer to the efimenu structure
1845 * Return: status code
1847 static efi_status_t eficonfig_choice_change_boot_order(struct efimenu *efi_menu)
1850 struct list_head *pos, *n;
1851 struct eficonfig_boot_order *tmp;
1852 enum bootmenu_key key = KEY_NONE;
1853 struct eficonfig_boot_order *entry;
1856 bootmenu_loop(NULL, &key, &esc);
1860 if (efi_menu->active > 0) {
1861 list_for_each_safe(pos, n, &efi_menu->list) {
1862 entry = list_entry(pos, struct eficonfig_boot_order, list);
1863 if (entry->num == efi_menu->active)
1866 tmp = list_entry(pos->prev, struct eficonfig_boot_order, list);
1869 list_del(&tmp->list);
1870 list_add(&tmp->list, &entry->list);
1874 if (efi_menu->active > 0)
1876 return EFI_NOT_READY;
1878 if (efi_menu->active < efi_menu->count - 3) {
1879 list_for_each_safe(pos, n, &efi_menu->list) {
1880 entry = list_entry(pos, struct eficonfig_boot_order, list);
1881 if (entry->num == efi_menu->active)
1884 tmp = list_entry(pos->next, struct eficonfig_boot_order, list);
1887 list_del(&entry->list);
1888 list_add(&entry->list, &tmp->list);
1892 return EFI_NOT_READY;
1894 if (efi_menu->active < efi_menu->count - 1)
1896 return EFI_NOT_READY;
1899 if (efi_menu->active == efi_menu->count - 2)
1903 if (efi_menu->active == efi_menu->count - 1)
1908 if (efi_menu->active < efi_menu->count - 2) {
1909 list_for_each_safe(pos, n, &efi_menu->list) {
1910 entry = list_entry(pos, struct eficonfig_boot_order, list);
1911 if (entry->num == efi_menu->active) {
1912 entry->active = entry->active ? false : true;
1913 return EFI_NOT_READY;
1921 /* Pressed key is not valid, no need to regenerate the menu */
1928 * eficonfig_add_change_boot_order_entry() - add boot order entry
1930 * @efi_menu: pointer to the efimenu structure
1931 * @boot_index: boot option index to be added
1932 * @active: flag to include the boot option into BootOrder
1933 * Return: status code
1935 static efi_status_t eficonfig_add_change_boot_order_entry(struct efimenu *efi_menu,
1936 u32 boot_index, bool active)
1941 struct efi_load_option lo;
1942 u16 varname[] = u"Boot####";
1943 struct eficonfig_boot_order *entry;
1945 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
1946 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1950 ret = efi_deserialize_load_option(&lo, load_option, &size);
1951 if (ret != EFI_SUCCESS) {
1956 entry = calloc(1, sizeof(struct eficonfig_boot_order));
1959 return EFI_OUT_OF_RESOURCES;
1962 entry->description = u16_strdup(lo.label);
1963 if (!entry->description) {
1966 return EFI_OUT_OF_RESOURCES;
1968 entry->num = efi_menu->count++;
1969 entry->boot_index = boot_index;
1970 entry->active = active;
1971 list_add_tail(&entry->list, &efi_menu->list);
1979 * eficonfig_create_change_boot_order_entry() - create boot order entry
1981 * @efi_menu: pointer to the efimenu structure
1982 * @bootorder: pointer to the BootOrder variable
1983 * @num: number of BootOrder entry
1984 * Return: status code
1986 static efi_status_t eficonfig_create_change_boot_order_entry(struct efimenu *efi_menu,
1987 u16 *bootorder, efi_uintn_t num)
1991 struct eficonfig_boot_order *entry;
1993 /* list the load option in the order of BootOrder variable */
1994 for (i = 0; i < num; i++) {
1995 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
1998 ret = eficonfig_add_change_boot_order_entry(efi_menu, bootorder[i], true);
1999 if (ret != EFI_SUCCESS)
2003 /* list the remaining load option not included in the BootOrder */
2004 for (i = 0; i < 0xFFFF; i++) {
2005 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2008 /* If the index is included in the BootOrder, skip it */
2009 if (search_bootorder(bootorder, num, i, NULL))
2012 ret = eficonfig_add_change_boot_order_entry(efi_menu, i, false);
2013 if (ret != EFI_SUCCESS)
2017 /* add "Save" and "Quit" entries */
2018 entry = calloc(1, sizeof(struct eficonfig_boot_order));
2022 entry->num = efi_menu->count++;
2023 entry->description = u16_strdup(u"Save");
2024 list_add_tail(&entry->list, &efi_menu->list);
2026 entry = calloc(1, sizeof(struct eficonfig_boot_order));
2030 entry->num = efi_menu->count++;
2031 entry->description = u16_strdup(u"Quit");
2032 list_add_tail(&entry->list, &efi_menu->list);
2034 efi_menu->active = 0;
2038 return EFI_OUT_OF_RESOURCES;
2042 * eficonfig_process_change_boot_order() - handler to change boot order
2044 * @data: pointer to the data for each entry
2045 * Return: status code
2047 static efi_status_t eficonfig_process_change_boot_order(void *data)
2052 efi_uintn_t num, size;
2053 struct list_head *pos, *n;
2054 struct eficonfig_boot_order *entry;
2055 struct efimenu *efi_menu;
2057 efi_menu = calloc(1, sizeof(struct efimenu));
2059 return EFI_OUT_OF_RESOURCES;
2061 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2063 INIT_LIST_HEAD(&efi_menu->list);
2064 num = size / sizeof(u16);
2065 ret = eficonfig_create_change_boot_order_entry(efi_menu, bootorder, num);
2066 if (ret != EFI_SUCCESS)
2070 eficonfig_display_change_boot_order(efi_menu);
2072 ret = eficonfig_choice_change_boot_order(efi_menu);
2073 if (ret == EFI_SUCCESS) {
2076 new_bootorder = calloc(1, (efi_menu->count - 2) * sizeof(u16));
2077 if (!new_bootorder) {
2078 ret = EFI_OUT_OF_RESOURCES;
2082 /* create new BootOrder */
2084 list_for_each_safe(pos, n, &efi_menu->list) {
2085 entry = list_entry(pos, struct eficonfig_boot_order, list);
2087 new_bootorder[count++] = entry->boot_index;
2090 size = count * sizeof(u16);
2091 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
2092 EFI_VARIABLE_NON_VOLATILE |
2093 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2094 EFI_VARIABLE_RUNTIME_ACCESS,
2095 size, new_bootorder, false);
2097 free(new_bootorder);
2099 } else if (ret == EFI_NOT_READY) {
2106 list_for_each_safe(pos, n, &efi_menu->list) {
2107 entry = list_entry(pos, struct eficonfig_boot_order, list);
2108 list_del(&entry->list);
2109 free(entry->description);
2116 /* to stay the parent menu */
2117 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2123 * delete_boot_option() - delete selected boot option
2125 * @boot_index: boot option index to delete
2126 * Return: status code
2128 static efi_status_t delete_boot_option(u16 boot_index)
2134 efi_uintn_t num, size;
2136 efi_create_indexed_name(varname, sizeof(varname),
2137 "Boot", boot_index);
2138 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
2140 if (ret != EFI_SUCCESS) {
2141 log_err("delete boot option(%ls) failed\n", varname);
2145 /* update BootOrder if necessary */
2146 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2150 num = size / sizeof(u16);
2151 if (!search_bootorder(bootorder, num, boot_index, &index))
2154 memmove(&bootorder[index], &bootorder[index + 1],
2155 (num - index - 1) * sizeof(u16));
2156 size -= sizeof(u16);
2157 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
2158 EFI_VARIABLE_NON_VOLATILE |
2159 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2160 EFI_VARIABLE_RUNTIME_ACCESS,
2161 size, bootorder, false);
2167 * eficonfig_process_delete_boot_option() - handler to delete boot option
2169 * @data: pointer to the data for each entry
2170 * Return: status code
2172 static efi_status_t eficonfig_process_delete_boot_option(void *data)
2175 unsigned int selected;
2178 ret = eficonfig_show_boot_selection(&selected);
2179 if (ret == EFI_SUCCESS)
2180 ret = delete_boot_option(selected);
2182 if (ret != EFI_SUCCESS)
2186 /* to stay the parent menu */
2187 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2193 * eficonfig_enumerate_boot_option() - enumerate the possible bootable media
2195 * @opt: pointer to the media boot option structure
2196 * @volume_handles: pointer to the efi handles
2197 * @count: number of efi handle
2198 * Return: status code
2200 efi_status_t eficonfig_enumerate_boot_option(struct eficonfig_media_boot_option *opt,
2201 efi_handle_t *volume_handles, efi_status_t count)
2204 struct efi_handler *handler;
2205 efi_status_t ret = EFI_SUCCESS;
2207 for (i = 0; i < count; i++) {
2209 u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
2210 char *optional_data;
2211 struct efi_load_option lo;
2212 char buf[BOOTMENU_DEVICE_NAME_MAX];
2213 struct efi_device_path *device_path;
2215 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
2216 if (ret != EFI_SUCCESS)
2218 ret = efi_protocol_open(handler, (void **)&device_path,
2219 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2220 if (ret != EFI_SUCCESS)
2223 ret = efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
2224 if (ret != EFI_SUCCESS)
2228 utf8_utf16_strncpy(&p, buf, strlen(buf));
2230 lo.label = dev_name;
2231 lo.attributes = LOAD_OPTION_ACTIVE;
2232 lo.file_path = device_path;
2233 lo.file_path_length = efi_dp_size(device_path) + sizeof(END);
2235 * Set the dedicated guid to optional_data, it is used to identify
2236 * the boot option that automatically generated by the bootmenu.
2237 * efi_serialize_load_option() expects optional_data is null-terminated
2238 * utf8 string, so set the "1234567" string to allocate enough space
2239 * to store guid, instead of realloc the load_option.
2241 lo.optional_data = "1234567";
2242 opt[i].size = efi_serialize_load_option(&lo, (u8 **)&opt[i].lo);
2244 ret = EFI_OUT_OF_RESOURCES;
2248 optional_data = (char *)opt[i].lo + (opt[i].size - u16_strsize(u"1234567"));
2249 memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
2257 * eficonfig_delete_invalid_boot_option() - delete non-existing boot option
2259 * @opt: pointer to the media boot option structure
2260 * @count: number of media boot option structure
2261 * Return: status code
2263 efi_status_t eficonfig_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
2269 struct efi_load_option lo;
2270 u16 varname[] = u"Boot####";
2271 efi_status_t ret = EFI_SUCCESS;
2273 for (i = 0; i <= 0xFFFF; i++) {
2276 efi_create_indexed_name(varname, sizeof(varname), "Boot", i);
2277 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
2282 ret = efi_deserialize_load_option(&lo, load_option, &size);
2283 if (ret != EFI_SUCCESS)
2286 if (size >= sizeof(efi_guid_bootmenu_auto_generated)) {
2287 if (guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated) == 0) {
2288 for (j = 0; j < count; j++) {
2289 if (opt[j].size == tmp &&
2290 memcmp(opt[j].lo, load_option, tmp) == 0) {
2291 opt[j].exist = true;
2297 ret = delete_boot_option(i);
2298 if (ret != EFI_SUCCESS) {
2314 * eficonfig_generate_media_device_boot_option() - generate the media device boot option
2316 * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
2317 * and generate the bootmenu entries.
2318 * This function also provide the BOOT#### variable maintenance for
2319 * the media device entries.
2320 * - Automatically create the BOOT#### variable for the newly detected device,
2321 * this BOOT#### variable is distinguished by the special GUID
2322 * stored in the EFI_LOAD_OPTION.optional_data
2323 * - If the device is not attached to the system, the associated BOOT#### variable
2324 * is automatically deleted.
2326 * Return: status code
2328 efi_status_t eficonfig_generate_media_device_boot_option(void)
2333 efi_handle_t *volume_handles = NULL;
2334 struct eficonfig_media_boot_option *opt = NULL;
2336 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
2337 NULL, &count, (efi_handle_t **)&volume_handles);
2338 if (ret != EFI_SUCCESS)
2341 opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
2345 /* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */
2346 ret = eficonfig_enumerate_boot_option(opt, volume_handles, count);
2347 if (ret != EFI_SUCCESS)
2351 * System hardware configuration may vary depending on the user setup.
2352 * The boot option is automatically added by the bootmenu.
2353 * If the device is not attached to the system, the boot option needs
2356 ret = eficonfig_delete_invalid_boot_option(opt, count);
2357 if (ret != EFI_SUCCESS)
2360 /* add non-existent boot option */
2361 for (i = 0; i < count; i++) {
2365 if (!opt[i].exist) {
2366 ret = eficonfig_get_unused_bootoption(var_name, sizeof(var_name),
2368 if (ret != EFI_SUCCESS)
2371 ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
2372 EFI_VARIABLE_NON_VOLATILE |
2373 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2374 EFI_VARIABLE_RUNTIME_ACCESS,
2375 opt[i].size, opt[i].lo, false);
2376 if (ret != EFI_SUCCESS)
2379 ret = eficonfig_append_bootorder(boot_index);
2380 if (ret != EFI_SUCCESS) {
2381 efi_set_variable_int(var_name, &efi_global_variable_guid,
2390 for (i = 0; i < count; i++)
2394 efi_free_pool(volume_handles);
2400 * eficonfig_init() - do required initialization for eficonfig command
2402 * Return: status code
2404 static efi_status_t eficonfig_init(void)
2406 efi_status_t ret = EFI_SUCCESS;
2408 struct efi_handler *handler;
2411 ret = efi_search_protocol(efi_root, &efi_guid_text_input_protocol, &handler);
2412 if (ret != EFI_SUCCESS)
2415 ret = efi_protocol_open(handler, (void **)&cin, efi_root, NULL,
2416 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2417 if (ret != EFI_SUCCESS)
2426 static const struct eficonfig_item maintenance_menu_items[] = {
2427 {"Add Boot Option", eficonfig_process_add_boot_option},
2428 {"Edit Boot Option", eficonfig_process_edit_boot_option},
2429 {"Change Boot Order", eficonfig_process_change_boot_order},
2430 {"Delete Boot Option", eficonfig_process_delete_boot_option},
2431 {"Quit", eficonfig_process_quit},
2435 * do_eficonfig() - execute `eficonfig` command
2437 * @cmdtp: table entry describing command
2438 * @flag: bitmap indicating how the command was invoked
2439 * @argc: number of arguments
2440 * @argv: command line arguments
2441 * Return: status code
2443 static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
2446 struct efimenu *efi_menu;
2449 return CMD_RET_USAGE;
2451 ret = efi_init_obj_list();
2452 if (ret != EFI_SUCCESS) {
2453 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
2454 ret & ~EFI_ERROR_MASK);
2456 return CMD_RET_FAILURE;
2459 ret = eficonfig_init();
2460 if (ret != EFI_SUCCESS)
2461 return CMD_RET_FAILURE;
2463 ret = eficonfig_generate_media_device_boot_option();
2464 if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
2468 efi_menu = eficonfig_create_fixed_menu(maintenance_menu_items,
2469 ARRAY_SIZE(maintenance_menu_items));
2471 return CMD_RET_FAILURE;
2473 ret = eficonfig_process_common(efi_menu, " ** UEFI Maintenance Menu **");
2474 eficonfig_destroy(efi_menu);
2476 if (ret == EFI_ABORTED)
2480 return CMD_RET_SUCCESS;
2484 eficonfig, 1, 0, do_eficonfig,
2485 "provide menu-driven UEFI variable maintenance interface",