1 // SPDX-License-Identifier: GPL-2.0+
3 * Menu-driven UEFI Variable maintenance
5 * Copyright (c) 2022 Masahisa Kojima, Linaro Limited
11 #include <efi_loader.h>
12 #include <efi_load_initrd.h>
13 #include <efi_config.h>
14 #include <efi_variable.h>
20 #include <asm/unaligned.h>
21 #include <linux/delay.h>
23 static struct efi_simple_text_input_protocol *cin;
25 #define EFICONFIG_DESCRIPTION_MAX 32
26 #define EFICONFIG_OPTIONAL_DATA_MAX 64
29 * struct eficonfig_filepath_info - structure to be used to store file path
31 * @name: file or directory name
32 * @list: list structure
34 struct eficonfig_filepath_info {
36 struct list_head list;
40 * struct eficonfig_boot_option - structure to be used for updating UEFI boot option
42 * @file_info: user selected file info
43 * @initrd_info: user selected initrd file info
44 * @boot_index: index of the boot option
45 * @description: pointer to the description string
46 * @optional_data: pointer to the optional_data
47 * @edit_completed: flag indicates edit complete
49 struct eficonfig_boot_option {
50 struct eficonfig_select_file_info file_info;
51 struct eficonfig_select_file_info initrd_info;
52 unsigned int boot_index;
59 * struct eficonfig_volume_entry_data - structure to be used to store volume info
61 * @file_info: pointer to file info structure
62 * @v: pointer to the protocol interface
63 * @dp: pointer to the device path
65 struct eficonfig_volume_entry_data {
66 struct eficonfig_select_file_info *file_info;
67 struct efi_simple_file_system_protocol *v;
68 struct efi_device_path *dp;
72 * struct eficonfig_file_entry_data - structure to be used to store file info
74 * @file_info: pointer to file info structure
75 * @is_directory: flag to identify the directory or file
76 * @file_name: name of directory or file
78 struct eficonfig_file_entry_data {
79 struct eficonfig_select_file_info *file_info;
85 * eficonfig_print_msg() - print message
87 * display the message to the user, user proceeds the screen
90 * @items: pointer to the structure of each menu entry
91 * @count: the number of menu entry
92 * @menu_header: pointer to the menu header string
95 void eficonfig_print_msg(char *msg)
101 printf(ANSI_CURSOR_HIDE
104 "%s\n\n Press any key to continue", 3, 4, msg);
110 * eficonfig_print_entry() - print each menu entry
112 * @data: pointer to the data associated with each menu entry
114 static void eficonfig_print_entry(void *data)
116 struct eficonfig_entry *entry = data;
117 int reverse = (entry->efi_menu->active == entry->num);
119 /* TODO: support scroll or page for many entries */
122 * Move cursor to line where the entry will be drawn (entry->num)
123 * First 3 lines(menu header) + 1 empty line
125 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
128 puts(ANSI_COLOR_REVERSE);
130 printf("%s", entry->title);
133 puts(ANSI_COLOR_RESET);
137 * eficonfig_display_statusline() - print status line
139 * @m: pointer to the menu structure
141 static void eficonfig_display_statusline(struct menu *m)
143 struct eficonfig_entry *entry;
145 if (menu_default_choice(m, (void *)&entry) < 0)
148 printf(ANSI_CURSOR_POSITION
150 ANSI_CURSOR_POSITION ANSI_CLEAR_LINE ANSI_CURSOR_POSITION
151 " Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit"
152 ANSI_CLEAR_LINE_TO_END ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
153 1, 1, entry->efi_menu->menu_header, entry->efi_menu->count + 5, 1,
154 entry->efi_menu->count + 6, 1, entry->efi_menu->count + 7, 1);
158 * eficonfig_choice_entry() - user key input handler
160 * @data: pointer to the efimenu structure
161 * Return: key string to identify the selected entry
163 static char *eficonfig_choice_entry(void *data)
166 struct list_head *pos, *n;
167 struct eficonfig_entry *entry;
168 enum bootmenu_key key = KEY_NONE;
169 struct efimenu *efi_menu = data;
172 bootmenu_loop((struct bootmenu_data *)efi_menu, &key, &esc);
176 if (efi_menu->active > 0)
178 /* no menu key selected, regenerate menu */
181 if (efi_menu->active < efi_menu->count - 1)
183 /* no menu key selected, regenerate menu */
186 list_for_each_safe(pos, n, &efi_menu->list) {
187 entry = list_entry(pos, struct eficonfig_entry, list);
188 if (entry->num == efi_menu->active)
193 /* Quit by choosing the last entry */
194 entry = list_last_entry(&efi_menu->list, struct eficonfig_entry, list);
197 /* Pressed key is not valid, no need to regenerate the menu */
204 * eficonfig_destroy() - destroy efimenu
206 * @efi_menu: pointer to the efimenu structure
208 void eficonfig_destroy(struct efimenu *efi_menu)
210 struct list_head *pos, *n;
211 struct eficonfig_entry *entry;
216 list_for_each_safe(pos, n, &efi_menu->list) {
217 entry = list_entry(pos, struct eficonfig_entry, list);
219 list_del(&entry->list);
222 free(efi_menu->menu_header);
227 * eficonfig_process_quit() - callback function for "Quit" entry
229 * @data: pointer to the data
230 * Return: status code
232 efi_status_t eficonfig_process_quit(void *data)
238 * append_entry() - append menu item
240 * @efi_menu: pointer to the efimenu structure
241 * @title: pointer to the entry title
242 * @func: callback of each entry
243 * @data: pointer to the data to be passed to each entry callback
244 * Return: status code
246 static efi_status_t append_entry(struct efimenu *efi_menu,
247 char *title, eficonfig_entry_func func, void *data)
249 struct eficonfig_entry *entry;
251 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX)
252 return EFI_OUT_OF_RESOURCES;
254 entry = calloc(1, sizeof(struct eficonfig_entry));
256 return EFI_OUT_OF_RESOURCES;
258 entry->title = title;
259 sprintf(entry->key, "%d", efi_menu->count);
260 entry->efi_menu = efi_menu;
263 entry->num = efi_menu->count++;
264 list_add_tail(&entry->list, &efi_menu->list);
270 * append_quit_entry() - append quit entry
272 * @efi_menu: pointer to the efimenu structure
273 * Return: status code
275 static efi_status_t append_quit_entry(struct efimenu *efi_menu)
280 title = strdup("Quit");
282 return EFI_OUT_OF_RESOURCES;
284 ret = append_entry(efi_menu, title, eficonfig_process_quit, NULL);
285 if (ret != EFI_SUCCESS)
292 * eficonfig_create_fixed_menu() - create fixed entry menu structure
294 * @items: pointer to the menu entry item
295 * @count: the number of menu entry
296 * Return: pointer to the efimenu structure
298 void *eficonfig_create_fixed_menu(const struct eficonfig_item *items, int count)
303 struct efimenu *efi_menu;
304 const struct eficonfig_item *iter = items;
306 efi_menu = calloc(1, sizeof(struct efimenu));
310 INIT_LIST_HEAD(&efi_menu->list);
311 for (i = 0; i < count; i++, iter++) {
312 title = strdup(iter->title);
316 ret = append_entry(efi_menu, title, iter->func, iter->data);
317 if (ret != EFI_SUCCESS) {
325 eficonfig_destroy(efi_menu);
331 * eficonfig_process_common() - main handler for UEFI menu
333 * Construct the structures required to show the menu, then handle
334 * the user input interacting with u-boot menu functions.
336 * @efi_menu: pointer to the efimenu structure
337 * @menu_header: pointer to the menu header string
338 * Return: status code
340 efi_status_t eficonfig_process_common(struct efimenu *efi_menu, char *menu_header)
344 struct list_head *pos, *n;
345 struct eficonfig_entry *entry;
346 efi_status_t ret = EFI_SUCCESS;
348 if (efi_menu->count > EFICONFIG_ENTRY_NUM_MAX)
349 return EFI_OUT_OF_RESOURCES;
351 efi_menu->delay = -1;
352 efi_menu->active = 0;
355 efi_menu->menu_header = strdup(menu_header);
356 if (!efi_menu->menu_header)
357 return EFI_OUT_OF_RESOURCES;
360 menu = menu_create(NULL, 0, 1, eficonfig_display_statusline,
361 eficonfig_print_entry, eficonfig_choice_entry,
364 return EFI_INVALID_PARAMETER;
366 list_for_each_safe(pos, n, &efi_menu->list) {
367 entry = list_entry(pos, struct eficonfig_entry, list);
368 if (!menu_item_add(menu, entry->key, entry)) {
369 ret = EFI_INVALID_PARAMETER;
374 entry = list_first_entry_or_null(&efi_menu->list, struct eficonfig_entry, list);
376 menu_default_set(menu, entry->key);
378 printf(ANSI_CURSOR_HIDE
380 ANSI_CURSOR_POSITION, 1, 1);
382 if (menu_get_choice(menu, &choice)) {
385 ret = entry->func(entry->data);
390 printf(ANSI_CLEAR_CONSOLE
392 ANSI_CURSOR_SHOW, 1, 1);
398 * eficonfig_volume_selected() - handler of volume selection
400 * @data: pointer to the data of selected entry
401 * Return: status code
403 static efi_status_t eficonfig_volume_selected(void *data)
405 struct eficonfig_volume_entry_data *info = data;
408 info->file_info->current_volume = info->v;
409 info->file_info->dp_volume = info->dp;
416 * create_selected_device_path() - create device path
418 * @file_info: pointer to the selected file information
420 * device path or NULL. Caller must free the returned value
423 struct efi_device_path *create_selected_device_path(struct eficonfig_select_file_info *file_info)
428 struct efi_device_path *dp;
429 struct efi_device_path_file_path *fp;
431 fp_size = sizeof(struct efi_device_path) +
432 ((u16_strlen(file_info->current_path) + 1) * sizeof(u16));
433 buf = calloc(1, fp_size + sizeof(END));
438 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
439 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
440 fp->dp.length = (u16)fp_size;
441 u16_strcpy(fp->str, file_info->current_path);
445 *((struct efi_device_path *)p) = END;
447 dp = efi_dp_append(file_info->dp_volume, (struct efi_device_path *)buf);
454 * eficonfig_file_selected() - handler of file selection
456 * @data: pointer to the data of selected entry
457 * Return: status code
459 static efi_status_t eficonfig_file_selected(void *data)
462 struct eficonfig_file_entry_data *info = data;
465 return EFI_INVALID_PARAMETER;
467 if (!strcmp(info->file_name, "..")) {
468 struct eficonfig_filepath_info *iter;
469 struct list_head *pos, *n;
472 tmp = info->file_info->current_path;
474 memset(info->file_info->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
475 filepath = calloc(1, EFICONFIG_FILE_PATH_MAX);
477 return EFI_OUT_OF_RESOURCES;
479 list_for_each_safe(pos, n, &info->file_info->filepath_list) {
480 iter = list_entry(pos, struct eficonfig_filepath_info, list);
482 is_last = list_is_last(&iter->list, &info->file_info->filepath_list);
484 list_del(&iter->list);
489 strlcat(filepath, iter->name, EFICONFIG_FILE_PATH_MAX);
491 utf8_utf16_strcpy(&tmp, filepath);
494 struct eficonfig_filepath_info *filepath_info;
496 new_len = u16_strlen(info->file_info->current_path) +
497 strlen(info->file_name);
498 if (new_len >= EFICONFIG_FILE_PATH_MAX) {
499 eficonfig_print_msg("File path is too long!");
500 return EFI_INVALID_PARAMETER;
502 tmp = &info->file_info->current_path[u16_strlen(info->file_info->current_path)];
503 utf8_utf16_strcpy(&tmp, info->file_name);
505 filepath_info = calloc(1, sizeof(struct eficonfig_filepath_info));
507 return EFI_OUT_OF_RESOURCES;
509 filepath_info->name = strdup(info->file_name);
510 if (!filepath_info->name) {
512 return EFI_OUT_OF_RESOURCES;
514 list_add_tail(&filepath_info->list, &info->file_info->filepath_list);
516 if (!info->is_directory)
517 info->file_info->file_selected = true;
524 * eficonfig_select_volume() - construct the volume selection menu
526 * @file_info: pointer to the file selection structure
527 * Return: status code
529 static efi_status_t eficonfig_select_volume(struct eficonfig_select_file_info *file_info)
534 struct efimenu *efi_menu;
535 struct list_head *pos, *n;
536 struct efi_handler *handler;
537 struct eficonfig_entry *entry;
538 struct efi_device_path *device_path;
539 efi_handle_t *volume_handles = NULL;
540 struct efi_simple_file_system_protocol *v;
542 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
543 NULL, &count, (efi_handle_t **)&volume_handles);
544 if (ret != EFI_SUCCESS) {
545 eficonfig_print_msg("No block device found!");
549 efi_menu = calloc(1, sizeof(struct efimenu));
551 return EFI_OUT_OF_RESOURCES;
553 INIT_LIST_HEAD(&efi_menu->list);
554 for (i = 0; i < count; i++) {
556 struct efi_block_io *block_io;
557 struct eficonfig_volume_entry_data *info;
559 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
562 ret = efi_search_protocol(volume_handles[i],
563 &efi_simple_file_system_protocol_guid, &handler);
564 if (ret != EFI_SUCCESS)
566 ret = efi_protocol_open(handler, (void **)&v, efi_root, NULL,
567 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
568 if (ret != EFI_SUCCESS)
571 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
572 if (ret != EFI_SUCCESS)
574 ret = efi_protocol_open(handler, (void **)&device_path,
575 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
576 if (ret != EFI_SUCCESS)
579 ret = efi_search_protocol(volume_handles[i], &efi_block_io_guid, &handler);
580 if (ret != EFI_SUCCESS)
582 ret = efi_protocol_open(handler, (void **)&block_io,
583 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
584 if (ret != EFI_SUCCESS)
587 info = calloc(1, sizeof(struct eficonfig_volume_entry_data));
589 ret = EFI_OUT_OF_RESOURCES;
593 devname = calloc(1, BOOTMENU_DEVICE_NAME_MAX);
596 ret = EFI_OUT_OF_RESOURCES;
599 ret = efi_disk_get_device_name(volume_handles[i], devname,
600 BOOTMENU_DEVICE_NAME_MAX);
601 if (ret != EFI_SUCCESS) {
607 info->dp = device_path;
608 info->file_info = file_info;
609 ret = append_entry(efi_menu, devname, eficonfig_volume_selected, info);
610 if (ret != EFI_SUCCESS) {
616 ret = append_quit_entry(efi_menu);
617 if (ret != EFI_SUCCESS)
620 ret = eficonfig_process_common(efi_menu, " ** Select Volume **");
622 efi_free_pool(volume_handles);
623 list_for_each_safe(pos, n, &efi_menu->list) {
624 entry = list_entry(pos, struct eficonfig_entry, list);
627 eficonfig_destroy(efi_menu);
633 * sort_file() - sort the file name in ascii order
635 * @data1: pointer to the file entry data
636 * @data2: pointer to the file entry data
637 * Return: -1 if the data1 file name is less than data2 file name,
638 * 0 if both file name match,
639 * 1 if the data1 file name is greater thant data2 file name.
641 static int sort_file(const void *arg1, const void *arg2)
643 const struct eficonfig_file_entry_data *data1, *data2;
645 data1 = *((const struct eficonfig_file_entry_data **)arg1);
646 data2 = *((const struct eficonfig_file_entry_data **)arg2);
648 return strcasecmp(data1->file_name, data2->file_name);
652 * eficonfig_create_file_entry() - construct the file menu entry
654 * @efi_menu: pointer to the efimenu structure
655 * @count: number of the directory and file
656 * @tmp_infos: pointer to the entry data array
657 * @f: pointer to the file handle
658 * @buf: pointer to the buffer to store the directory information
659 * @file_info: pointer to the file selection structure
660 * Return: status code
663 eficonfig_create_file_entry(struct efimenu *efi_menu, u32 count,
664 struct eficonfig_file_entry_data **tmp_infos,
665 struct efi_file_handle *f, struct efi_file_info *buf,
666 struct eficonfig_select_file_info *file_info)
671 u32 i, entry_num = 0;
672 struct eficonfig_file_entry_data *info;
674 efi_file_setpos_int(f, 0);
675 /* Read directory and construct menu structure */
676 for (i = 0; i < count; i++) {
677 if (entry_num >= EFICONFIG_ENTRY_NUM_MAX - 1)
680 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
681 ret = efi_file_read_int(f, &len, buf);
682 if (ret != EFI_SUCCESS || len == 0)
685 info = calloc(1, sizeof(struct eficonfig_file_entry_data));
687 ret = EFI_OUT_OF_RESOURCES;
691 /* append '\\' at the end of directory name */
692 name = calloc(1, utf16_utf8_strlen(buf->file_name) + 2);
694 ret = EFI_OUT_OF_RESOURCES;
699 utf16_utf8_strcpy(&p, buf->file_name);
700 if (buf->attribute & EFI_FILE_DIRECTORY) {
701 /* filter out u'.' */
702 if (!u16_strcmp(buf->file_name, u".")) {
707 name[u16_strlen(buf->file_name)] = '\\';
708 info->is_directory = true;
711 info->file_name = name;
712 info->file_info = file_info;
713 tmp_infos[entry_num++] = info;
716 qsort(tmp_infos, entry_num, sizeof(*tmp_infos),
717 (int (*)(const void *, const void *))sort_file);
719 for (i = 0; i < entry_num; i++) {
720 ret = append_entry(efi_menu, tmp_infos[i]->file_name,
721 eficonfig_file_selected, tmp_infos[i]);
722 if (ret != EFI_SUCCESS)
731 * eficonfig_select_file() - construct the file selection menu
733 * @file_info: pointer to the file selection structure
734 * @root: pointer to the file handle
735 * Return: status code
737 static efi_status_t eficonfig_select_file(struct eficonfig_select_file_info *file_info,
738 struct efi_file_handle *root)
743 struct efimenu *efi_menu;
744 struct efi_file_handle *f;
745 struct efi_file_info *buf;
746 struct eficonfig_file_entry_data **tmp_infos;
748 buf = calloc(1, sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE);
750 return EFI_OUT_OF_RESOURCES;
752 while (!file_info->file_selected) {
753 efi_menu = calloc(1, sizeof(struct efimenu));
755 ret = EFI_OUT_OF_RESOURCES;
758 INIT_LIST_HEAD(&efi_menu->list);
760 ret = efi_file_open_int(root, &f, file_info->current_path, EFI_FILE_MODE_READ, 0);
761 if (ret != EFI_SUCCESS) {
762 eficonfig_print_msg("Reading volume failed!");
768 /* Count the number of directory entries */
770 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
771 ret = efi_file_read_int(f, &len, buf);
772 if (ret != EFI_SUCCESS || len == 0)
778 /* allocate array to sort the entry */
779 tmp_infos = calloc(count, sizeof(*tmp_infos));
781 ret = EFI_OUT_OF_RESOURCES;
785 ret = eficonfig_create_file_entry(efi_menu, count, tmp_infos,
787 if (ret != EFI_SUCCESS)
790 ret = append_quit_entry(efi_menu);
791 if (ret != EFI_SUCCESS)
794 ret = eficonfig_process_common(efi_menu, " ** Select File **");
796 efi_file_close_int(f);
797 eficonfig_destroy(efi_menu);
800 for (i = 0; i < count; i++)
806 if (ret != EFI_SUCCESS)
817 * handle_user_input() - handle user input
819 * @buf: pointer to the buffer
820 * @buf_size: size of the buffer
821 * @cursor_col: cursor column for user input
822 * @msg: pointer to the string to display
823 * Return: status code
825 static efi_status_t handle_user_input(u16 *buf, int buf_size,
826 int cursor_col, char *msg)
831 printf(ANSI_CLEAR_CONSOLE
835 " Press ENTER to complete, ESC/CTRL+C to quit",
838 /* tmp is used to accept user cancel */
839 tmp = calloc(1, buf_size * sizeof(u16));
841 return EFI_OUT_OF_RESOURCES;
843 ret = efi_console_get_u16_string(cin, tmp, buf_size, NULL, 4, cursor_col);
844 if (ret == EFI_SUCCESS)
845 u16_strcpy(buf, tmp);
849 /* to stay the parent menu */
850 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
856 * eficonfig_boot_add_enter_description() - handle user input for description
858 * @data: pointer to the internal boot option structure
859 * Return: status code
861 static efi_status_t eficonfig_boot_add_enter_description(void *data)
863 struct eficonfig_boot_option *bo = data;
865 return handle_user_input(bo->description, EFICONFIG_DESCRIPTION_MAX, 22,
866 "\n ** Edit Description **\n"
868 " enter description: ");
872 * eficonfig_boot_add_optional_data() - handle user input for optional data
874 * @data: pointer to the internal boot option structure
875 * Return: status code
877 static efi_status_t eficonfig_boot_add_optional_data(void *data)
879 struct eficonfig_boot_option *bo = data;
881 return handle_user_input(bo->optional_data, EFICONFIG_OPTIONAL_DATA_MAX, 24,
882 "\n ** Edit Optional Data **\n"
884 " enter optional data:");
888 * eficonfig_boot_edit_save() - handler to save the boot option
890 * @data: pointer to the internal boot option structure
891 * Return: status code
893 static efi_status_t eficonfig_boot_edit_save(void *data)
895 struct eficonfig_boot_option *bo = data;
897 if (u16_strlen(bo->description) == 0) {
898 eficonfig_print_msg("Boot Description is empty!");
899 bo->edit_completed = false;
900 return EFI_NOT_READY;
902 if (u16_strlen(bo->file_info.current_path) == 0) {
903 eficonfig_print_msg("File is not selected!");
904 bo->edit_completed = false;
905 return EFI_NOT_READY;
908 bo->edit_completed = true;
914 * eficonfig_process_select_file() - callback function for "Select File" entry
916 * @data: pointer to the data
917 * Return: status code
919 efi_status_t eficonfig_process_select_file(void *data)
925 * eficonfig_process_clear_file_selection() - callback function for "Clear" entry
927 * @data: pointer to the data
928 * Return: status code
930 efi_status_t eficonfig_process_clear_file_selection(void *data)
932 struct eficonfig_select_file_info *file_info = data;
934 /* clear the existing file information */
935 file_info->current_volume = NULL;
936 file_info->current_path[0] = u'\0';
937 file_info->dp_volume = NULL;
942 static struct eficonfig_item select_file_menu_items[] = {
943 {"Select File", eficonfig_process_select_file},
944 {"Clear", eficonfig_process_clear_file_selection},
945 {"Quit", eficonfig_process_quit},
950 * eficonfig_display_select_file_option() - display select file option
952 * @file_info: pointer to the file information structure
953 * Return: status code
955 efi_status_t eficonfig_display_select_file_option(struct eficonfig_select_file_info *file_info)
958 struct efimenu *efi_menu;
960 select_file_menu_items[1].data = file_info;
961 efi_menu = eficonfig_create_fixed_menu(select_file_menu_items,
962 ARRAY_SIZE(select_file_menu_items));
964 return EFI_OUT_OF_RESOURCES;
966 ret = eficonfig_process_common(efi_menu, " ** Update File **");
967 if (ret != EFI_SUCCESS) /* User selects "Clear" or "Quit" */
970 eficonfig_destroy(efi_menu);
976 * eficonfig_select_file_handler() - handle user file selection
978 * @data: pointer to the data
979 * Return: status code
981 efi_status_t eficonfig_select_file_handler(void *data)
985 struct list_head *pos, *n;
986 struct efi_file_handle *root;
987 struct eficonfig_filepath_info *item;
988 struct eficonfig_select_file_info *tmp = NULL;
989 struct eficonfig_select_file_info *file_info = data;
991 ret = eficonfig_display_select_file_option(file_info);
992 if (ret != EFI_SUCCESS)
995 tmp = calloc(1, sizeof(struct eficonfig_select_file_info));
997 return EFI_OUT_OF_RESOURCES;
999 tmp->current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1000 if (!tmp->current_path) {
1002 return EFI_OUT_OF_RESOURCES;
1004 INIT_LIST_HEAD(&tmp->filepath_list);
1006 while (!tmp->file_selected) {
1007 tmp->current_volume = NULL;
1008 memset(tmp->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
1010 ret = eficonfig_select_volume(tmp);
1011 if (ret != EFI_SUCCESS)
1014 if (!tmp->current_volume)
1015 return EFI_INVALID_PARAMETER;
1017 ret = efi_open_volume_int(tmp->current_volume, &root);
1018 if (ret != EFI_SUCCESS)
1021 ret = eficonfig_select_file(tmp, root);
1022 if (ret == EFI_ABORTED)
1024 if (ret != EFI_SUCCESS)
1029 if (ret == EFI_SUCCESS) {
1030 len = u16_strlen(tmp->current_path);
1031 len = (len >= EFICONFIG_FILE_PATH_MAX) ? (EFICONFIG_FILE_PATH_MAX - 1) : len;
1032 memcpy(file_info->current_path, tmp->current_path, len * sizeof(u16));
1033 file_info->current_path[len] = u'\0';
1034 file_info->current_volume = tmp->current_volume;
1035 file_info->dp_volume = tmp->dp_volume;
1038 list_for_each_safe(pos, n, &tmp->filepath_list) {
1039 item = list_entry(pos, struct eficonfig_filepath_info, list);
1040 list_del(&item->list);
1044 free(tmp->current_path);
1047 /* to stay the parent menu */
1048 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1054 * eficonfig_get_unused_bootoption() - get unused "Boot####" index
1056 * @buf: pointer to the buffer to store boot option variable name
1057 * @buf_size: buffer size
1058 * @index: pointer to store the index in the BootOrder variable
1059 * Return: status code
1061 efi_status_t eficonfig_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
1062 unsigned int *index)
1068 if (buf_size < u16_strsize(u"Boot####"))
1069 return EFI_BUFFER_TOO_SMALL;
1071 for (i = 0; i <= 0xFFFF; i++) {
1073 efi_create_indexed_name(buf, buf_size, "Boot", i);
1074 ret = efi_get_variable_int(buf, &efi_global_variable_guid,
1075 NULL, &size, NULL, NULL);
1076 if (ret == EFI_BUFFER_TOO_SMALL)
1083 return EFI_OUT_OF_RESOURCES;
1091 * eficonfig_set_boot_option() - set boot option
1093 * @varname: pointer to variable name
1094 * @dp: pointer to device path
1095 * @label: pointer to label string
1096 * @optional_data: pointer to optional data
1097 * Return: status code
1099 static efi_status_t eficonfig_set_boot_option(u16 *varname, struct efi_device_path *dp,
1100 efi_uintn_t dp_size, u16 *label, char *optional_data)
1105 struct efi_load_option lo;
1108 lo.file_path_length = dp_size;
1109 lo.attributes = LOAD_OPTION_ACTIVE;
1110 lo.optional_data = optional_data;
1113 size = efi_serialize_load_option(&lo, (u8 **)&p);
1115 return EFI_INVALID_PARAMETER;
1117 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1118 EFI_VARIABLE_NON_VOLATILE |
1119 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1120 EFI_VARIABLE_RUNTIME_ACCESS,
1128 * eficonfig_append_bootorder() - append new boot option in BootOrder variable
1130 * @index: "Boot####" index to append to BootOrder variable
1131 * Return: status code
1133 efi_status_t eficonfig_append_bootorder(u16 index)
1137 u16 *new_bootorder = NULL;
1138 efi_uintn_t last, size, new_size;
1140 /* append new boot option */
1141 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1142 last = size / sizeof(u16);
1143 new_size = size + sizeof(u16);
1144 new_bootorder = calloc(1, new_size);
1145 if (!new_bootorder) {
1146 ret = EFI_OUT_OF_RESOURCES;
1149 memcpy(new_bootorder, bootorder, size);
1150 new_bootorder[last] = index;
1152 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1153 EFI_VARIABLE_NON_VOLATILE |
1154 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1155 EFI_VARIABLE_RUNTIME_ACCESS,
1156 new_size, new_bootorder, false);
1157 if (ret != EFI_SUCCESS)
1162 free(new_bootorder);
1168 * create_boot_option_entry() - create boot option entry
1170 * @efi_menu: pointer to the efimenu structure
1171 * @title: pointer to the entry title
1172 * @val: pointer to boot option label
1173 * @func: callback of each entry
1174 * @data: pointer to the data to be passed to each entry callback
1175 * Return: status code
1177 static efi_status_t create_boot_option_entry(struct efimenu *efi_menu, char *title, u16 *val,
1178 eficonfig_entry_func func, void *data)
1183 len = strlen(title) + 1;
1185 len += utf16_utf8_strlen(val);
1186 buf = calloc(1, len);
1188 return EFI_OUT_OF_RESOURCES;
1192 p = buf + strlen(title);
1193 utf16_utf8_strcpy(&p, val);
1196 return append_entry(efi_menu, buf, func, data);
1200 * prepare_file_selection_entry() - prepare file selection entry
1202 * @efi_menu: pointer to the efimenu structure
1203 * @title: pointer to the title string
1204 * @file_info: pointer to the file info
1205 * Return: status code
1207 static efi_status_t prepare_file_selection_entry(struct efimenu *efi_menu, char *title,
1208 struct eficonfig_select_file_info *file_info)
1213 efi_handle_t handle;
1214 char devname[BOOTMENU_DEVICE_NAME_MAX] = {0};
1216 /* get the device name only when the user already selected the file path */
1217 handle = efi_dp_find_obj(file_info->dp_volume, NULL, NULL);
1219 ret = efi_disk_get_device_name(handle, devname, BOOTMENU_DEVICE_NAME_MAX);
1220 if (ret != EFI_SUCCESS)
1224 /* append u'/' to devname, it is just for display purpose. */
1225 if (file_info->current_path[0] != u'\0' && file_info->current_path[0] != u'/')
1226 strlcat(devname, "/", BOOTMENU_DEVICE_NAME_MAX);
1228 len = strlen(devname);
1229 len += utf16_utf8_strlen(file_info->current_path) + 1;
1230 file_name = calloc(1, len * sizeof(u16));
1235 utf8_utf16_strcpy(&p, devname);
1236 u16_strlcat(file_name, file_info->current_path, len);
1237 ret = create_boot_option_entry(efi_menu, title, file_name,
1238 eficonfig_select_file_handler, file_info);
1244 * eficonfig_show_boot_option() - prepare menu entry for editing boot option
1246 * Construct the structures to create edit boot option menu
1248 * @bo: pointer to the boot option
1249 * @header_str: pointer to the header string
1250 * Return: status code
1252 static efi_status_t eficonfig_show_boot_option(struct eficonfig_boot_option *bo,
1256 struct efimenu *efi_menu;
1258 efi_menu = calloc(1, sizeof(struct efimenu));
1260 return EFI_OUT_OF_RESOURCES;
1262 INIT_LIST_HEAD(&efi_menu->list);
1264 ret = create_boot_option_entry(efi_menu, "Description: ", bo->description,
1265 eficonfig_boot_add_enter_description, bo);
1266 if (ret != EFI_SUCCESS)
1269 ret = prepare_file_selection_entry(efi_menu, "File: ", &bo->file_info);
1270 if (ret != EFI_SUCCESS)
1273 ret = prepare_file_selection_entry(efi_menu, "Initrd File: ", &bo->initrd_info);
1274 if (ret != EFI_SUCCESS)
1277 ret = create_boot_option_entry(efi_menu, "Optional Data: ", bo->optional_data,
1278 eficonfig_boot_add_optional_data, bo);
1279 if (ret != EFI_SUCCESS)
1282 ret = create_boot_option_entry(efi_menu, "Save", NULL,
1283 eficonfig_boot_edit_save, bo);
1284 if (ret != EFI_SUCCESS)
1287 ret = create_boot_option_entry(efi_menu, "Quit", NULL,
1288 eficonfig_process_quit, NULL);
1289 if (ret != EFI_SUCCESS)
1292 ret = eficonfig_process_common(efi_menu, header_str);
1294 eficonfig_destroy(efi_menu);
1300 * fill_file_info() - fill the file info from efi_device_path structure
1302 * @dp: pointer to the device path
1303 * @file_info: pointer to the file info structure
1304 * @device_dp: pointer to the volume device path
1306 static void fill_file_info(struct efi_device_path *dp,
1307 struct eficonfig_select_file_info *file_info,
1308 struct efi_device_path *device_dp)
1311 struct efi_device_path *file_dp = NULL;
1313 efi_dp_split_file_path(dp, &device_dp, &file_dp);
1314 file_info->dp_volume = device_dp;
1317 file_str = efi_dp_str(file_dp);
1319 * efi_convert_device_path_to_text() automatically adds u'/' at the
1320 * beginning of file name, remove u'/' before copying to current_path
1326 u16_strcpy(file_info->current_path, p);
1327 efi_free_pool(file_dp);
1328 efi_free_pool(file_str);
1333 * eficonfig_edit_boot_option() - prepare boot option structure for editing
1335 * Construct the boot option structure and copy the existing value
1337 * @varname: pointer to the UEFI variable name
1338 * @bo: pointer to the boot option
1339 * @load_option: pointer to the load option
1340 * @load_option_size: size of the load option
1341 * @header_str: pointer to the header string
1342 * Return : status code
1344 static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_boot_option *bo,
1345 void *load_option, efi_uintn_t load_option_size,
1350 char *tmp = NULL, *p;
1351 struct efi_load_option lo = {0};
1352 efi_uintn_t final_dp_size;
1353 struct efi_device_path *dp = NULL;
1354 efi_uintn_t size = load_option_size;
1355 struct efi_device_path *final_dp = NULL;
1356 struct efi_device_path *device_dp = NULL;
1357 struct efi_device_path *initrd_dp = NULL;
1358 struct efi_device_path *initrd_device_dp = NULL;
1360 const struct efi_initrd_dp id_dp = {
1363 DEVICE_PATH_TYPE_MEDIA_DEVICE,
1364 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
1365 sizeof(id_dp.vendor),
1367 EFI_INITRD_MEDIA_GUID,
1370 DEVICE_PATH_TYPE_END,
1371 DEVICE_PATH_SUB_TYPE_END,
1376 bo->file_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1377 if (!bo->file_info.current_path) {
1378 ret = EFI_OUT_OF_RESOURCES;
1382 bo->initrd_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1383 if (!bo->file_info.current_path) {
1384 ret = EFI_OUT_OF_RESOURCES;
1388 bo->description = calloc(1, EFICONFIG_DESCRIPTION_MAX * sizeof(u16));
1389 if (!bo->description) {
1390 ret = EFI_OUT_OF_RESOURCES;
1394 bo->optional_data = calloc(1, EFICONFIG_OPTIONAL_DATA_MAX * sizeof(u16));
1395 if (!bo->optional_data) {
1396 ret = EFI_OUT_OF_RESOURCES;
1400 /* copy the preset value */
1402 ret = efi_deserialize_load_option(&lo, load_option, &size);
1403 if (ret != EFI_SUCCESS)
1406 if (!lo.label || (lo.label && u16_strlen(lo.label) >= EFICONFIG_DESCRIPTION_MAX)) {
1407 ret = EFI_INVALID_PARAMETER;
1410 u16_strcpy(bo->description, lo.label);
1412 /* EFI image file path is a first instance */
1414 fill_file_info(lo.file_path, &bo->file_info, device_dp);
1416 /* Initrd file path(optional) is placed at second instance. */
1417 initrd_dp = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
1419 fill_file_info(initrd_dp, &bo->initrd_info, initrd_device_dp);
1420 efi_free_pool(initrd_dp);
1424 memcpy(bo->optional_data, lo.optional_data, size);
1428 ret = eficonfig_show_boot_option(bo, header_str);
1429 if (ret == EFI_SUCCESS && bo->edit_completed)
1431 if (ret == EFI_NOT_READY)
1433 if (ret != EFI_SUCCESS)
1437 if (bo->initrd_info.dp_volume) {
1438 dp = create_selected_device_path(&bo->initrd_info);
1440 ret = EFI_OUT_OF_RESOURCES;
1443 initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp, dp);
1447 dp = create_selected_device_path(&bo->file_info);
1449 ret = EFI_OUT_OF_RESOURCES;
1452 final_dp_size = efi_dp_size(dp) + sizeof(END);
1454 final_dp = efi_dp_concat(dp, initrd_dp);
1455 final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
1457 final_dp = efi_dp_dup(dp);
1464 if (utf16_utf8_strlen(bo->optional_data)) {
1465 len = utf16_utf8_strlen(bo->optional_data) + 1;
1466 tmp = calloc(1, len);
1470 utf16_utf8_strncpy(&p, bo->optional_data, u16_strlen(bo->optional_data));
1473 ret = eficonfig_set_boot_option(varname, final_dp, final_dp_size, bo->description, tmp);
1474 if (ret != EFI_SUCCESS)
1478 free(bo->optional_data);
1479 free(bo->description);
1480 free(bo->file_info.current_path);
1481 free(bo->initrd_info.current_path);
1482 efi_free_pool(device_dp);
1483 efi_free_pool(initrd_device_dp);
1484 efi_free_pool(initrd_dp);
1485 efi_free_pool(final_dp);
1491 * eficonfig_process_add_boot_option() - handler to add boot option
1493 * @data: pointer to the data for each entry
1494 * Return: status code
1496 static efi_status_t eficonfig_process_add_boot_option(void *data)
1500 struct eficonfig_boot_option *bo = NULL;
1502 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1504 return EFI_OUT_OF_RESOURCES;
1506 ret = eficonfig_get_unused_bootoption(varname, sizeof(varname), &bo->boot_index);
1507 if (ret != EFI_SUCCESS)
1510 ret = eficonfig_edit_boot_option(varname, bo, NULL, 0, " ** Add Boot Option ** ");
1511 if (ret != EFI_SUCCESS)
1514 ret = eficonfig_append_bootorder((u16)bo->boot_index);
1515 if (ret != EFI_SUCCESS)
1521 /* to stay the parent menu */
1522 ret = (ret == EFI_ABORTED) ? EFI_SUCCESS : ret;
1528 * eficonfig_init() - do required initialization for eficonfig command
1530 * Return: status code
1532 static efi_status_t eficonfig_init(void)
1534 efi_status_t ret = EFI_SUCCESS;
1536 struct efi_handler *handler;
1539 ret = efi_search_protocol(efi_root, &efi_guid_text_input_protocol, &handler);
1540 if (ret != EFI_SUCCESS)
1543 ret = efi_protocol_open(handler, (void **)&cin, efi_root, NULL,
1544 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1545 if (ret != EFI_SUCCESS)
1554 static const struct eficonfig_item maintenance_menu_items[] = {
1555 {"Add Boot Option", eficonfig_process_add_boot_option},
1556 {"Quit", eficonfig_process_quit},
1560 * do_eficonfig() - execute `eficonfig` command
1562 * @cmdtp: table entry describing command
1563 * @flag: bitmap indicating how the command was invoked
1564 * @argc: number of arguments
1565 * @argv: command line arguments
1566 * Return: status code
1568 static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1571 struct efimenu *efi_menu;
1574 return CMD_RET_USAGE;
1576 ret = efi_init_obj_list();
1577 if (ret != EFI_SUCCESS) {
1578 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1579 ret & ~EFI_ERROR_MASK);
1581 return CMD_RET_FAILURE;
1584 ret = eficonfig_init();
1585 if (ret != EFI_SUCCESS)
1586 return CMD_RET_FAILURE;
1589 efi_menu = eficonfig_create_fixed_menu(maintenance_menu_items,
1590 ARRAY_SIZE(maintenance_menu_items));
1592 return CMD_RET_FAILURE;
1594 ret = eficonfig_process_common(efi_menu, " ** UEFI Maintenance Menu **");
1595 eficonfig_destroy(efi_menu);
1597 if (ret == EFI_ABORTED)
1601 return CMD_RET_SUCCESS;
1605 eficonfig, 1, 0, do_eficonfig,
1606 "provide menu-driven UEFI variable maintenance interface",