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_data - structure to be used to update BootOrder variable
98 * @boot_index: boot option index
99 * @active: flag to include the boot option into BootOrder variable
101 struct eficonfig_boot_order_data {
107 * eficonfig_print_msg() - print message
109 * display the message to the user, user proceeds the screen
110 * with any key press.
112 * @items: pointer to the structure of each menu entry
113 * @count: the number of menu entry
114 * @menu_header: pointer to the menu header string
115 * Return: status code
117 void eficonfig_print_msg(char *msg)
123 printf(ANSI_CURSOR_HIDE
126 "%s\n\n Press any key to continue", 3, 4, msg);
132 * eficonfig_print_entry() - print each menu entry
134 * @data: pointer to the data associated with each menu entry
136 static void eficonfig_print_entry(void *data)
138 struct eficonfig_entry *entry = data;
139 int reverse = (entry->efi_menu->active == entry->num);
141 /* TODO: support scroll or page for many entries */
144 * Move cursor to line where the entry will be drawn (entry->num)
145 * First 3 lines(menu header) + 1 empty line
147 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
150 puts(ANSI_COLOR_REVERSE);
152 printf("%s", entry->title);
155 puts(ANSI_COLOR_RESET);
159 * eficonfig_display_statusline() - print status line
161 * @m: pointer to the menu structure
163 static void eficonfig_display_statusline(struct menu *m)
165 struct eficonfig_entry *entry;
167 if (menu_default_choice(m, (void *)&entry) < 0)
170 printf(ANSI_CURSOR_POSITION
172 ANSI_CURSOR_POSITION ANSI_CLEAR_LINE ANSI_CURSOR_POSITION
173 " Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit"
174 ANSI_CLEAR_LINE_TO_END ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
175 1, 1, entry->efi_menu->menu_header, entry->efi_menu->count + 5, 1,
176 entry->efi_menu->count + 6, 1, entry->efi_menu->count + 7, 1);
180 * eficonfig_choice_entry() - user key input handler
182 * @data: pointer to the efimenu structure
183 * Return: key string to identify the selected entry
185 static char *eficonfig_choice_entry(void *data)
188 struct list_head *pos, *n;
189 struct eficonfig_entry *entry;
190 enum bootmenu_key key = KEY_NONE;
191 struct efimenu *efi_menu = data;
194 bootmenu_loop((struct bootmenu_data *)efi_menu, &key, &esc);
198 if (efi_menu->active > 0)
200 /* no menu key selected, regenerate menu */
203 if (efi_menu->active < efi_menu->count - 1)
205 /* no menu key selected, regenerate menu */
208 list_for_each_safe(pos, n, &efi_menu->list) {
209 entry = list_entry(pos, struct eficonfig_entry, list);
210 if (entry->num == efi_menu->active)
215 /* Quit by choosing the last entry */
216 entry = list_last_entry(&efi_menu->list, struct eficonfig_entry, list);
219 /* Pressed key is not valid, no need to regenerate the menu */
226 * eficonfig_destroy() - destroy efimenu
228 * @efi_menu: pointer to the efimenu structure
230 void eficonfig_destroy(struct efimenu *efi_menu)
232 struct list_head *pos, *n;
233 struct eficonfig_entry *entry;
238 list_for_each_safe(pos, n, &efi_menu->list) {
239 entry = list_entry(pos, struct eficonfig_entry, list);
241 list_del(&entry->list);
244 free(efi_menu->menu_header);
249 * eficonfig_process_quit() - callback function for "Quit" entry
251 * @data: pointer to the data
252 * Return: status code
254 efi_status_t eficonfig_process_quit(void *data)
260 * eficonfig_append_menu_entry() - append menu item
262 * @efi_menu: pointer to the efimenu structure
263 * @title: pointer to the entry title
264 * @func: callback of each entry
265 * @data: pointer to the data to be passed to each entry callback
266 * Return: status code
268 efi_status_t eficonfig_append_menu_entry(struct efimenu *efi_menu,
269 char *title, eficonfig_entry_func func,
272 struct eficonfig_entry *entry;
274 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX)
275 return EFI_OUT_OF_RESOURCES;
277 entry = calloc(1, sizeof(struct eficonfig_entry));
279 return EFI_OUT_OF_RESOURCES;
281 entry->title = title;
282 sprintf(entry->key, "%d", efi_menu->count);
283 entry->efi_menu = efi_menu;
286 entry->num = efi_menu->count++;
287 list_add_tail(&entry->list, &efi_menu->list);
293 * eficonfig_append_quit_entry() - append quit entry
295 * @efi_menu: pointer to the efimenu structure
296 * Return: status code
298 efi_status_t eficonfig_append_quit_entry(struct efimenu *efi_menu)
303 title = strdup("Quit");
305 return EFI_OUT_OF_RESOURCES;
307 ret = eficonfig_append_menu_entry(efi_menu, title, eficonfig_process_quit, NULL);
308 if (ret != EFI_SUCCESS)
315 * eficonfig_create_fixed_menu() - create fixed entry menu structure
317 * @items: pointer to the menu entry item
318 * @count: the number of menu entry
319 * Return: pointer to the efimenu structure
321 void *eficonfig_create_fixed_menu(const struct eficonfig_item *items, int count)
326 struct efimenu *efi_menu;
327 const struct eficonfig_item *iter = items;
329 efi_menu = calloc(1, sizeof(struct efimenu));
333 INIT_LIST_HEAD(&efi_menu->list);
334 for (i = 0; i < count; i++, iter++) {
335 title = strdup(iter->title);
339 ret = eficonfig_append_menu_entry(efi_menu, title, iter->func, iter->data);
340 if (ret != EFI_SUCCESS) {
348 eficonfig_destroy(efi_menu);
354 * eficonfig_process_common() - main handler for UEFI menu
356 * Construct the structures required to show the menu, then handle
357 * the user input interacting with u-boot menu functions.
359 * @efi_menu: pointer to the efimenu structure
360 * @menu_header: pointer to the menu header string
361 * Return: status code
363 efi_status_t eficonfig_process_common(struct efimenu *efi_menu, char *menu_header)
367 struct list_head *pos, *n;
368 struct eficonfig_entry *entry;
369 efi_status_t ret = EFI_SUCCESS;
371 if (efi_menu->count > EFICONFIG_ENTRY_NUM_MAX)
372 return EFI_OUT_OF_RESOURCES;
374 efi_menu->delay = -1;
375 efi_menu->active = 0;
378 efi_menu->menu_header = strdup(menu_header);
379 if (!efi_menu->menu_header)
380 return EFI_OUT_OF_RESOURCES;
383 menu = menu_create(NULL, 0, 1, eficonfig_display_statusline,
384 eficonfig_print_entry, eficonfig_choice_entry,
387 return EFI_INVALID_PARAMETER;
389 list_for_each_safe(pos, n, &efi_menu->list) {
390 entry = list_entry(pos, struct eficonfig_entry, list);
391 if (!menu_item_add(menu, entry->key, entry)) {
392 ret = EFI_INVALID_PARAMETER;
397 entry = list_first_entry_or_null(&efi_menu->list, struct eficonfig_entry, list);
399 menu_default_set(menu, entry->key);
401 printf(ANSI_CURSOR_HIDE
403 ANSI_CURSOR_POSITION, 1, 1);
405 if (menu_get_choice(menu, &choice)) {
408 ret = entry->func(entry->data);
413 printf(ANSI_CLEAR_CONSOLE
415 ANSI_CURSOR_SHOW, 1, 1);
421 * eficonfig_volume_selected() - handler of volume selection
423 * @data: pointer to the data of selected entry
424 * Return: status code
426 static efi_status_t eficonfig_volume_selected(void *data)
428 struct eficonfig_volume_entry_data *info = data;
431 info->file_info->current_volume = info->v;
432 info->file_info->dp_volume = info->dp;
439 * eficonfig_create_device_path() - create device path
441 * @dp_volume: pointer to the volume
442 * @current_path: pointer to the file path u16 string
444 * device path or NULL. Caller must free the returned value
446 struct efi_device_path *eficonfig_create_device_path(struct efi_device_path *dp_volume,
452 struct efi_device_path *dp;
453 struct efi_device_path_file_path *fp;
455 fp_size = sizeof(struct efi_device_path) + u16_strsize(current_path);
456 buf = calloc(1, fp_size + sizeof(END));
461 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
462 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
463 fp->dp.length = (u16)fp_size;
464 u16_strcpy(fp->str, current_path);
468 *((struct efi_device_path *)p) = END;
470 dp = efi_dp_append(dp_volume, (struct efi_device_path *)buf);
477 * eficonfig_file_selected() - handler of file selection
479 * @data: pointer to the data of selected entry
480 * Return: status code
482 static efi_status_t eficonfig_file_selected(void *data)
485 struct eficonfig_file_entry_data *info = data;
488 return EFI_INVALID_PARAMETER;
490 if (!strcmp(info->file_name, "..\\")) {
491 struct eficonfig_filepath_info *iter;
492 struct list_head *pos, *n;
495 tmp = info->file_info->current_path;
497 memset(info->file_info->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
498 filepath = calloc(1, EFICONFIG_FILE_PATH_MAX);
500 return EFI_OUT_OF_RESOURCES;
502 list_for_each_safe(pos, n, &info->file_info->filepath_list) {
503 iter = list_entry(pos, struct eficonfig_filepath_info, list);
505 is_last = list_is_last(&iter->list, &info->file_info->filepath_list);
507 list_del(&iter->list);
512 strlcat(filepath, iter->name, EFICONFIG_FILE_PATH_MAX);
514 utf8_utf16_strcpy(&tmp, filepath);
517 struct eficonfig_filepath_info *filepath_info;
519 new_len = u16_strlen(info->file_info->current_path) +
520 strlen(info->file_name);
521 if (new_len >= EFICONFIG_FILE_PATH_MAX) {
522 eficonfig_print_msg("File path is too long!");
523 return EFI_INVALID_PARAMETER;
525 tmp = &info->file_info->current_path[u16_strlen(info->file_info->current_path)];
526 utf8_utf16_strcpy(&tmp, info->file_name);
528 filepath_info = calloc(1, sizeof(struct eficonfig_filepath_info));
530 return EFI_OUT_OF_RESOURCES;
532 filepath_info->name = strdup(info->file_name);
533 if (!filepath_info->name) {
535 return EFI_OUT_OF_RESOURCES;
537 list_add_tail(&filepath_info->list, &info->file_info->filepath_list);
539 if (!info->is_directory)
540 info->file_info->file_selected = true;
547 * eficonfig_select_volume() - construct the volume selection menu
549 * @file_info: pointer to the file selection structure
550 * Return: status code
552 static efi_status_t eficonfig_select_volume(struct eficonfig_select_file_info *file_info)
557 struct efimenu *efi_menu;
558 struct list_head *pos, *n;
559 struct efi_handler *handler;
560 struct eficonfig_entry *entry;
561 struct efi_device_path *device_path;
562 efi_handle_t *volume_handles = NULL;
563 struct efi_simple_file_system_protocol *v;
565 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
566 NULL, &count, (efi_handle_t **)&volume_handles);
567 if (ret != EFI_SUCCESS) {
568 eficonfig_print_msg("No block device found!");
572 efi_menu = calloc(1, sizeof(struct efimenu));
574 return EFI_OUT_OF_RESOURCES;
576 INIT_LIST_HEAD(&efi_menu->list);
577 for (i = 0; i < count; i++) {
579 struct efi_block_io *block_io;
580 struct eficonfig_volume_entry_data *info;
582 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
585 ret = efi_search_protocol(volume_handles[i],
586 &efi_simple_file_system_protocol_guid, &handler);
587 if (ret != EFI_SUCCESS)
589 ret = efi_protocol_open(handler, (void **)&v, efi_root, NULL,
590 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
591 if (ret != EFI_SUCCESS)
594 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
595 if (ret != EFI_SUCCESS)
597 ret = efi_protocol_open(handler, (void **)&device_path,
598 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
599 if (ret != EFI_SUCCESS)
602 ret = efi_search_protocol(volume_handles[i], &efi_block_io_guid, &handler);
603 if (ret != EFI_SUCCESS)
605 ret = efi_protocol_open(handler, (void **)&block_io,
606 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
607 if (ret != EFI_SUCCESS)
610 info = calloc(1, sizeof(struct eficonfig_volume_entry_data));
612 ret = EFI_OUT_OF_RESOURCES;
616 devname = calloc(1, BOOTMENU_DEVICE_NAME_MAX);
619 ret = EFI_OUT_OF_RESOURCES;
622 ret = efi_disk_get_device_name(volume_handles[i], devname,
623 BOOTMENU_DEVICE_NAME_MAX);
624 if (ret != EFI_SUCCESS) {
630 info->dp = device_path;
631 info->file_info = file_info;
632 ret = eficonfig_append_menu_entry(efi_menu, devname, eficonfig_volume_selected,
634 if (ret != EFI_SUCCESS) {
640 ret = eficonfig_append_quit_entry(efi_menu);
641 if (ret != EFI_SUCCESS)
644 ret = eficonfig_process_common(efi_menu, " ** Select Volume **");
646 efi_free_pool(volume_handles);
647 list_for_each_safe(pos, n, &efi_menu->list) {
648 entry = list_entry(pos, struct eficonfig_entry, list);
651 eficonfig_destroy(efi_menu);
657 * sort_file() - sort the file name in ascii order
659 * @data1: pointer to the file entry data
660 * @data2: pointer to the file entry data
661 * Return: -1 if the data1 file name is less than data2 file name,
662 * 0 if both file name match,
663 * 1 if the data1 file name is greater thant data2 file name.
665 static int sort_file(const void *arg1, const void *arg2)
667 const struct eficonfig_file_entry_data *data1, *data2;
669 data1 = *((const struct eficonfig_file_entry_data **)arg1);
670 data2 = *((const struct eficonfig_file_entry_data **)arg2);
672 return strcasecmp(data1->file_name, data2->file_name);
676 * eficonfig_create_file_entry() - construct the file menu entry
678 * @efi_menu: pointer to the efimenu structure
679 * @count: number of the directory and file
680 * @tmp_infos: pointer to the entry data array
681 * @f: pointer to the file handle
682 * @buf: pointer to the buffer to store the directory information
683 * @file_info: pointer to the file selection structure
684 * Return: status code
687 eficonfig_create_file_entry(struct efimenu *efi_menu, u32 count,
688 struct eficonfig_file_entry_data **tmp_infos,
689 struct efi_file_handle *f, struct efi_file_info *buf,
690 struct eficonfig_select_file_info *file_info)
695 u32 i, entry_num = 0;
696 struct eficonfig_file_entry_data *info;
698 EFI_CALL(f->setpos(f, 0));
699 /* Read directory and construct menu structure */
700 for (i = 0; i < count; i++) {
701 if (entry_num >= EFICONFIG_ENTRY_NUM_MAX - 1)
704 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
705 ret = EFI_CALL(f->read(f, &len, buf));
706 if (ret != EFI_SUCCESS || len == 0)
709 info = calloc(1, sizeof(struct eficonfig_file_entry_data));
711 ret = EFI_OUT_OF_RESOURCES;
715 /* append '\\' at the end of directory name */
716 name = calloc(1, utf16_utf8_strlen(buf->file_name) + 2);
718 ret = EFI_OUT_OF_RESOURCES;
723 utf16_utf8_strcpy(&p, buf->file_name);
724 if (buf->attribute & EFI_FILE_DIRECTORY) {
725 /* filter out u'.' */
726 if (!u16_strcmp(buf->file_name, u".")) {
731 name[u16_strlen(buf->file_name)] = '\\';
732 info->is_directory = true;
735 info->file_name = name;
736 info->file_info = file_info;
737 tmp_infos[entry_num++] = info;
740 qsort(tmp_infos, entry_num, sizeof(*tmp_infos),
741 (int (*)(const void *, const void *))sort_file);
743 for (i = 0; i < entry_num; i++) {
744 ret = eficonfig_append_menu_entry(efi_menu, tmp_infos[i]->file_name,
745 eficonfig_file_selected, tmp_infos[i]);
746 if (ret != EFI_SUCCESS)
755 * eficonfig_show_file_selection() - construct the file selection menu
757 * @file_info: pointer to the file selection structure
758 * @root: pointer to the file handle
759 * Return: status code
761 static efi_status_t eficonfig_show_file_selection(struct eficonfig_select_file_info *file_info,
762 struct efi_file_handle *root)
767 struct efimenu *efi_menu;
768 struct efi_file_handle *f;
769 struct efi_file_info *buf;
770 struct eficonfig_file_entry_data **tmp_infos;
772 buf = calloc(1, sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE);
774 return EFI_OUT_OF_RESOURCES;
776 while (!file_info->file_selected) {
777 efi_menu = calloc(1, sizeof(struct efimenu));
779 ret = EFI_OUT_OF_RESOURCES;
782 INIT_LIST_HEAD(&efi_menu->list);
784 ret = EFI_CALL(root->open(root, &f, file_info->current_path,
785 EFI_FILE_MODE_READ, 0));
786 if (ret != EFI_SUCCESS) {
787 eficonfig_print_msg("Reading volume failed!");
793 /* Count the number of directory entries */
795 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
796 ret = EFI_CALL(f->read(f, &len, buf));
797 if (ret != EFI_SUCCESS || len == 0)
803 /* allocate array to sort the entry */
804 tmp_infos = calloc(count, sizeof(*tmp_infos));
806 ret = EFI_OUT_OF_RESOURCES;
810 ret = eficonfig_create_file_entry(efi_menu, count, tmp_infos,
812 if (ret != EFI_SUCCESS)
815 ret = eficonfig_append_quit_entry(efi_menu);
816 if (ret != EFI_SUCCESS)
819 ret = eficonfig_process_common(efi_menu, " ** Select File **");
821 EFI_CALL(f->close(f));
822 eficonfig_destroy(efi_menu);
825 for (i = 0; i < count; i++)
831 if (ret != EFI_SUCCESS)
842 * handle_user_input() - handle user input
844 * @buf: pointer to the buffer
845 * @buf_size: size of the buffer
846 * @cursor_col: cursor column for user input
847 * @msg: pointer to the string to display
848 * Return: status code
850 static efi_status_t handle_user_input(u16 *buf, int buf_size,
851 int cursor_col, char *msg)
856 printf(ANSI_CLEAR_CONSOLE
860 " Press ENTER to complete, ESC/CTRL+C to quit",
863 /* tmp is used to accept user cancel */
864 tmp = calloc(1, buf_size * sizeof(u16));
866 return EFI_OUT_OF_RESOURCES;
868 ret = efi_console_get_u16_string(cin, tmp, buf_size, NULL, 4, cursor_col);
869 if (ret == EFI_SUCCESS)
870 u16_strcpy(buf, tmp);
874 /* to stay the parent menu */
875 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
881 * eficonfig_boot_add_enter_description() - handle user input for description
883 * @data: pointer to the internal boot option structure
884 * Return: status code
886 static efi_status_t eficonfig_boot_add_enter_description(void *data)
888 struct eficonfig_boot_option *bo = data;
890 return handle_user_input(bo->description, EFICONFIG_DESCRIPTION_MAX, 22,
891 "\n ** Edit Description **\n"
893 " enter description: ");
897 * eficonfig_boot_add_optional_data() - handle user input for optional data
899 * @data: pointer to the internal boot option structure
900 * Return: status code
902 static efi_status_t eficonfig_boot_add_optional_data(void *data)
904 struct eficonfig_boot_option *bo = data;
906 return handle_user_input(bo->optional_data, EFICONFIG_OPTIONAL_DATA_MAX, 24,
907 "\n ** Edit Optional Data **\n"
909 " enter optional data:");
913 * eficonfig_boot_edit_save() - handler to save the boot option
915 * @data: pointer to the internal boot option structure
916 * Return: status code
918 static efi_status_t eficonfig_boot_edit_save(void *data)
920 struct eficonfig_boot_option *bo = data;
922 if (u16_strlen(bo->description) == 0) {
923 eficonfig_print_msg("Boot Description is empty!");
924 bo->edit_completed = false;
925 return EFI_NOT_READY;
927 if (u16_strlen(bo->file_info.current_path) == 0) {
928 eficonfig_print_msg("File is not selected!");
929 bo->edit_completed = false;
930 return EFI_NOT_READY;
933 bo->edit_completed = true;
939 * eficonfig_process_clear_file_selection() - callback function for "Clear" entry
941 * @data: pointer to the data
942 * Return: status code
944 efi_status_t eficonfig_process_clear_file_selection(void *data)
946 struct eficonfig_select_file_info *file_info = data;
948 /* clear the existing file information */
949 file_info->current_volume = NULL;
950 file_info->current_path[0] = u'\0';
951 file_info->dp_volume = NULL;
956 static struct eficonfig_item select_file_menu_items[] = {
957 {"Select File", eficonfig_process_select_file},
958 {"Clear", eficonfig_process_clear_file_selection},
959 {"Quit", eficonfig_process_quit},
963 * eficonfig_process_show_file_option() - display select file option
965 * @file_info: pointer to the file information structure
966 * Return: status code
968 efi_status_t eficonfig_process_show_file_option(void *data)
971 struct efimenu *efi_menu;
973 select_file_menu_items[0].data = data;
974 select_file_menu_items[1].data = data;
975 efi_menu = eficonfig_create_fixed_menu(select_file_menu_items,
976 ARRAY_SIZE(select_file_menu_items));
978 return EFI_OUT_OF_RESOURCES;
980 ret = eficonfig_process_common(efi_menu, " ** Update File **");
981 if (ret != EFI_SUCCESS) /* User selects "Clear" or "Quit" */
984 eficonfig_destroy(efi_menu);
990 * eficonfig_process_select_file() - handle user file selection
992 * @data: pointer to the data
993 * Return: status code
995 efi_status_t eficonfig_process_select_file(void *data)
999 struct list_head *pos, *n;
1000 struct efi_file_handle *root;
1001 struct eficonfig_filepath_info *item;
1002 struct eficonfig_select_file_info *tmp = NULL;
1003 struct eficonfig_select_file_info *file_info = data;
1005 tmp = calloc(1, sizeof(struct eficonfig_select_file_info));
1007 return EFI_OUT_OF_RESOURCES;
1009 tmp->current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1010 if (!tmp->current_path) {
1012 return EFI_OUT_OF_RESOURCES;
1014 INIT_LIST_HEAD(&tmp->filepath_list);
1016 while (!tmp->file_selected) {
1017 tmp->current_volume = NULL;
1018 memset(tmp->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
1020 ret = eficonfig_select_volume(tmp);
1021 if (ret != EFI_SUCCESS)
1024 if (!tmp->current_volume)
1025 return EFI_INVALID_PARAMETER;
1027 ret = EFI_CALL(tmp->current_volume->open_volume(tmp->current_volume, &root));
1028 if (ret != EFI_SUCCESS)
1031 ret = eficonfig_show_file_selection(tmp, root);
1032 if (ret == EFI_ABORTED)
1034 if (ret != EFI_SUCCESS)
1039 if (ret == EFI_SUCCESS) {
1040 len = u16_strlen(tmp->current_path);
1041 len = (len >= EFICONFIG_FILE_PATH_MAX) ? (EFICONFIG_FILE_PATH_MAX - 1) : len;
1042 memcpy(file_info->current_path, tmp->current_path, len * sizeof(u16));
1043 file_info->current_path[len] = u'\0';
1044 file_info->current_volume = tmp->current_volume;
1045 file_info->dp_volume = tmp->dp_volume;
1048 list_for_each_safe(pos, n, &tmp->filepath_list) {
1049 item = list_entry(pos, struct eficonfig_filepath_info, list);
1050 list_del(&item->list);
1054 free(tmp->current_path);
1057 /* to stay the parent menu */
1058 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1064 * eficonfig_get_unused_bootoption() - get unused "Boot####" index
1066 * @buf: pointer to the buffer to store boot option variable name
1067 * @buf_size: buffer size
1068 * @index: pointer to store the index in the BootOrder variable
1069 * Return: status code
1071 efi_status_t eficonfig_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
1072 unsigned int *index)
1078 if (buf_size < u16_strsize(u"Boot####"))
1079 return EFI_BUFFER_TOO_SMALL;
1081 for (i = 0; i <= 0xFFFF; i++) {
1083 efi_create_indexed_name(buf, buf_size, "Boot", i);
1084 ret = efi_get_variable_int(buf, &efi_global_variable_guid,
1085 NULL, &size, NULL, NULL);
1086 if (ret == EFI_BUFFER_TOO_SMALL)
1093 return EFI_OUT_OF_RESOURCES;
1101 * eficonfig_set_boot_option() - set boot option
1103 * @varname: pointer to variable name
1104 * @dp: pointer to device path
1105 * @label: pointer to label string
1106 * @optional_data: pointer to optional data
1107 * Return: status code
1109 static efi_status_t eficonfig_set_boot_option(u16 *varname, struct efi_device_path *dp,
1110 efi_uintn_t dp_size, u16 *label, char *optional_data)
1115 struct efi_load_option lo;
1118 lo.file_path_length = dp_size;
1119 lo.attributes = LOAD_OPTION_ACTIVE;
1120 lo.optional_data = optional_data;
1123 size = efi_serialize_load_option(&lo, (u8 **)&p);
1125 return EFI_INVALID_PARAMETER;
1127 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1128 EFI_VARIABLE_NON_VOLATILE |
1129 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1130 EFI_VARIABLE_RUNTIME_ACCESS,
1138 * eficonfig_append_bootorder() - append new boot option in BootOrder variable
1140 * @index: "Boot####" index to append to BootOrder variable
1141 * Return: status code
1143 efi_status_t eficonfig_append_bootorder(u16 index)
1147 u16 *new_bootorder = NULL;
1148 efi_uintn_t last, size, new_size;
1150 /* append new boot option */
1151 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1152 last = size / sizeof(u16);
1153 new_size = size + sizeof(u16);
1154 new_bootorder = calloc(1, new_size);
1155 if (!new_bootorder) {
1156 ret = EFI_OUT_OF_RESOURCES;
1159 memcpy(new_bootorder, bootorder, size);
1160 new_bootorder[last] = index;
1162 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1163 EFI_VARIABLE_NON_VOLATILE |
1164 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1165 EFI_VARIABLE_RUNTIME_ACCESS,
1166 new_size, new_bootorder, false);
1167 if (ret != EFI_SUCCESS)
1172 free(new_bootorder);
1178 * create_boot_option_entry() - create boot option entry
1180 * @efi_menu: pointer to the efimenu structure
1181 * @title: pointer to the entry title
1182 * @val: pointer to boot option label
1183 * @func: callback of each entry
1184 * @data: pointer to the data to be passed to each entry callback
1185 * Return: status code
1187 static efi_status_t create_boot_option_entry(struct efimenu *efi_menu, char *title, u16 *val,
1188 eficonfig_entry_func func, void *data)
1193 len = strlen(title) + 1;
1195 len += utf16_utf8_strlen(val);
1196 buf = calloc(1, len);
1198 return EFI_OUT_OF_RESOURCES;
1202 p = buf + strlen(title);
1203 utf16_utf8_strcpy(&p, val);
1206 return eficonfig_append_menu_entry(efi_menu, buf, func, data);
1210 * prepare_file_selection_entry() - prepare file selection entry
1212 * @efi_menu: pointer to the efimenu structure
1213 * @title: pointer to the title string
1214 * @file_info: pointer to the file info
1215 * Return: status code
1217 static efi_status_t prepare_file_selection_entry(struct efimenu *efi_menu, char *title,
1218 struct eficonfig_select_file_info *file_info)
1222 u16 *file_name = NULL, *p;
1223 efi_handle_t handle;
1226 devname = calloc(1, EFICONFIG_VOLUME_PATH_MAX + 1);
1228 return EFI_OUT_OF_RESOURCES;
1230 /* get the device name only when the user already selected the file path */
1231 handle = efi_dp_find_obj(file_info->dp_volume, NULL, NULL);
1233 ret = efi_disk_get_device_name(handle, devname, EFICONFIG_VOLUME_PATH_MAX);
1234 if (ret != EFI_SUCCESS)
1239 * If the preconfigured volume does not exist in the system, display the text
1240 * converted volume device path instead of U-Boot friendly name(e.g. "usb 0:1").
1242 if (!handle && file_info->dp_volume) {
1246 dp_str = efi_dp_str(file_info->dp_volume);
1248 utf16_utf8_strncpy(&q, dp_str, EFICONFIG_VOLUME_PATH_MAX);
1250 efi_free_pool(dp_str);
1253 /* append u'/' to devname, it is just for display purpose. */
1254 if (file_info->current_path[0] != u'\0' && file_info->current_path[0] != u'/')
1255 strlcat(devname, "/", EFICONFIG_VOLUME_PATH_MAX + 1);
1257 len = strlen(devname);
1258 len += utf16_utf8_strlen(file_info->current_path) + 1;
1259 file_name = calloc(1, len * sizeof(u16));
1261 ret = EFI_OUT_OF_RESOURCES;
1266 utf8_utf16_strcpy(&p, devname);
1267 u16_strlcat(file_name, file_info->current_path, len);
1268 ret = create_boot_option_entry(efi_menu, title, file_name,
1269 eficonfig_process_show_file_option, file_info);
1278 * eficonfig_show_boot_option() - prepare menu entry for editing boot option
1280 * Construct the structures to create edit boot option menu
1282 * @bo: pointer to the boot option
1283 * @header_str: pointer to the header string
1284 * Return: status code
1286 static efi_status_t eficonfig_show_boot_option(struct eficonfig_boot_option *bo,
1290 struct efimenu *efi_menu;
1292 efi_menu = calloc(1, sizeof(struct efimenu));
1294 return EFI_OUT_OF_RESOURCES;
1296 INIT_LIST_HEAD(&efi_menu->list);
1298 ret = create_boot_option_entry(efi_menu, "Description: ", bo->description,
1299 eficonfig_boot_add_enter_description, bo);
1300 if (ret != EFI_SUCCESS)
1303 ret = prepare_file_selection_entry(efi_menu, "File: ", &bo->file_info);
1304 if (ret != EFI_SUCCESS)
1307 ret = prepare_file_selection_entry(efi_menu, "Initrd File: ", &bo->initrd_info);
1308 if (ret != EFI_SUCCESS)
1311 ret = create_boot_option_entry(efi_menu, "Optional Data: ", bo->optional_data,
1312 eficonfig_boot_add_optional_data, bo);
1313 if (ret != EFI_SUCCESS)
1316 ret = create_boot_option_entry(efi_menu, "Save", NULL,
1317 eficonfig_boot_edit_save, bo);
1318 if (ret != EFI_SUCCESS)
1321 ret = create_boot_option_entry(efi_menu, "Quit", NULL,
1322 eficonfig_process_quit, NULL);
1323 if (ret != EFI_SUCCESS)
1326 ret = eficonfig_process_common(efi_menu, header_str);
1328 eficonfig_destroy(efi_menu);
1334 * fill_file_info() - fill the file info from efi_device_path structure
1336 * @dp: pointer to the device path
1337 * @file_info: pointer to the file info structure
1338 * @device_dp: pointer to the volume device path
1340 static void fill_file_info(struct efi_device_path *dp,
1341 struct eficonfig_select_file_info *file_info,
1342 struct efi_device_path *device_dp)
1345 struct efi_device_path *file_dp = NULL;
1347 efi_dp_split_file_path(dp, &device_dp, &file_dp);
1348 file_info->dp_volume = device_dp;
1351 file_str = efi_dp_str(file_dp);
1353 * efi_convert_device_path_to_text() automatically adds u'/' at the
1354 * beginning of file name, remove u'/' before copying to current_path
1360 u16_strcpy(file_info->current_path, p);
1361 efi_free_pool(file_dp);
1362 efi_free_pool(file_str);
1367 * eficonfig_edit_boot_option() - prepare boot option structure for editing
1369 * Construct the boot option structure and copy the existing value
1371 * @varname: pointer to the UEFI variable name
1372 * @bo: pointer to the boot option
1373 * @load_option: pointer to the load option
1374 * @load_option_size: size of the load option
1375 * @header_str: pointer to the header string
1376 * Return : status code
1378 static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_boot_option *bo,
1379 void *load_option, efi_uintn_t load_option_size,
1384 char *tmp = NULL, *p;
1385 struct efi_load_option lo = {0};
1386 efi_uintn_t final_dp_size;
1387 struct efi_device_path *dp = NULL;
1388 efi_uintn_t size = load_option_size;
1389 struct efi_device_path *final_dp = NULL;
1390 struct efi_device_path *device_dp = NULL;
1391 struct efi_device_path *initrd_dp = NULL;
1392 struct efi_device_path *initrd_device_dp = NULL;
1394 const struct efi_initrd_dp id_dp = {
1397 DEVICE_PATH_TYPE_MEDIA_DEVICE,
1398 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
1399 sizeof(id_dp.vendor),
1401 EFI_INITRD_MEDIA_GUID,
1404 DEVICE_PATH_TYPE_END,
1405 DEVICE_PATH_SUB_TYPE_END,
1410 bo->file_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1411 if (!bo->file_info.current_path) {
1412 ret = EFI_OUT_OF_RESOURCES;
1416 bo->initrd_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1417 if (!bo->file_info.current_path) {
1418 ret = EFI_OUT_OF_RESOURCES;
1422 bo->description = calloc(1, EFICONFIG_DESCRIPTION_MAX * sizeof(u16));
1423 if (!bo->description) {
1424 ret = EFI_OUT_OF_RESOURCES;
1428 bo->optional_data = calloc(1, EFICONFIG_OPTIONAL_DATA_MAX * sizeof(u16));
1429 if (!bo->optional_data) {
1430 ret = EFI_OUT_OF_RESOURCES;
1434 /* copy the preset value */
1436 ret = efi_deserialize_load_option(&lo, load_option, &size);
1437 if (ret != EFI_SUCCESS)
1441 ret = EFI_INVALID_PARAMETER;
1444 /* truncate the long label string */
1445 if (u16_strlen(lo.label) >= EFICONFIG_DESCRIPTION_MAX)
1446 lo.label[EFICONFIG_DESCRIPTION_MAX - 1] = u'\0';
1448 u16_strcpy(bo->description, lo.label);
1450 /* EFI image file path is a first instance */
1452 fill_file_info(lo.file_path, &bo->file_info, device_dp);
1454 /* Initrd file path(optional) is placed at second instance. */
1455 initrd_dp = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
1457 fill_file_info(initrd_dp, &bo->initrd_info, initrd_device_dp);
1458 efi_free_pool(initrd_dp);
1462 memcpy(bo->optional_data, lo.optional_data, size);
1466 ret = eficonfig_show_boot_option(bo, header_str);
1467 if (ret == EFI_SUCCESS && bo->edit_completed)
1469 if (ret == EFI_NOT_READY)
1471 if (ret != EFI_SUCCESS)
1475 if (bo->initrd_info.dp_volume) {
1476 dp = eficonfig_create_device_path(bo->initrd_info.dp_volume,
1477 bo->initrd_info.current_path);
1479 ret = EFI_OUT_OF_RESOURCES;
1482 initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp, dp);
1486 dp = eficonfig_create_device_path(bo->file_info.dp_volume, bo->file_info.current_path);
1488 ret = EFI_OUT_OF_RESOURCES;
1491 final_dp_size = efi_dp_size(dp) + sizeof(END);
1493 final_dp = efi_dp_concat(dp, initrd_dp);
1494 final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
1496 final_dp = efi_dp_dup(dp);
1503 if (utf16_utf8_strlen(bo->optional_data)) {
1504 len = utf16_utf8_strlen(bo->optional_data) + 1;
1505 tmp = calloc(1, len);
1509 utf16_utf8_strncpy(&p, bo->optional_data, u16_strlen(bo->optional_data));
1512 ret = eficonfig_set_boot_option(varname, final_dp, final_dp_size, bo->description, tmp);
1515 free(bo->optional_data);
1516 free(bo->description);
1517 free(bo->file_info.current_path);
1518 free(bo->initrd_info.current_path);
1519 efi_free_pool(device_dp);
1520 efi_free_pool(initrd_device_dp);
1521 efi_free_pool(initrd_dp);
1522 efi_free_pool(final_dp);
1528 * eficonfig_process_add_boot_option() - handler to add boot option
1530 * @data: pointer to the data for each entry
1531 * Return: status code
1533 static efi_status_t eficonfig_process_add_boot_option(void *data)
1537 struct eficonfig_boot_option *bo = NULL;
1539 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1541 return EFI_OUT_OF_RESOURCES;
1543 ret = eficonfig_get_unused_bootoption(varname, sizeof(varname), &bo->boot_index);
1544 if (ret != EFI_SUCCESS)
1547 ret = eficonfig_edit_boot_option(varname, bo, NULL, 0, " ** Add Boot Option ** ");
1548 if (ret != EFI_SUCCESS)
1551 ret = eficonfig_append_bootorder((u16)bo->boot_index);
1552 if (ret != EFI_SUCCESS)
1558 /* to stay the parent menu */
1559 ret = (ret == EFI_ABORTED) ? EFI_SUCCESS : ret;
1565 * eficonfig_process_boot_selected() - handler to select boot option entry
1567 * @data: pointer to the data for each entry
1568 * Return: status code
1570 static efi_status_t eficonfig_process_boot_selected(void *data)
1572 struct eficonfig_boot_selection_data *info = data;
1575 *info->selected = info->boot_index;
1581 * search_bootorder() - search the boot option index in BootOrder
1583 * @bootorder: pointer to the BootOrder variable
1584 * @num: number of BootOrder entry
1585 * @target: target boot option index to search
1586 * @index: pointer to store the index of BootOrder variable
1587 * Return: true if exists, false otherwise
1589 static bool search_bootorder(u16 *bootorder, efi_uintn_t num, u32 target, u32 *index)
1593 for (i = 0; i < num; i++) {
1594 if (target == bootorder[i]) {
1606 * eficonfig_add_boot_selection_entry() - add boot option menu entry
1608 * @efi_menu: pointer to store the efimenu structure
1609 * @boot_index: boot option index to be added
1610 * @selected: pointer to store the selected boot option index
1611 * Return: status code
1613 static efi_status_t eficonfig_add_boot_selection_entry(struct efimenu *efi_menu,
1614 unsigned int boot_index,
1615 unsigned int *selected)
1621 struct efi_load_option lo;
1622 u16 varname[] = u"Boot####";
1623 struct eficonfig_boot_selection_data *info;
1625 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
1626 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1630 ret = efi_deserialize_load_option(&lo, load_option, &size);
1631 if (ret != EFI_SUCCESS) {
1632 log_warning("Invalid load option for %ls\n", varname);
1637 if (size >= sizeof(efi_guid_t) &&
1638 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
1640 * auto generated entry has GUID in optional_data,
1641 * skip auto generated entry because it will be generated
1642 * again even if it is edited or deleted.
1648 info = calloc(1, sizeof(struct eficonfig_boot_selection_data));
1651 return EFI_OUT_OF_RESOURCES;
1654 buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
1658 return EFI_OUT_OF_RESOURCES;
1661 utf16_utf8_strcpy(&p, lo.label);
1662 info->boot_index = boot_index;
1663 info->selected = selected;
1664 ret = eficonfig_append_menu_entry(efi_menu, buf, eficonfig_process_boot_selected, info);
1665 if (ret != EFI_SUCCESS) {
1676 * eficonfig_show_boot_selection() - construct boot option menu entry
1678 * @selected: pointer to store the selected boot option index
1679 * Return: status code
1681 static efi_status_t eficonfig_show_boot_selection(unsigned int *selected)
1686 u16 *var_name16 = NULL, *p;
1687 efi_uintn_t num, size, buf_size;
1688 struct efimenu *efi_menu;
1689 struct list_head *pos, *n;
1690 struct eficonfig_entry *entry;
1692 efi_menu = calloc(1, sizeof(struct efimenu));
1694 return EFI_OUT_OF_RESOURCES;
1696 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1698 INIT_LIST_HEAD(&efi_menu->list);
1699 num = size / sizeof(u16);
1700 /* list the load option in the order of BootOrder variable */
1701 for (i = 0; i < num; i++) {
1702 ret = eficonfig_add_boot_selection_entry(efi_menu, bootorder[i], selected);
1703 if (ret != EFI_SUCCESS)
1706 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1710 /* list the remaining load option not included in the BootOrder */
1712 var_name16 = malloc(buf_size);
1714 return EFI_OUT_OF_RESOURCES;
1722 ret = efi_get_next_variable_name_int(&size, var_name16, &guid);
1723 if (ret == EFI_NOT_FOUND)
1725 if (ret == EFI_BUFFER_TOO_SMALL) {
1727 p = realloc(var_name16, buf_size);
1730 return EFI_OUT_OF_RESOURCES;
1733 ret = efi_get_next_variable_name_int(&size, var_name16, &guid);
1735 if (ret != EFI_SUCCESS) {
1739 if (efi_varname_is_load_option(var_name16, &index)) {
1740 /* If the index is included in the BootOrder, skip it */
1741 if (search_bootorder(bootorder, num, index, NULL))
1744 ret = eficonfig_add_boot_selection_entry(efi_menu, index, selected);
1745 if (ret != EFI_SUCCESS)
1749 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1753 ret = eficonfig_append_quit_entry(efi_menu);
1754 if (ret != EFI_SUCCESS)
1757 ret = eficonfig_process_common(efi_menu, " ** Select Boot Option **");
1759 list_for_each_safe(pos, n, &efi_menu->list) {
1760 entry = list_entry(pos, struct eficonfig_entry, list);
1763 eficonfig_destroy(efi_menu);
1771 * eficonfig_process_edit_boot_option() - handler to edit boot option
1773 * @data: pointer to the data for each entry
1774 * Return: status code
1776 static efi_status_t eficonfig_process_edit_boot_option(void *data)
1780 struct eficonfig_boot_option *bo = NULL;
1783 unsigned int selected;
1785 u16 varname[] = u"Boot####";
1787 ret = eficonfig_show_boot_selection(&selected);
1788 if (ret != EFI_SUCCESS)
1791 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1793 ret = EFI_OUT_OF_RESOURCES;
1797 bo->boot_index = selected;
1798 efi_create_indexed_name(varname, sizeof(varname), "Boot", selected);
1799 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1802 ret = EFI_NOT_FOUND;
1806 ret = eficonfig_edit_boot_option(varname, bo, load_option, size,
1807 " ** Edit Boot Option ** ");
1811 if (ret != EFI_SUCCESS && ret != EFI_ABORTED)
1815 /* to stay the parent menu */
1816 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1822 * eficonfig_display_change_boot_order() - display the BootOrder list
1824 * @efi_menu: pointer to the efimenu structure
1825 * Return: status code
1827 static void eficonfig_display_change_boot_order(struct efimenu *efi_menu)
1830 struct list_head *pos, *n;
1831 struct eficonfig_entry *entry;
1833 printf(ANSI_CLEAR_CONSOLE ANSI_CURSOR_POSITION
1834 "\n ** Change Boot Order **\n"
1835 ANSI_CURSOR_POSITION
1836 " Press UP/DOWN to move, +/- to change order"
1837 ANSI_CURSOR_POSITION
1838 " Press SPACE to activate or deactivate the entry"
1839 ANSI_CURSOR_POSITION
1840 " Select [Save] to complete, ESC/CTRL+C to quit"
1841 ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
1842 1, 1, efi_menu->count + 5, 1, efi_menu->count + 6, 1,
1843 efi_menu->count + 7, 1, efi_menu->count + 8, 1);
1845 /* draw boot option list */
1846 list_for_each_safe(pos, n, &efi_menu->list) {
1847 entry = list_entry(pos, struct eficonfig_entry, list);
1848 reverse = (entry->num == efi_menu->active);
1850 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
1853 puts(ANSI_COLOR_REVERSE);
1855 if (entry->num < efi_menu->count - 2) {
1856 if (((struct eficonfig_boot_order_data *)entry->data)->active)
1862 printf("%s", entry->title);
1865 puts(ANSI_COLOR_RESET);
1870 * eficonfig_choice_change_boot_order() - handle the BootOrder update
1872 * @efi_menu: pointer to the efimenu structure
1873 * Return: status code
1875 static efi_status_t eficonfig_choice_change_boot_order(struct efimenu *efi_menu)
1878 struct list_head *pos, *n;
1879 enum bootmenu_key key = KEY_NONE;
1880 struct eficonfig_entry *entry, *tmp;
1883 bootmenu_loop(NULL, &key, &esc);
1887 if (efi_menu->active > 0) {
1888 list_for_each_safe(pos, n, &efi_menu->list) {
1889 entry = list_entry(pos, struct eficonfig_entry, list);
1890 if (entry->num == efi_menu->active)
1893 tmp = list_entry(pos->prev, struct eficonfig_entry, list);
1896 list_del(&tmp->list);
1897 list_add(&tmp->list, &entry->list);
1901 if (efi_menu->active > 0)
1903 return EFI_NOT_READY;
1905 if (efi_menu->active < efi_menu->count - 3) {
1906 list_for_each_safe(pos, n, &efi_menu->list) {
1907 entry = list_entry(pos, struct eficonfig_entry, list);
1908 if (entry->num == efi_menu->active)
1911 tmp = list_entry(pos->next, struct eficonfig_entry, list);
1914 list_del(&entry->list);
1915 list_add(&entry->list, &tmp->list);
1919 return EFI_NOT_READY;
1921 if (efi_menu->active < efi_menu->count - 1)
1923 return EFI_NOT_READY;
1926 if (efi_menu->active == efi_menu->count - 2)
1930 if (efi_menu->active == efi_menu->count - 1)
1935 if (efi_menu->active < efi_menu->count - 2) {
1936 list_for_each_safe(pos, n, &efi_menu->list) {
1937 entry = list_entry(pos, struct eficonfig_entry, list);
1938 if (entry->num == efi_menu->active) {
1939 struct eficonfig_boot_order_data *data = entry->data;
1941 data->active = !data->active;
1942 return EFI_NOT_READY;
1950 /* Pressed key is not valid, no need to regenerate the menu */
1957 * eficonfig_add_change_boot_order_entry() - add boot order entry
1959 * @efi_menu: pointer to the efimenu structure
1960 * @boot_index: boot option index to be added
1961 * @active: flag to include the boot option into BootOrder
1962 * Return: status code
1964 static efi_status_t eficonfig_add_change_boot_order_entry(struct efimenu *efi_menu,
1965 u32 boot_index, bool active)
1971 struct efi_load_option lo;
1972 u16 varname[] = u"Boot####";
1973 struct eficonfig_boot_order_data *data;
1975 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
1976 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1980 ret = efi_deserialize_load_option(&lo, load_option, &size);
1981 if (ret != EFI_SUCCESS)
1984 data = calloc(1, sizeof(*data));
1986 ret = EFI_OUT_OF_RESOURCES;
1990 title = calloc(1, utf16_utf8_strlen(lo.label) + 1);
1993 ret = EFI_OUT_OF_RESOURCES;
1997 utf16_utf8_strcpy(&p, lo.label);
1999 data->boot_index = boot_index;
2000 data->active = active;
2002 ret = eficonfig_append_menu_entry(efi_menu, title, NULL, data);
2003 if (ret != EFI_SUCCESS) {
2016 * eficonfig_create_change_boot_order_entry() - create boot order entry
2018 * @efi_menu: pointer to the efimenu structure
2019 * @bootorder: pointer to the BootOrder variable
2020 * @num: number of BootOrder entry
2021 * Return: status code
2023 static efi_status_t eficonfig_create_change_boot_order_entry(struct efimenu *efi_menu,
2024 u16 *bootorder, efi_uintn_t num)
2029 u16 *var_name16 = NULL, *p;
2030 efi_uintn_t size, buf_size;
2032 /* list the load option in the order of BootOrder variable */
2033 for (i = 0; i < num; i++) {
2034 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2037 ret = eficonfig_add_change_boot_order_entry(efi_menu, bootorder[i], true);
2038 if (ret != EFI_SUCCESS)
2042 /* list the remaining load option not included in the BootOrder */
2044 var_name16 = malloc(buf_size);
2046 return EFI_OUT_OF_RESOURCES;
2053 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2057 ret = efi_get_next_variable_name_int(&size, var_name16, &guid);
2058 if (ret == EFI_NOT_FOUND)
2060 if (ret == EFI_BUFFER_TOO_SMALL) {
2062 p = realloc(var_name16, buf_size);
2064 ret = EFI_OUT_OF_RESOURCES;
2068 ret = efi_get_next_variable_name_int(&size, var_name16, &guid);
2070 if (ret != EFI_SUCCESS)
2073 if (efi_varname_is_load_option(var_name16, &index)) {
2074 /* If the index is included in the BootOrder, skip it */
2075 if (search_bootorder(bootorder, num, index, NULL))
2078 ret = eficonfig_add_change_boot_order_entry(efi_menu, index, false);
2079 if (ret != EFI_SUCCESS)
2084 /* add "Save" and "Quit" entries */
2085 title = strdup("Save");
2087 ret = EFI_OUT_OF_RESOURCES;
2091 ret = eficonfig_append_menu_entry(efi_menu, title, NULL, NULL);
2092 if (ret != EFI_SUCCESS)
2095 ret = eficonfig_append_quit_entry(efi_menu);
2096 if (ret != EFI_SUCCESS)
2099 efi_menu->active = 0;
2107 * eficonfig_process_change_boot_order() - handler to change boot order
2109 * @data: pointer to the data for each entry
2110 * Return: status code
2112 static efi_status_t eficonfig_process_change_boot_order(void *data)
2117 efi_uintn_t num, size;
2118 struct list_head *pos, *n;
2119 struct eficonfig_entry *entry;
2120 struct efimenu *efi_menu;
2122 efi_menu = calloc(1, sizeof(struct efimenu));
2124 return EFI_OUT_OF_RESOURCES;
2126 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2128 INIT_LIST_HEAD(&efi_menu->list);
2129 num = size / sizeof(u16);
2130 ret = eficonfig_create_change_boot_order_entry(efi_menu, bootorder, num);
2131 if (ret != EFI_SUCCESS)
2135 eficonfig_display_change_boot_order(efi_menu);
2137 ret = eficonfig_choice_change_boot_order(efi_menu);
2138 if (ret == EFI_SUCCESS) {
2141 new_bootorder = calloc(1, (efi_menu->count - 2) * sizeof(u16));
2142 if (!new_bootorder) {
2143 ret = EFI_OUT_OF_RESOURCES;
2147 /* create new BootOrder */
2149 list_for_each_safe(pos, n, &efi_menu->list) {
2150 struct eficonfig_boot_order_data *data;
2152 entry = list_entry(pos, struct eficonfig_entry, list);
2153 /* exit the loop when iteration reaches "Save" */
2154 if (!strncmp(entry->title, "Save", strlen("Save")))
2159 new_bootorder[count++] = data->boot_index;
2162 size = count * sizeof(u16);
2163 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
2164 EFI_VARIABLE_NON_VOLATILE |
2165 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2166 EFI_VARIABLE_RUNTIME_ACCESS,
2167 size, new_bootorder, false);
2169 free(new_bootorder);
2171 } else if (ret == EFI_NOT_READY) {
2179 list_for_each_safe(pos, n, &efi_menu->list) {
2180 entry = list_entry(pos, struct eficonfig_entry, list);
2183 eficonfig_destroy(efi_menu);
2185 /* to stay the parent menu */
2186 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2192 * delete_boot_option() - delete selected boot option
2194 * @boot_index: boot option index to delete
2195 * Return: status code
2197 static efi_status_t delete_boot_option(u16 boot_index)
2203 efi_uintn_t num, size;
2205 efi_create_indexed_name(varname, sizeof(varname),
2206 "Boot", boot_index);
2207 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
2209 if (ret != EFI_SUCCESS) {
2210 log_err("delete boot option(%ls) failed\n", varname);
2214 /* update BootOrder if necessary */
2215 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2219 num = size / sizeof(u16);
2220 if (!search_bootorder(bootorder, num, boot_index, &index))
2223 memmove(&bootorder[index], &bootorder[index + 1],
2224 (num - index - 1) * sizeof(u16));
2225 size -= sizeof(u16);
2226 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
2227 EFI_VARIABLE_NON_VOLATILE |
2228 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2229 EFI_VARIABLE_RUNTIME_ACCESS,
2230 size, bootorder, false);
2236 * eficonfig_process_delete_boot_option() - handler to delete boot option
2238 * @data: pointer to the data for each entry
2239 * Return: status code
2241 static efi_status_t eficonfig_process_delete_boot_option(void *data)
2244 unsigned int selected;
2247 ret = eficonfig_show_boot_selection(&selected);
2248 if (ret == EFI_SUCCESS)
2249 ret = delete_boot_option(selected);
2251 if (ret != EFI_SUCCESS)
2255 /* to stay the parent menu */
2256 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2262 * eficonfig_enumerate_boot_option() - enumerate the possible bootable media
2264 * @opt: pointer to the media boot option structure
2265 * @volume_handles: pointer to the efi handles
2266 * @count: number of efi handle
2267 * Return: status code
2269 efi_status_t eficonfig_enumerate_boot_option(struct eficonfig_media_boot_option *opt,
2270 efi_handle_t *volume_handles, efi_status_t count)
2273 struct efi_handler *handler;
2274 efi_status_t ret = EFI_SUCCESS;
2276 for (i = 0; i < count; i++) {
2278 u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
2279 char *optional_data;
2280 struct efi_load_option lo;
2281 char buf[BOOTMENU_DEVICE_NAME_MAX];
2282 struct efi_device_path *device_path;
2284 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
2285 if (ret != EFI_SUCCESS)
2287 ret = efi_protocol_open(handler, (void **)&device_path,
2288 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2289 if (ret != EFI_SUCCESS)
2292 ret = efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
2293 if (ret != EFI_SUCCESS)
2297 utf8_utf16_strncpy(&p, buf, strlen(buf));
2299 lo.label = dev_name;
2300 lo.attributes = LOAD_OPTION_ACTIVE;
2301 lo.file_path = device_path;
2302 lo.file_path_length = efi_dp_size(device_path) + sizeof(END);
2304 * Set the dedicated guid to optional_data, it is used to identify
2305 * the boot option that automatically generated by the bootmenu.
2306 * efi_serialize_load_option() expects optional_data is null-terminated
2307 * utf8 string, so set the "1234567" string to allocate enough space
2308 * to store guid, instead of realloc the load_option.
2310 lo.optional_data = "1234567";
2311 opt[i].size = efi_serialize_load_option(&lo, (u8 **)&opt[i].lo);
2313 ret = EFI_OUT_OF_RESOURCES;
2317 optional_data = (char *)opt[i].lo + (opt[i].size - u16_strsize(u"1234567"));
2318 memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
2326 * eficonfig_delete_invalid_boot_option() - delete non-existing boot option
2328 * @opt: pointer to the media boot option structure
2329 * @count: number of media boot option structure
2330 * Return: status code
2332 efi_status_t eficonfig_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
2338 struct efi_load_option lo;
2339 u16 *var_name16 = NULL, *p;
2340 u16 varname[] = u"Boot####";
2341 efi_status_t ret = EFI_SUCCESS;
2342 efi_uintn_t varname_size, buf_size;
2345 var_name16 = malloc(buf_size);
2347 return EFI_OUT_OF_RESOURCES;
2355 varname_size = buf_size;
2356 ret = efi_get_next_variable_name_int(&varname_size, var_name16, &guid);
2357 if (ret == EFI_NOT_FOUND)
2359 if (ret == EFI_BUFFER_TOO_SMALL) {
2360 buf_size = varname_size;
2361 p = realloc(var_name16, buf_size);
2364 return EFI_OUT_OF_RESOURCES;
2367 ret = efi_get_next_variable_name_int(&varname_size, var_name16, &guid);
2369 if (ret != EFI_SUCCESS) {
2373 if (!efi_varname_is_load_option(var_name16, &index))
2376 efi_create_indexed_name(varname, sizeof(varname), "Boot", index);
2377 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
2382 ret = efi_deserialize_load_option(&lo, load_option, &size);
2383 if (ret != EFI_SUCCESS)
2386 if (size >= sizeof(efi_guid_bootmenu_auto_generated)) {
2387 if (guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated) == 0) {
2388 for (i = 0; i < count; i++) {
2389 if (opt[i].size == tmp &&
2390 memcmp(opt[i].lo, load_option, tmp) == 0) {
2391 opt[i].exist = true;
2397 ret = delete_boot_option(i);
2398 if (ret != EFI_SUCCESS) {
2414 * eficonfig_generate_media_device_boot_option() - generate the media device boot option
2416 * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
2417 * and generate the bootmenu entries.
2418 * This function also provide the BOOT#### variable maintenance for
2419 * the media device entries.
2420 * - Automatically create the BOOT#### variable for the newly detected device,
2421 * this BOOT#### variable is distinguished by the special GUID
2422 * stored in the EFI_LOAD_OPTION.optional_data
2423 * - If the device is not attached to the system, the associated BOOT#### variable
2424 * is automatically deleted.
2426 * Return: status code
2428 efi_status_t eficonfig_generate_media_device_boot_option(void)
2433 efi_handle_t *volume_handles = NULL;
2434 struct eficonfig_media_boot_option *opt = NULL;
2436 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
2437 NULL, &count, (efi_handle_t **)&volume_handles);
2438 if (ret != EFI_SUCCESS)
2441 opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
2445 /* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */
2446 ret = eficonfig_enumerate_boot_option(opt, volume_handles, count);
2447 if (ret != EFI_SUCCESS)
2451 * System hardware configuration may vary depending on the user setup.
2452 * The boot option is automatically added by the bootmenu.
2453 * If the device is not attached to the system, the boot option needs
2456 ret = eficonfig_delete_invalid_boot_option(opt, count);
2457 if (ret != EFI_SUCCESS)
2460 /* add non-existent boot option */
2461 for (i = 0; i < count; i++) {
2465 if (!opt[i].exist) {
2466 ret = eficonfig_get_unused_bootoption(var_name, sizeof(var_name),
2468 if (ret != EFI_SUCCESS)
2471 ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
2472 EFI_VARIABLE_NON_VOLATILE |
2473 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2474 EFI_VARIABLE_RUNTIME_ACCESS,
2475 opt[i].size, opt[i].lo, false);
2476 if (ret != EFI_SUCCESS)
2479 ret = eficonfig_append_bootorder(boot_index);
2480 if (ret != EFI_SUCCESS) {
2481 efi_set_variable_int(var_name, &efi_global_variable_guid,
2490 for (i = 0; i < count; i++)
2494 efi_free_pool(volume_handles);
2500 * eficonfig_init() - do required initialization for eficonfig command
2502 * Return: status code
2504 static efi_status_t eficonfig_init(void)
2506 efi_status_t ret = EFI_SUCCESS;
2508 struct efi_handler *handler;
2511 ret = efi_search_protocol(efi_root, &efi_guid_text_input_protocol, &handler);
2512 if (ret != EFI_SUCCESS)
2515 ret = efi_protocol_open(handler, (void **)&cin, efi_root, NULL,
2516 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2517 if (ret != EFI_SUCCESS)
2526 static const struct eficonfig_item maintenance_menu_items[] = {
2527 {"Add Boot Option", eficonfig_process_add_boot_option},
2528 {"Edit Boot Option", eficonfig_process_edit_boot_option},
2529 {"Change Boot Order", eficonfig_process_change_boot_order},
2530 {"Delete Boot Option", eficonfig_process_delete_boot_option},
2531 #if (CONFIG_IS_ENABLED(EFI_SECURE_BOOT) && CONFIG_IS_ENABLED(EFI_MM_COMM_TEE))
2532 {"Secure Boot Configuration", eficonfig_process_secure_boot_config},
2534 {"Quit", eficonfig_process_quit},
2538 * do_eficonfig() - execute `eficonfig` command
2540 * @cmdtp: table entry describing command
2541 * @flag: bitmap indicating how the command was invoked
2542 * @argc: number of arguments
2543 * @argv: command line arguments
2544 * Return: status code
2546 static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
2549 struct efimenu *efi_menu;
2552 return CMD_RET_USAGE;
2554 ret = efi_init_obj_list();
2555 if (ret != EFI_SUCCESS) {
2556 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
2557 ret & ~EFI_ERROR_MASK);
2559 return CMD_RET_FAILURE;
2562 ret = eficonfig_init();
2563 if (ret != EFI_SUCCESS)
2564 return CMD_RET_FAILURE;
2566 ret = eficonfig_generate_media_device_boot_option();
2567 if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
2571 efi_menu = eficonfig_create_fixed_menu(maintenance_menu_items,
2572 ARRAY_SIZE(maintenance_menu_items));
2574 return CMD_RET_FAILURE;
2576 ret = eficonfig_process_common(efi_menu, " ** UEFI Maintenance Menu **");
2577 eficonfig_destroy(efi_menu);
2579 if (ret == EFI_ABORTED)
2583 return CMD_RET_SUCCESS;
2587 eficonfig, 1, 0, do_eficonfig,
2588 "provide menu-driven UEFI variable maintenance interface",