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;
24 const char *eficonfig_menu_desc =
25 " Press UP/DOWN to move, ENTER to select, ESC to quit";
27 static const char *eficonfig_change_boot_order_desc =
28 " Press UP/DOWN to move, +/- to change orde\n"
29 " Press SPACE to activate or deactivate the entry\n"
30 " CTRL+S to save, ESC to quit";
32 static struct efi_simple_text_output_protocol *cout;
35 #define EFICONFIG_DESCRIPTION_MAX 32
36 #define EFICONFIG_OPTIONAL_DATA_MAX 64
37 #define EFICONFIG_MENU_HEADER_ROW_NUM 3
38 #define EFICONFIG_MENU_DESC_ROW_NUM 5
41 * struct eficonfig_filepath_info - structure to be used to store file path
43 * @name: file or directory name
44 * @list: list structure
46 struct eficonfig_filepath_info {
48 struct list_head list;
52 * struct eficonfig_boot_option - structure to be used for updating UEFI boot option
54 * @file_info: user selected file info
55 * @initrd_info: user selected initrd file info
56 * @boot_index: index of the boot option
57 * @description: pointer to the description string
58 * @optional_data: pointer to the optional_data
59 * @edit_completed: flag indicates edit complete
61 struct eficonfig_boot_option {
62 struct eficonfig_select_file_info file_info;
63 struct eficonfig_select_file_info initrd_info;
64 unsigned int boot_index;
71 * struct eficonfig_volume_entry_data - structure to be used to store volume info
73 * @file_info: pointer to file info structure
74 * @v: pointer to the protocol interface
75 * @dp: pointer to the device path
77 struct eficonfig_volume_entry_data {
78 struct eficonfig_select_file_info *file_info;
79 struct efi_simple_file_system_protocol *v;
80 struct efi_device_path *dp;
84 * struct eficonfig_file_entry_data - structure to be used to store file info
86 * @file_info: pointer to file info structure
87 * @is_directory: flag to identify the directory or file
88 * @file_name: name of directory or file
90 struct eficonfig_file_entry_data {
91 struct eficonfig_select_file_info *file_info;
97 * struct eficonfig_boot_selection_data - structure to be used to select the boot option entry
99 * @boot_index: index of the boot option
100 * @selected: pointer to store the selected index in the BootOrder variable
102 struct eficonfig_boot_selection_data {
108 * struct eficonfig_boot_order_data - structure to be used to update BootOrder variable
110 * @boot_index: boot option index
111 * @active: flag to include the boot option into BootOrder variable
113 struct eficonfig_boot_order_data {
119 * struct eficonfig_save_boot_order_data - structure to be used to change boot order
121 * @efi_menu: pointer to efimenu structure
122 * @selected: flag to indicate user selects "Save" entry
124 struct eficonfig_save_boot_order_data {
125 struct efimenu *efi_menu;
130 * struct eficonfig_menu_adjust - update start and end entry index
132 * @efi_menu: pointer to efimenu structure
133 * @add: flag to add or substract the index
135 static void eficonfig_menu_adjust(struct efimenu *efi_menu, bool add)
142 if (add && efi_menu->end < efi_menu->active) {
145 } else if (!add && efi_menu->start > efi_menu->active) {
150 #define eficonfig_menu_up(_a) eficonfig_menu_adjust(_a, false)
151 #define eficonfig_menu_down(_a) eficonfig_menu_adjust(_a, true)
154 * eficonfig_print_msg() - print message
156 * display the message to the user, user proceeds the screen
157 * with any key press.
159 * @items: pointer to the structure of each menu entry
160 * @count: the number of menu entry
161 * @menu_header: pointer to the menu header string
162 * Return: status code
164 void eficonfig_print_msg(char *msg)
170 printf(ANSI_CURSOR_HIDE
173 "%s\n\n Press any key to continue", 3, 4, msg);
179 * eficonfig_print_entry() - print each menu entry
181 * @data: pointer to the data associated with each menu entry
183 void eficonfig_print_entry(void *data)
185 struct eficonfig_entry *entry = data;
186 bool reverse = (entry->efi_menu->active == entry->num);
188 if (entry->efi_menu->start > entry->num || entry->efi_menu->end < entry->num)
191 printf(ANSI_CURSOR_POSITION, (entry->num - entry->efi_menu->start) +
192 EFICONFIG_MENU_HEADER_ROW_NUM + 1, 7);
195 puts(ANSI_COLOR_REVERSE);
197 printf(ANSI_CLEAR_LINE "%s", entry->title);
200 puts(ANSI_COLOR_RESET);
204 * eficonfig_display_statusline() - print status line
206 * @m: pointer to the menu structure
208 void eficonfig_display_statusline(struct menu *m)
210 struct eficonfig_entry *entry;
212 if (menu_default_choice(m, (void *)&entry) < 0)
215 printf(ANSI_CURSOR_POSITION
217 ANSI_CURSOR_POSITION ANSI_CLEAR_LINE ANSI_CURSOR_POSITION
219 ANSI_CLEAR_LINE_TO_END,
220 1, 1, entry->efi_menu->menu_header, avail_row + 4, 1,
221 avail_row + 5, 1, entry->efi_menu->menu_desc);
225 * eficonfig_choice_entry() - user key input handler
227 * @data: pointer to the efimenu structure
228 * Return: key string to identify the selected entry
230 char *eficonfig_choice_entry(void *data)
232 struct cli_ch_state s_cch, *cch = &s_cch;
233 struct list_head *pos, *n;
234 struct eficonfig_entry *entry;
235 enum bootmenu_key key = BKEY_NONE;
236 struct efimenu *efi_menu = data;
241 key = bootmenu_loop((struct bootmenu_data *)efi_menu, cch);
245 if (efi_menu->active > 0)
246 eficonfig_menu_up(efi_menu);
248 /* no menu key selected, regenerate menu */
251 if (efi_menu->active < efi_menu->count - 1)
252 eficonfig_menu_down(efi_menu);
254 /* no menu key selected, regenerate menu */
257 list_for_each_safe(pos, n, &efi_menu->list) {
258 entry = list_entry(pos, struct eficonfig_entry, list);
259 if (entry->num == efi_menu->active)
264 /* Quit by choosing the last entry */
265 entry = list_last_entry(&efi_menu->list, struct eficonfig_entry, list);
268 /* Pressed key is not valid, no need to regenerate the menu */
275 * eficonfig_destroy() - destroy efimenu
277 * @efi_menu: pointer to the efimenu structure
279 void eficonfig_destroy(struct efimenu *efi_menu)
281 struct list_head *pos, *n;
282 struct eficonfig_entry *entry;
287 list_for_each_safe(pos, n, &efi_menu->list) {
288 entry = list_entry(pos, struct eficonfig_entry, list);
290 list_del(&entry->list);
293 free(efi_menu->menu_header);
298 * eficonfig_process_quit() - callback function for "Quit" entry
300 * @data: pointer to the data
301 * Return: status code
303 efi_status_t eficonfig_process_quit(void *data)
309 * eficonfig_append_menu_entry() - append menu item
311 * @efi_menu: pointer to the efimenu structure
312 * @title: pointer to the entry title
313 * @func: callback of each entry
314 * @data: pointer to the data to be passed to each entry callback
315 * Return: status code
317 efi_status_t eficonfig_append_menu_entry(struct efimenu *efi_menu,
318 char *title, eficonfig_entry_func func,
321 struct eficonfig_entry *entry;
323 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX)
324 return EFI_OUT_OF_RESOURCES;
326 entry = calloc(1, sizeof(struct eficonfig_entry));
328 return EFI_OUT_OF_RESOURCES;
330 entry->title = title;
331 sprintf(entry->key, "%d", efi_menu->count);
332 entry->efi_menu = efi_menu;
335 entry->num = efi_menu->count++;
336 list_add_tail(&entry->list, &efi_menu->list);
342 * eficonfig_append_quit_entry() - append quit entry
344 * @efi_menu: pointer to the efimenu structure
345 * Return: status code
347 efi_status_t eficonfig_append_quit_entry(struct efimenu *efi_menu)
352 title = strdup("Quit");
354 return EFI_OUT_OF_RESOURCES;
356 ret = eficonfig_append_menu_entry(efi_menu, title, eficonfig_process_quit, NULL);
357 if (ret != EFI_SUCCESS)
364 * eficonfig_create_fixed_menu() - create fixed entry menu structure
366 * @items: pointer to the menu entry item
367 * @count: the number of menu entry
368 * Return: pointer to the efimenu structure
370 void *eficonfig_create_fixed_menu(const struct eficonfig_item *items, int count)
375 struct efimenu *efi_menu;
376 const struct eficonfig_item *iter = items;
378 efi_menu = calloc(1, sizeof(struct efimenu));
382 INIT_LIST_HEAD(&efi_menu->list);
383 for (i = 0; i < count; i++, iter++) {
384 title = strdup(iter->title);
388 ret = eficonfig_append_menu_entry(efi_menu, title, iter->func, iter->data);
389 if (ret != EFI_SUCCESS) {
397 eficonfig_destroy(efi_menu);
403 * eficonfig_process_common() - main handler for UEFI menu
405 * Construct the structures required to show the menu, then handle
406 * the user input interacting with u-boot menu functions.
408 * @efi_menu: pointer to the efimenu structure
409 * @menu_header: pointer to the menu header string
410 * @menu_desc: pointer to the menu description
411 * @display_statusline: function pointer to draw statusline
412 * @item_data_print: function pointer to draw the menu item
413 * @item_choice: function pointer to handle the key press
414 * Return: status code
416 efi_status_t eficonfig_process_common(struct efimenu *efi_menu,
417 char *menu_header, const char *menu_desc,
418 void (*display_statusline)(struct menu *),
419 void (*item_data_print)(void *),
420 char *(*item_choice)(void *))
424 struct list_head *pos, *n;
425 struct eficonfig_entry *entry;
426 efi_status_t ret = EFI_SUCCESS;
428 if (efi_menu->count > EFICONFIG_ENTRY_NUM_MAX)
429 return EFI_OUT_OF_RESOURCES;
431 efi_menu->delay = -1;
432 efi_menu->active = 0;
434 efi_menu->end = avail_row - 1;
437 efi_menu->menu_header = strdup(menu_header);
438 if (!efi_menu->menu_header)
439 return EFI_OUT_OF_RESOURCES;
442 efi_menu->menu_desc = menu_desc;
444 menu = menu_create(NULL, 0, 1, display_statusline, item_data_print,
445 item_choice, efi_menu);
447 return EFI_INVALID_PARAMETER;
449 list_for_each_safe(pos, n, &efi_menu->list) {
450 entry = list_entry(pos, struct eficonfig_entry, list);
451 if (!menu_item_add(menu, entry->key, entry)) {
452 ret = EFI_INVALID_PARAMETER;
457 entry = list_first_entry_or_null(&efi_menu->list, struct eficonfig_entry, list);
459 menu_default_set(menu, entry->key);
461 printf(ANSI_CURSOR_HIDE
463 ANSI_CURSOR_POSITION, 1, 1);
465 if (menu_get_choice(menu, &choice)) {
468 ret = entry->func(entry->data);
473 printf(ANSI_CLEAR_CONSOLE
475 ANSI_CURSOR_SHOW, 1, 1);
481 * eficonfig_volume_selected() - handler of volume selection
483 * @data: pointer to the data of selected entry
484 * Return: status code
486 static efi_status_t eficonfig_volume_selected(void *data)
488 struct eficonfig_volume_entry_data *info = data;
491 info->file_info->current_volume = info->v;
492 info->file_info->dp_volume = info->dp;
499 * eficonfig_create_device_path() - create device path
501 * @dp_volume: pointer to the volume
502 * @current_path: pointer to the file path u16 string
504 * device path or NULL. Caller must free the returned value
506 struct efi_device_path *eficonfig_create_device_path(struct efi_device_path *dp_volume,
512 struct efi_device_path *dp;
513 struct efi_device_path_file_path *fp;
515 fp_size = sizeof(struct efi_device_path) + u16_strsize(current_path);
516 buf = calloc(1, fp_size + sizeof(END));
521 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
522 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
523 fp->dp.length = (u16)fp_size;
524 u16_strcpy(fp->str, current_path);
528 *((struct efi_device_path *)p) = END;
530 dp = efi_dp_shorten(dp_volume);
533 dp = efi_dp_concat(dp, &fp->dp, false);
540 * eficonfig_file_selected() - handler of file selection
542 * @data: pointer to the data of selected entry
543 * Return: status code
545 static efi_status_t eficonfig_file_selected(void *data)
548 struct eficonfig_file_entry_data *info = data;
551 return EFI_INVALID_PARAMETER;
553 if (!strcmp(info->file_name, "..\\")) {
554 struct eficonfig_filepath_info *iter;
555 struct list_head *pos, *n;
558 tmp = info->file_info->current_path;
560 memset(info->file_info->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
561 filepath = calloc(1, EFICONFIG_FILE_PATH_MAX);
563 return EFI_OUT_OF_RESOURCES;
565 list_for_each_safe(pos, n, &info->file_info->filepath_list) {
566 iter = list_entry(pos, struct eficonfig_filepath_info, list);
568 is_last = list_is_last(&iter->list, &info->file_info->filepath_list);
570 list_del(&iter->list);
575 strlcat(filepath, iter->name, EFICONFIG_FILE_PATH_MAX);
577 utf8_utf16_strcpy(&tmp, filepath);
580 struct eficonfig_filepath_info *filepath_info;
582 new_len = u16_strlen(info->file_info->current_path) +
583 strlen(info->file_name);
584 if (new_len >= EFICONFIG_FILE_PATH_MAX) {
585 eficonfig_print_msg("File path is too long!");
586 return EFI_INVALID_PARAMETER;
588 tmp = &info->file_info->current_path[u16_strlen(info->file_info->current_path)];
589 utf8_utf16_strcpy(&tmp, info->file_name);
591 filepath_info = calloc(1, sizeof(struct eficonfig_filepath_info));
593 return EFI_OUT_OF_RESOURCES;
595 filepath_info->name = strdup(info->file_name);
596 if (!filepath_info->name) {
598 return EFI_OUT_OF_RESOURCES;
600 list_add_tail(&filepath_info->list, &info->file_info->filepath_list);
602 if (!info->is_directory)
603 info->file_info->file_selected = true;
610 * eficonfig_select_volume() - construct the volume selection menu
612 * @file_info: pointer to the file selection structure
613 * Return: status code
615 static efi_status_t eficonfig_select_volume(struct eficonfig_select_file_info *file_info)
620 struct efimenu *efi_menu;
621 struct list_head *pos, *n;
622 struct efi_handler *handler;
623 struct eficonfig_entry *entry;
624 struct efi_device_path *device_path;
625 efi_handle_t *volume_handles = NULL;
626 struct efi_simple_file_system_protocol *v;
628 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
629 NULL, &count, (efi_handle_t **)&volume_handles);
630 if (ret != EFI_SUCCESS) {
631 eficonfig_print_msg("No block device found!");
635 efi_menu = calloc(1, sizeof(struct efimenu));
637 return EFI_OUT_OF_RESOURCES;
639 INIT_LIST_HEAD(&efi_menu->list);
640 for (i = 0; i < count; i++) {
642 struct efi_block_io *block_io;
643 struct eficonfig_volume_entry_data *info;
645 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
648 ret = efi_search_protocol(volume_handles[i],
649 &efi_simple_file_system_protocol_guid, &handler);
650 if (ret != EFI_SUCCESS)
652 ret = efi_protocol_open(handler, (void **)&v, efi_root, NULL,
653 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
654 if (ret != EFI_SUCCESS)
657 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
658 if (ret != EFI_SUCCESS)
660 ret = efi_protocol_open(handler, (void **)&device_path,
661 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
662 if (ret != EFI_SUCCESS)
665 ret = efi_search_protocol(volume_handles[i], &efi_block_io_guid, &handler);
666 if (ret != EFI_SUCCESS)
668 ret = efi_protocol_open(handler, (void **)&block_io,
669 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
670 if (ret != EFI_SUCCESS)
673 info = calloc(1, sizeof(struct eficonfig_volume_entry_data));
675 ret = EFI_OUT_OF_RESOURCES;
679 devname = calloc(1, BOOTMENU_DEVICE_NAME_MAX);
682 ret = EFI_OUT_OF_RESOURCES;
685 ret = efi_disk_get_device_name(volume_handles[i], devname,
686 BOOTMENU_DEVICE_NAME_MAX);
687 if (ret != EFI_SUCCESS) {
693 info->dp = device_path;
694 info->file_info = file_info;
695 ret = eficonfig_append_menu_entry(efi_menu, devname, eficonfig_volume_selected,
697 if (ret != EFI_SUCCESS) {
703 ret = eficonfig_append_quit_entry(efi_menu);
704 if (ret != EFI_SUCCESS)
707 ret = eficonfig_process_common(efi_menu, " ** Select Volume **",
709 eficonfig_display_statusline,
710 eficonfig_print_entry,
711 eficonfig_choice_entry);
714 efi_free_pool(volume_handles);
715 list_for_each_safe(pos, n, &efi_menu->list) {
716 entry = list_entry(pos, struct eficonfig_entry, list);
719 eficonfig_destroy(efi_menu);
725 * sort_file() - sort the file name in ascii order
727 * @data1: pointer to the file entry data
728 * @data2: pointer to the file entry data
729 * Return: -1 if the data1 file name is less than data2 file name,
730 * 0 if both file name match,
731 * 1 if the data1 file name is greater thant data2 file name.
733 static int sort_file(const void *arg1, const void *arg2)
735 const struct eficonfig_file_entry_data *data1, *data2;
737 data1 = *((const struct eficonfig_file_entry_data **)arg1);
738 data2 = *((const struct eficonfig_file_entry_data **)arg2);
740 return strcasecmp(data1->file_name, data2->file_name);
744 * eficonfig_create_file_entry() - construct the file menu entry
746 * @efi_menu: pointer to the efimenu structure
747 * @count: number of the directory and file
748 * @tmp_infos: pointer to the entry data array
749 * @f: pointer to the file handle
750 * @buf: pointer to the buffer to store the directory information
751 * @file_info: pointer to the file selection structure
752 * Return: status code
755 eficonfig_create_file_entry(struct efimenu *efi_menu, u32 count,
756 struct eficonfig_file_entry_data **tmp_infos,
757 struct efi_file_handle *f, struct efi_file_info *buf,
758 struct eficonfig_select_file_info *file_info)
763 u32 i, entry_num = 0;
764 struct eficonfig_file_entry_data *info;
766 EFI_CALL(f->setpos(f, 0));
767 /* Read directory and construct menu structure */
768 for (i = 0; i < count; i++) {
769 if (entry_num >= EFICONFIG_ENTRY_NUM_MAX - 1)
772 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
773 ret = EFI_CALL(f->read(f, &len, buf));
774 if (ret != EFI_SUCCESS || len == 0)
777 info = calloc(1, sizeof(struct eficonfig_file_entry_data));
779 ret = EFI_OUT_OF_RESOURCES;
783 /* append '\\' at the end of directory name */
784 name = calloc(1, utf16_utf8_strlen(buf->file_name) + 2);
786 ret = EFI_OUT_OF_RESOURCES;
791 utf16_utf8_strcpy(&p, buf->file_name);
792 if (buf->attribute & EFI_FILE_DIRECTORY) {
793 /* filter out u'.' */
794 if (!u16_strcmp(buf->file_name, u".")) {
799 name[u16_strlen(buf->file_name)] = '\\';
800 info->is_directory = true;
803 info->file_name = name;
804 info->file_info = file_info;
805 tmp_infos[entry_num++] = info;
808 qsort(tmp_infos, entry_num, sizeof(*tmp_infos),
809 (int (*)(const void *, const void *))sort_file);
811 for (i = 0; i < entry_num; i++) {
812 ret = eficonfig_append_menu_entry(efi_menu, tmp_infos[i]->file_name,
813 eficonfig_file_selected, tmp_infos[i]);
814 if (ret != EFI_SUCCESS)
823 * eficonfig_show_file_selection() - construct the file selection menu
825 * @file_info: pointer to the file selection structure
826 * @root: pointer to the file handle
827 * Return: status code
829 static efi_status_t eficonfig_show_file_selection(struct eficonfig_select_file_info *file_info,
830 struct efi_file_handle *root)
835 struct efimenu *efi_menu;
836 struct efi_file_handle *f;
837 struct efi_file_info *buf;
838 struct eficonfig_file_entry_data **tmp_infos;
840 buf = calloc(1, sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE);
842 return EFI_OUT_OF_RESOURCES;
844 while (!file_info->file_selected) {
845 efi_menu = calloc(1, sizeof(struct efimenu));
847 ret = EFI_OUT_OF_RESOURCES;
850 INIT_LIST_HEAD(&efi_menu->list);
852 ret = EFI_CALL(root->open(root, &f, file_info->current_path,
853 EFI_FILE_MODE_READ, 0));
854 if (ret != EFI_SUCCESS) {
855 eficonfig_print_msg("Reading volume failed!");
861 /* Count the number of directory entries */
863 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
864 ret = EFI_CALL(f->read(f, &len, buf));
865 if (ret != EFI_SUCCESS || len == 0)
871 /* allocate array to sort the entry */
872 tmp_infos = calloc(count, sizeof(*tmp_infos));
874 ret = EFI_OUT_OF_RESOURCES;
878 ret = eficonfig_create_file_entry(efi_menu, count, tmp_infos,
880 if (ret != EFI_SUCCESS)
883 ret = eficonfig_append_quit_entry(efi_menu);
884 if (ret != EFI_SUCCESS)
887 ret = eficonfig_process_common(efi_menu, " ** Select File **",
889 eficonfig_display_statusline,
890 eficonfig_print_entry,
891 eficonfig_choice_entry);
893 EFI_CALL(f->close(f));
894 eficonfig_destroy(efi_menu);
897 for (i = 0; i < count; i++)
903 if (ret != EFI_SUCCESS)
914 * handle_user_input() - handle user input
916 * @buf: pointer to the buffer
917 * @buf_size: size of the buffer
918 * @cursor_col: cursor column for user input
919 * @msg: pointer to the string to display
920 * Return: status code
922 static efi_status_t handle_user_input(u16 *buf, int buf_size,
923 int cursor_col, char *msg)
928 printf(ANSI_CLEAR_CONSOLE
932 " Press ENTER to complete, ESC to quit",
935 /* tmp is used to accept user cancel */
936 tmp = calloc(1, buf_size * sizeof(u16));
938 return EFI_OUT_OF_RESOURCES;
940 ret = efi_console_get_u16_string(cin, tmp, buf_size, NULL, 4, cursor_col);
941 if (ret == EFI_SUCCESS)
942 u16_strcpy(buf, tmp);
946 /* to stay the parent menu */
947 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
953 * eficonfig_boot_add_enter_description() - handle user input for description
955 * @data: pointer to the internal boot option structure
956 * Return: status code
958 static efi_status_t eficonfig_boot_add_enter_description(void *data)
960 struct eficonfig_boot_option *bo = data;
962 return handle_user_input(bo->description, EFICONFIG_DESCRIPTION_MAX, 22,
963 "\n ** Edit Description **\n"
965 " enter description: ");
969 * eficonfig_boot_add_optional_data() - handle user input for optional data
971 * @data: pointer to the internal boot option structure
972 * Return: status code
974 static efi_status_t eficonfig_boot_add_optional_data(void *data)
976 struct eficonfig_boot_option *bo = data;
978 return handle_user_input(bo->optional_data, EFICONFIG_OPTIONAL_DATA_MAX, 24,
979 "\n ** Edit Optional Data **\n"
981 " enter optional data:");
985 * eficonfig_boot_edit_save() - handler to save the boot option
987 * @data: pointer to the internal boot option structure
988 * Return: status code
990 static efi_status_t eficonfig_boot_edit_save(void *data)
992 struct eficonfig_boot_option *bo = data;
994 if (u16_strlen(bo->description) == 0) {
995 eficonfig_print_msg("Boot Description is empty!");
996 bo->edit_completed = false;
997 return EFI_NOT_READY;
999 if (u16_strlen(bo->file_info.current_path) == 0) {
1000 eficonfig_print_msg("File is not selected!");
1001 bo->edit_completed = false;
1002 return EFI_NOT_READY;
1005 bo->edit_completed = true;
1011 * eficonfig_process_clear_file_selection() - callback function for "Clear" entry
1013 * @data: pointer to the data
1014 * Return: status code
1016 efi_status_t eficonfig_process_clear_file_selection(void *data)
1018 struct eficonfig_select_file_info *file_info = data;
1020 /* clear the existing file information */
1021 file_info->current_volume = NULL;
1022 file_info->current_path[0] = u'\0';
1023 file_info->dp_volume = NULL;
1028 static struct eficonfig_item select_file_menu_items[] = {
1029 {"Select File", eficonfig_process_select_file},
1030 {"Clear", eficonfig_process_clear_file_selection},
1031 {"Quit", eficonfig_process_quit},
1035 * eficonfig_process_show_file_option() - display select file option
1037 * @file_info: pointer to the file information structure
1038 * Return: status code
1040 efi_status_t eficonfig_process_show_file_option(void *data)
1043 struct efimenu *efi_menu;
1045 select_file_menu_items[0].data = data;
1046 select_file_menu_items[1].data = data;
1047 efi_menu = eficonfig_create_fixed_menu(select_file_menu_items,
1048 ARRAY_SIZE(select_file_menu_items));
1050 return EFI_OUT_OF_RESOURCES;
1052 ret = eficonfig_process_common(efi_menu, " ** Update File **",
1053 eficonfig_menu_desc,
1054 eficonfig_display_statusline,
1055 eficonfig_print_entry,
1056 eficonfig_choice_entry);
1057 if (ret != EFI_SUCCESS) /* User selects "Clear" or "Quit" */
1058 ret = EFI_NOT_READY;
1060 eficonfig_destroy(efi_menu);
1066 * eficonfig_process_select_file() - handle user file selection
1068 * @data: pointer to the data
1069 * Return: status code
1071 efi_status_t eficonfig_process_select_file(void *data)
1075 struct list_head *pos, *n;
1076 struct efi_file_handle *root;
1077 struct eficonfig_filepath_info *item;
1078 struct eficonfig_select_file_info *tmp = NULL;
1079 struct eficonfig_select_file_info *file_info = data;
1081 tmp = calloc(1, sizeof(struct eficonfig_select_file_info));
1083 return EFI_OUT_OF_RESOURCES;
1085 tmp->current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1086 if (!tmp->current_path) {
1088 return EFI_OUT_OF_RESOURCES;
1090 INIT_LIST_HEAD(&tmp->filepath_list);
1092 while (!tmp->file_selected) {
1093 tmp->current_volume = NULL;
1094 memset(tmp->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
1096 ret = eficonfig_select_volume(tmp);
1097 if (ret != EFI_SUCCESS)
1100 if (!tmp->current_volume)
1101 return EFI_INVALID_PARAMETER;
1103 ret = EFI_CALL(tmp->current_volume->open_volume(tmp->current_volume, &root));
1104 if (ret != EFI_SUCCESS)
1107 ret = eficonfig_show_file_selection(tmp, root);
1108 if (ret == EFI_ABORTED)
1110 if (ret != EFI_SUCCESS)
1115 if (ret == EFI_SUCCESS) {
1116 len = u16_strlen(tmp->current_path);
1117 len = (len >= EFICONFIG_FILE_PATH_MAX) ? (EFICONFIG_FILE_PATH_MAX - 1) : len;
1118 memcpy(file_info->current_path, tmp->current_path, len * sizeof(u16));
1119 file_info->current_path[len] = u'\0';
1120 file_info->current_volume = tmp->current_volume;
1121 file_info->dp_volume = tmp->dp_volume;
1124 list_for_each_safe(pos, n, &tmp->filepath_list) {
1125 item = list_entry(pos, struct eficonfig_filepath_info, list);
1126 list_del(&item->list);
1130 free(tmp->current_path);
1133 /* to stay the parent menu */
1134 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1140 * eficonfig_set_boot_option() - set boot option
1142 * @varname: pointer to variable name
1143 * @dp: pointer to device path
1144 * @label: pointer to label string
1145 * @optional_data: pointer to optional data
1146 * Return: status code
1148 static efi_status_t eficonfig_set_boot_option(u16 *varname, struct efi_device_path *dp,
1149 efi_uintn_t dp_size, u16 *label, char *optional_data)
1154 struct efi_load_option lo;
1157 lo.file_path_length = dp_size;
1158 lo.attributes = LOAD_OPTION_ACTIVE;
1159 lo.optional_data = optional_data;
1162 size = efi_serialize_load_option(&lo, (u8 **)&p);
1164 return EFI_INVALID_PARAMETER;
1166 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1167 EFI_VARIABLE_NON_VOLATILE |
1168 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1169 EFI_VARIABLE_RUNTIME_ACCESS,
1177 * create_boot_option_entry() - create boot option entry
1179 * @efi_menu: pointer to the efimenu structure
1180 * @title: pointer to the entry title
1181 * @val: pointer to boot option label
1182 * @func: callback of each entry
1183 * @data: pointer to the data to be passed to each entry callback
1184 * Return: status code
1186 static efi_status_t create_boot_option_entry(struct efimenu *efi_menu, char *title, u16 *val,
1187 eficonfig_entry_func func, void *data)
1192 len = strlen(title) + 1;
1194 len += utf16_utf8_strlen(val);
1195 buf = calloc(1, len);
1197 return EFI_OUT_OF_RESOURCES;
1201 p = buf + strlen(title);
1202 utf16_utf8_strcpy(&p, val);
1205 return eficonfig_append_menu_entry(efi_menu, buf, func, data);
1209 * prepare_file_selection_entry() - prepare file selection entry
1211 * @efi_menu: pointer to the efimenu structure
1212 * @title: pointer to the title string
1213 * @file_info: pointer to the file info
1214 * Return: status code
1216 static efi_status_t prepare_file_selection_entry(struct efimenu *efi_menu, char *title,
1217 struct eficonfig_select_file_info *file_info)
1221 u16 *file_name = NULL, *p;
1222 efi_handle_t handle;
1225 devname = calloc(1, EFICONFIG_VOLUME_PATH_MAX + 1);
1227 return EFI_OUT_OF_RESOURCES;
1229 /* get the device name only when the user already selected the file path */
1230 handle = efi_dp_find_obj(file_info->dp_volume, NULL, NULL);
1232 ret = efi_disk_get_device_name(handle, devname, EFICONFIG_VOLUME_PATH_MAX);
1233 if (ret != EFI_SUCCESS)
1238 * If the preconfigured volume does not exist in the system, display the text
1239 * converted volume device path instead of U-Boot friendly name(e.g. "usb 0:1").
1241 if (!handle && file_info->dp_volume) {
1245 dp_str = efi_dp_str(file_info->dp_volume);
1247 utf16_utf8_strncpy(&q, dp_str, EFICONFIG_VOLUME_PATH_MAX);
1249 efi_free_pool(dp_str);
1252 /* append u'/' to devname, it is just for display purpose. */
1253 if (file_info->current_path[0] != u'\0' && file_info->current_path[0] != u'/')
1254 strlcat(devname, "/", EFICONFIG_VOLUME_PATH_MAX + 1);
1256 len = strlen(devname);
1257 len += utf16_utf8_strlen(file_info->current_path) + 1;
1258 file_name = calloc(1, len * sizeof(u16));
1260 ret = EFI_OUT_OF_RESOURCES;
1265 utf8_utf16_strcpy(&p, devname);
1266 u16_strlcat(file_name, file_info->current_path, len);
1267 ret = create_boot_option_entry(efi_menu, title, file_name,
1268 eficonfig_process_show_file_option, file_info);
1277 * eficonfig_show_boot_option() - prepare menu entry for editing boot option
1279 * Construct the structures to create edit boot option menu
1281 * @bo: pointer to the boot option
1282 * @header_str: pointer to the header string
1283 * Return: status code
1285 static efi_status_t eficonfig_show_boot_option(struct eficonfig_boot_option *bo,
1289 struct efimenu *efi_menu;
1291 efi_menu = calloc(1, sizeof(struct efimenu));
1293 return EFI_OUT_OF_RESOURCES;
1295 INIT_LIST_HEAD(&efi_menu->list);
1297 ret = create_boot_option_entry(efi_menu, "Description: ", bo->description,
1298 eficonfig_boot_add_enter_description, bo);
1299 if (ret != EFI_SUCCESS)
1302 ret = prepare_file_selection_entry(efi_menu, "File: ", &bo->file_info);
1303 if (ret != EFI_SUCCESS)
1306 ret = prepare_file_selection_entry(efi_menu, "Initrd File: ", &bo->initrd_info);
1307 if (ret != EFI_SUCCESS)
1310 ret = create_boot_option_entry(efi_menu, "Optional Data: ", bo->optional_data,
1311 eficonfig_boot_add_optional_data, bo);
1312 if (ret != EFI_SUCCESS)
1315 ret = create_boot_option_entry(efi_menu, "Save", NULL,
1316 eficonfig_boot_edit_save, bo);
1317 if (ret != EFI_SUCCESS)
1320 ret = create_boot_option_entry(efi_menu, "Quit", NULL,
1321 eficonfig_process_quit, NULL);
1322 if (ret != EFI_SUCCESS)
1325 ret = eficonfig_process_common(efi_menu, header_str,
1326 eficonfig_menu_desc,
1327 eficonfig_display_statusline,
1328 eficonfig_print_entry,
1329 eficonfig_choice_entry);
1332 eficonfig_destroy(efi_menu);
1338 * fill_file_info() - fill the file info from efi_device_path structure
1340 * @dp: pointer to the device path
1341 * @file_info: pointer to the file info structure
1342 * @device_dp: pointer to the volume device path
1344 static void fill_file_info(struct efi_device_path *dp,
1345 struct eficonfig_select_file_info *file_info,
1346 struct efi_device_path *device_dp)
1349 struct efi_device_path *file_dp = NULL;
1351 efi_dp_split_file_path(dp, &device_dp, &file_dp);
1352 file_info->dp_volume = device_dp;
1355 file_str = efi_dp_str(file_dp);
1357 * efi_convert_device_path_to_text() automatically adds u'/' at the
1358 * beginning of file name, remove u'/' before copying to current_path
1364 u16_strcpy(file_info->current_path, p);
1365 efi_free_pool(file_dp);
1366 efi_free_pool(file_str);
1371 * eficonfig_edit_boot_option() - prepare boot option structure for editing
1373 * Construct the boot option structure and copy the existing value
1375 * @varname: pointer to the UEFI variable name
1376 * @bo: pointer to the boot option
1377 * @load_option: pointer to the load option
1378 * @load_option_size: size of the load option
1379 * @header_str: pointer to the header string
1380 * Return : status code
1382 static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_boot_option *bo,
1383 void *load_option, efi_uintn_t load_option_size,
1388 char *tmp = NULL, *p;
1389 struct efi_load_option lo = {0};
1390 efi_uintn_t final_dp_size;
1391 struct efi_device_path *dp = NULL;
1392 efi_uintn_t size = load_option_size;
1393 struct efi_device_path *final_dp = NULL;
1394 struct efi_device_path *device_dp = NULL;
1395 struct efi_device_path *initrd_dp = NULL;
1396 struct efi_device_path *initrd_device_dp = NULL;
1398 const struct efi_initrd_dp id_dp = {
1401 DEVICE_PATH_TYPE_MEDIA_DEVICE,
1402 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
1403 sizeof(id_dp.vendor),
1405 EFI_INITRD_MEDIA_GUID,
1408 DEVICE_PATH_TYPE_END,
1409 DEVICE_PATH_SUB_TYPE_END,
1414 bo->file_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1415 if (!bo->file_info.current_path) {
1416 ret = EFI_OUT_OF_RESOURCES;
1420 bo->initrd_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1421 if (!bo->initrd_info.current_path) {
1422 ret = EFI_OUT_OF_RESOURCES;
1426 bo->description = calloc(1, EFICONFIG_DESCRIPTION_MAX * sizeof(u16));
1427 if (!bo->description) {
1428 ret = EFI_OUT_OF_RESOURCES;
1432 bo->optional_data = calloc(1, EFICONFIG_OPTIONAL_DATA_MAX * sizeof(u16));
1433 if (!bo->optional_data) {
1434 ret = EFI_OUT_OF_RESOURCES;
1438 /* copy the preset value */
1440 ret = efi_deserialize_load_option(&lo, load_option, &size);
1441 if (ret != EFI_SUCCESS)
1445 ret = EFI_INVALID_PARAMETER;
1448 /* truncate the long label string */
1449 if (u16_strlen(lo.label) >= EFICONFIG_DESCRIPTION_MAX)
1450 lo.label[EFICONFIG_DESCRIPTION_MAX - 1] = u'\0';
1452 u16_strcpy(bo->description, lo.label);
1454 /* EFI image file path is a first instance */
1456 fill_file_info(lo.file_path, &bo->file_info, device_dp);
1458 /* Initrd file path(optional) is placed at second instance. */
1459 initrd_dp = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
1461 fill_file_info(initrd_dp, &bo->initrd_info, initrd_device_dp);
1462 efi_free_pool(initrd_dp);
1466 memcpy(bo->optional_data, lo.optional_data, size);
1470 ret = eficonfig_show_boot_option(bo, header_str);
1471 if (ret == EFI_SUCCESS && bo->edit_completed)
1473 if (ret == EFI_NOT_READY)
1475 if (ret != EFI_SUCCESS)
1479 if (bo->initrd_info.dp_volume) {
1480 dp = eficonfig_create_device_path(bo->initrd_info.dp_volume,
1481 bo->initrd_info.current_path);
1483 ret = EFI_OUT_OF_RESOURCES;
1486 initrd_dp = efi_dp_concat((const struct efi_device_path *)&id_dp,
1491 dp = eficonfig_create_device_path(bo->file_info.dp_volume, bo->file_info.current_path);
1493 ret = EFI_OUT_OF_RESOURCES;
1496 final_dp_size = efi_dp_size(dp) + sizeof(END);
1498 final_dp = efi_dp_concat(dp, initrd_dp, true);
1499 final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
1501 final_dp = efi_dp_dup(dp);
1508 if (utf16_utf8_strlen(bo->optional_data)) {
1509 len = utf16_utf8_strlen(bo->optional_data) + 1;
1510 tmp = calloc(1, len);
1514 utf16_utf8_strncpy(&p, bo->optional_data, u16_strlen(bo->optional_data));
1517 ret = eficonfig_set_boot_option(varname, final_dp, final_dp_size, bo->description, tmp);
1520 free(bo->optional_data);
1521 free(bo->description);
1522 free(bo->file_info.current_path);
1523 free(bo->initrd_info.current_path);
1524 efi_free_pool(device_dp);
1525 efi_free_pool(initrd_device_dp);
1526 efi_free_pool(initrd_dp);
1527 efi_free_pool(final_dp);
1533 * eficonfig_process_add_boot_option() - handler to add boot option
1535 * @data: pointer to the data for each entry
1536 * Return: status code
1538 static efi_status_t eficonfig_process_add_boot_option(void *data)
1542 struct eficonfig_boot_option *bo = NULL;
1544 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1546 return EFI_OUT_OF_RESOURCES;
1548 ret = efi_bootmgr_get_unused_bootoption(varname, sizeof(varname), &bo->boot_index);
1549 if (ret != EFI_SUCCESS)
1552 ret = eficonfig_edit_boot_option(varname, bo, NULL, 0, " ** Add Boot Option ** ");
1553 if (ret != EFI_SUCCESS)
1556 ret = efi_bootmgr_append_bootorder((u16)bo->boot_index);
1557 if (ret != EFI_SUCCESS)
1563 /* to stay the parent menu */
1564 ret = (ret == EFI_ABORTED) ? EFI_SUCCESS : ret;
1570 * eficonfig_process_boot_selected() - handler to select boot option entry
1572 * @data: pointer to the data for each entry
1573 * Return: status code
1575 static efi_status_t eficonfig_process_boot_selected(void *data)
1577 struct eficonfig_boot_selection_data *info = data;
1580 *info->selected = info->boot_index;
1586 * eficonfig_add_boot_selection_entry() - add boot option menu entry
1588 * @efi_menu: pointer to store the efimenu structure
1589 * @boot_index: boot option index to be added
1590 * @selected: pointer to store the selected boot option index
1591 * Return: status code
1593 static efi_status_t eficonfig_add_boot_selection_entry(struct efimenu *efi_menu,
1594 unsigned int boot_index,
1595 unsigned int *selected)
1601 struct efi_load_option lo;
1602 u16 varname[] = u"Boot####";
1603 struct eficonfig_boot_selection_data *info;
1605 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
1606 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1610 ret = efi_deserialize_load_option(&lo, load_option, &size);
1611 if (ret != EFI_SUCCESS) {
1612 log_warning("Invalid load option for %ls\n", varname);
1617 if (size >= sizeof(efi_guid_t) &&
1618 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
1620 * auto generated entry has GUID in optional_data,
1621 * skip auto generated entry because it will be generated
1622 * again even if it is edited or deleted.
1628 info = calloc(1, sizeof(struct eficonfig_boot_selection_data));
1631 return EFI_OUT_OF_RESOURCES;
1634 buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
1638 return EFI_OUT_OF_RESOURCES;
1641 utf16_utf8_strcpy(&p, lo.label);
1642 info->boot_index = boot_index;
1643 info->selected = selected;
1644 ret = eficonfig_append_menu_entry(efi_menu, buf, eficonfig_process_boot_selected, info);
1645 if (ret != EFI_SUCCESS) {
1656 * eficonfig_show_boot_selection() - construct boot option menu entry
1658 * @selected: pointer to store the selected boot option index
1659 * Return: status code
1661 static efi_status_t eficonfig_show_boot_selection(unsigned int *selected)
1666 u16 *var_name16 = NULL;
1667 efi_uintn_t num, size, buf_size;
1668 struct efimenu *efi_menu;
1669 struct list_head *pos, *n;
1670 struct eficonfig_entry *entry;
1672 efi_menu = calloc(1, sizeof(struct efimenu));
1674 return EFI_OUT_OF_RESOURCES;
1676 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1678 INIT_LIST_HEAD(&efi_menu->list);
1679 num = size / sizeof(u16);
1680 /* list the load option in the order of BootOrder variable */
1681 for (i = 0; i < num; i++) {
1682 ret = eficonfig_add_boot_selection_entry(efi_menu, bootorder[i], selected);
1683 if (ret != EFI_SUCCESS)
1686 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1690 /* list the remaining load option not included in the BootOrder */
1692 var_name16 = malloc(buf_size);
1694 return EFI_OUT_OF_RESOURCES;
1701 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
1702 if (ret == EFI_NOT_FOUND)
1704 if (ret != EFI_SUCCESS)
1707 if (efi_varname_is_load_option(var_name16, &index)) {
1708 /* If the index is included in the BootOrder, skip it */
1709 if (efi_search_bootorder(bootorder, num, index, NULL))
1712 ret = eficonfig_add_boot_selection_entry(efi_menu, index, selected);
1713 if (ret != EFI_SUCCESS)
1717 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1721 ret = eficonfig_append_quit_entry(efi_menu);
1722 if (ret != EFI_SUCCESS)
1725 ret = eficonfig_process_common(efi_menu, " ** Select Boot Option **",
1726 eficonfig_menu_desc,
1727 eficonfig_display_statusline,
1728 eficonfig_print_entry,
1729 eficonfig_choice_entry);
1731 list_for_each_safe(pos, n, &efi_menu->list) {
1732 entry = list_entry(pos, struct eficonfig_entry, list);
1735 eficonfig_destroy(efi_menu);
1743 * eficonfig_process_edit_boot_option() - handler to edit boot option
1745 * @data: pointer to the data for each entry
1746 * Return: status code
1748 static efi_status_t eficonfig_process_edit_boot_option(void *data)
1752 struct eficonfig_boot_option *bo = NULL;
1755 unsigned int selected;
1757 u16 varname[] = u"Boot####";
1759 ret = eficonfig_show_boot_selection(&selected);
1760 if (ret != EFI_SUCCESS)
1763 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1765 ret = EFI_OUT_OF_RESOURCES;
1769 bo->boot_index = selected;
1770 efi_create_indexed_name(varname, sizeof(varname), "Boot", selected);
1771 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1774 ret = EFI_NOT_FOUND;
1778 ret = eficonfig_edit_boot_option(varname, bo, load_option, size,
1779 " ** Edit Boot Option ** ");
1783 if (ret != EFI_SUCCESS && ret != EFI_ABORTED)
1787 /* to stay the parent menu */
1788 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1794 * eficonfig_print_change_boot_order_entry() - print the boot option entry
1796 * @data: pointer to the data associated with each menu entry
1798 static void eficonfig_print_change_boot_order_entry(void *data)
1800 struct eficonfig_entry *entry = data;
1801 bool reverse = (entry->efi_menu->active == entry->num);
1803 if (entry->efi_menu->start > entry->num || entry->efi_menu->end < entry->num)
1806 printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
1807 (entry->num - entry->efi_menu->start) + EFICONFIG_MENU_HEADER_ROW_NUM + 1, 7);
1810 puts(ANSI_COLOR_REVERSE);
1812 if (entry->num < entry->efi_menu->count - 2) {
1813 if (((struct eficonfig_boot_order_data *)entry->data)->active)
1819 printf("%s", entry->title);
1822 puts(ANSI_COLOR_RESET);
1826 * eficonfig_choice_change_boot_order() - user key input handler
1828 * @data: pointer to the menu entry
1829 * Return: key string to identify the selected entry
1831 char *eficonfig_choice_change_boot_order(void *data)
1833 struct cli_ch_state s_cch, *cch = &s_cch;
1834 struct list_head *pos, *n;
1835 struct efimenu *efi_menu = data;
1836 enum bootmenu_key key = BKEY_NONE;
1837 struct eficonfig_entry *entry, *tmp;
1841 key = bootmenu_loop(NULL, cch);
1845 if (efi_menu->active > 0 &&
1846 efi_menu->active < efi_menu->count - 2) {
1847 list_for_each_safe(pos, n, &efi_menu->list) {
1848 entry = list_entry(pos, struct eficonfig_entry, list);
1849 if (entry->num == efi_menu->active)
1852 tmp = list_entry(pos->prev, struct eficonfig_entry, list);
1855 list_del(&tmp->list);
1856 list_add(&tmp->list, &entry->list);
1858 eficonfig_menu_up(efi_menu);
1862 if (efi_menu->active > 0)
1863 eficonfig_menu_up(efi_menu);
1867 if (efi_menu->active < efi_menu->count - 3) {
1868 list_for_each_safe(pos, n, &efi_menu->list) {
1869 entry = list_entry(pos, struct eficonfig_entry, list);
1870 if (entry->num == efi_menu->active)
1873 tmp = list_entry(pos->next, struct eficonfig_entry, list);
1876 list_del(&entry->list);
1877 list_add(&entry->list, &tmp->list);
1879 eficonfig_menu_down(efi_menu);
1883 if (efi_menu->active < efi_menu->count - 1)
1884 eficonfig_menu_down(efi_menu);
1888 /* force to select "Save" entry */
1889 efi_menu->active = efi_menu->count - 2;
1893 if (efi_menu->active == efi_menu->count - 2) {
1894 list_for_each_prev_safe(pos, n, &efi_menu->list) {
1895 entry = list_entry(pos, struct eficonfig_entry, list);
1896 if (entry->num == efi_menu->active)
1902 if (efi_menu->active == efi_menu->count - 1) {
1903 entry = list_last_entry(&efi_menu->list,
1904 struct eficonfig_entry,
1908 /* Pressed key is not valid, wait next key press */
1911 if (efi_menu->active < efi_menu->count - 2) {
1912 list_for_each_safe(pos, n, &efi_menu->list) {
1913 entry = list_entry(pos, struct eficonfig_entry, list);
1914 if (entry->num == efi_menu->active) {
1915 struct eficonfig_boot_order_data *data = entry->data;
1917 data->active = !data->active;
1922 /* Pressed key is not valid, wait next key press */
1925 entry = list_last_entry(&efi_menu->list,
1926 struct eficonfig_entry, list);
1929 /* Pressed key is not valid, wait next key press */
1936 * eficonfig_process_save_boot_order() - callback function for "Save" entry
1938 * @data: pointer to the data
1939 * Return: status code
1941 static efi_status_t eficonfig_process_save_boot_order(void *data)
1946 struct list_head *pos, *n;
1948 struct efimenu *efi_menu;
1949 struct eficonfig_entry *entry;
1950 struct eficonfig_save_boot_order_data *save_data = data;
1952 efi_menu = save_data->efi_menu;
1955 * The change boot order menu always has "Save" and "Quit" entries.
1956 * !(efi_menu->count - 2) means there is no user defined boot option.
1958 if (!(efi_menu->count - 2))
1961 new_bootorder = calloc(1, (efi_menu->count - 2) * sizeof(u16));
1962 if (!new_bootorder) {
1963 ret = EFI_OUT_OF_RESOURCES;
1967 /* create new BootOrder */
1969 list_for_each_safe(pos, n, &efi_menu->list) {
1970 struct eficonfig_boot_order_data *data;
1972 entry = list_entry(pos, struct eficonfig_entry, list);
1973 /* exit the loop when iteration reaches "Save" */
1974 if (!strncmp(entry->title, "Save", strlen("Save")))
1979 new_bootorder[count++] = data->boot_index;
1982 size = count * sizeof(u16);
1983 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1984 EFI_VARIABLE_NON_VOLATILE |
1985 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1986 EFI_VARIABLE_RUNTIME_ACCESS,
1987 size, new_bootorder, false);
1989 save_data->selected = true;
1991 free(new_bootorder);
1997 * eficonfig_add_change_boot_order_entry() - add boot order entry
1999 * @efi_menu: pointer to the efimenu structure
2000 * @boot_index: boot option index to be added
2001 * @active: flag to include the boot option into BootOrder
2002 * Return: status code
2004 static efi_status_t eficonfig_add_change_boot_order_entry(struct efimenu *efi_menu,
2005 u32 boot_index, bool active)
2011 struct efi_load_option lo;
2012 u16 varname[] = u"Boot####";
2013 struct eficonfig_boot_order_data *data;
2015 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
2016 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
2020 ret = efi_deserialize_load_option(&lo, load_option, &size);
2021 if (ret != EFI_SUCCESS)
2024 data = calloc(1, sizeof(*data));
2026 ret = EFI_OUT_OF_RESOURCES;
2030 title = calloc(1, utf16_utf8_strlen(lo.label) + 1);
2033 ret = EFI_OUT_OF_RESOURCES;
2037 utf16_utf8_strcpy(&p, lo.label);
2039 data->boot_index = boot_index;
2040 data->active = active;
2042 ret = eficonfig_append_menu_entry(efi_menu, title, NULL, data);
2043 if (ret != EFI_SUCCESS) {
2056 * eficonfig_create_change_boot_order_entry() - create boot order entry
2058 * @efi_menu: pointer to the efimenu structure
2059 * @bootorder: pointer to the BootOrder variable
2060 * @num: number of BootOrder entry
2061 * Return: status code
2063 static efi_status_t eficonfig_create_change_boot_order_entry(struct efimenu *efi_menu,
2064 u16 *bootorder, efi_uintn_t num)
2069 u16 *var_name16 = NULL;
2070 efi_uintn_t size, buf_size;
2071 struct eficonfig_save_boot_order_data *save_data;
2073 /* list the load option in the order of BootOrder variable */
2074 for (i = 0; i < num; i++) {
2075 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2078 ret = eficonfig_add_change_boot_order_entry(efi_menu, bootorder[i], true);
2079 if (ret != EFI_SUCCESS)
2083 /* list the remaining load option not included in the BootOrder */
2085 var_name16 = malloc(buf_size);
2087 return EFI_OUT_OF_RESOURCES;
2094 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2098 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
2099 if (ret == EFI_NOT_FOUND)
2101 if (ret != EFI_SUCCESS)
2104 if (efi_varname_is_load_option(var_name16, &index)) {
2105 /* If the index is included in the BootOrder, skip it */
2106 if (efi_search_bootorder(bootorder, num, index, NULL))
2109 ret = eficonfig_add_change_boot_order_entry(efi_menu, index, false);
2110 if (ret != EFI_SUCCESS)
2115 /* add "Save" and "Quit" entries */
2116 title = strdup("Save");
2118 ret = EFI_OUT_OF_RESOURCES;
2122 save_data = malloc(sizeof(struct eficonfig_save_boot_order_data));
2124 ret = EFI_OUT_OF_RESOURCES;
2127 save_data->efi_menu = efi_menu;
2128 save_data->selected = false;
2130 ret = eficonfig_append_menu_entry(efi_menu, title,
2131 eficonfig_process_save_boot_order,
2133 if (ret != EFI_SUCCESS)
2136 ret = eficonfig_append_quit_entry(efi_menu);
2137 if (ret != EFI_SUCCESS)
2140 efi_menu->active = 0;
2148 * eficonfig_process_change_boot_order() - handler to change boot order
2150 * @data: pointer to the data for each entry
2151 * Return: status code
2153 static efi_status_t eficonfig_process_change_boot_order(void *data)
2157 efi_uintn_t num, size;
2158 struct list_head *pos, *n;
2159 struct eficonfig_entry *entry;
2160 struct efimenu *efi_menu;
2162 efi_menu = calloc(1, sizeof(struct efimenu));
2164 return EFI_OUT_OF_RESOURCES;
2166 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2168 INIT_LIST_HEAD(&efi_menu->list);
2169 num = size / sizeof(u16);
2170 ret = eficonfig_create_change_boot_order_entry(efi_menu, bootorder, num);
2171 if (ret != EFI_SUCCESS)
2175 ret = eficonfig_process_common(efi_menu,
2176 " ** Change Boot Order **",
2177 eficonfig_change_boot_order_desc,
2178 eficonfig_display_statusline,
2179 eficonfig_print_change_boot_order_entry,
2180 eficonfig_choice_change_boot_order);
2181 /* exit from the menu if user selects the "Save" entry. */
2182 if (ret == EFI_SUCCESS && efi_menu->active == (efi_menu->count - 2)) {
2183 list_for_each_prev_safe(pos, n, &efi_menu->list) {
2184 entry = list_entry(pos, struct eficonfig_entry, list);
2185 if (entry->num == efi_menu->active)
2188 if (((struct eficonfig_save_boot_order_data *)entry->data)->selected)
2191 if (ret != EFI_SUCCESS)
2196 list_for_each_safe(pos, n, &efi_menu->list) {
2197 entry = list_entry(pos, struct eficonfig_entry, list);
2200 eficonfig_destroy(efi_menu);
2202 /* to stay the parent menu */
2203 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2209 * eficonfig_process_delete_boot_option() - handler to delete boot option
2211 * @data: pointer to the data for each entry
2212 * Return: status code
2214 static efi_status_t eficonfig_process_delete_boot_option(void *data)
2217 unsigned int selected;
2220 ret = eficonfig_show_boot_selection(&selected);
2221 if (ret == EFI_SUCCESS)
2222 ret = efi_bootmgr_delete_boot_option(selected);
2224 if (ret != EFI_SUCCESS)
2228 /* to stay the parent menu */
2229 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2235 * eficonfig_init() - do required initialization for eficonfig command
2237 * Return: status code
2239 static efi_status_t eficonfig_init(void)
2241 efi_status_t ret = EFI_SUCCESS;
2243 struct efi_handler *handler;
2244 unsigned long columns, rows;
2247 ret = efi_search_protocol(efi_root, &efi_guid_text_input_protocol, &handler);
2248 if (ret != EFI_SUCCESS)
2251 ret = efi_protocol_open(handler, (void **)&cin, efi_root, NULL,
2252 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2253 if (ret != EFI_SUCCESS)
2255 ret = efi_search_protocol(efi_root, &efi_guid_text_output_protocol, &handler);
2256 if (ret != EFI_SUCCESS)
2259 ret = efi_protocol_open(handler, (void **)&cout, efi_root, NULL,
2260 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2261 if (ret != EFI_SUCCESS)
2264 cout->query_mode(cout, cout->mode->mode, &columns, &rows);
2265 avail_row = rows - (EFICONFIG_MENU_HEADER_ROW_NUM +
2266 EFICONFIG_MENU_DESC_ROW_NUM);
2267 if (avail_row <= 0) {
2268 eficonfig_print_msg("Console size is too small!");
2269 return EFI_INVALID_PARAMETER;
2271 /* TODO: Should we check the minimum column size? */
2279 static const struct eficonfig_item maintenance_menu_items[] = {
2280 {"Add Boot Option", eficonfig_process_add_boot_option},
2281 {"Edit Boot Option", eficonfig_process_edit_boot_option},
2282 {"Change Boot Order", eficonfig_process_change_boot_order},
2283 {"Delete Boot Option", eficonfig_process_delete_boot_option},
2284 #if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT) && IS_ENABLED(CONFIG_EFI_MM_COMM_TEE))
2285 {"Secure Boot Configuration", eficonfig_process_secure_boot_config},
2287 {"Quit", eficonfig_process_quit},
2291 * do_eficonfig() - execute `eficonfig` command
2293 * @cmdtp: table entry describing command
2294 * @flag: bitmap indicating how the command was invoked
2295 * @argc: number of arguments
2296 * @argv: command line arguments
2297 * Return: status code
2299 static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
2302 struct efimenu *efi_menu;
2305 return CMD_RET_USAGE;
2307 ret = efi_init_obj_list();
2308 if (ret != EFI_SUCCESS) {
2309 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
2310 ret & ~EFI_ERROR_MASK);
2312 return CMD_RET_FAILURE;
2315 ret = eficonfig_init();
2316 if (ret != EFI_SUCCESS)
2317 return CMD_RET_FAILURE;
2319 ret = efi_bootmgr_update_media_device_boot_option();
2320 if (ret != EFI_SUCCESS)
2324 efi_menu = eficonfig_create_fixed_menu(maintenance_menu_items,
2325 ARRAY_SIZE(maintenance_menu_items));
2327 return CMD_RET_FAILURE;
2329 ret = eficonfig_process_common(efi_menu,
2330 " ** UEFI Maintenance Menu **",
2331 eficonfig_menu_desc,
2332 eficonfig_display_statusline,
2333 eficonfig_print_entry,
2334 eficonfig_choice_entry);
2335 eficonfig_destroy(efi_menu);
2337 if (ret == EFI_ABORTED)
2341 return CMD_RET_SUCCESS;
2345 eficonfig, 1, 0, do_eficonfig,
2346 "provide menu-driven UEFI variable maintenance interface",