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/CTRL+C 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 " Select [Save] to complete, ESC/CTRL+C to quit";
33 #define EFICONFIG_DESCRIPTION_MAX 32
34 #define EFICONFIG_OPTIONAL_DATA_MAX 64
37 * struct eficonfig_filepath_info - structure to be used to store file path
39 * @name: file or directory name
40 * @list: list structure
42 struct eficonfig_filepath_info {
44 struct list_head list;
48 * struct eficonfig_boot_option - structure to be used for updating UEFI boot option
50 * @file_info: user selected file info
51 * @initrd_info: user selected initrd file info
52 * @boot_index: index of the boot option
53 * @description: pointer to the description string
54 * @optional_data: pointer to the optional_data
55 * @edit_completed: flag indicates edit complete
57 struct eficonfig_boot_option {
58 struct eficonfig_select_file_info file_info;
59 struct eficonfig_select_file_info initrd_info;
60 unsigned int boot_index;
67 * struct eficonfig_volume_entry_data - structure to be used to store volume info
69 * @file_info: pointer to file info structure
70 * @v: pointer to the protocol interface
71 * @dp: pointer to the device path
73 struct eficonfig_volume_entry_data {
74 struct eficonfig_select_file_info *file_info;
75 struct efi_simple_file_system_protocol *v;
76 struct efi_device_path *dp;
80 * struct eficonfig_file_entry_data - structure to be used to store file info
82 * @file_info: pointer to file info structure
83 * @is_directory: flag to identify the directory or file
84 * @file_name: name of directory or file
86 struct eficonfig_file_entry_data {
87 struct eficonfig_select_file_info *file_info;
93 * struct eficonfig_boot_selection_data - structure to be used to select the boot option entry
95 * @boot_index: index of the boot option
96 * @selected: pointer to store the selected index in the BootOrder variable
98 struct eficonfig_boot_selection_data {
104 * struct eficonfig_boot_order_data - structure to be used to update BootOrder variable
106 * @boot_index: boot option index
107 * @active: flag to include the boot option into BootOrder variable
109 struct eficonfig_boot_order_data {
115 * struct eficonfig_save_boot_order_data - structure to be used to change boot order
117 * @efi_menu: pointer to efimenu structure
118 * @selected: flag to indicate user selects "Save" entry
120 struct eficonfig_save_boot_order_data {
121 struct efimenu *efi_menu;
126 * eficonfig_print_msg() - print message
128 * display the message to the user, user proceeds the screen
129 * with any key press.
131 * @items: pointer to the structure of each menu entry
132 * @count: the number of menu entry
133 * @menu_header: pointer to the menu header string
134 * Return: status code
136 void eficonfig_print_msg(char *msg)
142 printf(ANSI_CURSOR_HIDE
145 "%s\n\n Press any key to continue", 3, 4, msg);
151 * eficonfig_print_entry() - print each menu entry
153 * @data: pointer to the data associated with each menu entry
155 void eficonfig_print_entry(void *data)
157 struct eficonfig_entry *entry = data;
158 bool reverse = (entry->efi_menu->active == entry->num);
160 /* TODO: support scroll or page for many entries */
163 * Move cursor to line where the entry will be drawn (entry->num)
164 * First 3 lines(menu header) + 1 empty line
166 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
169 puts(ANSI_COLOR_REVERSE);
171 printf("%s", entry->title);
174 puts(ANSI_COLOR_RESET);
178 * eficonfig_display_statusline() - print status line
180 * @m: pointer to the menu structure
182 void eficonfig_display_statusline(struct menu *m)
184 struct eficonfig_entry *entry;
186 if (menu_default_choice(m, (void *)&entry) < 0)
189 printf(ANSI_CURSOR_POSITION
191 ANSI_CURSOR_POSITION ANSI_CLEAR_LINE ANSI_CURSOR_POSITION
193 ANSI_CLEAR_LINE_TO_END,
194 1, 1, entry->efi_menu->menu_header, entry->efi_menu->count + 5, 1,
195 entry->efi_menu->count + 6, 1, entry->efi_menu->menu_desc);
199 * eficonfig_choice_entry() - user key input handler
201 * @data: pointer to the efimenu structure
202 * Return: key string to identify the selected entry
204 char *eficonfig_choice_entry(void *data)
206 struct cli_ch_state s_cch, *cch = &s_cch;
207 struct list_head *pos, *n;
208 struct eficonfig_entry *entry;
209 enum bootmenu_key key = BKEY_NONE;
210 struct efimenu *efi_menu = data;
215 key = bootmenu_loop((struct bootmenu_data *)efi_menu, cch);
219 if (efi_menu->active > 0)
221 /* no menu key selected, regenerate menu */
224 if (efi_menu->active < efi_menu->count - 1)
226 /* no menu key selected, regenerate menu */
229 list_for_each_safe(pos, n, &efi_menu->list) {
230 entry = list_entry(pos, struct eficonfig_entry, list);
231 if (entry->num == efi_menu->active)
236 /* Quit by choosing the last entry */
237 entry = list_last_entry(&efi_menu->list, struct eficonfig_entry, list);
240 /* Pressed key is not valid, no need to regenerate the menu */
247 * eficonfig_destroy() - destroy efimenu
249 * @efi_menu: pointer to the efimenu structure
251 void eficonfig_destroy(struct efimenu *efi_menu)
253 struct list_head *pos, *n;
254 struct eficonfig_entry *entry;
259 list_for_each_safe(pos, n, &efi_menu->list) {
260 entry = list_entry(pos, struct eficonfig_entry, list);
262 list_del(&entry->list);
265 free(efi_menu->menu_header);
270 * eficonfig_process_quit() - callback function for "Quit" entry
272 * @data: pointer to the data
273 * Return: status code
275 efi_status_t eficonfig_process_quit(void *data)
281 * eficonfig_append_menu_entry() - append menu item
283 * @efi_menu: pointer to the efimenu structure
284 * @title: pointer to the entry title
285 * @func: callback of each entry
286 * @data: pointer to the data to be passed to each entry callback
287 * Return: status code
289 efi_status_t eficonfig_append_menu_entry(struct efimenu *efi_menu,
290 char *title, eficonfig_entry_func func,
293 struct eficonfig_entry *entry;
295 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX)
296 return EFI_OUT_OF_RESOURCES;
298 entry = calloc(1, sizeof(struct eficonfig_entry));
300 return EFI_OUT_OF_RESOURCES;
302 entry->title = title;
303 sprintf(entry->key, "%d", efi_menu->count);
304 entry->efi_menu = efi_menu;
307 entry->num = efi_menu->count++;
308 list_add_tail(&entry->list, &efi_menu->list);
314 * eficonfig_append_quit_entry() - append quit entry
316 * @efi_menu: pointer to the efimenu structure
317 * Return: status code
319 efi_status_t eficonfig_append_quit_entry(struct efimenu *efi_menu)
324 title = strdup("Quit");
326 return EFI_OUT_OF_RESOURCES;
328 ret = eficonfig_append_menu_entry(efi_menu, title, eficonfig_process_quit, NULL);
329 if (ret != EFI_SUCCESS)
336 * eficonfig_create_fixed_menu() - create fixed entry menu structure
338 * @items: pointer to the menu entry item
339 * @count: the number of menu entry
340 * Return: pointer to the efimenu structure
342 void *eficonfig_create_fixed_menu(const struct eficonfig_item *items, int count)
347 struct efimenu *efi_menu;
348 const struct eficonfig_item *iter = items;
350 efi_menu = calloc(1, sizeof(struct efimenu));
354 INIT_LIST_HEAD(&efi_menu->list);
355 for (i = 0; i < count; i++, iter++) {
356 title = strdup(iter->title);
360 ret = eficonfig_append_menu_entry(efi_menu, title, iter->func, iter->data);
361 if (ret != EFI_SUCCESS) {
369 eficonfig_destroy(efi_menu);
375 * eficonfig_process_common() - main handler for UEFI menu
377 * Construct the structures required to show the menu, then handle
378 * the user input interacting with u-boot menu functions.
380 * @efi_menu: pointer to the efimenu structure
381 * @menu_header: pointer to the menu header string
382 * @menu_desc: pointer to the menu description
383 * @display_statusline: function pointer to draw statusline
384 * @item_data_print: function pointer to draw the menu item
385 * @item_choice: function pointer to handle the key press
386 * Return: status code
388 efi_status_t eficonfig_process_common(struct efimenu *efi_menu,
389 char *menu_header, const char *menu_desc,
390 void (*display_statusline)(struct menu *),
391 void (*item_data_print)(void *),
392 char *(*item_choice)(void *))
396 struct list_head *pos, *n;
397 struct eficonfig_entry *entry;
398 efi_status_t ret = EFI_SUCCESS;
400 if (efi_menu->count > EFICONFIG_ENTRY_NUM_MAX)
401 return EFI_OUT_OF_RESOURCES;
403 efi_menu->delay = -1;
404 efi_menu->active = 0;
407 efi_menu->menu_header = strdup(menu_header);
408 if (!efi_menu->menu_header)
409 return EFI_OUT_OF_RESOURCES;
412 efi_menu->menu_desc = menu_desc;
414 menu = menu_create(NULL, 0, 1, display_statusline, item_data_print,
415 item_choice, efi_menu);
417 return EFI_INVALID_PARAMETER;
419 list_for_each_safe(pos, n, &efi_menu->list) {
420 entry = list_entry(pos, struct eficonfig_entry, list);
421 if (!menu_item_add(menu, entry->key, entry)) {
422 ret = EFI_INVALID_PARAMETER;
427 entry = list_first_entry_or_null(&efi_menu->list, struct eficonfig_entry, list);
429 menu_default_set(menu, entry->key);
431 printf(ANSI_CURSOR_HIDE
433 ANSI_CURSOR_POSITION, 1, 1);
435 if (menu_get_choice(menu, &choice)) {
438 ret = entry->func(entry->data);
443 printf(ANSI_CLEAR_CONSOLE
445 ANSI_CURSOR_SHOW, 1, 1);
451 * eficonfig_volume_selected() - handler of volume selection
453 * @data: pointer to the data of selected entry
454 * Return: status code
456 static efi_status_t eficonfig_volume_selected(void *data)
458 struct eficonfig_volume_entry_data *info = data;
461 info->file_info->current_volume = info->v;
462 info->file_info->dp_volume = info->dp;
469 * eficonfig_create_device_path() - create device path
471 * @dp_volume: pointer to the volume
472 * @current_path: pointer to the file path u16 string
474 * device path or NULL. Caller must free the returned value
476 struct efi_device_path *eficonfig_create_device_path(struct efi_device_path *dp_volume,
482 struct efi_device_path *dp;
483 struct efi_device_path_file_path *fp;
485 fp_size = sizeof(struct efi_device_path) + u16_strsize(current_path);
486 buf = calloc(1, fp_size + sizeof(END));
491 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
492 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
493 fp->dp.length = (u16)fp_size;
494 u16_strcpy(fp->str, current_path);
498 *((struct efi_device_path *)p) = END;
500 dp = efi_dp_append(dp_volume, (struct efi_device_path *)buf);
507 * eficonfig_file_selected() - handler of file selection
509 * @data: pointer to the data of selected entry
510 * Return: status code
512 static efi_status_t eficonfig_file_selected(void *data)
515 struct eficonfig_file_entry_data *info = data;
518 return EFI_INVALID_PARAMETER;
520 if (!strcmp(info->file_name, "..\\")) {
521 struct eficonfig_filepath_info *iter;
522 struct list_head *pos, *n;
525 tmp = info->file_info->current_path;
527 memset(info->file_info->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
528 filepath = calloc(1, EFICONFIG_FILE_PATH_MAX);
530 return EFI_OUT_OF_RESOURCES;
532 list_for_each_safe(pos, n, &info->file_info->filepath_list) {
533 iter = list_entry(pos, struct eficonfig_filepath_info, list);
535 is_last = list_is_last(&iter->list, &info->file_info->filepath_list);
537 list_del(&iter->list);
542 strlcat(filepath, iter->name, EFICONFIG_FILE_PATH_MAX);
544 utf8_utf16_strcpy(&tmp, filepath);
547 struct eficonfig_filepath_info *filepath_info;
549 new_len = u16_strlen(info->file_info->current_path) +
550 strlen(info->file_name);
551 if (new_len >= EFICONFIG_FILE_PATH_MAX) {
552 eficonfig_print_msg("File path is too long!");
553 return EFI_INVALID_PARAMETER;
555 tmp = &info->file_info->current_path[u16_strlen(info->file_info->current_path)];
556 utf8_utf16_strcpy(&tmp, info->file_name);
558 filepath_info = calloc(1, sizeof(struct eficonfig_filepath_info));
560 return EFI_OUT_OF_RESOURCES;
562 filepath_info->name = strdup(info->file_name);
563 if (!filepath_info->name) {
565 return EFI_OUT_OF_RESOURCES;
567 list_add_tail(&filepath_info->list, &info->file_info->filepath_list);
569 if (!info->is_directory)
570 info->file_info->file_selected = true;
577 * eficonfig_select_volume() - construct the volume selection menu
579 * @file_info: pointer to the file selection structure
580 * Return: status code
582 static efi_status_t eficonfig_select_volume(struct eficonfig_select_file_info *file_info)
587 struct efimenu *efi_menu;
588 struct list_head *pos, *n;
589 struct efi_handler *handler;
590 struct eficonfig_entry *entry;
591 struct efi_device_path *device_path;
592 efi_handle_t *volume_handles = NULL;
593 struct efi_simple_file_system_protocol *v;
595 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
596 NULL, &count, (efi_handle_t **)&volume_handles);
597 if (ret != EFI_SUCCESS) {
598 eficonfig_print_msg("No block device found!");
602 efi_menu = calloc(1, sizeof(struct efimenu));
604 return EFI_OUT_OF_RESOURCES;
606 INIT_LIST_HEAD(&efi_menu->list);
607 for (i = 0; i < count; i++) {
609 struct efi_block_io *block_io;
610 struct eficonfig_volume_entry_data *info;
612 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
615 ret = efi_search_protocol(volume_handles[i],
616 &efi_simple_file_system_protocol_guid, &handler);
617 if (ret != EFI_SUCCESS)
619 ret = efi_protocol_open(handler, (void **)&v, efi_root, NULL,
620 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
621 if (ret != EFI_SUCCESS)
624 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
625 if (ret != EFI_SUCCESS)
627 ret = efi_protocol_open(handler, (void **)&device_path,
628 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
629 if (ret != EFI_SUCCESS)
632 ret = efi_search_protocol(volume_handles[i], &efi_block_io_guid, &handler);
633 if (ret != EFI_SUCCESS)
635 ret = efi_protocol_open(handler, (void **)&block_io,
636 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
637 if (ret != EFI_SUCCESS)
640 info = calloc(1, sizeof(struct eficonfig_volume_entry_data));
642 ret = EFI_OUT_OF_RESOURCES;
646 devname = calloc(1, BOOTMENU_DEVICE_NAME_MAX);
649 ret = EFI_OUT_OF_RESOURCES;
652 ret = efi_disk_get_device_name(volume_handles[i], devname,
653 BOOTMENU_DEVICE_NAME_MAX);
654 if (ret != EFI_SUCCESS) {
660 info->dp = device_path;
661 info->file_info = file_info;
662 ret = eficonfig_append_menu_entry(efi_menu, devname, eficonfig_volume_selected,
664 if (ret != EFI_SUCCESS) {
670 ret = eficonfig_append_quit_entry(efi_menu);
671 if (ret != EFI_SUCCESS)
674 ret = eficonfig_process_common(efi_menu, " ** Select Volume **",
676 eficonfig_display_statusline,
677 eficonfig_print_entry,
678 eficonfig_choice_entry);
681 efi_free_pool(volume_handles);
682 list_for_each_safe(pos, n, &efi_menu->list) {
683 entry = list_entry(pos, struct eficonfig_entry, list);
686 eficonfig_destroy(efi_menu);
692 * sort_file() - sort the file name in ascii order
694 * @data1: pointer to the file entry data
695 * @data2: pointer to the file entry data
696 * Return: -1 if the data1 file name is less than data2 file name,
697 * 0 if both file name match,
698 * 1 if the data1 file name is greater thant data2 file name.
700 static int sort_file(const void *arg1, const void *arg2)
702 const struct eficonfig_file_entry_data *data1, *data2;
704 data1 = *((const struct eficonfig_file_entry_data **)arg1);
705 data2 = *((const struct eficonfig_file_entry_data **)arg2);
707 return strcasecmp(data1->file_name, data2->file_name);
711 * eficonfig_create_file_entry() - construct the file menu entry
713 * @efi_menu: pointer to the efimenu structure
714 * @count: number of the directory and file
715 * @tmp_infos: pointer to the entry data array
716 * @f: pointer to the file handle
717 * @buf: pointer to the buffer to store the directory information
718 * @file_info: pointer to the file selection structure
719 * Return: status code
722 eficonfig_create_file_entry(struct efimenu *efi_menu, u32 count,
723 struct eficonfig_file_entry_data **tmp_infos,
724 struct efi_file_handle *f, struct efi_file_info *buf,
725 struct eficonfig_select_file_info *file_info)
730 u32 i, entry_num = 0;
731 struct eficonfig_file_entry_data *info;
733 EFI_CALL(f->setpos(f, 0));
734 /* Read directory and construct menu structure */
735 for (i = 0; i < count; i++) {
736 if (entry_num >= EFICONFIG_ENTRY_NUM_MAX - 1)
739 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
740 ret = EFI_CALL(f->read(f, &len, buf));
741 if (ret != EFI_SUCCESS || len == 0)
744 info = calloc(1, sizeof(struct eficonfig_file_entry_data));
746 ret = EFI_OUT_OF_RESOURCES;
750 /* append '\\' at the end of directory name */
751 name = calloc(1, utf16_utf8_strlen(buf->file_name) + 2);
753 ret = EFI_OUT_OF_RESOURCES;
758 utf16_utf8_strcpy(&p, buf->file_name);
759 if (buf->attribute & EFI_FILE_DIRECTORY) {
760 /* filter out u'.' */
761 if (!u16_strcmp(buf->file_name, u".")) {
766 name[u16_strlen(buf->file_name)] = '\\';
767 info->is_directory = true;
770 info->file_name = name;
771 info->file_info = file_info;
772 tmp_infos[entry_num++] = info;
775 qsort(tmp_infos, entry_num, sizeof(*tmp_infos),
776 (int (*)(const void *, const void *))sort_file);
778 for (i = 0; i < entry_num; i++) {
779 ret = eficonfig_append_menu_entry(efi_menu, tmp_infos[i]->file_name,
780 eficonfig_file_selected, tmp_infos[i]);
781 if (ret != EFI_SUCCESS)
790 * eficonfig_show_file_selection() - construct the file selection menu
792 * @file_info: pointer to the file selection structure
793 * @root: pointer to the file handle
794 * Return: status code
796 static efi_status_t eficonfig_show_file_selection(struct eficonfig_select_file_info *file_info,
797 struct efi_file_handle *root)
802 struct efimenu *efi_menu;
803 struct efi_file_handle *f;
804 struct efi_file_info *buf;
805 struct eficonfig_file_entry_data **tmp_infos;
807 buf = calloc(1, sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE);
809 return EFI_OUT_OF_RESOURCES;
811 while (!file_info->file_selected) {
812 efi_menu = calloc(1, sizeof(struct efimenu));
814 ret = EFI_OUT_OF_RESOURCES;
817 INIT_LIST_HEAD(&efi_menu->list);
819 ret = EFI_CALL(root->open(root, &f, file_info->current_path,
820 EFI_FILE_MODE_READ, 0));
821 if (ret != EFI_SUCCESS) {
822 eficonfig_print_msg("Reading volume failed!");
828 /* Count the number of directory entries */
830 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
831 ret = EFI_CALL(f->read(f, &len, buf));
832 if (ret != EFI_SUCCESS || len == 0)
838 /* allocate array to sort the entry */
839 tmp_infos = calloc(count, sizeof(*tmp_infos));
841 ret = EFI_OUT_OF_RESOURCES;
845 ret = eficonfig_create_file_entry(efi_menu, count, tmp_infos,
847 if (ret != EFI_SUCCESS)
850 ret = eficonfig_append_quit_entry(efi_menu);
851 if (ret != EFI_SUCCESS)
854 ret = eficonfig_process_common(efi_menu, " ** Select File **",
856 eficonfig_display_statusline,
857 eficonfig_print_entry,
858 eficonfig_choice_entry);
860 EFI_CALL(f->close(f));
861 eficonfig_destroy(efi_menu);
864 for (i = 0; i < count; i++)
870 if (ret != EFI_SUCCESS)
881 * handle_user_input() - handle user input
883 * @buf: pointer to the buffer
884 * @buf_size: size of the buffer
885 * @cursor_col: cursor column for user input
886 * @msg: pointer to the string to display
887 * Return: status code
889 static efi_status_t handle_user_input(u16 *buf, int buf_size,
890 int cursor_col, char *msg)
895 printf(ANSI_CLEAR_CONSOLE
899 " Press ENTER to complete, ESC/CTRL+C to quit",
902 /* tmp is used to accept user cancel */
903 tmp = calloc(1, buf_size * sizeof(u16));
905 return EFI_OUT_OF_RESOURCES;
907 ret = efi_console_get_u16_string(cin, tmp, buf_size, NULL, 4, cursor_col);
908 if (ret == EFI_SUCCESS)
909 u16_strcpy(buf, tmp);
913 /* to stay the parent menu */
914 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
920 * eficonfig_boot_add_enter_description() - handle user input for description
922 * @data: pointer to the internal boot option structure
923 * Return: status code
925 static efi_status_t eficonfig_boot_add_enter_description(void *data)
927 struct eficonfig_boot_option *bo = data;
929 return handle_user_input(bo->description, EFICONFIG_DESCRIPTION_MAX, 22,
930 "\n ** Edit Description **\n"
932 " enter description: ");
936 * eficonfig_boot_add_optional_data() - handle user input for optional data
938 * @data: pointer to the internal boot option structure
939 * Return: status code
941 static efi_status_t eficonfig_boot_add_optional_data(void *data)
943 struct eficonfig_boot_option *bo = data;
945 return handle_user_input(bo->optional_data, EFICONFIG_OPTIONAL_DATA_MAX, 24,
946 "\n ** Edit Optional Data **\n"
948 " enter optional data:");
952 * eficonfig_boot_edit_save() - handler to save the boot option
954 * @data: pointer to the internal boot option structure
955 * Return: status code
957 static efi_status_t eficonfig_boot_edit_save(void *data)
959 struct eficonfig_boot_option *bo = data;
961 if (u16_strlen(bo->description) == 0) {
962 eficonfig_print_msg("Boot Description is empty!");
963 bo->edit_completed = false;
964 return EFI_NOT_READY;
966 if (u16_strlen(bo->file_info.current_path) == 0) {
967 eficonfig_print_msg("File is not selected!");
968 bo->edit_completed = false;
969 return EFI_NOT_READY;
972 bo->edit_completed = true;
978 * eficonfig_process_clear_file_selection() - callback function for "Clear" entry
980 * @data: pointer to the data
981 * Return: status code
983 efi_status_t eficonfig_process_clear_file_selection(void *data)
985 struct eficonfig_select_file_info *file_info = data;
987 /* clear the existing file information */
988 file_info->current_volume = NULL;
989 file_info->current_path[0] = u'\0';
990 file_info->dp_volume = NULL;
995 static struct eficonfig_item select_file_menu_items[] = {
996 {"Select File", eficonfig_process_select_file},
997 {"Clear", eficonfig_process_clear_file_selection},
998 {"Quit", eficonfig_process_quit},
1002 * eficonfig_process_show_file_option() - display select file option
1004 * @file_info: pointer to the file information structure
1005 * Return: status code
1007 efi_status_t eficonfig_process_show_file_option(void *data)
1010 struct efimenu *efi_menu;
1012 select_file_menu_items[0].data = data;
1013 select_file_menu_items[1].data = data;
1014 efi_menu = eficonfig_create_fixed_menu(select_file_menu_items,
1015 ARRAY_SIZE(select_file_menu_items));
1017 return EFI_OUT_OF_RESOURCES;
1019 ret = eficonfig_process_common(efi_menu, " ** Update File **",
1020 eficonfig_menu_desc,
1021 eficonfig_display_statusline,
1022 eficonfig_print_entry,
1023 eficonfig_choice_entry);
1024 if (ret != EFI_SUCCESS) /* User selects "Clear" or "Quit" */
1025 ret = EFI_NOT_READY;
1027 eficonfig_destroy(efi_menu);
1033 * eficonfig_process_select_file() - handle user file selection
1035 * @data: pointer to the data
1036 * Return: status code
1038 efi_status_t eficonfig_process_select_file(void *data)
1042 struct list_head *pos, *n;
1043 struct efi_file_handle *root;
1044 struct eficonfig_filepath_info *item;
1045 struct eficonfig_select_file_info *tmp = NULL;
1046 struct eficonfig_select_file_info *file_info = data;
1048 tmp = calloc(1, sizeof(struct eficonfig_select_file_info));
1050 return EFI_OUT_OF_RESOURCES;
1052 tmp->current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1053 if (!tmp->current_path) {
1055 return EFI_OUT_OF_RESOURCES;
1057 INIT_LIST_HEAD(&tmp->filepath_list);
1059 while (!tmp->file_selected) {
1060 tmp->current_volume = NULL;
1061 memset(tmp->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
1063 ret = eficonfig_select_volume(tmp);
1064 if (ret != EFI_SUCCESS)
1067 if (!tmp->current_volume)
1068 return EFI_INVALID_PARAMETER;
1070 ret = EFI_CALL(tmp->current_volume->open_volume(tmp->current_volume, &root));
1071 if (ret != EFI_SUCCESS)
1074 ret = eficonfig_show_file_selection(tmp, root);
1075 if (ret == EFI_ABORTED)
1077 if (ret != EFI_SUCCESS)
1082 if (ret == EFI_SUCCESS) {
1083 len = u16_strlen(tmp->current_path);
1084 len = (len >= EFICONFIG_FILE_PATH_MAX) ? (EFICONFIG_FILE_PATH_MAX - 1) : len;
1085 memcpy(file_info->current_path, tmp->current_path, len * sizeof(u16));
1086 file_info->current_path[len] = u'\0';
1087 file_info->current_volume = tmp->current_volume;
1088 file_info->dp_volume = tmp->dp_volume;
1091 list_for_each_safe(pos, n, &tmp->filepath_list) {
1092 item = list_entry(pos, struct eficonfig_filepath_info, list);
1093 list_del(&item->list);
1097 free(tmp->current_path);
1100 /* to stay the parent menu */
1101 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1107 * eficonfig_get_unused_bootoption() - get unused "Boot####" index
1109 * @buf: pointer to the buffer to store boot option variable name
1110 * @buf_size: buffer size
1111 * @index: pointer to store the index in the BootOrder variable
1112 * Return: status code
1114 efi_status_t eficonfig_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
1115 unsigned int *index)
1121 if (buf_size < u16_strsize(u"Boot####"))
1122 return EFI_BUFFER_TOO_SMALL;
1124 for (i = 0; i <= 0xFFFF; i++) {
1126 efi_create_indexed_name(buf, buf_size, "Boot", i);
1127 ret = efi_get_variable_int(buf, &efi_global_variable_guid,
1128 NULL, &size, NULL, NULL);
1129 if (ret == EFI_BUFFER_TOO_SMALL)
1136 return EFI_OUT_OF_RESOURCES;
1144 * eficonfig_set_boot_option() - set boot option
1146 * @varname: pointer to variable name
1147 * @dp: pointer to device path
1148 * @label: pointer to label string
1149 * @optional_data: pointer to optional data
1150 * Return: status code
1152 static efi_status_t eficonfig_set_boot_option(u16 *varname, struct efi_device_path *dp,
1153 efi_uintn_t dp_size, u16 *label, char *optional_data)
1158 struct efi_load_option lo;
1161 lo.file_path_length = dp_size;
1162 lo.attributes = LOAD_OPTION_ACTIVE;
1163 lo.optional_data = optional_data;
1166 size = efi_serialize_load_option(&lo, (u8 **)&p);
1168 return EFI_INVALID_PARAMETER;
1170 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1171 EFI_VARIABLE_NON_VOLATILE |
1172 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1173 EFI_VARIABLE_RUNTIME_ACCESS,
1181 * eficonfig_append_bootorder() - append new boot option in BootOrder variable
1183 * @index: "Boot####" index to append to BootOrder variable
1184 * Return: status code
1186 efi_status_t eficonfig_append_bootorder(u16 index)
1190 u16 *new_bootorder = NULL;
1191 efi_uintn_t last, size, new_size;
1193 /* append new boot option */
1194 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1195 last = size / sizeof(u16);
1196 new_size = size + sizeof(u16);
1197 new_bootorder = calloc(1, new_size);
1198 if (!new_bootorder) {
1199 ret = EFI_OUT_OF_RESOURCES;
1202 memcpy(new_bootorder, bootorder, size);
1203 new_bootorder[last] = index;
1205 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1206 EFI_VARIABLE_NON_VOLATILE |
1207 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1208 EFI_VARIABLE_RUNTIME_ACCESS,
1209 new_size, new_bootorder, false);
1210 if (ret != EFI_SUCCESS)
1215 free(new_bootorder);
1221 * create_boot_option_entry() - create boot option entry
1223 * @efi_menu: pointer to the efimenu structure
1224 * @title: pointer to the entry title
1225 * @val: pointer to boot option label
1226 * @func: callback of each entry
1227 * @data: pointer to the data to be passed to each entry callback
1228 * Return: status code
1230 static efi_status_t create_boot_option_entry(struct efimenu *efi_menu, char *title, u16 *val,
1231 eficonfig_entry_func func, void *data)
1236 len = strlen(title) + 1;
1238 len += utf16_utf8_strlen(val);
1239 buf = calloc(1, len);
1241 return EFI_OUT_OF_RESOURCES;
1245 p = buf + strlen(title);
1246 utf16_utf8_strcpy(&p, val);
1249 return eficonfig_append_menu_entry(efi_menu, buf, func, data);
1253 * prepare_file_selection_entry() - prepare file selection entry
1255 * @efi_menu: pointer to the efimenu structure
1256 * @title: pointer to the title string
1257 * @file_info: pointer to the file info
1258 * Return: status code
1260 static efi_status_t prepare_file_selection_entry(struct efimenu *efi_menu, char *title,
1261 struct eficonfig_select_file_info *file_info)
1265 u16 *file_name = NULL, *p;
1266 efi_handle_t handle;
1269 devname = calloc(1, EFICONFIG_VOLUME_PATH_MAX + 1);
1271 return EFI_OUT_OF_RESOURCES;
1273 /* get the device name only when the user already selected the file path */
1274 handle = efi_dp_find_obj(file_info->dp_volume, NULL, NULL);
1276 ret = efi_disk_get_device_name(handle, devname, EFICONFIG_VOLUME_PATH_MAX);
1277 if (ret != EFI_SUCCESS)
1282 * If the preconfigured volume does not exist in the system, display the text
1283 * converted volume device path instead of U-Boot friendly name(e.g. "usb 0:1").
1285 if (!handle && file_info->dp_volume) {
1289 dp_str = efi_dp_str(file_info->dp_volume);
1291 utf16_utf8_strncpy(&q, dp_str, EFICONFIG_VOLUME_PATH_MAX);
1293 efi_free_pool(dp_str);
1296 /* append u'/' to devname, it is just for display purpose. */
1297 if (file_info->current_path[0] != u'\0' && file_info->current_path[0] != u'/')
1298 strlcat(devname, "/", EFICONFIG_VOLUME_PATH_MAX + 1);
1300 len = strlen(devname);
1301 len += utf16_utf8_strlen(file_info->current_path) + 1;
1302 file_name = calloc(1, len * sizeof(u16));
1304 ret = EFI_OUT_OF_RESOURCES;
1309 utf8_utf16_strcpy(&p, devname);
1310 u16_strlcat(file_name, file_info->current_path, len);
1311 ret = create_boot_option_entry(efi_menu, title, file_name,
1312 eficonfig_process_show_file_option, file_info);
1321 * eficonfig_show_boot_option() - prepare menu entry for editing boot option
1323 * Construct the structures to create edit boot option menu
1325 * @bo: pointer to the boot option
1326 * @header_str: pointer to the header string
1327 * Return: status code
1329 static efi_status_t eficonfig_show_boot_option(struct eficonfig_boot_option *bo,
1333 struct efimenu *efi_menu;
1335 efi_menu = calloc(1, sizeof(struct efimenu));
1337 return EFI_OUT_OF_RESOURCES;
1339 INIT_LIST_HEAD(&efi_menu->list);
1341 ret = create_boot_option_entry(efi_menu, "Description: ", bo->description,
1342 eficonfig_boot_add_enter_description, bo);
1343 if (ret != EFI_SUCCESS)
1346 ret = prepare_file_selection_entry(efi_menu, "File: ", &bo->file_info);
1347 if (ret != EFI_SUCCESS)
1350 ret = prepare_file_selection_entry(efi_menu, "Initrd File: ", &bo->initrd_info);
1351 if (ret != EFI_SUCCESS)
1354 ret = create_boot_option_entry(efi_menu, "Optional Data: ", bo->optional_data,
1355 eficonfig_boot_add_optional_data, bo);
1356 if (ret != EFI_SUCCESS)
1359 ret = create_boot_option_entry(efi_menu, "Save", NULL,
1360 eficonfig_boot_edit_save, bo);
1361 if (ret != EFI_SUCCESS)
1364 ret = create_boot_option_entry(efi_menu, "Quit", NULL,
1365 eficonfig_process_quit, NULL);
1366 if (ret != EFI_SUCCESS)
1369 ret = eficonfig_process_common(efi_menu, header_str,
1370 eficonfig_menu_desc,
1371 eficonfig_display_statusline,
1372 eficonfig_print_entry,
1373 eficonfig_choice_entry);
1376 eficonfig_destroy(efi_menu);
1382 * fill_file_info() - fill the file info from efi_device_path structure
1384 * @dp: pointer to the device path
1385 * @file_info: pointer to the file info structure
1386 * @device_dp: pointer to the volume device path
1388 static void fill_file_info(struct efi_device_path *dp,
1389 struct eficonfig_select_file_info *file_info,
1390 struct efi_device_path *device_dp)
1393 struct efi_device_path *file_dp = NULL;
1395 efi_dp_split_file_path(dp, &device_dp, &file_dp);
1396 file_info->dp_volume = device_dp;
1399 file_str = efi_dp_str(file_dp);
1401 * efi_convert_device_path_to_text() automatically adds u'/' at the
1402 * beginning of file name, remove u'/' before copying to current_path
1408 u16_strcpy(file_info->current_path, p);
1409 efi_free_pool(file_dp);
1410 efi_free_pool(file_str);
1415 * eficonfig_edit_boot_option() - prepare boot option structure for editing
1417 * Construct the boot option structure and copy the existing value
1419 * @varname: pointer to the UEFI variable name
1420 * @bo: pointer to the boot option
1421 * @load_option: pointer to the load option
1422 * @load_option_size: size of the load option
1423 * @header_str: pointer to the header string
1424 * Return : status code
1426 static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_boot_option *bo,
1427 void *load_option, efi_uintn_t load_option_size,
1432 char *tmp = NULL, *p;
1433 struct efi_load_option lo = {0};
1434 efi_uintn_t final_dp_size;
1435 struct efi_device_path *dp = NULL;
1436 efi_uintn_t size = load_option_size;
1437 struct efi_device_path *final_dp = NULL;
1438 struct efi_device_path *device_dp = NULL;
1439 struct efi_device_path *initrd_dp = NULL;
1440 struct efi_device_path *initrd_device_dp = NULL;
1442 const struct efi_initrd_dp id_dp = {
1445 DEVICE_PATH_TYPE_MEDIA_DEVICE,
1446 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
1447 sizeof(id_dp.vendor),
1449 EFI_INITRD_MEDIA_GUID,
1452 DEVICE_PATH_TYPE_END,
1453 DEVICE_PATH_SUB_TYPE_END,
1458 bo->file_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1459 if (!bo->file_info.current_path) {
1460 ret = EFI_OUT_OF_RESOURCES;
1464 bo->initrd_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1465 if (!bo->file_info.current_path) {
1466 ret = EFI_OUT_OF_RESOURCES;
1470 bo->description = calloc(1, EFICONFIG_DESCRIPTION_MAX * sizeof(u16));
1471 if (!bo->description) {
1472 ret = EFI_OUT_OF_RESOURCES;
1476 bo->optional_data = calloc(1, EFICONFIG_OPTIONAL_DATA_MAX * sizeof(u16));
1477 if (!bo->optional_data) {
1478 ret = EFI_OUT_OF_RESOURCES;
1482 /* copy the preset value */
1484 ret = efi_deserialize_load_option(&lo, load_option, &size);
1485 if (ret != EFI_SUCCESS)
1489 ret = EFI_INVALID_PARAMETER;
1492 /* truncate the long label string */
1493 if (u16_strlen(lo.label) >= EFICONFIG_DESCRIPTION_MAX)
1494 lo.label[EFICONFIG_DESCRIPTION_MAX - 1] = u'\0';
1496 u16_strcpy(bo->description, lo.label);
1498 /* EFI image file path is a first instance */
1500 fill_file_info(lo.file_path, &bo->file_info, device_dp);
1502 /* Initrd file path(optional) is placed at second instance. */
1503 initrd_dp = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
1505 fill_file_info(initrd_dp, &bo->initrd_info, initrd_device_dp);
1506 efi_free_pool(initrd_dp);
1510 memcpy(bo->optional_data, lo.optional_data, size);
1514 ret = eficonfig_show_boot_option(bo, header_str);
1515 if (ret == EFI_SUCCESS && bo->edit_completed)
1517 if (ret == EFI_NOT_READY)
1519 if (ret != EFI_SUCCESS)
1523 if (bo->initrd_info.dp_volume) {
1524 dp = eficonfig_create_device_path(bo->initrd_info.dp_volume,
1525 bo->initrd_info.current_path);
1527 ret = EFI_OUT_OF_RESOURCES;
1530 initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp, dp);
1534 dp = eficonfig_create_device_path(bo->file_info.dp_volume, bo->file_info.current_path);
1536 ret = EFI_OUT_OF_RESOURCES;
1539 final_dp_size = efi_dp_size(dp) + sizeof(END);
1541 final_dp = efi_dp_concat(dp, initrd_dp);
1542 final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
1544 final_dp = efi_dp_dup(dp);
1551 if (utf16_utf8_strlen(bo->optional_data)) {
1552 len = utf16_utf8_strlen(bo->optional_data) + 1;
1553 tmp = calloc(1, len);
1557 utf16_utf8_strncpy(&p, bo->optional_data, u16_strlen(bo->optional_data));
1560 ret = eficonfig_set_boot_option(varname, final_dp, final_dp_size, bo->description, tmp);
1563 free(bo->optional_data);
1564 free(bo->description);
1565 free(bo->file_info.current_path);
1566 free(bo->initrd_info.current_path);
1567 efi_free_pool(device_dp);
1568 efi_free_pool(initrd_device_dp);
1569 efi_free_pool(initrd_dp);
1570 efi_free_pool(final_dp);
1576 * eficonfig_process_add_boot_option() - handler to add boot option
1578 * @data: pointer to the data for each entry
1579 * Return: status code
1581 static efi_status_t eficonfig_process_add_boot_option(void *data)
1585 struct eficonfig_boot_option *bo = NULL;
1587 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1589 return EFI_OUT_OF_RESOURCES;
1591 ret = eficonfig_get_unused_bootoption(varname, sizeof(varname), &bo->boot_index);
1592 if (ret != EFI_SUCCESS)
1595 ret = eficonfig_edit_boot_option(varname, bo, NULL, 0, " ** Add Boot Option ** ");
1596 if (ret != EFI_SUCCESS)
1599 ret = eficonfig_append_bootorder((u16)bo->boot_index);
1600 if (ret != EFI_SUCCESS)
1606 /* to stay the parent menu */
1607 ret = (ret == EFI_ABORTED) ? EFI_SUCCESS : ret;
1613 * eficonfig_process_boot_selected() - handler to select boot option entry
1615 * @data: pointer to the data for each entry
1616 * Return: status code
1618 static efi_status_t eficonfig_process_boot_selected(void *data)
1620 struct eficonfig_boot_selection_data *info = data;
1623 *info->selected = info->boot_index;
1629 * search_bootorder() - search the boot option index in BootOrder
1631 * @bootorder: pointer to the BootOrder variable
1632 * @num: number of BootOrder entry
1633 * @target: target boot option index to search
1634 * @index: pointer to store the index of BootOrder variable
1635 * Return: true if exists, false otherwise
1637 static bool search_bootorder(u16 *bootorder, efi_uintn_t num, u32 target, u32 *index)
1641 for (i = 0; i < num; i++) {
1642 if (target == bootorder[i]) {
1654 * eficonfig_add_boot_selection_entry() - add boot option menu entry
1656 * @efi_menu: pointer to store the efimenu structure
1657 * @boot_index: boot option index to be added
1658 * @selected: pointer to store the selected boot option index
1659 * Return: status code
1661 static efi_status_t eficonfig_add_boot_selection_entry(struct efimenu *efi_menu,
1662 unsigned int boot_index,
1663 unsigned int *selected)
1669 struct efi_load_option lo;
1670 u16 varname[] = u"Boot####";
1671 struct eficonfig_boot_selection_data *info;
1673 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
1674 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1678 ret = efi_deserialize_load_option(&lo, load_option, &size);
1679 if (ret != EFI_SUCCESS) {
1680 log_warning("Invalid load option for %ls\n", varname);
1685 if (size >= sizeof(efi_guid_t) &&
1686 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
1688 * auto generated entry has GUID in optional_data,
1689 * skip auto generated entry because it will be generated
1690 * again even if it is edited or deleted.
1696 info = calloc(1, sizeof(struct eficonfig_boot_selection_data));
1699 return EFI_OUT_OF_RESOURCES;
1702 buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
1706 return EFI_OUT_OF_RESOURCES;
1709 utf16_utf8_strcpy(&p, lo.label);
1710 info->boot_index = boot_index;
1711 info->selected = selected;
1712 ret = eficonfig_append_menu_entry(efi_menu, buf, eficonfig_process_boot_selected, info);
1713 if (ret != EFI_SUCCESS) {
1724 * eficonfig_show_boot_selection() - construct boot option menu entry
1726 * @selected: pointer to store the selected boot option index
1727 * Return: status code
1729 static efi_status_t eficonfig_show_boot_selection(unsigned int *selected)
1734 u16 *var_name16 = NULL;
1735 efi_uintn_t num, size, buf_size;
1736 struct efimenu *efi_menu;
1737 struct list_head *pos, *n;
1738 struct eficonfig_entry *entry;
1740 efi_menu = calloc(1, sizeof(struct efimenu));
1742 return EFI_OUT_OF_RESOURCES;
1744 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1746 INIT_LIST_HEAD(&efi_menu->list);
1747 num = size / sizeof(u16);
1748 /* list the load option in the order of BootOrder variable */
1749 for (i = 0; i < num; i++) {
1750 ret = eficonfig_add_boot_selection_entry(efi_menu, bootorder[i], selected);
1751 if (ret != EFI_SUCCESS)
1754 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1758 /* list the remaining load option not included in the BootOrder */
1760 var_name16 = malloc(buf_size);
1762 return EFI_OUT_OF_RESOURCES;
1769 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
1770 if (ret == EFI_NOT_FOUND)
1772 if (ret != EFI_SUCCESS)
1775 if (efi_varname_is_load_option(var_name16, &index)) {
1776 /* If the index is included in the BootOrder, skip it */
1777 if (search_bootorder(bootorder, num, index, NULL))
1780 ret = eficonfig_add_boot_selection_entry(efi_menu, index, selected);
1781 if (ret != EFI_SUCCESS)
1785 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1789 ret = eficonfig_append_quit_entry(efi_menu);
1790 if (ret != EFI_SUCCESS)
1793 ret = eficonfig_process_common(efi_menu, " ** Select Boot Option **",
1794 eficonfig_menu_desc,
1795 eficonfig_display_statusline,
1796 eficonfig_print_entry,
1797 eficonfig_choice_entry);
1799 list_for_each_safe(pos, n, &efi_menu->list) {
1800 entry = list_entry(pos, struct eficonfig_entry, list);
1803 eficonfig_destroy(efi_menu);
1811 * eficonfig_process_edit_boot_option() - handler to edit boot option
1813 * @data: pointer to the data for each entry
1814 * Return: status code
1816 static efi_status_t eficonfig_process_edit_boot_option(void *data)
1820 struct eficonfig_boot_option *bo = NULL;
1823 unsigned int selected;
1825 u16 varname[] = u"Boot####";
1827 ret = eficonfig_show_boot_selection(&selected);
1828 if (ret != EFI_SUCCESS)
1831 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1833 ret = EFI_OUT_OF_RESOURCES;
1837 bo->boot_index = selected;
1838 efi_create_indexed_name(varname, sizeof(varname), "Boot", selected);
1839 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1842 ret = EFI_NOT_FOUND;
1846 ret = eficonfig_edit_boot_option(varname, bo, load_option, size,
1847 " ** Edit Boot Option ** ");
1851 if (ret != EFI_SUCCESS && ret != EFI_ABORTED)
1855 /* to stay the parent menu */
1856 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1862 * eficonfig_print_change_boot_order_entry() - print the boot option entry
1864 * @data: pointer to the data associated with each menu entry
1866 static void eficonfig_print_change_boot_order_entry(void *data)
1868 struct eficonfig_entry *entry = data;
1869 bool reverse = (entry->efi_menu->active == entry->num);
1871 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
1874 puts(ANSI_COLOR_REVERSE);
1876 if (entry->num < entry->efi_menu->count - 2) {
1877 if (((struct eficonfig_boot_order_data *)entry->data)->active)
1883 printf("%s", entry->title);
1886 puts(ANSI_COLOR_RESET);
1890 * eficonfig_choice_change_boot_order() - user key input handler
1892 * @data: pointer to the menu entry
1893 * Return: key string to identify the selected entry
1895 char *eficonfig_choice_change_boot_order(void *data)
1897 struct cli_ch_state s_cch, *cch = &s_cch;
1898 struct list_head *pos, *n;
1899 struct efimenu *efi_menu = data;
1900 enum bootmenu_key key = BKEY_NONE;
1901 struct eficonfig_entry *entry, *tmp;
1905 key = bootmenu_loop(NULL, cch);
1909 if (efi_menu->active > 0) {
1910 list_for_each_safe(pos, n, &efi_menu->list) {
1911 entry = list_entry(pos, struct eficonfig_entry, list);
1912 if (entry->num == efi_menu->active)
1915 tmp = list_entry(pos->prev, struct eficonfig_entry, list);
1918 list_del(&tmp->list);
1919 list_add(&tmp->list, &entry->list);
1923 if (efi_menu->active > 0)
1927 if (efi_menu->active < efi_menu->count - 3) {
1928 list_for_each_safe(pos, n, &efi_menu->list) {
1929 entry = list_entry(pos, struct eficonfig_entry, list);
1930 if (entry->num == efi_menu->active)
1933 tmp = list_entry(pos->next, struct eficonfig_entry, list);
1936 list_del(&entry->list);
1937 list_add(&entry->list, &tmp->list);
1943 if (efi_menu->active < efi_menu->count - 1)
1948 if (efi_menu->active == efi_menu->count - 2) {
1949 list_for_each_prev_safe(pos, n, &efi_menu->list) {
1950 entry = list_entry(pos, struct eficonfig_entry, list);
1951 if (entry->num == efi_menu->active)
1957 if (efi_menu->active == efi_menu->count - 1) {
1958 entry = list_last_entry(&efi_menu->list,
1959 struct eficonfig_entry,
1963 /* Pressed key is not valid, wait next key press */
1966 if (efi_menu->active < efi_menu->count - 2) {
1967 list_for_each_safe(pos, n, &efi_menu->list) {
1968 entry = list_entry(pos, struct eficonfig_entry, list);
1969 if (entry->num == efi_menu->active) {
1970 struct eficonfig_boot_order_data *data = entry->data;
1972 data->active = !data->active;
1977 /* Pressed key is not valid, wait next key press */
1980 entry = list_last_entry(&efi_menu->list,
1981 struct eficonfig_entry, list);
1984 /* Pressed key is not valid, wait next key press */
1991 * eficonfig_process_save_boot_order() - callback function for "Save" entry
1993 * @data: pointer to the data
1994 * Return: status code
1996 static efi_status_t eficonfig_process_save_boot_order(void *data)
2001 struct list_head *pos, *n;
2003 struct efimenu *efi_menu;
2004 struct eficonfig_entry *entry;
2005 struct eficonfig_save_boot_order_data *save_data = data;
2007 efi_menu = save_data->efi_menu;
2010 * The change boot order menu always has "Save" and "Quit" entries.
2011 * !(efi_menu->count - 2) means there is no user defined boot option.
2013 if (!(efi_menu->count - 2))
2016 new_bootorder = calloc(1, (efi_menu->count - 2) * sizeof(u16));
2017 if (!new_bootorder) {
2018 ret = EFI_OUT_OF_RESOURCES;
2022 /* create new BootOrder */
2024 list_for_each_safe(pos, n, &efi_menu->list) {
2025 struct eficonfig_boot_order_data *data;
2027 entry = list_entry(pos, struct eficonfig_entry, list);
2028 /* exit the loop when iteration reaches "Save" */
2029 if (!strncmp(entry->title, "Save", strlen("Save")))
2034 new_bootorder[count++] = data->boot_index;
2037 size = count * sizeof(u16);
2038 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
2039 EFI_VARIABLE_NON_VOLATILE |
2040 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2041 EFI_VARIABLE_RUNTIME_ACCESS,
2042 size, new_bootorder, false);
2044 save_data->selected = true;
2046 free(new_bootorder);
2052 * eficonfig_add_change_boot_order_entry() - add boot order entry
2054 * @efi_menu: pointer to the efimenu structure
2055 * @boot_index: boot option index to be added
2056 * @active: flag to include the boot option into BootOrder
2057 * Return: status code
2059 static efi_status_t eficonfig_add_change_boot_order_entry(struct efimenu *efi_menu,
2060 u32 boot_index, bool active)
2066 struct efi_load_option lo;
2067 u16 varname[] = u"Boot####";
2068 struct eficonfig_boot_order_data *data;
2070 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
2071 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
2075 ret = efi_deserialize_load_option(&lo, load_option, &size);
2076 if (ret != EFI_SUCCESS)
2079 data = calloc(1, sizeof(*data));
2081 ret = EFI_OUT_OF_RESOURCES;
2085 title = calloc(1, utf16_utf8_strlen(lo.label) + 1);
2088 ret = EFI_OUT_OF_RESOURCES;
2092 utf16_utf8_strcpy(&p, lo.label);
2094 data->boot_index = boot_index;
2095 data->active = active;
2097 ret = eficonfig_append_menu_entry(efi_menu, title, NULL, data);
2098 if (ret != EFI_SUCCESS) {
2111 * eficonfig_create_change_boot_order_entry() - create boot order entry
2113 * @efi_menu: pointer to the efimenu structure
2114 * @bootorder: pointer to the BootOrder variable
2115 * @num: number of BootOrder entry
2116 * Return: status code
2118 static efi_status_t eficonfig_create_change_boot_order_entry(struct efimenu *efi_menu,
2119 u16 *bootorder, efi_uintn_t num)
2124 u16 *var_name16 = NULL;
2125 efi_uintn_t size, buf_size;
2126 struct eficonfig_save_boot_order_data *save_data;
2128 /* list the load option in the order of BootOrder variable */
2129 for (i = 0; i < num; i++) {
2130 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2133 ret = eficonfig_add_change_boot_order_entry(efi_menu, bootorder[i], true);
2134 if (ret != EFI_SUCCESS)
2138 /* list the remaining load option not included in the BootOrder */
2140 var_name16 = malloc(buf_size);
2142 return EFI_OUT_OF_RESOURCES;
2149 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2153 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
2154 if (ret == EFI_NOT_FOUND)
2156 if (ret != EFI_SUCCESS)
2159 if (efi_varname_is_load_option(var_name16, &index)) {
2160 /* If the index is included in the BootOrder, skip it */
2161 if (search_bootorder(bootorder, num, index, NULL))
2164 ret = eficonfig_add_change_boot_order_entry(efi_menu, index, false);
2165 if (ret != EFI_SUCCESS)
2170 /* add "Save" and "Quit" entries */
2171 title = strdup("Save");
2173 ret = EFI_OUT_OF_RESOURCES;
2177 save_data = malloc(sizeof(struct eficonfig_save_boot_order_data));
2179 ret = EFI_OUT_OF_RESOURCES;
2182 save_data->efi_menu = efi_menu;
2183 save_data->selected = false;
2185 ret = eficonfig_append_menu_entry(efi_menu, title,
2186 eficonfig_process_save_boot_order,
2188 if (ret != EFI_SUCCESS)
2191 ret = eficonfig_append_quit_entry(efi_menu);
2192 if (ret != EFI_SUCCESS)
2195 efi_menu->active = 0;
2203 * eficonfig_process_change_boot_order() - handler to change boot order
2205 * @data: pointer to the data for each entry
2206 * Return: status code
2208 static efi_status_t eficonfig_process_change_boot_order(void *data)
2212 efi_uintn_t num, size;
2213 struct list_head *pos, *n;
2214 struct eficonfig_entry *entry;
2215 struct efimenu *efi_menu;
2217 efi_menu = calloc(1, sizeof(struct efimenu));
2219 return EFI_OUT_OF_RESOURCES;
2221 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2223 INIT_LIST_HEAD(&efi_menu->list);
2224 num = size / sizeof(u16);
2225 ret = eficonfig_create_change_boot_order_entry(efi_menu, bootorder, num);
2226 if (ret != EFI_SUCCESS)
2230 ret = eficonfig_process_common(efi_menu,
2231 " ** Change Boot Order **",
2232 eficonfig_change_boot_order_desc,
2233 eficonfig_display_statusline,
2234 eficonfig_print_change_boot_order_entry,
2235 eficonfig_choice_change_boot_order);
2236 /* exit from the menu if user selects the "Save" entry. */
2237 if (ret == EFI_SUCCESS && efi_menu->active == (efi_menu->count - 2)) {
2238 list_for_each_prev_safe(pos, n, &efi_menu->list) {
2239 entry = list_entry(pos, struct eficonfig_entry, list);
2240 if (entry->num == efi_menu->active)
2243 if (((struct eficonfig_save_boot_order_data *)entry->data)->selected)
2246 if (ret != EFI_SUCCESS)
2251 list_for_each_safe(pos, n, &efi_menu->list) {
2252 entry = list_entry(pos, struct eficonfig_entry, list);
2255 eficonfig_destroy(efi_menu);
2257 /* to stay the parent menu */
2258 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2264 * delete_boot_option() - delete selected boot option
2266 * @boot_index: boot option index to delete
2267 * Return: status code
2269 static efi_status_t delete_boot_option(u16 boot_index)
2275 efi_uintn_t num, size;
2277 efi_create_indexed_name(varname, sizeof(varname),
2278 "Boot", boot_index);
2279 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
2281 if (ret != EFI_SUCCESS) {
2282 log_err("delete boot option(%ls) failed\n", varname);
2286 /* update BootOrder if necessary */
2287 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2291 num = size / sizeof(u16);
2292 if (!search_bootorder(bootorder, num, boot_index, &index))
2295 memmove(&bootorder[index], &bootorder[index + 1],
2296 (num - index - 1) * sizeof(u16));
2297 size -= sizeof(u16);
2298 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
2299 EFI_VARIABLE_NON_VOLATILE |
2300 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2301 EFI_VARIABLE_RUNTIME_ACCESS,
2302 size, bootorder, false);
2308 * eficonfig_process_delete_boot_option() - handler to delete boot option
2310 * @data: pointer to the data for each entry
2311 * Return: status code
2313 static efi_status_t eficonfig_process_delete_boot_option(void *data)
2316 unsigned int selected;
2319 ret = eficonfig_show_boot_selection(&selected);
2320 if (ret == EFI_SUCCESS)
2321 ret = delete_boot_option(selected);
2323 if (ret != EFI_SUCCESS)
2327 /* to stay the parent menu */
2328 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2334 * eficonfig_enumerate_boot_option() - enumerate the possible bootable media
2336 * @opt: pointer to the media boot option structure
2337 * @volume_handles: pointer to the efi handles
2338 * @count: number of efi handle
2339 * Return: status code
2341 efi_status_t eficonfig_enumerate_boot_option(struct eficonfig_media_boot_option *opt,
2342 efi_handle_t *volume_handles, efi_status_t count)
2345 struct efi_handler *handler;
2346 efi_status_t ret = EFI_SUCCESS;
2348 for (i = 0; i < count; i++) {
2350 u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
2351 char *optional_data;
2352 struct efi_load_option lo;
2353 char buf[BOOTMENU_DEVICE_NAME_MAX];
2354 struct efi_device_path *device_path;
2356 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
2357 if (ret != EFI_SUCCESS)
2359 ret = efi_protocol_open(handler, (void **)&device_path,
2360 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2361 if (ret != EFI_SUCCESS)
2364 ret = efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
2365 if (ret != EFI_SUCCESS)
2369 utf8_utf16_strncpy(&p, buf, strlen(buf));
2371 lo.label = dev_name;
2372 lo.attributes = LOAD_OPTION_ACTIVE;
2373 lo.file_path = device_path;
2374 lo.file_path_length = efi_dp_size(device_path) + sizeof(END);
2376 * Set the dedicated guid to optional_data, it is used to identify
2377 * the boot option that automatically generated by the bootmenu.
2378 * efi_serialize_load_option() expects optional_data is null-terminated
2379 * utf8 string, so set the "1234567" string to allocate enough space
2380 * to store guid, instead of realloc the load_option.
2382 lo.optional_data = "1234567";
2383 opt[i].size = efi_serialize_load_option(&lo, (u8 **)&opt[i].lo);
2385 ret = EFI_OUT_OF_RESOURCES;
2389 optional_data = (char *)opt[i].lo + (opt[i].size - u16_strsize(u"1234567"));
2390 memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
2398 * eficonfig_delete_invalid_boot_option() - delete non-existing boot option
2400 * @opt: pointer to the media boot option structure
2401 * @count: number of media boot option structure
2402 * Return: status code
2404 efi_status_t eficonfig_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
2409 u32 i, list_size = 0;
2410 struct efi_load_option lo;
2411 u16 *var_name16 = NULL;
2412 u16 varname[] = u"Boot####";
2413 efi_status_t ret = EFI_SUCCESS;
2414 u16 *delete_index_list = NULL, *p;
2415 efi_uintn_t buf_size;
2418 var_name16 = malloc(buf_size);
2420 return EFI_OUT_OF_RESOURCES;
2428 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
2429 if (ret == EFI_NOT_FOUND) {
2431 * EFI_NOT_FOUND indicates we retrieved all EFI variables.
2432 * This should be treated as success.
2437 if (ret != EFI_SUCCESS)
2440 if (!efi_varname_is_load_option(var_name16, &index))
2443 efi_create_indexed_name(varname, sizeof(varname), "Boot", index);
2444 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
2449 ret = efi_deserialize_load_option(&lo, load_option, &size);
2450 if (ret != EFI_SUCCESS)
2453 if (size >= sizeof(efi_guid_bootmenu_auto_generated) &&
2454 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
2455 for (i = 0; i < count; i++) {
2456 if (opt[i].size == tmp &&
2457 memcmp(opt[i].lo, load_option, tmp) == 0) {
2458 opt[i].exist = true;
2464 * The entire list of variables must be retrieved by
2465 * efi_get_next_variable_name_int() before deleting the invalid
2466 * boot option, just save the index here.
2469 p = realloc(delete_index_list, sizeof(u32) *
2472 ret = EFI_OUT_OF_RESOURCES;
2475 delete_index_list = p;
2476 delete_index_list[list_size++] = index;
2483 /* delete all invalid boot options */
2484 for (i = 0; i < list_size; i++) {
2485 ret = delete_boot_option(delete_index_list[i]);
2486 if (ret != EFI_SUCCESS)
2492 free(delete_index_list);
2498 * eficonfig_generate_media_device_boot_option() - generate the media device boot option
2500 * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
2501 * and generate the bootmenu entries.
2502 * This function also provide the BOOT#### variable maintenance for
2503 * the media device entries.
2504 * - Automatically create the BOOT#### variable for the newly detected device,
2505 * this BOOT#### variable is distinguished by the special GUID
2506 * stored in the EFI_LOAD_OPTION.optional_data
2507 * - If the device is not attached to the system, the associated BOOT#### variable
2508 * is automatically deleted.
2510 * Return: status code
2512 efi_status_t eficonfig_generate_media_device_boot_option(void)
2517 efi_handle_t *volume_handles = NULL;
2518 struct eficonfig_media_boot_option *opt = NULL;
2520 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
2521 NULL, &count, (efi_handle_t **)&volume_handles);
2522 if (ret != EFI_SUCCESS)
2525 opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
2529 /* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */
2530 ret = eficonfig_enumerate_boot_option(opt, volume_handles, count);
2531 if (ret != EFI_SUCCESS)
2535 * System hardware configuration may vary depending on the user setup.
2536 * The boot option is automatically added by the bootmenu.
2537 * If the device is not attached to the system, the boot option needs
2540 ret = eficonfig_delete_invalid_boot_option(opt, count);
2541 if (ret != EFI_SUCCESS)
2544 /* add non-existent boot option */
2545 for (i = 0; i < count; i++) {
2549 if (!opt[i].exist) {
2550 ret = eficonfig_get_unused_bootoption(var_name, sizeof(var_name),
2552 if (ret != EFI_SUCCESS)
2555 ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
2556 EFI_VARIABLE_NON_VOLATILE |
2557 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2558 EFI_VARIABLE_RUNTIME_ACCESS,
2559 opt[i].size, opt[i].lo, false);
2560 if (ret != EFI_SUCCESS)
2563 ret = eficonfig_append_bootorder(boot_index);
2564 if (ret != EFI_SUCCESS) {
2565 efi_set_variable_int(var_name, &efi_global_variable_guid,
2574 for (i = 0; i < count; i++)
2578 efi_free_pool(volume_handles);
2584 * eficonfig_init() - do required initialization for eficonfig command
2586 * Return: status code
2588 static efi_status_t eficonfig_init(void)
2590 efi_status_t ret = EFI_SUCCESS;
2592 struct efi_handler *handler;
2595 ret = efi_search_protocol(efi_root, &efi_guid_text_input_protocol, &handler);
2596 if (ret != EFI_SUCCESS)
2599 ret = efi_protocol_open(handler, (void **)&cin, efi_root, NULL,
2600 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2601 if (ret != EFI_SUCCESS)
2610 static const struct eficonfig_item maintenance_menu_items[] = {
2611 {"Add Boot Option", eficonfig_process_add_boot_option},
2612 {"Edit Boot Option", eficonfig_process_edit_boot_option},
2613 {"Change Boot Order", eficonfig_process_change_boot_order},
2614 {"Delete Boot Option", eficonfig_process_delete_boot_option},
2615 #if (CONFIG_IS_ENABLED(EFI_SECURE_BOOT) && CONFIG_IS_ENABLED(EFI_MM_COMM_TEE))
2616 {"Secure Boot Configuration", eficonfig_process_secure_boot_config},
2618 {"Quit", eficonfig_process_quit},
2622 * do_eficonfig() - execute `eficonfig` command
2624 * @cmdtp: table entry describing command
2625 * @flag: bitmap indicating how the command was invoked
2626 * @argc: number of arguments
2627 * @argv: command line arguments
2628 * Return: status code
2630 static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
2633 struct efimenu *efi_menu;
2636 return CMD_RET_USAGE;
2638 ret = efi_init_obj_list();
2639 if (ret != EFI_SUCCESS) {
2640 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
2641 ret & ~EFI_ERROR_MASK);
2643 return CMD_RET_FAILURE;
2646 ret = eficonfig_init();
2647 if (ret != EFI_SUCCESS)
2648 return CMD_RET_FAILURE;
2650 ret = eficonfig_generate_media_device_boot_option();
2651 if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
2655 efi_menu = eficonfig_create_fixed_menu(maintenance_menu_items,
2656 ARRAY_SIZE(maintenance_menu_items));
2658 return CMD_RET_FAILURE;
2660 ret = eficonfig_process_common(efi_menu,
2661 " ** UEFI Maintenance Menu **",
2662 eficonfig_menu_desc,
2663 eficonfig_display_statusline,
2664 eficonfig_print_entry,
2665 eficonfig_choice_entry);
2666 eficonfig_destroy(efi_menu);
2668 if (ret == EFI_ABORTED)
2672 return CMD_RET_SUCCESS;
2676 eficonfig, 1, 0, do_eficonfig,
2677 "provide menu-driven UEFI variable maintenance interface",