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 * append_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 static efi_status_t append_entry(struct efimenu *efi_menu,
275 char *title, eficonfig_entry_func func, void *data)
277 struct eficonfig_entry *entry;
279 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX)
280 return EFI_OUT_OF_RESOURCES;
282 entry = calloc(1, sizeof(struct eficonfig_entry));
284 return EFI_OUT_OF_RESOURCES;
286 entry->title = title;
287 sprintf(entry->key, "%d", efi_menu->count);
288 entry->efi_menu = efi_menu;
291 entry->num = efi_menu->count++;
292 list_add_tail(&entry->list, &efi_menu->list);
298 * append_quit_entry() - append quit entry
300 * @efi_menu: pointer to the efimenu structure
301 * Return: status code
303 static efi_status_t append_quit_entry(struct efimenu *efi_menu)
308 title = strdup("Quit");
310 return EFI_OUT_OF_RESOURCES;
312 ret = append_entry(efi_menu, title, eficonfig_process_quit, NULL);
313 if (ret != EFI_SUCCESS)
320 * eficonfig_create_fixed_menu() - create fixed entry menu structure
322 * @items: pointer to the menu entry item
323 * @count: the number of menu entry
324 * Return: pointer to the efimenu structure
326 void *eficonfig_create_fixed_menu(const struct eficonfig_item *items, int count)
331 struct efimenu *efi_menu;
332 const struct eficonfig_item *iter = items;
334 efi_menu = calloc(1, sizeof(struct efimenu));
338 INIT_LIST_HEAD(&efi_menu->list);
339 for (i = 0; i < count; i++, iter++) {
340 title = strdup(iter->title);
344 ret = append_entry(efi_menu, title, iter->func, iter->data);
345 if (ret != EFI_SUCCESS) {
353 eficonfig_destroy(efi_menu);
359 * eficonfig_process_common() - main handler for UEFI menu
361 * Construct the structures required to show the menu, then handle
362 * the user input interacting with u-boot menu functions.
364 * @efi_menu: pointer to the efimenu structure
365 * @menu_header: pointer to the menu header string
366 * Return: status code
368 efi_status_t eficonfig_process_common(struct efimenu *efi_menu, char *menu_header)
372 struct list_head *pos, *n;
373 struct eficonfig_entry *entry;
374 efi_status_t ret = EFI_SUCCESS;
376 if (efi_menu->count > EFICONFIG_ENTRY_NUM_MAX)
377 return EFI_OUT_OF_RESOURCES;
379 efi_menu->delay = -1;
380 efi_menu->active = 0;
383 efi_menu->menu_header = strdup(menu_header);
384 if (!efi_menu->menu_header)
385 return EFI_OUT_OF_RESOURCES;
388 menu = menu_create(NULL, 0, 1, eficonfig_display_statusline,
389 eficonfig_print_entry, eficonfig_choice_entry,
392 return EFI_INVALID_PARAMETER;
394 list_for_each_safe(pos, n, &efi_menu->list) {
395 entry = list_entry(pos, struct eficonfig_entry, list);
396 if (!menu_item_add(menu, entry->key, entry)) {
397 ret = EFI_INVALID_PARAMETER;
402 entry = list_first_entry_or_null(&efi_menu->list, struct eficonfig_entry, list);
404 menu_default_set(menu, entry->key);
406 printf(ANSI_CURSOR_HIDE
408 ANSI_CURSOR_POSITION, 1, 1);
410 if (menu_get_choice(menu, &choice)) {
413 ret = entry->func(entry->data);
418 printf(ANSI_CLEAR_CONSOLE
420 ANSI_CURSOR_SHOW, 1, 1);
426 * eficonfig_volume_selected() - handler of volume selection
428 * @data: pointer to the data of selected entry
429 * Return: status code
431 static efi_status_t eficonfig_volume_selected(void *data)
433 struct eficonfig_volume_entry_data *info = data;
436 info->file_info->current_volume = info->v;
437 info->file_info->dp_volume = info->dp;
444 * create_selected_device_path() - create device path
446 * @file_info: pointer to the selected file information
448 * device path or NULL. Caller must free the returned value
451 struct efi_device_path *create_selected_device_path(struct eficonfig_select_file_info *file_info)
456 struct efi_device_path *dp;
457 struct efi_device_path_file_path *fp;
459 fp_size = sizeof(struct efi_device_path) +
460 ((u16_strlen(file_info->current_path) + 1) * sizeof(u16));
461 buf = calloc(1, fp_size + sizeof(END));
466 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
467 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
468 fp->dp.length = (u16)fp_size;
469 u16_strcpy(fp->str, file_info->current_path);
473 *((struct efi_device_path *)p) = END;
475 dp = efi_dp_append(file_info->dp_volume, (struct efi_device_path *)buf);
482 * eficonfig_file_selected() - handler of file selection
484 * @data: pointer to the data of selected entry
485 * Return: status code
487 static efi_status_t eficonfig_file_selected(void *data)
490 struct eficonfig_file_entry_data *info = data;
493 return EFI_INVALID_PARAMETER;
495 if (!strcmp(info->file_name, "..")) {
496 struct eficonfig_filepath_info *iter;
497 struct list_head *pos, *n;
500 tmp = info->file_info->current_path;
502 memset(info->file_info->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
503 filepath = calloc(1, EFICONFIG_FILE_PATH_MAX);
505 return EFI_OUT_OF_RESOURCES;
507 list_for_each_safe(pos, n, &info->file_info->filepath_list) {
508 iter = list_entry(pos, struct eficonfig_filepath_info, list);
510 is_last = list_is_last(&iter->list, &info->file_info->filepath_list);
512 list_del(&iter->list);
517 strlcat(filepath, iter->name, EFICONFIG_FILE_PATH_MAX);
519 utf8_utf16_strcpy(&tmp, filepath);
522 struct eficonfig_filepath_info *filepath_info;
524 new_len = u16_strlen(info->file_info->current_path) +
525 strlen(info->file_name);
526 if (new_len >= EFICONFIG_FILE_PATH_MAX) {
527 eficonfig_print_msg("File path is too long!");
528 return EFI_INVALID_PARAMETER;
530 tmp = &info->file_info->current_path[u16_strlen(info->file_info->current_path)];
531 utf8_utf16_strcpy(&tmp, info->file_name);
533 filepath_info = calloc(1, sizeof(struct eficonfig_filepath_info));
535 return EFI_OUT_OF_RESOURCES;
537 filepath_info->name = strdup(info->file_name);
538 if (!filepath_info->name) {
540 return EFI_OUT_OF_RESOURCES;
542 list_add_tail(&filepath_info->list, &info->file_info->filepath_list);
544 if (!info->is_directory)
545 info->file_info->file_selected = true;
552 * eficonfig_select_volume() - construct the volume selection menu
554 * @file_info: pointer to the file selection structure
555 * Return: status code
557 static efi_status_t eficonfig_select_volume(struct eficonfig_select_file_info *file_info)
562 struct efimenu *efi_menu;
563 struct list_head *pos, *n;
564 struct efi_handler *handler;
565 struct eficonfig_entry *entry;
566 struct efi_device_path *device_path;
567 efi_handle_t *volume_handles = NULL;
568 struct efi_simple_file_system_protocol *v;
570 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
571 NULL, &count, (efi_handle_t **)&volume_handles);
572 if (ret != EFI_SUCCESS) {
573 eficonfig_print_msg("No block device found!");
577 efi_menu = calloc(1, sizeof(struct efimenu));
579 return EFI_OUT_OF_RESOURCES;
581 INIT_LIST_HEAD(&efi_menu->list);
582 for (i = 0; i < count; i++) {
584 struct efi_block_io *block_io;
585 struct eficonfig_volume_entry_data *info;
587 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
590 ret = efi_search_protocol(volume_handles[i],
591 &efi_simple_file_system_protocol_guid, &handler);
592 if (ret != EFI_SUCCESS)
594 ret = efi_protocol_open(handler, (void **)&v, efi_root, NULL,
595 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
596 if (ret != EFI_SUCCESS)
599 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
600 if (ret != EFI_SUCCESS)
602 ret = efi_protocol_open(handler, (void **)&device_path,
603 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
604 if (ret != EFI_SUCCESS)
607 ret = efi_search_protocol(volume_handles[i], &efi_block_io_guid, &handler);
608 if (ret != EFI_SUCCESS)
610 ret = efi_protocol_open(handler, (void **)&block_io,
611 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
612 if (ret != EFI_SUCCESS)
615 info = calloc(1, sizeof(struct eficonfig_volume_entry_data));
617 ret = EFI_OUT_OF_RESOURCES;
621 devname = calloc(1, BOOTMENU_DEVICE_NAME_MAX);
624 ret = EFI_OUT_OF_RESOURCES;
627 ret = efi_disk_get_device_name(volume_handles[i], devname,
628 BOOTMENU_DEVICE_NAME_MAX);
629 if (ret != EFI_SUCCESS) {
635 info->dp = device_path;
636 info->file_info = file_info;
637 ret = append_entry(efi_menu, devname, eficonfig_volume_selected, info);
638 if (ret != EFI_SUCCESS) {
644 ret = append_quit_entry(efi_menu);
645 if (ret != EFI_SUCCESS)
648 ret = eficonfig_process_common(efi_menu, " ** Select Volume **");
650 efi_free_pool(volume_handles);
651 list_for_each_safe(pos, n, &efi_menu->list) {
652 entry = list_entry(pos, struct eficonfig_entry, list);
655 eficonfig_destroy(efi_menu);
661 * sort_file() - sort the file name in ascii order
663 * @data1: pointer to the file entry data
664 * @data2: pointer to the file entry data
665 * Return: -1 if the data1 file name is less than data2 file name,
666 * 0 if both file name match,
667 * 1 if the data1 file name is greater thant data2 file name.
669 static int sort_file(const void *arg1, const void *arg2)
671 const struct eficonfig_file_entry_data *data1, *data2;
673 data1 = *((const struct eficonfig_file_entry_data **)arg1);
674 data2 = *((const struct eficonfig_file_entry_data **)arg2);
676 return strcasecmp(data1->file_name, data2->file_name);
680 * eficonfig_create_file_entry() - construct the file menu entry
682 * @efi_menu: pointer to the efimenu structure
683 * @count: number of the directory and file
684 * @tmp_infos: pointer to the entry data array
685 * @f: pointer to the file handle
686 * @buf: pointer to the buffer to store the directory information
687 * @file_info: pointer to the file selection structure
688 * Return: status code
691 eficonfig_create_file_entry(struct efimenu *efi_menu, u32 count,
692 struct eficonfig_file_entry_data **tmp_infos,
693 struct efi_file_handle *f, struct efi_file_info *buf,
694 struct eficonfig_select_file_info *file_info)
699 u32 i, entry_num = 0;
700 struct eficonfig_file_entry_data *info;
702 efi_file_setpos_int(f, 0);
703 /* Read directory and construct menu structure */
704 for (i = 0; i < count; i++) {
705 if (entry_num >= EFICONFIG_ENTRY_NUM_MAX - 1)
708 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
709 ret = efi_file_read_int(f, &len, buf);
710 if (ret != EFI_SUCCESS || len == 0)
713 info = calloc(1, sizeof(struct eficonfig_file_entry_data));
715 ret = EFI_OUT_OF_RESOURCES;
719 /* append '\\' at the end of directory name */
720 name = calloc(1, utf16_utf8_strlen(buf->file_name) + 2);
722 ret = EFI_OUT_OF_RESOURCES;
727 utf16_utf8_strcpy(&p, buf->file_name);
728 if (buf->attribute & EFI_FILE_DIRECTORY) {
729 /* filter out u'.' */
730 if (!u16_strcmp(buf->file_name, u".")) {
735 name[u16_strlen(buf->file_name)] = '\\';
736 info->is_directory = true;
739 info->file_name = name;
740 info->file_info = file_info;
741 tmp_infos[entry_num++] = info;
744 qsort(tmp_infos, entry_num, sizeof(*tmp_infos),
745 (int (*)(const void *, const void *))sort_file);
747 for (i = 0; i < entry_num; i++) {
748 ret = append_entry(efi_menu, tmp_infos[i]->file_name,
749 eficonfig_file_selected, tmp_infos[i]);
750 if (ret != EFI_SUCCESS)
759 * eficonfig_select_file() - construct the file selection menu
761 * @file_info: pointer to the file selection structure
762 * @root: pointer to the file handle
763 * Return: status code
765 static efi_status_t eficonfig_select_file(struct eficonfig_select_file_info *file_info,
766 struct efi_file_handle *root)
771 struct efimenu *efi_menu;
772 struct efi_file_handle *f;
773 struct efi_file_info *buf;
774 struct eficonfig_file_entry_data **tmp_infos;
776 buf = calloc(1, sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE);
778 return EFI_OUT_OF_RESOURCES;
780 while (!file_info->file_selected) {
781 efi_menu = calloc(1, sizeof(struct efimenu));
783 ret = EFI_OUT_OF_RESOURCES;
786 INIT_LIST_HEAD(&efi_menu->list);
788 ret = efi_file_open_int(root, &f, file_info->current_path, EFI_FILE_MODE_READ, 0);
789 if (ret != EFI_SUCCESS) {
790 eficonfig_print_msg("Reading volume failed!");
796 /* Count the number of directory entries */
798 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
799 ret = efi_file_read_int(f, &len, buf);
800 if (ret != EFI_SUCCESS || len == 0)
806 /* allocate array to sort the entry */
807 tmp_infos = calloc(count, sizeof(*tmp_infos));
809 ret = EFI_OUT_OF_RESOURCES;
813 ret = eficonfig_create_file_entry(efi_menu, count, tmp_infos,
815 if (ret != EFI_SUCCESS)
818 ret = append_quit_entry(efi_menu);
819 if (ret != EFI_SUCCESS)
822 ret = eficonfig_process_common(efi_menu, " ** Select File **");
824 efi_file_close_int(f);
825 eficonfig_destroy(efi_menu);
828 for (i = 0; i < count; i++)
834 if (ret != EFI_SUCCESS)
845 * handle_user_input() - handle user input
847 * @buf: pointer to the buffer
848 * @buf_size: size of the buffer
849 * @cursor_col: cursor column for user input
850 * @msg: pointer to the string to display
851 * Return: status code
853 static efi_status_t handle_user_input(u16 *buf, int buf_size,
854 int cursor_col, char *msg)
859 printf(ANSI_CLEAR_CONSOLE
863 " Press ENTER to complete, ESC/CTRL+C to quit",
866 /* tmp is used to accept user cancel */
867 tmp = calloc(1, buf_size * sizeof(u16));
869 return EFI_OUT_OF_RESOURCES;
871 ret = efi_console_get_u16_string(cin, tmp, buf_size, NULL, 4, cursor_col);
872 if (ret == EFI_SUCCESS)
873 u16_strcpy(buf, tmp);
877 /* to stay the parent menu */
878 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
884 * eficonfig_boot_add_enter_description() - handle user input for description
886 * @data: pointer to the internal boot option structure
887 * Return: status code
889 static efi_status_t eficonfig_boot_add_enter_description(void *data)
891 struct eficonfig_boot_option *bo = data;
893 return handle_user_input(bo->description, EFICONFIG_DESCRIPTION_MAX, 22,
894 "\n ** Edit Description **\n"
896 " enter description: ");
900 * eficonfig_boot_add_optional_data() - handle user input for optional data
902 * @data: pointer to the internal boot option structure
903 * Return: status code
905 static efi_status_t eficonfig_boot_add_optional_data(void *data)
907 struct eficonfig_boot_option *bo = data;
909 return handle_user_input(bo->optional_data, EFICONFIG_OPTIONAL_DATA_MAX, 24,
910 "\n ** Edit Optional Data **\n"
912 " enter optional data:");
916 * eficonfig_boot_edit_save() - handler to save the boot option
918 * @data: pointer to the internal boot option structure
919 * Return: status code
921 static efi_status_t eficonfig_boot_edit_save(void *data)
923 struct eficonfig_boot_option *bo = data;
925 if (u16_strlen(bo->description) == 0) {
926 eficonfig_print_msg("Boot Description is empty!");
927 bo->edit_completed = false;
928 return EFI_NOT_READY;
930 if (u16_strlen(bo->file_info.current_path) == 0) {
931 eficonfig_print_msg("File is not selected!");
932 bo->edit_completed = false;
933 return EFI_NOT_READY;
936 bo->edit_completed = true;
942 * eficonfig_process_select_file() - callback function for "Select File" entry
944 * @data: pointer to the data
945 * Return: status code
947 efi_status_t eficonfig_process_select_file(void *data)
953 * eficonfig_process_clear_file_selection() - callback function for "Clear" entry
955 * @data: pointer to the data
956 * Return: status code
958 efi_status_t eficonfig_process_clear_file_selection(void *data)
960 struct eficonfig_select_file_info *file_info = data;
962 /* clear the existing file information */
963 file_info->current_volume = NULL;
964 file_info->current_path[0] = u'\0';
965 file_info->dp_volume = NULL;
970 static struct eficonfig_item select_file_menu_items[] = {
971 {"Select File", eficonfig_process_select_file},
972 {"Clear", eficonfig_process_clear_file_selection},
973 {"Quit", eficonfig_process_quit},
978 * eficonfig_display_select_file_option() - display select file option
980 * @file_info: pointer to the file information structure
981 * Return: status code
983 efi_status_t eficonfig_display_select_file_option(struct eficonfig_select_file_info *file_info)
986 struct efimenu *efi_menu;
988 select_file_menu_items[1].data = file_info;
989 efi_menu = eficonfig_create_fixed_menu(select_file_menu_items,
990 ARRAY_SIZE(select_file_menu_items));
992 return EFI_OUT_OF_RESOURCES;
994 ret = eficonfig_process_common(efi_menu, " ** Update File **");
995 if (ret != EFI_SUCCESS) /* User selects "Clear" or "Quit" */
998 eficonfig_destroy(efi_menu);
1004 * eficonfig_select_file_handler() - handle user file selection
1006 * @data: pointer to the data
1007 * Return: status code
1009 efi_status_t eficonfig_select_file_handler(void *data)
1013 struct list_head *pos, *n;
1014 struct efi_file_handle *root;
1015 struct eficonfig_filepath_info *item;
1016 struct eficonfig_select_file_info *tmp = NULL;
1017 struct eficonfig_select_file_info *file_info = data;
1019 ret = eficonfig_display_select_file_option(file_info);
1020 if (ret != EFI_SUCCESS)
1023 tmp = calloc(1, sizeof(struct eficonfig_select_file_info));
1025 return EFI_OUT_OF_RESOURCES;
1027 tmp->current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1028 if (!tmp->current_path) {
1030 return EFI_OUT_OF_RESOURCES;
1032 INIT_LIST_HEAD(&tmp->filepath_list);
1034 while (!tmp->file_selected) {
1035 tmp->current_volume = NULL;
1036 memset(tmp->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
1038 ret = eficonfig_select_volume(tmp);
1039 if (ret != EFI_SUCCESS)
1042 if (!tmp->current_volume)
1043 return EFI_INVALID_PARAMETER;
1045 ret = efi_open_volume_int(tmp->current_volume, &root);
1046 if (ret != EFI_SUCCESS)
1049 ret = eficonfig_select_file(tmp, root);
1050 if (ret == EFI_ABORTED)
1052 if (ret != EFI_SUCCESS)
1057 if (ret == EFI_SUCCESS) {
1058 len = u16_strlen(tmp->current_path);
1059 len = (len >= EFICONFIG_FILE_PATH_MAX) ? (EFICONFIG_FILE_PATH_MAX - 1) : len;
1060 memcpy(file_info->current_path, tmp->current_path, len * sizeof(u16));
1061 file_info->current_path[len] = u'\0';
1062 file_info->current_volume = tmp->current_volume;
1063 file_info->dp_volume = tmp->dp_volume;
1066 list_for_each_safe(pos, n, &tmp->filepath_list) {
1067 item = list_entry(pos, struct eficonfig_filepath_info, list);
1068 list_del(&item->list);
1072 free(tmp->current_path);
1075 /* to stay the parent menu */
1076 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1082 * eficonfig_get_unused_bootoption() - get unused "Boot####" index
1084 * @buf: pointer to the buffer to store boot option variable name
1085 * @buf_size: buffer size
1086 * @index: pointer to store the index in the BootOrder variable
1087 * Return: status code
1089 efi_status_t eficonfig_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
1090 unsigned int *index)
1096 if (buf_size < u16_strsize(u"Boot####"))
1097 return EFI_BUFFER_TOO_SMALL;
1099 for (i = 0; i <= 0xFFFF; i++) {
1101 efi_create_indexed_name(buf, buf_size, "Boot", i);
1102 ret = efi_get_variable_int(buf, &efi_global_variable_guid,
1103 NULL, &size, NULL, NULL);
1104 if (ret == EFI_BUFFER_TOO_SMALL)
1111 return EFI_OUT_OF_RESOURCES;
1119 * eficonfig_set_boot_option() - set boot option
1121 * @varname: pointer to variable name
1122 * @dp: pointer to device path
1123 * @label: pointer to label string
1124 * @optional_data: pointer to optional data
1125 * Return: status code
1127 static efi_status_t eficonfig_set_boot_option(u16 *varname, struct efi_device_path *dp,
1128 efi_uintn_t dp_size, u16 *label, char *optional_data)
1133 struct efi_load_option lo;
1136 lo.file_path_length = dp_size;
1137 lo.attributes = LOAD_OPTION_ACTIVE;
1138 lo.optional_data = optional_data;
1141 size = efi_serialize_load_option(&lo, (u8 **)&p);
1143 return EFI_INVALID_PARAMETER;
1145 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1146 EFI_VARIABLE_NON_VOLATILE |
1147 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1148 EFI_VARIABLE_RUNTIME_ACCESS,
1156 * eficonfig_append_bootorder() - append new boot option in BootOrder variable
1158 * @index: "Boot####" index to append to BootOrder variable
1159 * Return: status code
1161 efi_status_t eficonfig_append_bootorder(u16 index)
1165 u16 *new_bootorder = NULL;
1166 efi_uintn_t last, size, new_size;
1168 /* append new boot option */
1169 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1170 last = size / sizeof(u16);
1171 new_size = size + sizeof(u16);
1172 new_bootorder = calloc(1, new_size);
1173 if (!new_bootorder) {
1174 ret = EFI_OUT_OF_RESOURCES;
1177 memcpy(new_bootorder, bootorder, size);
1178 new_bootorder[last] = index;
1180 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1181 EFI_VARIABLE_NON_VOLATILE |
1182 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1183 EFI_VARIABLE_RUNTIME_ACCESS,
1184 new_size, new_bootorder, false);
1185 if (ret != EFI_SUCCESS)
1190 free(new_bootorder);
1196 * create_boot_option_entry() - create boot option entry
1198 * @efi_menu: pointer to the efimenu structure
1199 * @title: pointer to the entry title
1200 * @val: pointer to boot option label
1201 * @func: callback of each entry
1202 * @data: pointer to the data to be passed to each entry callback
1203 * Return: status code
1205 static efi_status_t create_boot_option_entry(struct efimenu *efi_menu, char *title, u16 *val,
1206 eficonfig_entry_func func, void *data)
1211 len = strlen(title) + 1;
1213 len += utf16_utf8_strlen(val);
1214 buf = calloc(1, len);
1216 return EFI_OUT_OF_RESOURCES;
1220 p = buf + strlen(title);
1221 utf16_utf8_strcpy(&p, val);
1224 return append_entry(efi_menu, buf, func, data);
1228 * prepare_file_selection_entry() - prepare file selection entry
1230 * @efi_menu: pointer to the efimenu structure
1231 * @title: pointer to the title string
1232 * @file_info: pointer to the file info
1233 * Return: status code
1235 static efi_status_t prepare_file_selection_entry(struct efimenu *efi_menu, char *title,
1236 struct eficonfig_select_file_info *file_info)
1240 u16 *file_name = NULL, *p;
1241 efi_handle_t handle;
1244 devname = calloc(1, EFICONFIG_VOLUME_PATH_MAX + 1);
1246 return EFI_OUT_OF_RESOURCES;
1248 /* get the device name only when the user already selected the file path */
1249 handle = efi_dp_find_obj(file_info->dp_volume, NULL, NULL);
1251 ret = efi_disk_get_device_name(handle, devname, EFICONFIG_VOLUME_PATH_MAX);
1252 if (ret != EFI_SUCCESS)
1257 * If the preconfigured volume does not exist in the system, display the text
1258 * converted volume device path instead of U-Boot friendly name(e.g. "usb 0:1").
1260 if (!handle && file_info->dp_volume) {
1264 dp_str = efi_dp_str(file_info->dp_volume);
1266 utf16_utf8_strncpy(&q, dp_str, EFICONFIG_VOLUME_PATH_MAX);
1268 efi_free_pool(dp_str);
1271 /* append u'/' to devname, it is just for display purpose. */
1272 if (file_info->current_path[0] != u'\0' && file_info->current_path[0] != u'/')
1273 strlcat(devname, "/", EFICONFIG_VOLUME_PATH_MAX + 1);
1275 len = strlen(devname);
1276 len += utf16_utf8_strlen(file_info->current_path) + 1;
1277 file_name = calloc(1, len * sizeof(u16));
1279 ret = EFI_OUT_OF_RESOURCES;
1284 utf8_utf16_strcpy(&p, devname);
1285 u16_strlcat(file_name, file_info->current_path, len);
1286 ret = create_boot_option_entry(efi_menu, title, file_name,
1287 eficonfig_select_file_handler, file_info);
1296 * eficonfig_show_boot_option() - prepare menu entry for editing boot option
1298 * Construct the structures to create edit boot option menu
1300 * @bo: pointer to the boot option
1301 * @header_str: pointer to the header string
1302 * Return: status code
1304 static efi_status_t eficonfig_show_boot_option(struct eficonfig_boot_option *bo,
1308 struct efimenu *efi_menu;
1310 efi_menu = calloc(1, sizeof(struct efimenu));
1312 return EFI_OUT_OF_RESOURCES;
1314 INIT_LIST_HEAD(&efi_menu->list);
1316 ret = create_boot_option_entry(efi_menu, "Description: ", bo->description,
1317 eficonfig_boot_add_enter_description, bo);
1318 if (ret != EFI_SUCCESS)
1321 ret = prepare_file_selection_entry(efi_menu, "File: ", &bo->file_info);
1322 if (ret != EFI_SUCCESS)
1325 ret = prepare_file_selection_entry(efi_menu, "Initrd File: ", &bo->initrd_info);
1326 if (ret != EFI_SUCCESS)
1329 ret = create_boot_option_entry(efi_menu, "Optional Data: ", bo->optional_data,
1330 eficonfig_boot_add_optional_data, bo);
1331 if (ret != EFI_SUCCESS)
1334 ret = create_boot_option_entry(efi_menu, "Save", NULL,
1335 eficonfig_boot_edit_save, bo);
1336 if (ret != EFI_SUCCESS)
1339 ret = create_boot_option_entry(efi_menu, "Quit", NULL,
1340 eficonfig_process_quit, NULL);
1341 if (ret != EFI_SUCCESS)
1344 ret = eficonfig_process_common(efi_menu, header_str);
1346 eficonfig_destroy(efi_menu);
1352 * fill_file_info() - fill the file info from efi_device_path structure
1354 * @dp: pointer to the device path
1355 * @file_info: pointer to the file info structure
1356 * @device_dp: pointer to the volume device path
1358 static void fill_file_info(struct efi_device_path *dp,
1359 struct eficonfig_select_file_info *file_info,
1360 struct efi_device_path *device_dp)
1363 struct efi_device_path *file_dp = NULL;
1365 efi_dp_split_file_path(dp, &device_dp, &file_dp);
1366 file_info->dp_volume = device_dp;
1369 file_str = efi_dp_str(file_dp);
1371 * efi_convert_device_path_to_text() automatically adds u'/' at the
1372 * beginning of file name, remove u'/' before copying to current_path
1378 u16_strcpy(file_info->current_path, p);
1379 efi_free_pool(file_dp);
1380 efi_free_pool(file_str);
1385 * eficonfig_edit_boot_option() - prepare boot option structure for editing
1387 * Construct the boot option structure and copy the existing value
1389 * @varname: pointer to the UEFI variable name
1390 * @bo: pointer to the boot option
1391 * @load_option: pointer to the load option
1392 * @load_option_size: size of the load option
1393 * @header_str: pointer to the header string
1394 * Return : status code
1396 static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_boot_option *bo,
1397 void *load_option, efi_uintn_t load_option_size,
1402 char *tmp = NULL, *p;
1403 struct efi_load_option lo = {0};
1404 efi_uintn_t final_dp_size;
1405 struct efi_device_path *dp = NULL;
1406 efi_uintn_t size = load_option_size;
1407 struct efi_device_path *final_dp = NULL;
1408 struct efi_device_path *device_dp = NULL;
1409 struct efi_device_path *initrd_dp = NULL;
1410 struct efi_device_path *initrd_device_dp = NULL;
1412 const struct efi_initrd_dp id_dp = {
1415 DEVICE_PATH_TYPE_MEDIA_DEVICE,
1416 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
1417 sizeof(id_dp.vendor),
1419 EFI_INITRD_MEDIA_GUID,
1422 DEVICE_PATH_TYPE_END,
1423 DEVICE_PATH_SUB_TYPE_END,
1428 bo->file_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1429 if (!bo->file_info.current_path) {
1430 ret = EFI_OUT_OF_RESOURCES;
1434 bo->initrd_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1435 if (!bo->file_info.current_path) {
1436 ret = EFI_OUT_OF_RESOURCES;
1440 bo->description = calloc(1, EFICONFIG_DESCRIPTION_MAX * sizeof(u16));
1441 if (!bo->description) {
1442 ret = EFI_OUT_OF_RESOURCES;
1446 bo->optional_data = calloc(1, EFICONFIG_OPTIONAL_DATA_MAX * sizeof(u16));
1447 if (!bo->optional_data) {
1448 ret = EFI_OUT_OF_RESOURCES;
1452 /* copy the preset value */
1454 ret = efi_deserialize_load_option(&lo, load_option, &size);
1455 if (ret != EFI_SUCCESS)
1459 ret = EFI_INVALID_PARAMETER;
1462 /* truncate the long label string */
1463 if (u16_strlen(lo.label) >= EFICONFIG_DESCRIPTION_MAX)
1464 lo.label[EFICONFIG_DESCRIPTION_MAX - 1] = u'\0';
1466 u16_strcpy(bo->description, lo.label);
1468 /* EFI image file path is a first instance */
1470 fill_file_info(lo.file_path, &bo->file_info, device_dp);
1472 /* Initrd file path(optional) is placed at second instance. */
1473 initrd_dp = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
1475 fill_file_info(initrd_dp, &bo->initrd_info, initrd_device_dp);
1476 efi_free_pool(initrd_dp);
1480 memcpy(bo->optional_data, lo.optional_data, size);
1484 ret = eficonfig_show_boot_option(bo, header_str);
1485 if (ret == EFI_SUCCESS && bo->edit_completed)
1487 if (ret == EFI_NOT_READY)
1489 if (ret != EFI_SUCCESS)
1493 if (bo->initrd_info.dp_volume) {
1494 dp = create_selected_device_path(&bo->initrd_info);
1496 ret = EFI_OUT_OF_RESOURCES;
1499 initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp, dp);
1503 dp = create_selected_device_path(&bo->file_info);
1505 ret = EFI_OUT_OF_RESOURCES;
1508 final_dp_size = efi_dp_size(dp) + sizeof(END);
1510 final_dp = efi_dp_concat(dp, initrd_dp);
1511 final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
1513 final_dp = efi_dp_dup(dp);
1520 if (utf16_utf8_strlen(bo->optional_data)) {
1521 len = utf16_utf8_strlen(bo->optional_data) + 1;
1522 tmp = calloc(1, len);
1526 utf16_utf8_strncpy(&p, bo->optional_data, u16_strlen(bo->optional_data));
1529 ret = eficonfig_set_boot_option(varname, final_dp, final_dp_size, bo->description, tmp);
1530 if (ret != EFI_SUCCESS)
1534 free(bo->optional_data);
1535 free(bo->description);
1536 free(bo->file_info.current_path);
1537 free(bo->initrd_info.current_path);
1538 efi_free_pool(device_dp);
1539 efi_free_pool(initrd_device_dp);
1540 efi_free_pool(initrd_dp);
1541 efi_free_pool(final_dp);
1547 * eficonfig_process_add_boot_option() - handler to add boot option
1549 * @data: pointer to the data for each entry
1550 * Return: status code
1552 static efi_status_t eficonfig_process_add_boot_option(void *data)
1556 struct eficonfig_boot_option *bo = NULL;
1558 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1560 return EFI_OUT_OF_RESOURCES;
1562 ret = eficonfig_get_unused_bootoption(varname, sizeof(varname), &bo->boot_index);
1563 if (ret != EFI_SUCCESS)
1566 ret = eficonfig_edit_boot_option(varname, bo, NULL, 0, " ** Add Boot Option ** ");
1567 if (ret != EFI_SUCCESS)
1570 ret = eficonfig_append_bootorder((u16)bo->boot_index);
1571 if (ret != EFI_SUCCESS)
1577 /* to stay the parent menu */
1578 ret = (ret == EFI_ABORTED) ? EFI_SUCCESS : ret;
1584 * eficonfig_process_boot_selected() - handler to select boot option entry
1586 * @data: pointer to the data for each entry
1587 * Return: status code
1589 static efi_status_t eficonfig_process_boot_selected(void *data)
1591 struct eficonfig_boot_selection_data *info = data;
1594 *info->selected = info->boot_index;
1600 * search_bootorder() - search the boot option index in BootOrder
1602 * @bootorder: pointer to the BootOrder variable
1603 * @num: number of BootOrder entry
1604 * @target: target boot option index to search
1605 * @index: pointer to store the index of BootOrder variable
1606 * Return: true if exists, false otherwise
1608 static bool search_bootorder(u16 *bootorder, efi_uintn_t num, u32 target, u32 *index)
1612 for (i = 0; i < num; i++) {
1613 if (target == bootorder[i]) {
1625 * eficonfig_add_boot_selection_entry() - add boot option menu entry
1627 * @efi_menu: pointer to store the efimenu structure
1628 * @boot_index: boot option index to be added
1629 * @selected: pointer to store the selected boot option index
1630 * Return: status code
1632 static efi_status_t eficonfig_add_boot_selection_entry(struct efimenu *efi_menu,
1633 unsigned int boot_index,
1634 unsigned int *selected)
1640 struct efi_load_option lo;
1641 u16 varname[] = u"Boot####";
1642 struct eficonfig_boot_selection_data *info;
1644 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
1645 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1649 ret = efi_deserialize_load_option(&lo, load_option, &size);
1650 if (ret != EFI_SUCCESS) {
1651 log_warning("Invalid load option for %ls\n", varname);
1656 if (size >= sizeof(efi_guid_t) &&
1657 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
1659 * auto generated entry has GUID in optional_data,
1660 * skip auto generated entry because it will be generated
1661 * again even if it is edited or deleted.
1667 info = calloc(1, sizeof(struct eficonfig_boot_selection_data));
1670 return EFI_OUT_OF_RESOURCES;
1673 buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
1677 return EFI_OUT_OF_RESOURCES;
1680 utf16_utf8_strcpy(&p, lo.label);
1681 info->boot_index = boot_index;
1682 info->selected = selected;
1683 ret = append_entry(efi_menu, buf, eficonfig_process_boot_selected, info);
1684 if (ret != EFI_SUCCESS) {
1695 * eficonfig_show_boot_selection() - construct boot option menu entry
1697 * @selected: pointer to store the selected boot option index
1698 * Return: status code
1700 static efi_status_t eficonfig_show_boot_selection(unsigned int *selected)
1705 efi_uintn_t num, size;
1706 struct efimenu *efi_menu;
1707 struct list_head *pos, *n;
1708 struct eficonfig_entry *entry;
1710 efi_menu = calloc(1, sizeof(struct efimenu));
1712 return EFI_OUT_OF_RESOURCES;
1714 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1716 INIT_LIST_HEAD(&efi_menu->list);
1717 num = size / sizeof(u16);
1718 /* list the load option in the order of BootOrder variable */
1719 for (i = 0; i < num; i++) {
1720 ret = eficonfig_add_boot_selection_entry(efi_menu, bootorder[i], selected);
1721 if (ret != EFI_SUCCESS)
1724 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1728 /* list the remaining load option not included in the BootOrder */
1729 for (i = 0; i <= 0xFFFF; i++) {
1730 /* If the index is included in the BootOrder, skip it */
1731 if (search_bootorder(bootorder, num, i, NULL))
1734 ret = eficonfig_add_boot_selection_entry(efi_menu, i, selected);
1735 if (ret != EFI_SUCCESS)
1738 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1742 ret = append_quit_entry(efi_menu);
1743 if (ret != EFI_SUCCESS)
1746 ret = eficonfig_process_common(efi_menu, " ** Select Boot Option **");
1748 list_for_each_safe(pos, n, &efi_menu->list) {
1749 entry = list_entry(pos, struct eficonfig_entry, list);
1752 eficonfig_destroy(efi_menu);
1758 * eficonfig_process_edit_boot_option() - handler to edit boot option
1760 * @data: pointer to the data for each entry
1761 * Return: status code
1763 static efi_status_t eficonfig_process_edit_boot_option(void *data)
1767 struct eficonfig_boot_option *bo = NULL;
1770 unsigned int selected;
1772 u16 varname[] = u"Boot####";
1774 ret = eficonfig_show_boot_selection(&selected);
1775 if (ret != EFI_SUCCESS)
1778 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1780 ret = EFI_OUT_OF_RESOURCES;
1784 bo->boot_index = selected;
1785 efi_create_indexed_name(varname, sizeof(varname), "Boot", selected);
1786 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1789 ret = EFI_NOT_FOUND;
1793 ret = eficonfig_edit_boot_option(varname, bo, load_option, size,
1794 " ** Edit Boot Option ** ");
1798 if (ret != EFI_SUCCESS && ret != EFI_ABORTED)
1802 /* to stay the parent menu */
1803 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1809 * eficonfig_display_change_boot_order() - display the BootOrder list
1811 * @efi_menu: pointer to the efimenu structure
1812 * Return: status code
1814 static void eficonfig_display_change_boot_order(struct efimenu *efi_menu)
1817 struct list_head *pos, *n;
1818 struct eficonfig_boot_order *entry;
1820 printf(ANSI_CLEAR_CONSOLE ANSI_CURSOR_POSITION
1821 "\n ** Change Boot Order **\n"
1822 ANSI_CURSOR_POSITION
1823 " Press UP/DOWN to move, +/- to change order"
1824 ANSI_CURSOR_POSITION
1825 " Press SPACE to activate or deactivate the entry"
1826 ANSI_CURSOR_POSITION
1827 " Select [Save] to complete, ESC/CTRL+C to quit"
1828 ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
1829 1, 1, efi_menu->count + 5, 1, efi_menu->count + 6, 1,
1830 efi_menu->count + 7, 1, efi_menu->count + 8, 1);
1832 /* draw boot option list */
1833 list_for_each_safe(pos, n, &efi_menu->list) {
1834 entry = list_entry(pos, struct eficonfig_boot_order, list);
1835 reverse = (entry->num == efi_menu->active);
1837 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
1840 puts(ANSI_COLOR_REVERSE);
1842 if (entry->num < efi_menu->count - 2) {
1849 printf("%ls", entry->description);
1852 puts(ANSI_COLOR_RESET);
1857 * eficonfig_choice_change_boot_order() - handle the BootOrder update
1859 * @efi_menu: pointer to the efimenu structure
1860 * Return: status code
1862 static efi_status_t eficonfig_choice_change_boot_order(struct efimenu *efi_menu)
1865 struct list_head *pos, *n;
1866 struct eficonfig_boot_order *tmp;
1867 enum bootmenu_key key = KEY_NONE;
1868 struct eficonfig_boot_order *entry;
1871 bootmenu_loop(NULL, &key, &esc);
1875 if (efi_menu->active > 0) {
1876 list_for_each_safe(pos, n, &efi_menu->list) {
1877 entry = list_entry(pos, struct eficonfig_boot_order, list);
1878 if (entry->num == efi_menu->active)
1881 tmp = list_entry(pos->prev, struct eficonfig_boot_order, list);
1884 list_del(&tmp->list);
1885 list_add(&tmp->list, &entry->list);
1889 if (efi_menu->active > 0)
1891 return EFI_NOT_READY;
1893 if (efi_menu->active < efi_menu->count - 3) {
1894 list_for_each_safe(pos, n, &efi_menu->list) {
1895 entry = list_entry(pos, struct eficonfig_boot_order, list);
1896 if (entry->num == efi_menu->active)
1899 tmp = list_entry(pos->next, struct eficonfig_boot_order, list);
1902 list_del(&entry->list);
1903 list_add(&entry->list, &tmp->list);
1907 return EFI_NOT_READY;
1909 if (efi_menu->active < efi_menu->count - 1)
1911 return EFI_NOT_READY;
1914 if (efi_menu->active == efi_menu->count - 2)
1918 if (efi_menu->active == efi_menu->count - 1)
1923 if (efi_menu->active < efi_menu->count - 2) {
1924 list_for_each_safe(pos, n, &efi_menu->list) {
1925 entry = list_entry(pos, struct eficonfig_boot_order, list);
1926 if (entry->num == efi_menu->active) {
1927 entry->active = entry->active ? false : true;
1928 return EFI_NOT_READY;
1936 /* Pressed key is not valid, no need to regenerate the menu */
1943 * eficonfig_add_change_boot_order_entry() - add boot order entry
1945 * @efi_menu: pointer to the efimenu structure
1946 * @boot_index: boot option index to be added
1947 * @active: flag to include the boot option into BootOrder
1948 * Return: status code
1950 static efi_status_t eficonfig_add_change_boot_order_entry(struct efimenu *efi_menu,
1951 u32 boot_index, bool active)
1956 struct efi_load_option lo;
1957 u16 varname[] = u"Boot####";
1958 struct eficonfig_boot_order *entry;
1960 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
1961 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1965 ret = efi_deserialize_load_option(&lo, load_option, &size);
1966 if (ret != EFI_SUCCESS) {
1971 entry = calloc(1, sizeof(struct eficonfig_boot_order));
1974 return EFI_OUT_OF_RESOURCES;
1977 entry->description = u16_strdup(lo.label);
1978 if (!entry->description) {
1981 return EFI_OUT_OF_RESOURCES;
1983 entry->num = efi_menu->count++;
1984 entry->boot_index = boot_index;
1985 entry->active = active;
1986 list_add_tail(&entry->list, &efi_menu->list);
1994 * eficonfig_create_change_boot_order_entry() - create boot order entry
1996 * @efi_menu: pointer to the efimenu structure
1997 * @bootorder: pointer to the BootOrder variable
1998 * @num: number of BootOrder entry
1999 * Return: status code
2001 static efi_status_t eficonfig_create_change_boot_order_entry(struct efimenu *efi_menu,
2002 u16 *bootorder, efi_uintn_t num)
2006 struct eficonfig_boot_order *entry;
2008 /* list the load option in the order of BootOrder variable */
2009 for (i = 0; i < num; i++) {
2010 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2013 ret = eficonfig_add_change_boot_order_entry(efi_menu, bootorder[i], true);
2014 if (ret != EFI_SUCCESS)
2018 /* list the remaining load option not included in the BootOrder */
2019 for (i = 0; i < 0xFFFF; i++) {
2020 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2023 /* If the index is included in the BootOrder, skip it */
2024 if (search_bootorder(bootorder, num, i, NULL))
2027 ret = eficonfig_add_change_boot_order_entry(efi_menu, i, false);
2028 if (ret != EFI_SUCCESS)
2032 /* add "Save" and "Quit" entries */
2033 entry = calloc(1, sizeof(struct eficonfig_boot_order));
2037 entry->num = efi_menu->count++;
2038 entry->description = u16_strdup(u"Save");
2039 list_add_tail(&entry->list, &efi_menu->list);
2041 entry = calloc(1, sizeof(struct eficonfig_boot_order));
2045 entry->num = efi_menu->count++;
2046 entry->description = u16_strdup(u"Quit");
2047 list_add_tail(&entry->list, &efi_menu->list);
2049 efi_menu->active = 0;
2053 return EFI_OUT_OF_RESOURCES;
2057 * eficonfig_process_change_boot_order() - handler to change boot order
2059 * @data: pointer to the data for each entry
2060 * Return: status code
2062 static efi_status_t eficonfig_process_change_boot_order(void *data)
2067 efi_uintn_t num, size;
2068 struct list_head *pos, *n;
2069 struct eficonfig_boot_order *entry;
2070 struct efimenu *efi_menu;
2072 efi_menu = calloc(1, sizeof(struct efimenu));
2074 return EFI_OUT_OF_RESOURCES;
2076 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2078 INIT_LIST_HEAD(&efi_menu->list);
2079 num = size / sizeof(u16);
2080 ret = eficonfig_create_change_boot_order_entry(efi_menu, bootorder, num);
2081 if (ret != EFI_SUCCESS)
2085 eficonfig_display_change_boot_order(efi_menu);
2087 ret = eficonfig_choice_change_boot_order(efi_menu);
2088 if (ret == EFI_SUCCESS) {
2091 new_bootorder = calloc(1, (efi_menu->count - 2) * sizeof(u16));
2092 if (!new_bootorder) {
2093 ret = EFI_OUT_OF_RESOURCES;
2097 /* create new BootOrder */
2099 list_for_each_safe(pos, n, &efi_menu->list) {
2100 entry = list_entry(pos, struct eficonfig_boot_order, list);
2102 new_bootorder[count++] = entry->boot_index;
2105 size = count * sizeof(u16);
2106 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
2107 EFI_VARIABLE_NON_VOLATILE |
2108 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2109 EFI_VARIABLE_RUNTIME_ACCESS,
2110 size, new_bootorder, false);
2112 free(new_bootorder);
2114 } else if (ret == EFI_NOT_READY) {
2121 list_for_each_safe(pos, n, &efi_menu->list) {
2122 entry = list_entry(pos, struct eficonfig_boot_order, list);
2123 list_del(&entry->list);
2124 free(entry->description);
2131 /* to stay the parent menu */
2132 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2138 * delete_boot_option() - delete selected boot option
2140 * @boot_index: boot option index to delete
2141 * Return: status code
2143 static efi_status_t delete_boot_option(u16 boot_index)
2149 efi_uintn_t num, size;
2151 efi_create_indexed_name(varname, sizeof(varname),
2152 "Boot", boot_index);
2153 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
2155 if (ret != EFI_SUCCESS) {
2156 log_err("delete boot option(%ls) failed\n", varname);
2160 /* update BootOrder if necessary */
2161 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2165 num = size / sizeof(u16);
2166 if (!search_bootorder(bootorder, num, boot_index, &index))
2169 memmove(&bootorder[index], &bootorder[index + 1],
2170 (num - index - 1) * sizeof(u16));
2171 size -= sizeof(u16);
2172 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
2173 EFI_VARIABLE_NON_VOLATILE |
2174 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2175 EFI_VARIABLE_RUNTIME_ACCESS,
2176 size, bootorder, false);
2182 * eficonfig_process_delete_boot_option() - handler to delete boot option
2184 * @data: pointer to the data for each entry
2185 * Return: status code
2187 static efi_status_t eficonfig_process_delete_boot_option(void *data)
2190 unsigned int selected;
2193 ret = eficonfig_show_boot_selection(&selected);
2194 if (ret == EFI_SUCCESS)
2195 ret = delete_boot_option(selected);
2197 if (ret != EFI_SUCCESS)
2201 /* to stay the parent menu */
2202 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2208 * eficonfig_enumerate_boot_option() - enumerate the possible bootable media
2210 * @opt: pointer to the media boot option structure
2211 * @volume_handles: pointer to the efi handles
2212 * @count: number of efi handle
2213 * Return: status code
2215 efi_status_t eficonfig_enumerate_boot_option(struct eficonfig_media_boot_option *opt,
2216 efi_handle_t *volume_handles, efi_status_t count)
2219 struct efi_handler *handler;
2220 efi_status_t ret = EFI_SUCCESS;
2222 for (i = 0; i < count; i++) {
2224 u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
2225 char *optional_data;
2226 struct efi_load_option lo;
2227 char buf[BOOTMENU_DEVICE_NAME_MAX];
2228 struct efi_device_path *device_path;
2230 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
2231 if (ret != EFI_SUCCESS)
2233 ret = efi_protocol_open(handler, (void **)&device_path,
2234 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2235 if (ret != EFI_SUCCESS)
2238 ret = efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
2239 if (ret != EFI_SUCCESS)
2243 utf8_utf16_strncpy(&p, buf, strlen(buf));
2245 lo.label = dev_name;
2246 lo.attributes = LOAD_OPTION_ACTIVE;
2247 lo.file_path = device_path;
2248 lo.file_path_length = efi_dp_size(device_path) + sizeof(END);
2250 * Set the dedicated guid to optional_data, it is used to identify
2251 * the boot option that automatically generated by the bootmenu.
2252 * efi_serialize_load_option() expects optional_data is null-terminated
2253 * utf8 string, so set the "1234567" string to allocate enough space
2254 * to store guid, instead of realloc the load_option.
2256 lo.optional_data = "1234567";
2257 opt[i].size = efi_serialize_load_option(&lo, (u8 **)&opt[i].lo);
2259 ret = EFI_OUT_OF_RESOURCES;
2263 optional_data = (char *)opt[i].lo + (opt[i].size - u16_strsize(u"1234567"));
2264 memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
2272 * eficonfig_delete_invalid_boot_option() - delete non-existing boot option
2274 * @opt: pointer to the media boot option structure
2275 * @count: number of media boot option structure
2276 * Return: status code
2278 efi_status_t eficonfig_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
2285 struct efi_load_option lo;
2286 u16 varname[] = u"Boot####";
2288 for (i = 0; i <= 0xFFFF; i++) {
2291 efi_create_indexed_name(varname, sizeof(varname), "Boot", i);
2292 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
2297 ret = efi_deserialize_load_option(&lo, load_option, &size);
2298 if (ret != EFI_SUCCESS)
2301 if (size >= sizeof(efi_guid_bootmenu_auto_generated)) {
2302 if (guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated) == 0) {
2303 for (j = 0; j < count; j++) {
2304 if (opt[j].size == tmp &&
2305 memcmp(opt[j].lo, load_option, tmp) == 0) {
2306 opt[j].exist = true;
2312 ret = delete_boot_option(i);
2313 if (ret != EFI_SUCCESS) {
2329 * eficonfig_generate_media_device_boot_option() - generate the media device boot option
2331 * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
2332 * and generate the bootmenu entries.
2333 * This function also provide the BOOT#### variable maintenance for
2334 * the media device entries.
2335 * - Automatically create the BOOT#### variable for the newly detected device,
2336 * this BOOT#### variable is distinguished by the special GUID
2337 * stored in the EFI_LOAD_OPTION.optional_data
2338 * - If the device is not attached to the system, the associated BOOT#### variable
2339 * is automatically deleted.
2341 * Return: status code
2343 efi_status_t eficonfig_generate_media_device_boot_option(void)
2348 efi_handle_t *volume_handles = NULL;
2349 struct eficonfig_media_boot_option *opt = NULL;
2351 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
2352 NULL, &count, (efi_handle_t **)&volume_handles);
2353 if (ret != EFI_SUCCESS)
2356 opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
2360 /* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */
2361 ret = eficonfig_enumerate_boot_option(opt, volume_handles, count);
2362 if (ret != EFI_SUCCESS)
2366 * System hardware configuration may vary depending on the user setup.
2367 * The boot option is automatically added by the bootmenu.
2368 * If the device is not attached to the system, the boot option needs
2371 ret = eficonfig_delete_invalid_boot_option(opt, count);
2372 if (ret != EFI_SUCCESS)
2375 /* add non-existent boot option */
2376 for (i = 0; i < count; i++) {
2380 if (!opt[i].exist) {
2381 ret = eficonfig_get_unused_bootoption(var_name, sizeof(var_name),
2383 if (ret != EFI_SUCCESS)
2386 ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
2387 EFI_VARIABLE_NON_VOLATILE |
2388 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2389 EFI_VARIABLE_RUNTIME_ACCESS,
2390 opt[i].size, opt[i].lo, false);
2391 if (ret != EFI_SUCCESS)
2394 ret = eficonfig_append_bootorder(boot_index);
2395 if (ret != EFI_SUCCESS) {
2396 efi_set_variable_int(var_name, &efi_global_variable_guid,
2405 for (i = 0; i < count; i++)
2409 efi_free_pool(volume_handles);
2415 * eficonfig_init() - do required initialization for eficonfig command
2417 * Return: status code
2419 static efi_status_t eficonfig_init(void)
2421 efi_status_t ret = EFI_SUCCESS;
2423 struct efi_handler *handler;
2426 ret = efi_search_protocol(efi_root, &efi_guid_text_input_protocol, &handler);
2427 if (ret != EFI_SUCCESS)
2430 ret = efi_protocol_open(handler, (void **)&cin, efi_root, NULL,
2431 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2432 if (ret != EFI_SUCCESS)
2441 static const struct eficonfig_item maintenance_menu_items[] = {
2442 {"Add Boot Option", eficonfig_process_add_boot_option},
2443 {"Edit Boot Option", eficonfig_process_edit_boot_option},
2444 {"Change Boot Order", eficonfig_process_change_boot_order},
2445 {"Delete Boot Option", eficonfig_process_delete_boot_option},
2446 {"Quit", eficonfig_process_quit},
2450 * do_eficonfig() - execute `eficonfig` command
2452 * @cmdtp: table entry describing command
2453 * @flag: bitmap indicating how the command was invoked
2454 * @argc: number of arguments
2455 * @argv: command line arguments
2456 * Return: status code
2458 static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
2461 struct efimenu *efi_menu;
2464 return CMD_RET_USAGE;
2466 ret = efi_init_obj_list();
2467 if (ret != EFI_SUCCESS) {
2468 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
2469 ret & ~EFI_ERROR_MASK);
2471 return CMD_RET_FAILURE;
2474 ret = eficonfig_init();
2475 if (ret != EFI_SUCCESS)
2476 return CMD_RET_FAILURE;
2478 ret = eficonfig_generate_media_device_boot_option();
2479 if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
2483 efi_menu = eficonfig_create_fixed_menu(maintenance_menu_items,
2484 ARRAY_SIZE(maintenance_menu_items));
2486 return CMD_RET_FAILURE;
2488 ret = eficonfig_process_common(efi_menu, " ** UEFI Maintenance Menu **");
2489 eficonfig_destroy(efi_menu);
2491 if (ret == EFI_ABORTED)
2495 return CMD_RET_SUCCESS;
2499 eficonfig, 1, 0, do_eficonfig,
2500 "provide menu-driven UEFI variable maintenance interface",