1 // SPDX-License-Identifier: GPL-2.0+
3 * Menu-driven UEFI Variable maintenance
5 * Copyright (c) 2022 Masahisa Kojima, Linaro Limited
12 #include <efi_loader.h>
13 #include <efi_load_initrd.h>
14 #include <efi_config.h>
15 #include <efi_variable.h>
21 #include <asm/unaligned.h>
22 #include <linux/delay.h>
24 static struct efi_simple_text_input_protocol *cin;
25 const char *eficonfig_menu_desc =
26 " Press UP/DOWN to move, ENTER to select, ESC to quit";
28 static const char *eficonfig_change_boot_order_desc =
29 " Press UP/DOWN to move, +/- to change orde\n"
30 " Press SPACE to activate or deactivate the entry\n"
31 " CTRL+S to save, ESC to quit";
33 static struct efi_simple_text_output_protocol *cout;
36 #define EFICONFIG_DESCRIPTION_MAX 32
37 #define EFICONFIG_OPTIONAL_DATA_MAX 64
38 #define EFICONFIG_MENU_HEADER_ROW_NUM 3
39 #define EFICONFIG_MENU_DESC_ROW_NUM 5
42 * struct eficonfig_filepath_info - structure to be used to store file path
44 * @name: file or directory name
45 * @list: list structure
47 struct eficonfig_filepath_info {
49 struct list_head list;
53 * struct eficonfig_boot_option - structure to be used for updating UEFI boot option
55 * @file_info: user selected file info
56 * @initrd_info: user selected initrd file info
57 * @boot_index: index of the boot option
58 * @description: pointer to the description string
59 * @optional_data: pointer to the optional_data
60 * @edit_completed: flag indicates edit complete
62 struct eficonfig_boot_option {
63 struct eficonfig_select_file_info file_info;
64 struct eficonfig_select_file_info initrd_info;
65 unsigned int boot_index;
72 * struct eficonfig_volume_entry_data - structure to be used to store volume info
74 * @file_info: pointer to file info structure
75 * @v: pointer to the protocol interface
76 * @dp: pointer to the device path
78 struct eficonfig_volume_entry_data {
79 struct eficonfig_select_file_info *file_info;
80 struct efi_simple_file_system_protocol *v;
81 struct efi_device_path *dp;
85 * struct eficonfig_file_entry_data - structure to be used to store file info
87 * @file_info: pointer to file info structure
88 * @is_directory: flag to identify the directory or file
89 * @file_name: name of directory or file
91 struct eficonfig_file_entry_data {
92 struct eficonfig_select_file_info *file_info;
98 * struct eficonfig_boot_selection_data - structure to be used to select the boot option entry
100 * @boot_index: index of the boot option
101 * @selected: pointer to store the selected index in the BootOrder variable
103 struct eficonfig_boot_selection_data {
109 * struct eficonfig_boot_order_data - structure to be used to update BootOrder variable
111 * @boot_index: boot option index
112 * @active: flag to include the boot option into BootOrder variable
114 struct eficonfig_boot_order_data {
120 * struct eficonfig_save_boot_order_data - structure to be used to change boot order
122 * @efi_menu: pointer to efimenu structure
123 * @selected: flag to indicate user selects "Save" entry
125 struct eficonfig_save_boot_order_data {
126 struct efimenu *efi_menu;
131 * struct eficonfig_menu_adjust - update start and end entry index
133 * @efi_menu: pointer to efimenu structure
134 * @add: flag to add or substract the index
136 static void eficonfig_menu_adjust(struct efimenu *efi_menu, bool add)
143 if (add && efi_menu->end < efi_menu->active) {
146 } else if (!add && efi_menu->start > efi_menu->active) {
151 #define eficonfig_menu_up(_a) eficonfig_menu_adjust(_a, false)
152 #define eficonfig_menu_down(_a) eficonfig_menu_adjust(_a, true)
155 * eficonfig_print_msg() - print message
157 * display the message to the user, user proceeds the screen
158 * with any key press.
160 * @items: pointer to the structure of each menu entry
161 * @count: the number of menu entry
162 * @menu_header: pointer to the menu header string
163 * Return: status code
165 void eficonfig_print_msg(char *msg)
171 printf(ANSI_CURSOR_HIDE
174 "%s\n\n Press any key to continue", 3, 4, msg);
180 * eficonfig_print_entry() - print each menu entry
182 * @data: pointer to the data associated with each menu entry
184 void eficonfig_print_entry(void *data)
186 struct eficonfig_entry *entry = data;
187 bool reverse = (entry->efi_menu->active == entry->num);
189 if (entry->efi_menu->start > entry->num || entry->efi_menu->end < entry->num)
192 printf(ANSI_CURSOR_POSITION, (entry->num - entry->efi_menu->start) +
193 EFICONFIG_MENU_HEADER_ROW_NUM + 1, 7);
196 puts(ANSI_COLOR_REVERSE);
198 printf(ANSI_CLEAR_LINE "%s", entry->title);
201 puts(ANSI_COLOR_RESET);
205 * eficonfig_display_statusline() - print status line
207 * @m: pointer to the menu structure
209 void eficonfig_display_statusline(struct menu *m)
211 struct eficonfig_entry *entry;
213 if (menu_default_choice(m, (void *)&entry) < 0)
216 printf(ANSI_CURSOR_POSITION
218 ANSI_CURSOR_POSITION ANSI_CLEAR_LINE ANSI_CURSOR_POSITION
220 ANSI_CLEAR_LINE_TO_END,
221 1, 1, entry->efi_menu->menu_header, avail_row + 4, 1,
222 avail_row + 5, 1, entry->efi_menu->menu_desc);
226 * eficonfig_choice_entry() - user key input handler
228 * @data: pointer to the efimenu structure
229 * Return: key string to identify the selected entry
231 char *eficonfig_choice_entry(void *data)
233 struct cli_ch_state s_cch, *cch = &s_cch;
234 struct list_head *pos, *n;
235 struct eficonfig_entry *entry;
236 enum bootmenu_key key = BKEY_NONE;
237 struct efimenu *efi_menu = data;
242 key = bootmenu_loop((struct bootmenu_data *)efi_menu, cch);
246 if (efi_menu->active > 0)
247 eficonfig_menu_up(efi_menu);
249 /* no menu key selected, regenerate menu */
252 if (efi_menu->active < efi_menu->count - 1)
253 eficonfig_menu_down(efi_menu);
255 /* no menu key selected, regenerate menu */
258 list_for_each_safe(pos, n, &efi_menu->list) {
259 entry = list_entry(pos, struct eficonfig_entry, list);
260 if (entry->num == efi_menu->active)
265 /* Quit by choosing the last entry */
266 entry = list_last_entry(&efi_menu->list, struct eficonfig_entry, list);
269 /* Pressed key is not valid, no need to regenerate the menu */
276 * eficonfig_destroy() - destroy efimenu
278 * @efi_menu: pointer to the efimenu structure
280 void eficonfig_destroy(struct efimenu *efi_menu)
282 struct list_head *pos, *n;
283 struct eficonfig_entry *entry;
288 list_for_each_safe(pos, n, &efi_menu->list) {
289 entry = list_entry(pos, struct eficonfig_entry, list);
291 list_del(&entry->list);
294 free(efi_menu->menu_header);
299 * eficonfig_process_quit() - callback function for "Quit" entry
301 * @data: pointer to the data
302 * Return: status code
304 efi_status_t eficonfig_process_quit(void *data)
310 * eficonfig_append_menu_entry() - append menu item
312 * @efi_menu: pointer to the efimenu structure
313 * @title: pointer to the entry title
314 * @func: callback of each entry
315 * @data: pointer to the data to be passed to each entry callback
316 * Return: status code
318 efi_status_t eficonfig_append_menu_entry(struct efimenu *efi_menu,
319 char *title, eficonfig_entry_func func,
322 struct eficonfig_entry *entry;
324 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX)
325 return EFI_OUT_OF_RESOURCES;
327 entry = calloc(1, sizeof(struct eficonfig_entry));
329 return EFI_OUT_OF_RESOURCES;
331 entry->title = title;
332 sprintf(entry->key, "%d", efi_menu->count);
333 entry->efi_menu = efi_menu;
336 entry->num = efi_menu->count++;
337 list_add_tail(&entry->list, &efi_menu->list);
343 * eficonfig_append_quit_entry() - append quit entry
345 * @efi_menu: pointer to the efimenu structure
346 * Return: status code
348 efi_status_t eficonfig_append_quit_entry(struct efimenu *efi_menu)
353 title = strdup("Quit");
355 return EFI_OUT_OF_RESOURCES;
357 ret = eficonfig_append_menu_entry(efi_menu, title, eficonfig_process_quit, NULL);
358 if (ret != EFI_SUCCESS)
365 * eficonfig_create_fixed_menu() - create fixed entry menu structure
367 * @items: pointer to the menu entry item
368 * @count: the number of menu entry
369 * Return: pointer to the efimenu structure
371 void *eficonfig_create_fixed_menu(const struct eficonfig_item *items, int count)
376 struct efimenu *efi_menu;
377 const struct eficonfig_item *iter = items;
379 efi_menu = calloc(1, sizeof(struct efimenu));
383 INIT_LIST_HEAD(&efi_menu->list);
384 for (i = 0; i < count; i++, iter++) {
385 title = strdup(iter->title);
389 ret = eficonfig_append_menu_entry(efi_menu, title, iter->func, iter->data);
390 if (ret != EFI_SUCCESS) {
398 eficonfig_destroy(efi_menu);
404 * eficonfig_process_common() - main handler for UEFI menu
406 * Construct the structures required to show the menu, then handle
407 * the user input interacting with u-boot menu functions.
409 * @efi_menu: pointer to the efimenu structure
410 * @menu_header: pointer to the menu header string
411 * @menu_desc: pointer to the menu description
412 * @display_statusline: function pointer to draw statusline
413 * @item_data_print: function pointer to draw the menu item
414 * @item_choice: function pointer to handle the key press
415 * Return: status code
417 efi_status_t eficonfig_process_common(struct efimenu *efi_menu,
418 char *menu_header, const char *menu_desc,
419 void (*display_statusline)(struct menu *),
420 void (*item_data_print)(void *),
421 char *(*item_choice)(void *))
425 struct list_head *pos, *n;
426 struct eficonfig_entry *entry;
427 efi_status_t ret = EFI_SUCCESS;
429 if (efi_menu->count > EFICONFIG_ENTRY_NUM_MAX)
430 return EFI_OUT_OF_RESOURCES;
432 efi_menu->delay = -1;
433 efi_menu->active = 0;
435 efi_menu->end = avail_row - 1;
438 efi_menu->menu_header = strdup(menu_header);
439 if (!efi_menu->menu_header)
440 return EFI_OUT_OF_RESOURCES;
443 efi_menu->menu_desc = menu_desc;
445 menu = menu_create(NULL, 0, 1, display_statusline, item_data_print,
446 item_choice, efi_menu);
448 return EFI_INVALID_PARAMETER;
450 list_for_each_safe(pos, n, &efi_menu->list) {
451 entry = list_entry(pos, struct eficonfig_entry, list);
452 if (!menu_item_add(menu, entry->key, entry)) {
453 ret = EFI_INVALID_PARAMETER;
458 entry = list_first_entry_or_null(&efi_menu->list, struct eficonfig_entry, list);
460 menu_default_set(menu, entry->key);
462 printf(ANSI_CURSOR_HIDE
464 ANSI_CURSOR_POSITION, 1, 1);
466 if (menu_get_choice(menu, &choice)) {
469 ret = entry->func(entry->data);
474 printf(ANSI_CLEAR_CONSOLE
476 ANSI_CURSOR_SHOW, 1, 1);
482 * eficonfig_volume_selected() - handler of volume selection
484 * @data: pointer to the data of selected entry
485 * Return: status code
487 static efi_status_t eficonfig_volume_selected(void *data)
489 struct eficonfig_volume_entry_data *info = data;
492 info->file_info->current_volume = info->v;
493 info->file_info->dp_volume = info->dp;
500 * eficonfig_create_device_path() - create device path
502 * @dp_volume: pointer to the volume
503 * @current_path: pointer to the file path u16 string
505 * device path or NULL. Caller must free the returned value
507 struct efi_device_path *eficonfig_create_device_path(struct efi_device_path *dp_volume,
513 struct efi_device_path *dp;
514 struct efi_device_path_file_path *fp;
516 fp_size = sizeof(struct efi_device_path) + u16_strsize(current_path);
517 buf = calloc(1, fp_size + sizeof(END));
522 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
523 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
524 fp->dp.length = (u16)fp_size;
525 u16_strcpy(fp->str, current_path);
529 *((struct efi_device_path *)p) = END;
531 dp = efi_dp_shorten(dp_volume);
534 dp = efi_dp_concat(dp, &fp->dp, false);
541 * eficonfig_file_selected() - handler of file selection
543 * @data: pointer to the data of selected entry
544 * Return: status code
546 static efi_status_t eficonfig_file_selected(void *data)
549 struct eficonfig_file_entry_data *info = data;
552 return EFI_INVALID_PARAMETER;
554 if (!strcmp(info->file_name, "..\\")) {
555 struct eficonfig_filepath_info *iter;
556 struct list_head *pos, *n;
559 tmp = info->file_info->current_path;
561 memset(info->file_info->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
562 filepath = calloc(1, EFICONFIG_FILE_PATH_MAX);
564 return EFI_OUT_OF_RESOURCES;
566 list_for_each_safe(pos, n, &info->file_info->filepath_list) {
567 iter = list_entry(pos, struct eficonfig_filepath_info, list);
569 is_last = list_is_last(&iter->list, &info->file_info->filepath_list);
571 list_del(&iter->list);
576 strlcat(filepath, iter->name, EFICONFIG_FILE_PATH_MAX);
578 utf8_utf16_strcpy(&tmp, filepath);
581 struct eficonfig_filepath_info *filepath_info;
583 new_len = u16_strlen(info->file_info->current_path) +
584 strlen(info->file_name);
585 if (new_len >= EFICONFIG_FILE_PATH_MAX) {
586 eficonfig_print_msg("File path is too long!");
587 return EFI_INVALID_PARAMETER;
589 tmp = &info->file_info->current_path[u16_strlen(info->file_info->current_path)];
590 utf8_utf16_strcpy(&tmp, info->file_name);
592 filepath_info = calloc(1, sizeof(struct eficonfig_filepath_info));
594 return EFI_OUT_OF_RESOURCES;
596 filepath_info->name = strdup(info->file_name);
597 if (!filepath_info->name) {
599 return EFI_OUT_OF_RESOURCES;
601 list_add_tail(&filepath_info->list, &info->file_info->filepath_list);
603 if (!info->is_directory)
604 info->file_info->file_selected = true;
611 * eficonfig_select_volume() - construct the volume selection menu
613 * @file_info: pointer to the file selection structure
614 * Return: status code
616 static efi_status_t eficonfig_select_volume(struct eficonfig_select_file_info *file_info)
621 struct efimenu *efi_menu;
622 struct list_head *pos, *n;
623 struct efi_handler *handler;
624 struct eficonfig_entry *entry;
625 struct efi_device_path *device_path;
626 efi_handle_t *volume_handles = NULL;
627 struct efi_simple_file_system_protocol *v;
629 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
630 NULL, &count, (efi_handle_t **)&volume_handles);
631 if (ret != EFI_SUCCESS) {
632 eficonfig_print_msg("No block device found!");
636 efi_menu = calloc(1, sizeof(struct efimenu));
638 return EFI_OUT_OF_RESOURCES;
640 INIT_LIST_HEAD(&efi_menu->list);
641 for (i = 0; i < count; i++) {
643 struct efi_block_io *block_io;
644 struct eficonfig_volume_entry_data *info;
646 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
649 ret = efi_search_protocol(volume_handles[i],
650 &efi_simple_file_system_protocol_guid, &handler);
651 if (ret != EFI_SUCCESS)
653 ret = efi_protocol_open(handler, (void **)&v, efi_root, NULL,
654 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
655 if (ret != EFI_SUCCESS)
658 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
659 if (ret != EFI_SUCCESS)
661 ret = efi_protocol_open(handler, (void **)&device_path,
662 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
663 if (ret != EFI_SUCCESS)
666 ret = efi_search_protocol(volume_handles[i], &efi_block_io_guid, &handler);
667 if (ret != EFI_SUCCESS)
669 ret = efi_protocol_open(handler, (void **)&block_io,
670 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
671 if (ret != EFI_SUCCESS)
674 info = calloc(1, sizeof(struct eficonfig_volume_entry_data));
676 ret = EFI_OUT_OF_RESOURCES;
680 devname = calloc(1, BOOTMENU_DEVICE_NAME_MAX);
683 ret = EFI_OUT_OF_RESOURCES;
686 ret = efi_disk_get_device_name(volume_handles[i], devname,
687 BOOTMENU_DEVICE_NAME_MAX);
688 if (ret != EFI_SUCCESS) {
694 info->dp = device_path;
695 info->file_info = file_info;
696 ret = eficonfig_append_menu_entry(efi_menu, devname, eficonfig_volume_selected,
698 if (ret != EFI_SUCCESS) {
704 ret = eficonfig_append_quit_entry(efi_menu);
705 if (ret != EFI_SUCCESS)
708 ret = eficonfig_process_common(efi_menu, " ** Select Volume **",
710 eficonfig_display_statusline,
711 eficonfig_print_entry,
712 eficonfig_choice_entry);
715 efi_free_pool(volume_handles);
716 list_for_each_safe(pos, n, &efi_menu->list) {
717 entry = list_entry(pos, struct eficonfig_entry, list);
720 eficonfig_destroy(efi_menu);
726 * sort_file() - sort the file name in ascii order
728 * @data1: pointer to the file entry data
729 * @data2: pointer to the file entry data
730 * Return: -1 if the data1 file name is less than data2 file name,
731 * 0 if both file name match,
732 * 1 if the data1 file name is greater thant data2 file name.
734 static int sort_file(const void *arg1, const void *arg2)
736 const struct eficonfig_file_entry_data *data1, *data2;
738 data1 = *((const struct eficonfig_file_entry_data **)arg1);
739 data2 = *((const struct eficonfig_file_entry_data **)arg2);
741 return strcasecmp(data1->file_name, data2->file_name);
745 * eficonfig_create_file_entry() - construct the file menu entry
747 * @efi_menu: pointer to the efimenu structure
748 * @count: number of the directory and file
749 * @tmp_infos: pointer to the entry data array
750 * @f: pointer to the file handle
751 * @buf: pointer to the buffer to store the directory information
752 * @file_info: pointer to the file selection structure
753 * Return: status code
756 eficonfig_create_file_entry(struct efimenu *efi_menu, u32 count,
757 struct eficonfig_file_entry_data **tmp_infos,
758 struct efi_file_handle *f, struct efi_file_info *buf,
759 struct eficonfig_select_file_info *file_info)
764 u32 i, entry_num = 0;
765 struct eficonfig_file_entry_data *info;
767 EFI_CALL(f->setpos(f, 0));
768 /* Read directory and construct menu structure */
769 for (i = 0; i < count; i++) {
770 if (entry_num >= EFICONFIG_ENTRY_NUM_MAX - 1)
773 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
774 ret = EFI_CALL(f->read(f, &len, buf));
775 if (ret != EFI_SUCCESS || len == 0)
778 info = calloc(1, sizeof(struct eficonfig_file_entry_data));
780 ret = EFI_OUT_OF_RESOURCES;
784 /* append '\\' at the end of directory name */
785 name = calloc(1, utf16_utf8_strlen(buf->file_name) + 2);
787 ret = EFI_OUT_OF_RESOURCES;
792 utf16_utf8_strcpy(&p, buf->file_name);
793 if (buf->attribute & EFI_FILE_DIRECTORY) {
794 /* filter out u'.' */
795 if (!u16_strcmp(buf->file_name, u".")) {
800 name[u16_strlen(buf->file_name)] = '\\';
801 info->is_directory = true;
804 info->file_name = name;
805 info->file_info = file_info;
806 tmp_infos[entry_num++] = info;
809 qsort(tmp_infos, entry_num, sizeof(*tmp_infos),
810 (int (*)(const void *, const void *))sort_file);
812 for (i = 0; i < entry_num; i++) {
813 ret = eficonfig_append_menu_entry(efi_menu, tmp_infos[i]->file_name,
814 eficonfig_file_selected, tmp_infos[i]);
815 if (ret != EFI_SUCCESS)
824 * eficonfig_show_file_selection() - construct the file selection menu
826 * @file_info: pointer to the file selection structure
827 * @root: pointer to the file handle
828 * Return: status code
830 static efi_status_t eficonfig_show_file_selection(struct eficonfig_select_file_info *file_info,
831 struct efi_file_handle *root)
836 struct efimenu *efi_menu;
837 struct efi_file_handle *f;
838 struct efi_file_info *buf;
839 struct eficonfig_file_entry_data **tmp_infos;
841 buf = calloc(1, sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE);
843 return EFI_OUT_OF_RESOURCES;
845 while (!file_info->file_selected) {
846 efi_menu = calloc(1, sizeof(struct efimenu));
848 ret = EFI_OUT_OF_RESOURCES;
851 INIT_LIST_HEAD(&efi_menu->list);
853 ret = EFI_CALL(root->open(root, &f, file_info->current_path,
854 EFI_FILE_MODE_READ, 0));
855 if (ret != EFI_SUCCESS) {
856 eficonfig_print_msg("Reading volume failed!");
862 /* Count the number of directory entries */
864 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
865 ret = EFI_CALL(f->read(f, &len, buf));
866 if (ret != EFI_SUCCESS || len == 0)
872 /* allocate array to sort the entry */
873 tmp_infos = calloc(count, sizeof(*tmp_infos));
875 ret = EFI_OUT_OF_RESOURCES;
879 ret = eficonfig_create_file_entry(efi_menu, count, tmp_infos,
881 if (ret != EFI_SUCCESS)
884 ret = eficonfig_append_quit_entry(efi_menu);
885 if (ret != EFI_SUCCESS)
888 ret = eficonfig_process_common(efi_menu, " ** Select File **",
890 eficonfig_display_statusline,
891 eficonfig_print_entry,
892 eficonfig_choice_entry);
894 EFI_CALL(f->close(f));
895 eficonfig_destroy(efi_menu);
898 for (i = 0; i < count; i++)
904 if (ret != EFI_SUCCESS)
915 * handle_user_input() - handle user input
917 * @buf: pointer to the buffer
918 * @buf_size: size of the buffer
919 * @cursor_col: cursor column for user input
920 * @msg: pointer to the string to display
921 * Return: status code
923 static efi_status_t handle_user_input(u16 *buf, int buf_size,
924 int cursor_col, char *msg)
929 printf(ANSI_CLEAR_CONSOLE
933 " Press ENTER to complete, ESC to quit",
936 /* tmp is used to accept user cancel */
937 tmp = calloc(1, buf_size * sizeof(u16));
939 return EFI_OUT_OF_RESOURCES;
941 ret = efi_console_get_u16_string(cin, tmp, buf_size, NULL, 4, cursor_col);
942 if (ret == EFI_SUCCESS)
943 u16_strcpy(buf, tmp);
947 /* to stay the parent menu */
948 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
954 * eficonfig_boot_add_enter_description() - handle user input for description
956 * @data: pointer to the internal boot option structure
957 * Return: status code
959 static efi_status_t eficonfig_boot_add_enter_description(void *data)
961 struct eficonfig_boot_option *bo = data;
963 return handle_user_input(bo->description, EFICONFIG_DESCRIPTION_MAX, 22,
964 "\n ** Edit Description **\n"
966 " enter description: ");
970 * eficonfig_boot_add_optional_data() - handle user input for optional data
972 * @data: pointer to the internal boot option structure
973 * Return: status code
975 static efi_status_t eficonfig_boot_add_optional_data(void *data)
977 struct eficonfig_boot_option *bo = data;
979 return handle_user_input(bo->optional_data, EFICONFIG_OPTIONAL_DATA_MAX, 24,
980 "\n ** Edit Optional Data **\n"
982 " enter optional data:");
986 * eficonfig_boot_edit_save() - handler to save the boot option
988 * @data: pointer to the internal boot option structure
989 * Return: status code
991 static efi_status_t eficonfig_boot_edit_save(void *data)
993 struct eficonfig_boot_option *bo = data;
995 if (u16_strlen(bo->description) == 0) {
996 eficonfig_print_msg("Boot Description is empty!");
997 bo->edit_completed = false;
998 return EFI_NOT_READY;
1000 if (u16_strlen(bo->file_info.current_path) == 0) {
1001 eficonfig_print_msg("File is not selected!");
1002 bo->edit_completed = false;
1003 return EFI_NOT_READY;
1006 bo->edit_completed = true;
1012 * eficonfig_process_clear_file_selection() - callback function for "Clear" entry
1014 * @data: pointer to the data
1015 * Return: status code
1017 efi_status_t eficonfig_process_clear_file_selection(void *data)
1019 struct eficonfig_select_file_info *file_info = data;
1021 /* clear the existing file information */
1022 file_info->current_volume = NULL;
1023 file_info->current_path[0] = u'\0';
1024 file_info->dp_volume = NULL;
1029 static struct eficonfig_item select_file_menu_items[] = {
1030 {"Select File", eficonfig_process_select_file},
1031 {"Clear", eficonfig_process_clear_file_selection},
1032 {"Quit", eficonfig_process_quit},
1036 * eficonfig_process_show_file_option() - display select file option
1038 * @file_info: pointer to the file information structure
1039 * Return: status code
1041 efi_status_t eficonfig_process_show_file_option(void *data)
1044 struct efimenu *efi_menu;
1046 select_file_menu_items[0].data = data;
1047 select_file_menu_items[1].data = data;
1048 efi_menu = eficonfig_create_fixed_menu(select_file_menu_items,
1049 ARRAY_SIZE(select_file_menu_items));
1051 return EFI_OUT_OF_RESOURCES;
1053 ret = eficonfig_process_common(efi_menu, " ** Update File **",
1054 eficonfig_menu_desc,
1055 eficonfig_display_statusline,
1056 eficonfig_print_entry,
1057 eficonfig_choice_entry);
1058 if (ret != EFI_SUCCESS) /* User selects "Clear" or "Quit" */
1059 ret = EFI_NOT_READY;
1061 eficonfig_destroy(efi_menu);
1067 * eficonfig_process_select_file() - handle user file selection
1069 * @data: pointer to the data
1070 * Return: status code
1072 efi_status_t eficonfig_process_select_file(void *data)
1076 struct list_head *pos, *n;
1077 struct efi_file_handle *root;
1078 struct eficonfig_filepath_info *item;
1079 struct eficonfig_select_file_info *tmp = NULL;
1080 struct eficonfig_select_file_info *file_info = data;
1082 tmp = calloc(1, sizeof(struct eficonfig_select_file_info));
1084 return EFI_OUT_OF_RESOURCES;
1086 tmp->current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1087 if (!tmp->current_path) {
1089 return EFI_OUT_OF_RESOURCES;
1091 INIT_LIST_HEAD(&tmp->filepath_list);
1093 while (!tmp->file_selected) {
1094 tmp->current_volume = NULL;
1095 memset(tmp->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
1097 ret = eficonfig_select_volume(tmp);
1098 if (ret != EFI_SUCCESS)
1101 if (!tmp->current_volume)
1102 return EFI_INVALID_PARAMETER;
1104 ret = EFI_CALL(tmp->current_volume->open_volume(tmp->current_volume, &root));
1105 if (ret != EFI_SUCCESS)
1108 ret = eficonfig_show_file_selection(tmp, root);
1109 if (ret == EFI_ABORTED)
1111 if (ret != EFI_SUCCESS)
1116 if (ret == EFI_SUCCESS) {
1117 len = u16_strlen(tmp->current_path);
1118 len = (len >= EFICONFIG_FILE_PATH_MAX) ? (EFICONFIG_FILE_PATH_MAX - 1) : len;
1119 memcpy(file_info->current_path, tmp->current_path, len * sizeof(u16));
1120 file_info->current_path[len] = u'\0';
1121 file_info->current_volume = tmp->current_volume;
1122 file_info->dp_volume = tmp->dp_volume;
1125 list_for_each_safe(pos, n, &tmp->filepath_list) {
1126 item = list_entry(pos, struct eficonfig_filepath_info, list);
1127 list_del(&item->list);
1131 free(tmp->current_path);
1134 /* to stay the parent menu */
1135 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1141 * eficonfig_set_boot_option() - set boot option
1143 * @varname: pointer to variable name
1144 * @dp: pointer to device path
1145 * @label: pointer to label string
1146 * @optional_data: pointer to optional data
1147 * Return: status code
1149 static efi_status_t eficonfig_set_boot_option(u16 *varname, struct efi_device_path *dp,
1150 efi_uintn_t dp_size, u16 *label, char *optional_data)
1155 struct efi_load_option lo;
1158 lo.file_path_length = dp_size;
1159 lo.attributes = LOAD_OPTION_ACTIVE;
1160 lo.optional_data = optional_data;
1163 size = efi_serialize_load_option(&lo, (u8 **)&p);
1165 return EFI_INVALID_PARAMETER;
1167 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1168 EFI_VARIABLE_NON_VOLATILE |
1169 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1170 EFI_VARIABLE_RUNTIME_ACCESS,
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,
1327 eficonfig_menu_desc,
1328 eficonfig_display_statusline,
1329 eficonfig_print_entry,
1330 eficonfig_choice_entry);
1333 eficonfig_destroy(efi_menu);
1339 * fill_file_info() - fill the file info from efi_device_path structure
1341 * @dp: pointer to the device path
1342 * @file_info: pointer to the file info structure
1343 * @device_dp: pointer to the volume device path
1345 static void fill_file_info(struct efi_device_path *dp,
1346 struct eficonfig_select_file_info *file_info,
1347 struct efi_device_path *device_dp)
1350 struct efi_device_path *file_dp = NULL;
1352 efi_dp_split_file_path(dp, &device_dp, &file_dp);
1353 file_info->dp_volume = device_dp;
1356 file_str = efi_dp_str(file_dp);
1358 * efi_convert_device_path_to_text() automatically adds u'/' at the
1359 * beginning of file name, remove u'/' before copying to current_path
1365 u16_strcpy(file_info->current_path, p);
1366 efi_free_pool(file_dp);
1367 efi_free_pool(file_str);
1372 * eficonfig_edit_boot_option() - prepare boot option structure for editing
1374 * Construct the boot option structure and copy the existing value
1376 * @varname: pointer to the UEFI variable name
1377 * @bo: pointer to the boot option
1378 * @load_option: pointer to the load option
1379 * @load_option_size: size of the load option
1380 * @header_str: pointer to the header string
1381 * Return : status code
1383 static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_boot_option *bo,
1384 void *load_option, efi_uintn_t load_option_size,
1389 char *tmp = NULL, *p;
1390 struct efi_load_option lo = {0};
1391 efi_uintn_t final_dp_size;
1392 struct efi_device_path *dp = NULL;
1393 efi_uintn_t size = load_option_size;
1394 struct efi_device_path *final_dp = NULL;
1395 struct efi_device_path *device_dp = NULL;
1396 struct efi_device_path *initrd_dp = NULL;
1397 struct efi_device_path *initrd_device_dp = NULL;
1399 const struct efi_initrd_dp id_dp = {
1402 DEVICE_PATH_TYPE_MEDIA_DEVICE,
1403 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
1404 sizeof(id_dp.vendor),
1406 EFI_INITRD_MEDIA_GUID,
1409 DEVICE_PATH_TYPE_END,
1410 DEVICE_PATH_SUB_TYPE_END,
1415 bo->file_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1416 if (!bo->file_info.current_path) {
1417 ret = EFI_OUT_OF_RESOURCES;
1421 bo->initrd_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1422 if (!bo->initrd_info.current_path) {
1423 ret = EFI_OUT_OF_RESOURCES;
1427 bo->description = calloc(1, EFICONFIG_DESCRIPTION_MAX * sizeof(u16));
1428 if (!bo->description) {
1429 ret = EFI_OUT_OF_RESOURCES;
1433 bo->optional_data = calloc(1, EFICONFIG_OPTIONAL_DATA_MAX * sizeof(u16));
1434 if (!bo->optional_data) {
1435 ret = EFI_OUT_OF_RESOURCES;
1439 /* copy the preset value */
1441 ret = efi_deserialize_load_option(&lo, load_option, &size);
1442 if (ret != EFI_SUCCESS)
1446 ret = EFI_INVALID_PARAMETER;
1449 /* truncate the long label string */
1450 if (u16_strlen(lo.label) >= EFICONFIG_DESCRIPTION_MAX)
1451 lo.label[EFICONFIG_DESCRIPTION_MAX - 1] = u'\0';
1453 u16_strcpy(bo->description, lo.label);
1455 /* EFI image file path is a first instance */
1457 fill_file_info(lo.file_path, &bo->file_info, device_dp);
1459 /* Initrd file path(optional) is placed at second instance. */
1460 initrd_dp = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
1462 fill_file_info(initrd_dp, &bo->initrd_info, initrd_device_dp);
1463 efi_free_pool(initrd_dp);
1467 memcpy(bo->optional_data, lo.optional_data, size);
1471 ret = eficonfig_show_boot_option(bo, header_str);
1472 if (ret == EFI_SUCCESS && bo->edit_completed)
1474 if (ret == EFI_NOT_READY)
1476 if (ret != EFI_SUCCESS)
1480 if (bo->initrd_info.dp_volume) {
1481 dp = eficonfig_create_device_path(bo->initrd_info.dp_volume,
1482 bo->initrd_info.current_path);
1484 ret = EFI_OUT_OF_RESOURCES;
1487 initrd_dp = efi_dp_concat((const struct efi_device_path *)&id_dp,
1492 dp = eficonfig_create_device_path(bo->file_info.dp_volume, bo->file_info.current_path);
1494 ret = EFI_OUT_OF_RESOURCES;
1497 final_dp_size = efi_dp_size(dp) + sizeof(END);
1499 final_dp = efi_dp_concat(dp, initrd_dp, true);
1500 final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
1502 final_dp = efi_dp_dup(dp);
1509 if (utf16_utf8_strlen(bo->optional_data)) {
1510 len = utf16_utf8_strlen(bo->optional_data) + 1;
1511 tmp = calloc(1, len);
1515 utf16_utf8_strncpy(&p, bo->optional_data, u16_strlen(bo->optional_data));
1518 ret = eficonfig_set_boot_option(varname, final_dp, final_dp_size, bo->description, tmp);
1521 free(bo->optional_data);
1522 free(bo->description);
1523 free(bo->file_info.current_path);
1524 free(bo->initrd_info.current_path);
1525 efi_free_pool(device_dp);
1526 efi_free_pool(initrd_device_dp);
1527 efi_free_pool(initrd_dp);
1528 efi_free_pool(final_dp);
1534 * eficonfig_process_add_boot_option() - handler to add boot option
1536 * @data: pointer to the data for each entry
1537 * Return: status code
1539 static efi_status_t eficonfig_process_add_boot_option(void *data)
1543 struct eficonfig_boot_option *bo = NULL;
1545 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1547 return EFI_OUT_OF_RESOURCES;
1549 ret = efi_bootmgr_get_unused_bootoption(varname, sizeof(varname), &bo->boot_index);
1550 if (ret != EFI_SUCCESS)
1553 ret = eficonfig_edit_boot_option(varname, bo, NULL, 0, " ** Add Boot Option ** ");
1554 if (ret != EFI_SUCCESS)
1557 ret = efi_bootmgr_append_bootorder((u16)bo->boot_index);
1558 if (ret != EFI_SUCCESS)
1564 /* to stay the parent menu */
1565 ret = (ret == EFI_ABORTED) ? EFI_SUCCESS : ret;
1571 * eficonfig_process_boot_selected() - handler to select boot option entry
1573 * @data: pointer to the data for each entry
1574 * Return: status code
1576 static efi_status_t eficonfig_process_boot_selected(void *data)
1578 struct eficonfig_boot_selection_data *info = data;
1581 *info->selected = info->boot_index;
1587 * eficonfig_add_boot_selection_entry() - add boot option menu entry
1589 * @efi_menu: pointer to store the efimenu structure
1590 * @boot_index: boot option index to be added
1591 * @selected: pointer to store the selected boot option index
1592 * Return: status code
1594 static efi_status_t eficonfig_add_boot_selection_entry(struct efimenu *efi_menu,
1595 unsigned int boot_index,
1596 unsigned int *selected)
1602 struct efi_load_option lo;
1603 u16 varname[] = u"Boot####";
1604 struct eficonfig_boot_selection_data *info;
1606 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
1607 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1611 ret = efi_deserialize_load_option(&lo, load_option, &size);
1612 if (ret != EFI_SUCCESS) {
1613 log_warning("Invalid load option for %ls\n", varname);
1618 if (size >= sizeof(efi_guid_t) &&
1619 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
1621 * auto generated entry has GUID in optional_data,
1622 * skip auto generated entry because it will be generated
1623 * again even if it is edited or deleted.
1629 info = calloc(1, sizeof(struct eficonfig_boot_selection_data));
1632 return EFI_OUT_OF_RESOURCES;
1635 buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
1639 return EFI_OUT_OF_RESOURCES;
1642 utf16_utf8_strcpy(&p, lo.label);
1643 info->boot_index = boot_index;
1644 info->selected = selected;
1645 ret = eficonfig_append_menu_entry(efi_menu, buf, eficonfig_process_boot_selected, info);
1646 if (ret != EFI_SUCCESS) {
1657 * eficonfig_show_boot_selection() - construct boot option menu entry
1659 * @selected: pointer to store the selected boot option index
1660 * Return: status code
1662 static efi_status_t eficonfig_show_boot_selection(unsigned int *selected)
1667 u16 *var_name16 = NULL;
1668 efi_uintn_t num, size, buf_size;
1669 struct efimenu *efi_menu;
1670 struct list_head *pos, *n;
1671 struct eficonfig_entry *entry;
1673 efi_menu = calloc(1, sizeof(struct efimenu));
1675 return EFI_OUT_OF_RESOURCES;
1677 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1679 INIT_LIST_HEAD(&efi_menu->list);
1680 num = size / sizeof(u16);
1681 /* list the load option in the order of BootOrder variable */
1682 for (i = 0; i < num; i++) {
1683 ret = eficonfig_add_boot_selection_entry(efi_menu, bootorder[i], selected);
1684 if (ret != EFI_SUCCESS)
1687 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1691 /* list the remaining load option not included in the BootOrder */
1693 var_name16 = malloc(buf_size);
1695 return EFI_OUT_OF_RESOURCES;
1702 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
1703 if (ret == EFI_NOT_FOUND)
1705 if (ret != EFI_SUCCESS)
1708 if (efi_varname_is_load_option(var_name16, &index)) {
1709 /* If the index is included in the BootOrder, skip it */
1710 if (efi_search_bootorder(bootorder, num, index, NULL))
1713 ret = eficonfig_add_boot_selection_entry(efi_menu, index, selected);
1714 if (ret != EFI_SUCCESS)
1718 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1722 ret = eficonfig_append_quit_entry(efi_menu);
1723 if (ret != EFI_SUCCESS)
1726 ret = eficonfig_process_common(efi_menu, " ** Select Boot Option **",
1727 eficonfig_menu_desc,
1728 eficonfig_display_statusline,
1729 eficonfig_print_entry,
1730 eficonfig_choice_entry);
1732 list_for_each_safe(pos, n, &efi_menu->list) {
1733 entry = list_entry(pos, struct eficonfig_entry, list);
1736 eficonfig_destroy(efi_menu);
1744 * eficonfig_process_edit_boot_option() - handler to edit boot option
1746 * @data: pointer to the data for each entry
1747 * Return: status code
1749 static efi_status_t eficonfig_process_edit_boot_option(void *data)
1753 struct eficonfig_boot_option *bo = NULL;
1756 unsigned int selected;
1758 u16 varname[] = u"Boot####";
1760 ret = eficonfig_show_boot_selection(&selected);
1761 if (ret != EFI_SUCCESS)
1764 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1766 ret = EFI_OUT_OF_RESOURCES;
1770 bo->boot_index = selected;
1771 efi_create_indexed_name(varname, sizeof(varname), "Boot", selected);
1772 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1775 ret = EFI_NOT_FOUND;
1779 ret = eficonfig_edit_boot_option(varname, bo, load_option, size,
1780 " ** Edit Boot Option ** ");
1784 if (ret != EFI_SUCCESS && ret != EFI_ABORTED)
1788 /* to stay the parent menu */
1789 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1795 * eficonfig_print_change_boot_order_entry() - print the boot option entry
1797 * @data: pointer to the data associated with each menu entry
1799 static void eficonfig_print_change_boot_order_entry(void *data)
1801 struct eficonfig_entry *entry = data;
1802 bool reverse = (entry->efi_menu->active == entry->num);
1804 if (entry->efi_menu->start > entry->num || entry->efi_menu->end < entry->num)
1807 printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
1808 (entry->num - entry->efi_menu->start) + EFICONFIG_MENU_HEADER_ROW_NUM + 1, 7);
1811 puts(ANSI_COLOR_REVERSE);
1813 if (entry->num < entry->efi_menu->count - 2) {
1814 if (((struct eficonfig_boot_order_data *)entry->data)->active)
1820 printf("%s", entry->title);
1823 puts(ANSI_COLOR_RESET);
1827 * eficonfig_choice_change_boot_order() - user key input handler
1829 * @data: pointer to the menu entry
1830 * Return: key string to identify the selected entry
1832 char *eficonfig_choice_change_boot_order(void *data)
1834 struct cli_ch_state s_cch, *cch = &s_cch;
1835 struct list_head *pos, *n;
1836 struct efimenu *efi_menu = data;
1837 enum bootmenu_key key = BKEY_NONE;
1838 struct eficonfig_entry *entry, *tmp;
1842 key = bootmenu_loop(NULL, cch);
1846 if (efi_menu->active > 0 &&
1847 efi_menu->active < efi_menu->count - 2) {
1848 list_for_each_safe(pos, n, &efi_menu->list) {
1849 entry = list_entry(pos, struct eficonfig_entry, list);
1850 if (entry->num == efi_menu->active)
1853 tmp = list_entry(pos->prev, struct eficonfig_entry, list);
1856 list_del(&tmp->list);
1857 list_add(&tmp->list, &entry->list);
1859 eficonfig_menu_up(efi_menu);
1863 if (efi_menu->active > 0)
1864 eficonfig_menu_up(efi_menu);
1868 if (efi_menu->active < efi_menu->count - 3) {
1869 list_for_each_safe(pos, n, &efi_menu->list) {
1870 entry = list_entry(pos, struct eficonfig_entry, list);
1871 if (entry->num == efi_menu->active)
1874 tmp = list_entry(pos->next, struct eficonfig_entry, list);
1877 list_del(&entry->list);
1878 list_add(&entry->list, &tmp->list);
1880 eficonfig_menu_down(efi_menu);
1884 if (efi_menu->active < efi_menu->count - 1)
1885 eficonfig_menu_down(efi_menu);
1889 /* force to select "Save" entry */
1890 efi_menu->active = efi_menu->count - 2;
1894 if (efi_menu->active == efi_menu->count - 2) {
1895 list_for_each_prev_safe(pos, n, &efi_menu->list) {
1896 entry = list_entry(pos, struct eficonfig_entry, list);
1897 if (entry->num == efi_menu->active)
1903 if (efi_menu->active == efi_menu->count - 1) {
1904 entry = list_last_entry(&efi_menu->list,
1905 struct eficonfig_entry,
1909 /* Pressed key is not valid, wait next key press */
1912 if (efi_menu->active < efi_menu->count - 2) {
1913 list_for_each_safe(pos, n, &efi_menu->list) {
1914 entry = list_entry(pos, struct eficonfig_entry, list);
1915 if (entry->num == efi_menu->active) {
1916 struct eficonfig_boot_order_data *data = entry->data;
1918 data->active = !data->active;
1923 /* Pressed key is not valid, wait next key press */
1926 entry = list_last_entry(&efi_menu->list,
1927 struct eficonfig_entry, list);
1930 /* Pressed key is not valid, wait next key press */
1937 * eficonfig_process_save_boot_order() - callback function for "Save" entry
1939 * @data: pointer to the data
1940 * Return: status code
1942 static efi_status_t eficonfig_process_save_boot_order(void *data)
1947 struct list_head *pos, *n;
1949 struct efimenu *efi_menu;
1950 struct eficonfig_entry *entry;
1951 struct eficonfig_save_boot_order_data *save_data = data;
1953 efi_menu = save_data->efi_menu;
1956 * The change boot order menu always has "Save" and "Quit" entries.
1957 * !(efi_menu->count - 2) means there is no user defined boot option.
1959 if (!(efi_menu->count - 2))
1962 new_bootorder = calloc(1, (efi_menu->count - 2) * sizeof(u16));
1963 if (!new_bootorder) {
1964 ret = EFI_OUT_OF_RESOURCES;
1968 /* create new BootOrder */
1970 list_for_each_safe(pos, n, &efi_menu->list) {
1971 struct eficonfig_boot_order_data *data;
1973 entry = list_entry(pos, struct eficonfig_entry, list);
1974 /* exit the loop when iteration reaches "Save" */
1975 if (!strncmp(entry->title, "Save", strlen("Save")))
1980 new_bootorder[count++] = data->boot_index;
1983 size = count * sizeof(u16);
1984 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1985 EFI_VARIABLE_NON_VOLATILE |
1986 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1987 EFI_VARIABLE_RUNTIME_ACCESS,
1988 size, new_bootorder, false);
1990 save_data->selected = true;
1992 free(new_bootorder);
1998 * eficonfig_add_change_boot_order_entry() - add boot order entry
2000 * @efi_menu: pointer to the efimenu structure
2001 * @boot_index: boot option index to be added
2002 * @active: flag to include the boot option into BootOrder
2003 * Return: status code
2005 static efi_status_t eficonfig_add_change_boot_order_entry(struct efimenu *efi_menu,
2006 u32 boot_index, bool active)
2012 struct efi_load_option lo;
2013 u16 varname[] = u"Boot####";
2014 struct eficonfig_boot_order_data *data;
2016 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
2017 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
2021 ret = efi_deserialize_load_option(&lo, load_option, &size);
2022 if (ret != EFI_SUCCESS)
2025 data = calloc(1, sizeof(*data));
2027 ret = EFI_OUT_OF_RESOURCES;
2031 title = calloc(1, utf16_utf8_strlen(lo.label) + 1);
2034 ret = EFI_OUT_OF_RESOURCES;
2038 utf16_utf8_strcpy(&p, lo.label);
2040 data->boot_index = boot_index;
2041 data->active = active;
2043 ret = eficonfig_append_menu_entry(efi_menu, title, NULL, data);
2044 if (ret != EFI_SUCCESS) {
2057 * eficonfig_create_change_boot_order_entry() - create boot order entry
2059 * @efi_menu: pointer to the efimenu structure
2060 * @bootorder: pointer to the BootOrder variable
2061 * @num: number of BootOrder entry
2062 * Return: status code
2064 static efi_status_t eficonfig_create_change_boot_order_entry(struct efimenu *efi_menu,
2065 u16 *bootorder, efi_uintn_t num)
2070 u16 *var_name16 = NULL;
2071 efi_uintn_t size, buf_size;
2072 struct eficonfig_save_boot_order_data *save_data;
2074 /* list the load option in the order of BootOrder variable */
2075 for (i = 0; i < num; i++) {
2076 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2079 ret = eficonfig_add_change_boot_order_entry(efi_menu, bootorder[i], true);
2080 if (ret != EFI_SUCCESS)
2084 /* list the remaining load option not included in the BootOrder */
2086 var_name16 = malloc(buf_size);
2088 return EFI_OUT_OF_RESOURCES;
2095 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2099 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
2100 if (ret == EFI_NOT_FOUND)
2102 if (ret != EFI_SUCCESS)
2105 if (efi_varname_is_load_option(var_name16, &index)) {
2106 /* If the index is included in the BootOrder, skip it */
2107 if (efi_search_bootorder(bootorder, num, index, NULL))
2110 ret = eficonfig_add_change_boot_order_entry(efi_menu, index, false);
2111 if (ret != EFI_SUCCESS)
2116 /* add "Save" and "Quit" entries */
2117 title = strdup("Save");
2119 ret = EFI_OUT_OF_RESOURCES;
2123 save_data = malloc(sizeof(struct eficonfig_save_boot_order_data));
2125 ret = EFI_OUT_OF_RESOURCES;
2128 save_data->efi_menu = efi_menu;
2129 save_data->selected = false;
2131 ret = eficonfig_append_menu_entry(efi_menu, title,
2132 eficonfig_process_save_boot_order,
2134 if (ret != EFI_SUCCESS)
2137 ret = eficonfig_append_quit_entry(efi_menu);
2138 if (ret != EFI_SUCCESS)
2141 efi_menu->active = 0;
2149 * eficonfig_process_change_boot_order() - handler to change boot order
2151 * @data: pointer to the data for each entry
2152 * Return: status code
2154 static efi_status_t eficonfig_process_change_boot_order(void *data)
2158 efi_uintn_t num, size;
2159 struct list_head *pos, *n;
2160 struct eficonfig_entry *entry;
2161 struct efimenu *efi_menu;
2163 efi_menu = calloc(1, sizeof(struct efimenu));
2165 return EFI_OUT_OF_RESOURCES;
2167 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2169 INIT_LIST_HEAD(&efi_menu->list);
2170 num = size / sizeof(u16);
2171 ret = eficonfig_create_change_boot_order_entry(efi_menu, bootorder, num);
2172 if (ret != EFI_SUCCESS)
2176 ret = eficonfig_process_common(efi_menu,
2177 " ** Change Boot Order **",
2178 eficonfig_change_boot_order_desc,
2179 eficonfig_display_statusline,
2180 eficonfig_print_change_boot_order_entry,
2181 eficonfig_choice_change_boot_order);
2182 /* exit from the menu if user selects the "Save" entry. */
2183 if (ret == EFI_SUCCESS && efi_menu->active == (efi_menu->count - 2)) {
2184 list_for_each_prev_safe(pos, n, &efi_menu->list) {
2185 entry = list_entry(pos, struct eficonfig_entry, list);
2186 if (entry->num == efi_menu->active)
2189 if (((struct eficonfig_save_boot_order_data *)entry->data)->selected)
2192 if (ret != EFI_SUCCESS)
2197 list_for_each_safe(pos, n, &efi_menu->list) {
2198 entry = list_entry(pos, struct eficonfig_entry, list);
2201 eficonfig_destroy(efi_menu);
2203 /* to stay the parent menu */
2204 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2210 * eficonfig_process_delete_boot_option() - handler to delete boot option
2212 * @data: pointer to the data for each entry
2213 * Return: status code
2215 static efi_status_t eficonfig_process_delete_boot_option(void *data)
2218 unsigned int selected;
2221 ret = eficonfig_show_boot_selection(&selected);
2222 if (ret == EFI_SUCCESS)
2223 ret = efi_bootmgr_delete_boot_option(selected);
2225 if (ret != EFI_SUCCESS)
2229 /* to stay the parent menu */
2230 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2236 * eficonfig_init() - do required initialization for eficonfig command
2238 * Return: status code
2240 static efi_status_t eficonfig_init(void)
2242 efi_status_t ret = EFI_SUCCESS;
2244 struct efi_handler *handler;
2245 unsigned long columns, rows;
2248 ret = efi_search_protocol(efi_root, &efi_guid_text_input_protocol, &handler);
2249 if (ret != EFI_SUCCESS)
2252 ret = efi_protocol_open(handler, (void **)&cin, efi_root, NULL,
2253 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2254 if (ret != EFI_SUCCESS)
2256 ret = efi_search_protocol(efi_root, &efi_guid_text_output_protocol, &handler);
2257 if (ret != EFI_SUCCESS)
2260 ret = efi_protocol_open(handler, (void **)&cout, efi_root, NULL,
2261 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2262 if (ret != EFI_SUCCESS)
2265 cout->query_mode(cout, cout->mode->mode, &columns, &rows);
2266 avail_row = rows - (EFICONFIG_MENU_HEADER_ROW_NUM +
2267 EFICONFIG_MENU_DESC_ROW_NUM);
2268 if (avail_row <= 0) {
2269 eficonfig_print_msg("Console size is too small!");
2270 return EFI_INVALID_PARAMETER;
2272 /* TODO: Should we check the minimum column size? */
2280 static const struct eficonfig_item maintenance_menu_items[] = {
2281 {"Add Boot Option", eficonfig_process_add_boot_option},
2282 {"Edit Boot Option", eficonfig_process_edit_boot_option},
2283 {"Change Boot Order", eficonfig_process_change_boot_order},
2284 {"Delete Boot Option", eficonfig_process_delete_boot_option},
2285 #if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT) && IS_ENABLED(CONFIG_EFI_MM_COMM_TEE))
2286 {"Secure Boot Configuration", eficonfig_process_secure_boot_config},
2288 {"Quit", eficonfig_process_quit},
2292 * do_eficonfig() - execute `eficonfig` command
2294 * @cmdtp: table entry describing command
2295 * @flag: bitmap indicating how the command was invoked
2296 * @argc: number of arguments
2297 * @argv: command line arguments
2298 * Return: status code
2300 static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
2303 struct efimenu *efi_menu;
2306 return CMD_RET_USAGE;
2308 ret = efi_init_obj_list();
2309 if (ret != EFI_SUCCESS) {
2310 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
2311 ret & ~EFI_ERROR_MASK);
2313 return CMD_RET_FAILURE;
2316 ret = eficonfig_init();
2317 if (ret != EFI_SUCCESS)
2318 return CMD_RET_FAILURE;
2320 ret = efi_bootmgr_update_media_device_boot_option();
2321 if (ret != EFI_SUCCESS)
2325 efi_menu = eficonfig_create_fixed_menu(maintenance_menu_items,
2326 ARRAY_SIZE(maintenance_menu_items));
2328 return CMD_RET_FAILURE;
2330 ret = eficonfig_process_common(efi_menu,
2331 " ** UEFI Maintenance Menu **",
2332 eficonfig_menu_desc,
2333 eficonfig_display_statusline,
2334 eficonfig_print_entry,
2335 eficonfig_choice_entry);
2336 eficonfig_destroy(efi_menu);
2338 if (ret == EFI_ABORTED)
2342 return CMD_RET_SUCCESS;
2346 eficonfig, 1, 0, do_eficonfig,
2347 "provide menu-driven UEFI variable maintenance interface",