Merge tag 'u-boot-imx-next-20230404' of https://gitlab.denx.de/u-boot/custodians...
[platform/kernel/u-boot.git] / cmd / eficonfig.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Menu-driven UEFI Variable maintenance
4  *
5  *  Copyright (c) 2022 Masahisa Kojima, Linaro Limited
6  */
7
8 #include <ansi.h>
9 #include <cli.h>
10 #include <common.h>
11 #include <charset.h>
12 #include <efi_loader.h>
13 #include <efi_load_initrd.h>
14 #include <efi_config.h>
15 #include <efi_variable.h>
16 #include <log.h>
17 #include <malloc.h>
18 #include <menu.h>
19 #include <sort.h>
20 #include <watchdog.h>
21 #include <asm/unaligned.h>
22 #include <linux/delay.h>
23
24 static struct efi_simple_text_input_protocol *cin;
25 const char *eficonfig_menu_desc =
26         "  Press UP/DOWN to move, ENTER to select, ESC to quit";
27
28 static const char *eficonfig_change_boot_order_desc =
29         "  Press UP/DOWN to move, +/- to change orde\n"
30         "  Press SPACE to activate or deactivate the entry\n"
31         "  CTRL+S to save, ESC to quit";
32
33 static struct efi_simple_text_output_protocol *cout;
34 static int avail_row;
35
36 #define EFICONFIG_DESCRIPTION_MAX 32
37 #define EFICONFIG_OPTIONAL_DATA_MAX 64
38 #define EFICONFIG_MENU_HEADER_ROW_NUM 3
39 #define EFICONFIG_MENU_DESC_ROW_NUM 5
40
41 /**
42  * struct eficonfig_filepath_info - structure to be used to store file path
43  *
44  * @name:       file or directory name
45  * @list:       list structure
46  */
47 struct eficonfig_filepath_info {
48         char *name;
49         struct list_head list;
50 };
51
52 /**
53  * struct eficonfig_boot_option - structure to be used for updating UEFI boot option
54  *
55  * @file_info:          user selected file info
56  * @initrd_info:        user selected initrd file info
57  * @boot_index:         index of the boot option
58  * @description:        pointer to the description string
59  * @optional_data:      pointer to the optional_data
60  * @edit_completed:     flag indicates edit complete
61  */
62 struct eficonfig_boot_option {
63         struct eficonfig_select_file_info file_info;
64         struct eficonfig_select_file_info initrd_info;
65         unsigned int boot_index;
66         u16 *description;
67         u16 *optional_data;
68         bool edit_completed;
69 };
70
71 /**
72  * struct eficonfig_volume_entry_data - structure to be used to store volume info
73  *
74  * @file_info:  pointer to file info structure
75  * @v:          pointer to the protocol interface
76  * @dp:         pointer to the device path
77  */
78 struct eficonfig_volume_entry_data {
79         struct eficonfig_select_file_info *file_info;
80         struct efi_simple_file_system_protocol *v;
81         struct efi_device_path *dp;
82 };
83
84 /**
85  * struct eficonfig_file_entry_data - structure to be used to store file info
86  *
87  * @file_info:          pointer to file info structure
88  * @is_directory:       flag to identify the directory or file
89  * @file_name:          name of directory or file
90  */
91 struct eficonfig_file_entry_data {
92         struct eficonfig_select_file_info *file_info;
93         bool is_directory;
94         char *file_name;
95 };
96
97 /**
98  * struct eficonfig_boot_selection_data - structure to be used to select the boot option entry
99  *
100  * @boot_index: index of the boot option
101  * @selected:           pointer to store the selected index in the BootOrder variable
102  */
103 struct eficonfig_boot_selection_data {
104         u16 boot_index;
105         int *selected;
106 };
107
108 /**
109  * struct eficonfig_boot_order_data - structure to be used to update BootOrder variable
110  *
111  * @boot_index:         boot option index
112  * @active:             flag to include the boot option into BootOrder variable
113  */
114 struct eficonfig_boot_order_data {
115         u32 boot_index;
116         bool active;
117 };
118
119 /**
120  * struct eficonfig_save_boot_order_data - structure to be used to change boot order
121  *
122  * @efi_menu:           pointer to efimenu structure
123  * @selected:           flag to indicate user selects "Save" entry
124  */
125 struct eficonfig_save_boot_order_data {
126         struct efimenu *efi_menu;
127         bool selected;
128 };
129
130 /**
131  * struct eficonfig_menu_adjust - update start and end entry index
132  *
133  * @efi_menu:   pointer to efimenu structure
134  * @add:        flag to add or substract the index
135  */
136 static void eficonfig_menu_adjust(struct efimenu *efi_menu, bool add)
137 {
138         if (add)
139                 ++efi_menu->active;
140         else
141                 --efi_menu->active;
142
143         if (add && efi_menu->end < efi_menu->active) {
144                 efi_menu->start++;
145                 efi_menu->end++;
146         } else if (!add && efi_menu->start > efi_menu->active) {
147                 efi_menu->start--;
148                 efi_menu->end--;
149         }
150 }
151 #define eficonfig_menu_up(_a) eficonfig_menu_adjust(_a, false)
152 #define eficonfig_menu_down(_a) eficonfig_menu_adjust(_a, true)
153
154 /**
155  * eficonfig_print_msg() - print message
156  *
157  * display the message to the user, user proceeds the screen
158  * with any key press.
159  *
160  * @items:              pointer to the structure of each menu entry
161  * @count:              the number of menu entry
162  * @menu_header:        pointer to the menu header string
163  * Return:      status code
164  */
165 void eficonfig_print_msg(char *msg)
166 {
167         /* Flush input */
168         while (tstc())
169                 getchar();
170
171         printf(ANSI_CURSOR_HIDE
172                ANSI_CLEAR_CONSOLE
173                ANSI_CURSOR_POSITION
174                "%s\n\n  Press any key to continue", 3, 4, msg);
175
176         getchar();
177 }
178
179 /**
180  * eficonfig_print_entry() - print each menu entry
181  *
182  * @data:       pointer to the data associated with each menu entry
183  */
184 void eficonfig_print_entry(void *data)
185 {
186         struct eficonfig_entry *entry = data;
187         bool reverse = (entry->efi_menu->active == entry->num);
188
189         if (entry->efi_menu->start > entry->num || entry->efi_menu->end < entry->num)
190                 return;
191
192         printf(ANSI_CURSOR_POSITION, (entry->num - entry->efi_menu->start) +
193                EFICONFIG_MENU_HEADER_ROW_NUM + 1, 7);
194
195         if (reverse)
196                 puts(ANSI_COLOR_REVERSE);
197
198         printf(ANSI_CLEAR_LINE "%s", entry->title);
199
200         if (reverse)
201                 puts(ANSI_COLOR_RESET);
202 }
203
204 /**
205  * eficonfig_display_statusline() - print status line
206  *
207  * @m:  pointer to the menu structure
208  */
209 void eficonfig_display_statusline(struct menu *m)
210 {
211         struct eficonfig_entry *entry;
212
213         if (menu_default_choice(m, (void *)&entry) < 0)
214                 return;
215
216         printf(ANSI_CURSOR_POSITION
217               "\n%s\n"
218                ANSI_CURSOR_POSITION ANSI_CLEAR_LINE ANSI_CURSOR_POSITION
219                "%s"
220                ANSI_CLEAR_LINE_TO_END,
221                1, 1, entry->efi_menu->menu_header, avail_row + 4, 1,
222                avail_row + 5, 1, entry->efi_menu->menu_desc);
223 }
224
225 /**
226  * eficonfig_choice_entry() - user key input handler
227  *
228  * @data:       pointer to the efimenu structure
229  * Return:      key string to identify the selected entry
230  */
231 char *eficonfig_choice_entry(void *data)
232 {
233         struct cli_ch_state s_cch, *cch = &s_cch;
234         struct list_head *pos, *n;
235         struct eficonfig_entry *entry;
236         enum bootmenu_key key = BKEY_NONE;
237         struct efimenu *efi_menu = data;
238
239         cli_ch_init(cch);
240
241         while (1) {
242                 key = bootmenu_loop((struct bootmenu_data *)efi_menu, cch);
243
244                 switch (key) {
245                 case BKEY_UP:
246                         if (efi_menu->active > 0)
247                                 eficonfig_menu_up(efi_menu);
248
249                         /* no menu key selected, regenerate menu */
250                         return NULL;
251                 case BKEY_DOWN:
252                         if (efi_menu->active < efi_menu->count - 1)
253                                 eficonfig_menu_down(efi_menu);
254
255                         /* no menu key selected, regenerate menu */
256                         return NULL;
257                 case BKEY_SELECT:
258                         list_for_each_safe(pos, n, &efi_menu->list) {
259                                 entry = list_entry(pos, struct eficonfig_entry, list);
260                                 if (entry->num == efi_menu->active)
261                                         return entry->key;
262                         }
263                         break;
264                 case BKEY_QUIT:
265                         /* Quit by choosing the last entry */
266                         entry = list_last_entry(&efi_menu->list, struct eficonfig_entry, list);
267                         return entry->key;
268                 default:
269                         /* Pressed key is not valid, no need to regenerate the menu */
270                         break;
271                 }
272         }
273 }
274
275 /**
276  * eficonfig_destroy() - destroy efimenu
277  *
278  * @efi_menu:   pointer to the efimenu structure
279  */
280 void eficonfig_destroy(struct efimenu *efi_menu)
281 {
282         struct list_head *pos, *n;
283         struct eficonfig_entry *entry;
284
285         if (!efi_menu)
286                 return;
287
288         list_for_each_safe(pos, n, &efi_menu->list) {
289                 entry = list_entry(pos, struct eficonfig_entry, list);
290                 free(entry->title);
291                 list_del(&entry->list);
292                 free(entry);
293         }
294         free(efi_menu->menu_header);
295         free(efi_menu);
296 }
297
298 /**
299  * eficonfig_process_quit() - callback function for "Quit" entry
300  *
301  * @data:       pointer to the data
302  * Return:      status code
303  */
304 efi_status_t eficonfig_process_quit(void *data)
305 {
306         return EFI_ABORTED;
307 }
308
309 /**
310  * eficonfig_append_menu_entry() - append menu item
311  *
312  * @efi_menu:   pointer to the efimenu structure
313  * @title:      pointer to the entry title
314  * @func:       callback of each entry
315  * @data:       pointer to the data to be passed to each entry callback
316  * Return:      status code
317  */
318 efi_status_t eficonfig_append_menu_entry(struct efimenu *efi_menu,
319                                          char *title, eficonfig_entry_func func,
320                                          void *data)
321 {
322         struct eficonfig_entry *entry;
323
324         if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX)
325                 return EFI_OUT_OF_RESOURCES;
326
327         entry = calloc(1, sizeof(struct eficonfig_entry));
328         if (!entry)
329                 return EFI_OUT_OF_RESOURCES;
330
331         entry->title = title;
332         sprintf(entry->key, "%d", efi_menu->count);
333         entry->efi_menu = efi_menu;
334         entry->func = func;
335         entry->data = data;
336         entry->num = efi_menu->count++;
337         list_add_tail(&entry->list, &efi_menu->list);
338
339         return EFI_SUCCESS;
340 }
341
342 /**
343  * eficonfig_append_quit_entry() - append quit entry
344  *
345  * @efi_menu:   pointer to the efimenu structure
346  * Return:      status code
347  */
348 efi_status_t eficonfig_append_quit_entry(struct efimenu *efi_menu)
349 {
350         char *title;
351         efi_status_t ret;
352
353         title = strdup("Quit");
354         if (!title)
355                 return EFI_OUT_OF_RESOURCES;
356
357         ret = eficonfig_append_menu_entry(efi_menu, title, eficonfig_process_quit, NULL);
358         if (ret != EFI_SUCCESS)
359                 free(title);
360
361         return ret;
362 }
363
364 /**
365  * eficonfig_create_fixed_menu() - create fixed entry menu structure
366  *
367  * @items:      pointer to the menu entry item
368  * @count:      the number of menu entry
369  * Return:      pointer to the efimenu structure
370  */
371 void *eficonfig_create_fixed_menu(const struct eficonfig_item *items, int count)
372 {
373         u32 i;
374         char *title;
375         efi_status_t ret;
376         struct efimenu *efi_menu;
377         const struct eficonfig_item *iter = items;
378
379         efi_menu = calloc(1, sizeof(struct efimenu));
380         if (!efi_menu)
381                 return NULL;
382
383         INIT_LIST_HEAD(&efi_menu->list);
384         for (i = 0; i < count; i++, iter++) {
385                 title = strdup(iter->title);
386                 if (!title)
387                         goto out;
388
389                 ret = eficonfig_append_menu_entry(efi_menu, title, iter->func, iter->data);
390                 if (ret != EFI_SUCCESS) {
391                         free(title);
392                         goto out;
393                 }
394         }
395
396         return efi_menu;
397 out:
398         eficonfig_destroy(efi_menu);
399
400         return NULL;
401 }
402
403 /**
404  * eficonfig_process_common() - main handler for UEFI menu
405  *
406  * Construct the structures required to show the menu, then handle
407  * the user input interacting with u-boot menu functions.
408  *
409  * @efi_menu:           pointer to the efimenu structure
410  * @menu_header:        pointer to the menu header string
411  * @menu_desc:          pointer to the menu description
412  * @display_statusline: function pointer to draw statusline
413  * @item_data_print:    function pointer to draw the menu item
414  * @item_choice:        function pointer to handle the key press
415  * Return:              status code
416  */
417 efi_status_t eficonfig_process_common(struct efimenu *efi_menu,
418                                       char *menu_header, const char *menu_desc,
419                                       void (*display_statusline)(struct menu *),
420                                       void (*item_data_print)(void *),
421                                       char *(*item_choice)(void *))
422 {
423         struct menu *menu;
424         void *choice = NULL;
425         struct list_head *pos, *n;
426         struct eficonfig_entry *entry;
427         efi_status_t ret = EFI_SUCCESS;
428
429         if (efi_menu->count > EFICONFIG_ENTRY_NUM_MAX)
430                 return EFI_OUT_OF_RESOURCES;
431
432         efi_menu->delay = -1;
433         efi_menu->active = 0;
434         efi_menu->start = 0;
435         efi_menu->end = avail_row - 1;
436
437         if (menu_header) {
438                 efi_menu->menu_header = strdup(menu_header);
439                 if (!efi_menu->menu_header)
440                         return EFI_OUT_OF_RESOURCES;
441         }
442         if (menu_desc)
443                 efi_menu->menu_desc = menu_desc;
444
445         menu = menu_create(NULL, 0, 1, display_statusline, item_data_print,
446                            item_choice, efi_menu);
447         if (!menu)
448                 return EFI_INVALID_PARAMETER;
449
450         list_for_each_safe(pos, n, &efi_menu->list) {
451                 entry = list_entry(pos, struct eficonfig_entry, list);
452                 if (!menu_item_add(menu, entry->key, entry)) {
453                         ret = EFI_INVALID_PARAMETER;
454                         goto out;
455                 }
456         }
457
458         entry = list_first_entry_or_null(&efi_menu->list, struct eficonfig_entry, list);
459         if (entry)
460                 menu_default_set(menu, entry->key);
461
462         printf(ANSI_CURSOR_HIDE
463                ANSI_CLEAR_CONSOLE
464                ANSI_CURSOR_POSITION, 1, 1);
465
466         if (menu_get_choice(menu, &choice)) {
467                 entry = choice;
468                 if (entry->func)
469                         ret = entry->func(entry->data);
470         }
471 out:
472         menu_destroy(menu);
473
474         printf(ANSI_CLEAR_CONSOLE
475                ANSI_CURSOR_POSITION
476                ANSI_CURSOR_SHOW, 1, 1);
477
478         return ret;
479 }
480
481 /**
482  * eficonfig_volume_selected() - handler of volume selection
483  *
484  * @data:       pointer to the data of selected entry
485  * Return:      status code
486  */
487 static efi_status_t eficonfig_volume_selected(void *data)
488 {
489         struct eficonfig_volume_entry_data *info = data;
490
491         if (info) {
492                 info->file_info->current_volume = info->v;
493                 info->file_info->dp_volume = info->dp;
494         }
495
496         return EFI_SUCCESS;
497 }
498
499 /**
500  * eficonfig_create_device_path() - create device path
501  *
502  * @dp_volume:  pointer to the volume
503  * @current_path: pointer to the file path u16 string
504  * Return:
505  * device path or NULL. Caller must free the returned value
506  */
507 struct efi_device_path *eficonfig_create_device_path(struct efi_device_path *dp_volume,
508                                                      u16 *current_path)
509 {
510         char *p;
511         void *buf;
512         efi_uintn_t fp_size;
513         struct efi_device_path *dp;
514         struct efi_device_path_file_path *fp;
515
516         fp_size = sizeof(struct efi_device_path) + u16_strsize(current_path);
517         buf = calloc(1, fp_size + sizeof(END));
518         if (!buf)
519                 return NULL;
520
521         fp = buf;
522         fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
523         fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
524         fp->dp.length = (u16)fp_size;
525         u16_strcpy(fp->str, current_path);
526
527         p = buf;
528         p += fp_size;
529         *((struct efi_device_path *)p) = END;
530
531         dp = efi_dp_append(dp_volume, (struct efi_device_path *)buf);
532         free(buf);
533
534         return dp;
535 }
536
537 /**
538  * eficonfig_file_selected() - handler of file selection
539  *
540  * @data:       pointer to the data of selected entry
541  * Return:      status code
542  */
543 static efi_status_t eficonfig_file_selected(void *data)
544 {
545         u16 *tmp;
546         struct eficonfig_file_entry_data *info = data;
547
548         if (!info)
549                 return EFI_INVALID_PARAMETER;
550
551         if (!strcmp(info->file_name, "..\\")) {
552                 struct eficonfig_filepath_info *iter;
553                 struct list_head *pos, *n;
554                 int is_last;
555                 char *filepath;
556                 tmp = info->file_info->current_path;
557
558                 memset(info->file_info->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
559                 filepath = calloc(1, EFICONFIG_FILE_PATH_MAX);
560                 if (!filepath)
561                         return EFI_OUT_OF_RESOURCES;
562
563                 list_for_each_safe(pos, n, &info->file_info->filepath_list) {
564                         iter = list_entry(pos, struct eficonfig_filepath_info, list);
565
566                         is_last = list_is_last(&iter->list, &info->file_info->filepath_list);
567                         if (is_last) {
568                                 list_del(&iter->list);
569                                 free(iter->name);
570                                 free(iter);
571                                 break;
572                         }
573                         strlcat(filepath, iter->name, EFICONFIG_FILE_PATH_MAX);
574                 }
575                 utf8_utf16_strcpy(&tmp, filepath);
576         } else {
577                 size_t new_len;
578                 struct eficonfig_filepath_info *filepath_info;
579
580                 new_len = u16_strlen(info->file_info->current_path) +
581                                      strlen(info->file_name);
582                 if (new_len >= EFICONFIG_FILE_PATH_MAX) {
583                         eficonfig_print_msg("File path is too long!");
584                         return EFI_INVALID_PARAMETER;
585                 }
586                 tmp = &info->file_info->current_path[u16_strlen(info->file_info->current_path)];
587                 utf8_utf16_strcpy(&tmp, info->file_name);
588
589                 filepath_info = calloc(1, sizeof(struct eficonfig_filepath_info));
590                 if (!filepath_info)
591                         return EFI_OUT_OF_RESOURCES;
592
593                 filepath_info->name = strdup(info->file_name);
594                 if (!filepath_info->name) {
595                         free(filepath_info);
596                         return EFI_OUT_OF_RESOURCES;
597                 }
598                 list_add_tail(&filepath_info->list, &info->file_info->filepath_list);
599
600                 if (!info->is_directory)
601                         info->file_info->file_selected = true;
602         }
603
604         return EFI_SUCCESS;
605 }
606
607 /**
608  * eficonfig_select_volume() - construct the volume selection menu
609  *
610  * @file_info:  pointer to the file selection structure
611  * Return:      status code
612  */
613 static efi_status_t eficonfig_select_volume(struct eficonfig_select_file_info *file_info)
614 {
615         u32 i;
616         efi_status_t ret;
617         efi_uintn_t count;
618         struct efimenu *efi_menu;
619         struct list_head *pos, *n;
620         struct efi_handler *handler;
621         struct eficonfig_entry *entry;
622         struct efi_device_path *device_path;
623         efi_handle_t *volume_handles = NULL;
624         struct efi_simple_file_system_protocol *v;
625
626         ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
627                                            NULL, &count, (efi_handle_t **)&volume_handles);
628         if (ret != EFI_SUCCESS) {
629                 eficonfig_print_msg("No block device found!");
630                 return ret;
631         }
632
633         efi_menu = calloc(1, sizeof(struct efimenu));
634         if (!efi_menu)
635                 return EFI_OUT_OF_RESOURCES;
636
637         INIT_LIST_HEAD(&efi_menu->list);
638         for (i = 0; i < count; i++) {
639                 char *devname;
640                 struct efi_block_io *block_io;
641                 struct eficonfig_volume_entry_data *info;
642
643                 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
644                         break;
645
646                 ret = efi_search_protocol(volume_handles[i],
647                                           &efi_simple_file_system_protocol_guid, &handler);
648                 if (ret != EFI_SUCCESS)
649                         continue;
650                 ret = efi_protocol_open(handler, (void **)&v, efi_root, NULL,
651                                         EFI_OPEN_PROTOCOL_GET_PROTOCOL);
652                 if (ret != EFI_SUCCESS)
653                         continue;
654
655                 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
656                 if (ret != EFI_SUCCESS)
657                         continue;
658                 ret = efi_protocol_open(handler, (void **)&device_path,
659                                         efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
660                 if (ret != EFI_SUCCESS)
661                         continue;
662
663                 ret = efi_search_protocol(volume_handles[i], &efi_block_io_guid, &handler);
664                 if (ret != EFI_SUCCESS)
665                         continue;
666                 ret = efi_protocol_open(handler, (void **)&block_io,
667                                         efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
668                 if (ret != EFI_SUCCESS)
669                         continue;
670
671                 info = calloc(1, sizeof(struct eficonfig_volume_entry_data));
672                 if (!info) {
673                         ret = EFI_OUT_OF_RESOURCES;
674                         goto out;
675                 }
676
677                 devname = calloc(1, BOOTMENU_DEVICE_NAME_MAX);
678                 if (!devname) {
679                         free(info);
680                         ret = EFI_OUT_OF_RESOURCES;
681                         goto out;
682                 }
683                 ret = efi_disk_get_device_name(volume_handles[i], devname,
684                                                BOOTMENU_DEVICE_NAME_MAX);
685                 if (ret != EFI_SUCCESS) {
686                         free(info);
687                         goto out;
688                 }
689
690                 info->v = v;
691                 info->dp = device_path;
692                 info->file_info = file_info;
693                 ret = eficonfig_append_menu_entry(efi_menu, devname, eficonfig_volume_selected,
694                                                   info);
695                 if (ret != EFI_SUCCESS) {
696                         free(info);
697                         goto out;
698                 }
699         }
700
701         ret = eficonfig_append_quit_entry(efi_menu);
702         if (ret != EFI_SUCCESS)
703                 goto out;
704
705         ret = eficonfig_process_common(efi_menu, "  ** Select Volume **",
706                                        eficonfig_menu_desc,
707                                        eficonfig_display_statusline,
708                                        eficonfig_print_entry,
709                                        eficonfig_choice_entry);
710
711 out:
712         efi_free_pool(volume_handles);
713         list_for_each_safe(pos, n, &efi_menu->list) {
714                 entry = list_entry(pos, struct eficonfig_entry, list);
715                 free(entry->data);
716         }
717         eficonfig_destroy(efi_menu);
718
719         return ret;
720 }
721
722 /**
723  * sort_file() - sort the file name in ascii order
724  *
725  * @data1:      pointer to the file entry data
726  * @data2:      pointer to the file entry data
727  * Return:      -1 if the data1 file name is less than data2 file name,
728  *              0 if both file name match,
729  *              1 if the data1 file name is greater thant data2 file name.
730  */
731 static int sort_file(const void *arg1, const void *arg2)
732 {
733         const struct eficonfig_file_entry_data *data1, *data2;
734
735         data1 = *((const struct eficonfig_file_entry_data **)arg1);
736         data2 = *((const struct eficonfig_file_entry_data **)arg2);
737
738         return strcasecmp(data1->file_name, data2->file_name);
739 }
740
741 /**
742  * eficonfig_create_file_entry() - construct the file menu entry
743  *
744  * @efi_menu:   pointer to the efimenu structure
745  * @count:      number of the directory and file
746  * @tmp_infos:  pointer to the entry data array
747  * @f:          pointer to the file handle
748  * @buf:        pointer to the buffer to store the directory information
749  * @file_info:  pointer to the file selection structure
750  * Return:      status code
751  */
752 static efi_status_t
753 eficonfig_create_file_entry(struct efimenu *efi_menu, u32 count,
754                             struct eficonfig_file_entry_data **tmp_infos,
755                             struct efi_file_handle *f, struct efi_file_info *buf,
756                             struct eficonfig_select_file_info *file_info)
757 {
758         char *name, *p;
759         efi_uintn_t len;
760         efi_status_t ret;
761         u32 i, entry_num = 0;
762         struct eficonfig_file_entry_data *info;
763
764         EFI_CALL(f->setpos(f, 0));
765         /* Read directory and construct menu structure */
766         for (i = 0; i < count; i++) {
767                 if (entry_num >= EFICONFIG_ENTRY_NUM_MAX - 1)
768                         break;
769
770                 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
771                 ret = EFI_CALL(f->read(f, &len, buf));
772                 if (ret != EFI_SUCCESS || len == 0)
773                         break;
774
775                 info = calloc(1, sizeof(struct eficonfig_file_entry_data));
776                 if (!info) {
777                         ret = EFI_OUT_OF_RESOURCES;
778                         goto out;
779                 }
780
781                 /* append '\\' at the end of directory name */
782                 name = calloc(1, utf16_utf8_strlen(buf->file_name) + 2);
783                 if (!name) {
784                         ret = EFI_OUT_OF_RESOURCES;
785                         free(info);
786                         goto out;
787                 }
788                 p = name;
789                 utf16_utf8_strcpy(&p, buf->file_name);
790                 if (buf->attribute & EFI_FILE_DIRECTORY) {
791                         /* filter out u'.' */
792                         if (!u16_strcmp(buf->file_name, u".")) {
793                                 free(info);
794                                 free(name);
795                                 continue;
796                         }
797                         name[u16_strlen(buf->file_name)] = '\\';
798                         info->is_directory = true;
799                 }
800
801                 info->file_name = name;
802                 info->file_info = file_info;
803                 tmp_infos[entry_num++] = info;
804         }
805
806         qsort(tmp_infos, entry_num, sizeof(*tmp_infos),
807               (int (*)(const void *, const void *))sort_file);
808
809         for (i = 0; i < entry_num; i++) {
810                 ret = eficonfig_append_menu_entry(efi_menu, tmp_infos[i]->file_name,
811                                                   eficonfig_file_selected, tmp_infos[i]);
812                 if (ret != EFI_SUCCESS)
813                         goto out;
814         }
815
816 out:
817         return ret;
818 }
819
820 /**
821  * eficonfig_show_file_selection() - construct the file selection menu
822  *
823  * @file_info:  pointer to the file selection structure
824  * @root:       pointer to the file handle
825  * Return:      status code
826  */
827 static efi_status_t eficonfig_show_file_selection(struct eficonfig_select_file_info *file_info,
828                                                   struct efi_file_handle *root)
829 {
830         u32 count = 0, i;
831         efi_uintn_t len;
832         efi_status_t ret;
833         struct efimenu *efi_menu;
834         struct efi_file_handle *f;
835         struct efi_file_info *buf;
836         struct eficonfig_file_entry_data **tmp_infos;
837
838         buf = calloc(1, sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE);
839         if (!buf)
840                 return EFI_OUT_OF_RESOURCES;
841
842         while (!file_info->file_selected) {
843                 efi_menu = calloc(1, sizeof(struct efimenu));
844                 if (!efi_menu) {
845                         ret = EFI_OUT_OF_RESOURCES;
846                         goto out;
847                 }
848                 INIT_LIST_HEAD(&efi_menu->list);
849
850                 ret = EFI_CALL(root->open(root, &f, file_info->current_path,
851                                           EFI_FILE_MODE_READ, 0));
852                 if (ret != EFI_SUCCESS) {
853                         eficonfig_print_msg("Reading volume failed!");
854                         free(efi_menu);
855                         ret = EFI_ABORTED;
856                         goto out;
857                 }
858
859                 /* Count the number of directory entries */
860                 for (;;) {
861                         len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
862                         ret = EFI_CALL(f->read(f, &len, buf));
863                         if (ret != EFI_SUCCESS || len == 0)
864                                 break;
865
866                         count++;
867                 }
868
869                 /* allocate array to sort the entry */
870                 tmp_infos = calloc(count, sizeof(*tmp_infos));
871                 if (!tmp_infos) {
872                         ret = EFI_OUT_OF_RESOURCES;
873                         goto err;
874                 }
875
876                 ret = eficonfig_create_file_entry(efi_menu, count, tmp_infos,
877                                                   f, buf, file_info);
878                 if (ret != EFI_SUCCESS)
879                         goto err;
880
881                 ret = eficonfig_append_quit_entry(efi_menu);
882                 if (ret != EFI_SUCCESS)
883                         goto err;
884
885                 ret = eficonfig_process_common(efi_menu, "  ** Select File **",
886                                                eficonfig_menu_desc,
887                                                eficonfig_display_statusline,
888                                                eficonfig_print_entry,
889                                                eficonfig_choice_entry);
890 err:
891                 EFI_CALL(f->close(f));
892                 eficonfig_destroy(efi_menu);
893
894                 if (tmp_infos) {
895                         for (i = 0; i < count; i++)
896                                 free(tmp_infos[i]);
897                 }
898
899                 free(tmp_infos);
900
901                 if (ret != EFI_SUCCESS)
902                         break;
903         }
904
905 out:
906         free(buf);
907
908         return ret;
909 }
910
911 /**
912  * handle_user_input() - handle user input
913  *
914  * @buf:        pointer to the buffer
915  * @buf_size:   size of the buffer
916  * @cursor_col: cursor column for user input
917  * @msg:        pointer to the string to display
918  * Return:      status code
919  */
920 static efi_status_t handle_user_input(u16 *buf, int buf_size,
921                                       int cursor_col, char *msg)
922 {
923         u16 *tmp;
924         efi_status_t ret;
925
926         printf(ANSI_CLEAR_CONSOLE
927                ANSI_CURSOR_POSITION
928                "%s"
929                ANSI_CURSOR_POSITION
930                "  Press ENTER to complete, ESC to quit",
931                0, 1, msg, 8, 1);
932
933         /* tmp is used to accept user cancel */
934         tmp = calloc(1, buf_size * sizeof(u16));
935         if (!tmp)
936                 return EFI_OUT_OF_RESOURCES;
937
938         ret = efi_console_get_u16_string(cin, tmp, buf_size, NULL, 4, cursor_col);
939         if (ret == EFI_SUCCESS)
940                 u16_strcpy(buf, tmp);
941
942         free(tmp);
943
944         /* to stay the parent menu */
945         ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
946
947         return ret;
948 }
949
950 /**
951  * eficonfig_boot_add_enter_description() - handle user input for description
952  *
953  * @data:       pointer to the internal boot option structure
954  * Return:      status code
955  */
956 static efi_status_t eficonfig_boot_add_enter_description(void *data)
957 {
958         struct eficonfig_boot_option *bo = data;
959
960         return handle_user_input(bo->description, EFICONFIG_DESCRIPTION_MAX, 22,
961                                  "\n  ** Edit Description **\n"
962                                  "\n"
963                                  "  enter description: ");
964 }
965
966 /**
967  * eficonfig_boot_add_optional_data() - handle user input for optional data
968  *
969  * @data:       pointer to the internal boot option structure
970  * Return:      status code
971  */
972 static efi_status_t eficonfig_boot_add_optional_data(void *data)
973 {
974         struct eficonfig_boot_option *bo = data;
975
976         return handle_user_input(bo->optional_data, EFICONFIG_OPTIONAL_DATA_MAX, 24,
977                                  "\n  ** Edit Optional Data **\n"
978                                  "\n"
979                                  "  enter optional data:");
980 }
981
982 /**
983  * eficonfig_boot_edit_save() - handler to save the boot option
984  *
985  * @data:       pointer to the internal boot option structure
986  * Return:      status code
987  */
988 static efi_status_t eficonfig_boot_edit_save(void *data)
989 {
990         struct eficonfig_boot_option *bo = data;
991
992         if (u16_strlen(bo->description) == 0) {
993                 eficonfig_print_msg("Boot Description is empty!");
994                 bo->edit_completed = false;
995                 return EFI_NOT_READY;
996         }
997         if (u16_strlen(bo->file_info.current_path) == 0) {
998                 eficonfig_print_msg("File is not selected!");
999                 bo->edit_completed = false;
1000                 return EFI_NOT_READY;
1001         }
1002
1003         bo->edit_completed = true;
1004
1005         return EFI_SUCCESS;
1006 }
1007
1008 /**
1009  * eficonfig_process_clear_file_selection() - callback function for "Clear" entry
1010  *
1011  * @data:       pointer to the data
1012  * Return:      status code
1013  */
1014 efi_status_t eficonfig_process_clear_file_selection(void *data)
1015 {
1016         struct eficonfig_select_file_info *file_info = data;
1017
1018         /* clear the existing file information */
1019         file_info->current_volume = NULL;
1020         file_info->current_path[0] = u'\0';
1021         file_info->dp_volume = NULL;
1022
1023         return EFI_ABORTED;
1024 }
1025
1026 static struct eficonfig_item select_file_menu_items[] = {
1027         {"Select File", eficonfig_process_select_file},
1028         {"Clear", eficonfig_process_clear_file_selection},
1029         {"Quit", eficonfig_process_quit},
1030 };
1031
1032 /**
1033  * eficonfig_process_show_file_option() - display select file option
1034  *
1035  * @file_info:  pointer to the file information structure
1036  * Return:      status code
1037  */
1038 efi_status_t eficonfig_process_show_file_option(void *data)
1039 {
1040         efi_status_t ret;
1041         struct efimenu *efi_menu;
1042
1043         select_file_menu_items[0].data = data;
1044         select_file_menu_items[1].data = data;
1045         efi_menu = eficonfig_create_fixed_menu(select_file_menu_items,
1046                                                ARRAY_SIZE(select_file_menu_items));
1047         if (!efi_menu)
1048                 return EFI_OUT_OF_RESOURCES;
1049
1050         ret = eficonfig_process_common(efi_menu, "  ** Update File **",
1051                                        eficonfig_menu_desc,
1052                                        eficonfig_display_statusline,
1053                                        eficonfig_print_entry,
1054                                        eficonfig_choice_entry);
1055         if (ret != EFI_SUCCESS) /* User selects "Clear" or "Quit" */
1056                 ret = EFI_NOT_READY;
1057
1058         eficonfig_destroy(efi_menu);
1059
1060         return ret;
1061 }
1062
1063 /**
1064  * eficonfig_process_select_file() - handle user file selection
1065  *
1066  * @data:       pointer to the data
1067  * Return:      status code
1068  */
1069 efi_status_t eficonfig_process_select_file(void *data)
1070 {
1071         size_t len;
1072         efi_status_t ret;
1073         struct list_head *pos, *n;
1074         struct efi_file_handle *root;
1075         struct eficonfig_filepath_info *item;
1076         struct eficonfig_select_file_info *tmp = NULL;
1077         struct eficonfig_select_file_info *file_info = data;
1078
1079         tmp = calloc(1, sizeof(struct eficonfig_select_file_info));
1080         if (!tmp)
1081                 return EFI_OUT_OF_RESOURCES;
1082
1083         tmp->current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1084         if (!tmp->current_path) {
1085                 free(tmp);
1086                 return EFI_OUT_OF_RESOURCES;
1087         }
1088         INIT_LIST_HEAD(&tmp->filepath_list);
1089
1090         while (!tmp->file_selected) {
1091                 tmp->current_volume = NULL;
1092                 memset(tmp->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
1093
1094                 ret = eficonfig_select_volume(tmp);
1095                 if (ret != EFI_SUCCESS)
1096                         goto out;
1097
1098                 if (!tmp->current_volume)
1099                         return EFI_INVALID_PARAMETER;
1100
1101                 ret = EFI_CALL(tmp->current_volume->open_volume(tmp->current_volume, &root));
1102                 if (ret != EFI_SUCCESS)
1103                         goto out;
1104
1105                 ret = eficonfig_show_file_selection(tmp, root);
1106                 if (ret == EFI_ABORTED)
1107                         continue;
1108                 if (ret != EFI_SUCCESS)
1109                         goto out;
1110         }
1111
1112 out:
1113         if (ret == EFI_SUCCESS) {
1114                 len = u16_strlen(tmp->current_path);
1115                 len = (len >= EFICONFIG_FILE_PATH_MAX) ? (EFICONFIG_FILE_PATH_MAX - 1) : len;
1116                 memcpy(file_info->current_path, tmp->current_path, len * sizeof(u16));
1117                 file_info->current_path[len] = u'\0';
1118                 file_info->current_volume = tmp->current_volume;
1119                 file_info->dp_volume = tmp->dp_volume;
1120         }
1121
1122         list_for_each_safe(pos, n, &tmp->filepath_list) {
1123                 item = list_entry(pos, struct eficonfig_filepath_info, list);
1124                 list_del(&item->list);
1125                 free(item->name);
1126                 free(item);
1127         }
1128         free(tmp->current_path);
1129         free(tmp);
1130
1131         /* to stay the parent menu */
1132         ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1133
1134         return ret;
1135 }
1136
1137 /**
1138  * eficonfig_get_unused_bootoption() - get unused "Boot####" index
1139  *
1140  * @buf:        pointer to the buffer to store boot option variable name
1141  * @buf_size:   buffer size
1142  * @index:      pointer to store the index in the BootOrder variable
1143  * Return:      status code
1144  */
1145 efi_status_t eficonfig_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
1146                                              unsigned int *index)
1147 {
1148         u32 i;
1149         efi_status_t ret;
1150         efi_uintn_t size;
1151
1152         if (buf_size < u16_strsize(u"Boot####"))
1153                 return EFI_BUFFER_TOO_SMALL;
1154
1155         for (i = 0; i <= 0xFFFF; i++) {
1156                 size = 0;
1157                 efi_create_indexed_name(buf, buf_size, "Boot", i);
1158                 ret = efi_get_variable_int(buf, &efi_global_variable_guid,
1159                                            NULL, &size, NULL, NULL);
1160                 if (ret == EFI_BUFFER_TOO_SMALL)
1161                         continue;
1162                 else
1163                         break;
1164         }
1165
1166         if (i > 0xFFFF)
1167                 return EFI_OUT_OF_RESOURCES;
1168
1169         *index = i;
1170
1171         return EFI_SUCCESS;
1172 }
1173
1174 /**
1175  * eficonfig_set_boot_option() - set boot option
1176  *
1177  * @varname:            pointer to variable name
1178  * @dp:                 pointer to device path
1179  * @label:              pointer to label string
1180  * @optional_data:      pointer to optional data
1181  * Return:              status code
1182  */
1183 static efi_status_t eficonfig_set_boot_option(u16 *varname, struct efi_device_path *dp,
1184                                               efi_uintn_t dp_size, u16 *label, char *optional_data)
1185 {
1186         void *p = NULL;
1187         efi_status_t ret;
1188         efi_uintn_t size;
1189         struct efi_load_option lo;
1190
1191         lo.file_path = dp;
1192         lo.file_path_length = dp_size;
1193         lo.attributes = LOAD_OPTION_ACTIVE;
1194         lo.optional_data = optional_data;
1195         lo.label = label;
1196
1197         size = efi_serialize_load_option(&lo, (u8 **)&p);
1198         if (!size)
1199                 return EFI_INVALID_PARAMETER;
1200
1201         ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1202                                    EFI_VARIABLE_NON_VOLATILE |
1203                                    EFI_VARIABLE_BOOTSERVICE_ACCESS |
1204                                    EFI_VARIABLE_RUNTIME_ACCESS,
1205                                    size, p, false);
1206         free(p);
1207
1208         return ret;
1209 }
1210
1211 /**
1212  * eficonfig_append_bootorder() - append new boot option in BootOrder variable
1213  *
1214  * @index:      "Boot####" index to append to BootOrder variable
1215  * Return:      status code
1216  */
1217 efi_status_t eficonfig_append_bootorder(u16 index)
1218 {
1219         u16 *bootorder;
1220         efi_status_t ret;
1221         u16 *new_bootorder = NULL;
1222         efi_uintn_t last, size, new_size;
1223
1224         /* append new boot option */
1225         bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1226         last = size / sizeof(u16);
1227         new_size = size + sizeof(u16);
1228         new_bootorder = calloc(1, new_size);
1229         if (!new_bootorder) {
1230                 ret = EFI_OUT_OF_RESOURCES;
1231                 goto out;
1232         }
1233         memcpy(new_bootorder, bootorder, size);
1234         new_bootorder[last] = index;
1235
1236         ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1237                                    EFI_VARIABLE_NON_VOLATILE |
1238                                    EFI_VARIABLE_BOOTSERVICE_ACCESS |
1239                                    EFI_VARIABLE_RUNTIME_ACCESS,
1240                                    new_size, new_bootorder, false);
1241         if (ret != EFI_SUCCESS)
1242                 goto out;
1243
1244 out:
1245         free(bootorder);
1246         free(new_bootorder);
1247
1248         return ret;
1249 }
1250
1251 /**
1252  * create_boot_option_entry() - create boot option entry
1253  *
1254  * @efi_menu:   pointer to the efimenu structure
1255  * @title:      pointer to the entry title
1256  * @val:        pointer to boot option label
1257  * @func:       callback of each entry
1258  * @data:       pointer to the data to be passed to each entry callback
1259  * Return:      status code
1260  */
1261 static efi_status_t create_boot_option_entry(struct efimenu *efi_menu, char *title, u16 *val,
1262                                              eficonfig_entry_func func, void *data)
1263 {
1264         u32 len;
1265         char *p, *buf;
1266
1267         len = strlen(title) + 1;
1268         if (val)
1269                 len += utf16_utf8_strlen(val);
1270         buf = calloc(1, len);
1271         if (!buf)
1272                 return EFI_OUT_OF_RESOURCES;
1273
1274         strcpy(buf, title);
1275         if (val) {
1276                 p = buf + strlen(title);
1277                 utf16_utf8_strcpy(&p, val);
1278         }
1279
1280         return eficonfig_append_menu_entry(efi_menu, buf, func, data);
1281 }
1282
1283 /**
1284  * prepare_file_selection_entry() - prepare file selection entry
1285  *
1286  * @efi_menu:   pointer to the efimenu structure
1287  * @title:      pointer to the title string
1288  * @file_info:  pointer to the file info
1289  * Return:      status code
1290  */
1291 static efi_status_t prepare_file_selection_entry(struct efimenu *efi_menu, char *title,
1292                                                  struct eficonfig_select_file_info *file_info)
1293 {
1294         u32 len;
1295         efi_status_t ret;
1296         u16 *file_name = NULL, *p;
1297         efi_handle_t handle;
1298         char *devname;
1299
1300         devname = calloc(1, EFICONFIG_VOLUME_PATH_MAX + 1);
1301         if (!devname)
1302                 return EFI_OUT_OF_RESOURCES;
1303
1304         /* get the device name only when the user already selected the file path */
1305         handle = efi_dp_find_obj(file_info->dp_volume, NULL, NULL);
1306         if (handle) {
1307                 ret = efi_disk_get_device_name(handle, devname, EFICONFIG_VOLUME_PATH_MAX);
1308                 if (ret != EFI_SUCCESS)
1309                         goto out;
1310         }
1311
1312         /*
1313          * If the preconfigured volume does not exist in the system, display the text
1314          * converted volume device path instead of U-Boot friendly name(e.g. "usb 0:1").
1315          */
1316         if (!handle && file_info->dp_volume) {
1317                 u16 *dp_str;
1318                 char *q = devname;
1319
1320                 dp_str = efi_dp_str(file_info->dp_volume);
1321                 if (dp_str)
1322                         utf16_utf8_strncpy(&q, dp_str, EFICONFIG_VOLUME_PATH_MAX);
1323
1324                 efi_free_pool(dp_str);
1325         }
1326
1327         /* append u'/' to devname, it is just for display purpose. */
1328         if (file_info->current_path[0] != u'\0' && file_info->current_path[0] != u'/')
1329                 strlcat(devname, "/", EFICONFIG_VOLUME_PATH_MAX + 1);
1330
1331         len = strlen(devname);
1332         len += utf16_utf8_strlen(file_info->current_path) + 1;
1333         file_name = calloc(1, len * sizeof(u16));
1334         if (!file_name) {
1335                 ret = EFI_OUT_OF_RESOURCES;
1336                 goto out;
1337         }
1338
1339         p = file_name;
1340         utf8_utf16_strcpy(&p, devname);
1341         u16_strlcat(file_name, file_info->current_path, len);
1342         ret = create_boot_option_entry(efi_menu, title, file_name,
1343                                        eficonfig_process_show_file_option, file_info);
1344 out:
1345         free(devname);
1346         free(file_name);
1347
1348         return ret;
1349 }
1350
1351 /**
1352  * eficonfig_show_boot_option() - prepare menu entry for editing boot option
1353  *
1354  * Construct the structures to create edit boot option menu
1355  *
1356  * @bo:         pointer to the boot option
1357  * @header_str: pointer to the header string
1358  * Return:      status code
1359  */
1360 static efi_status_t eficonfig_show_boot_option(struct eficonfig_boot_option *bo,
1361                                                char *header_str)
1362 {
1363         efi_status_t ret;
1364         struct efimenu *efi_menu;
1365
1366         efi_menu = calloc(1, sizeof(struct efimenu));
1367         if (!efi_menu)
1368                 return EFI_OUT_OF_RESOURCES;
1369
1370         INIT_LIST_HEAD(&efi_menu->list);
1371
1372         ret = create_boot_option_entry(efi_menu, "Description: ", bo->description,
1373                                        eficonfig_boot_add_enter_description, bo);
1374         if (ret != EFI_SUCCESS)
1375                 goto out;
1376
1377         ret = prepare_file_selection_entry(efi_menu, "File: ", &bo->file_info);
1378         if (ret != EFI_SUCCESS)
1379                 goto out;
1380
1381         ret = prepare_file_selection_entry(efi_menu, "Initrd File: ", &bo->initrd_info);
1382         if (ret != EFI_SUCCESS)
1383                 goto out;
1384
1385         ret = create_boot_option_entry(efi_menu, "Optional Data: ", bo->optional_data,
1386                                        eficonfig_boot_add_optional_data, bo);
1387         if (ret != EFI_SUCCESS)
1388                 goto out;
1389
1390         ret = create_boot_option_entry(efi_menu, "Save", NULL,
1391                                        eficonfig_boot_edit_save, bo);
1392         if (ret != EFI_SUCCESS)
1393                 goto out;
1394
1395         ret = create_boot_option_entry(efi_menu, "Quit", NULL,
1396                                        eficonfig_process_quit, NULL);
1397         if (ret != EFI_SUCCESS)
1398                 goto out;
1399
1400         ret = eficonfig_process_common(efi_menu, header_str,
1401                                        eficonfig_menu_desc,
1402                                        eficonfig_display_statusline,
1403                                        eficonfig_print_entry,
1404                                        eficonfig_choice_entry);
1405
1406 out:
1407         eficonfig_destroy(efi_menu);
1408
1409         return ret;
1410 }
1411
1412 /**
1413  * fill_file_info() - fill the file info from efi_device_path structure
1414  *
1415  * @dp:         pointer to the device path
1416  * @file_info:  pointer to the file info structure
1417  * @device_dp:  pointer to the volume device path
1418  */
1419 static void fill_file_info(struct efi_device_path *dp,
1420                            struct eficonfig_select_file_info *file_info,
1421                            struct efi_device_path *device_dp)
1422 {
1423         u16 *file_str, *p;
1424         struct efi_device_path *file_dp = NULL;
1425
1426         efi_dp_split_file_path(dp, &device_dp, &file_dp);
1427         file_info->dp_volume = device_dp;
1428
1429         if (file_dp) {
1430                 file_str = efi_dp_str(file_dp);
1431                 /*
1432                  * efi_convert_device_path_to_text() automatically adds u'/' at the
1433                  * beginning of file name, remove u'/' before copying to current_path
1434                  */
1435                 p = file_str;
1436                 if (p[0] == u'/')
1437                         p++;
1438
1439                 u16_strcpy(file_info->current_path, p);
1440                 efi_free_pool(file_dp);
1441                 efi_free_pool(file_str);
1442         }
1443 }
1444
1445 /**
1446  * eficonfig_edit_boot_option() - prepare boot option structure for editing
1447  *
1448  * Construct the boot option structure and copy the existing value
1449  *
1450  * @varname:            pointer to the UEFI variable name
1451  * @bo:                 pointer to the boot option
1452  * @load_option:        pointer to the load option
1453  * @load_option_size:   size of the load option
1454  * @header_str:         pointer to the header string
1455  * Return       :       status code
1456  */
1457 static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_boot_option *bo,
1458                                                void *load_option, efi_uintn_t load_option_size,
1459                                                char *header_str)
1460 {
1461         size_t len;
1462         efi_status_t ret;
1463         char *tmp = NULL, *p;
1464         struct efi_load_option lo = {0};
1465         efi_uintn_t final_dp_size;
1466         struct efi_device_path *dp = NULL;
1467         efi_uintn_t size = load_option_size;
1468         struct efi_device_path *final_dp = NULL;
1469         struct efi_device_path *device_dp = NULL;
1470         struct efi_device_path *initrd_dp = NULL;
1471         struct efi_device_path *initrd_device_dp = NULL;
1472
1473         const struct efi_initrd_dp id_dp = {
1474                 .vendor = {
1475                         {
1476                         DEVICE_PATH_TYPE_MEDIA_DEVICE,
1477                         DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
1478                         sizeof(id_dp.vendor),
1479                         },
1480                         EFI_INITRD_MEDIA_GUID,
1481                 },
1482                 .end = {
1483                         DEVICE_PATH_TYPE_END,
1484                         DEVICE_PATH_SUB_TYPE_END,
1485                         sizeof(id_dp.end),
1486                 }
1487         };
1488
1489         bo->file_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1490         if (!bo->file_info.current_path) {
1491                 ret =  EFI_OUT_OF_RESOURCES;
1492                 goto out;
1493         }
1494
1495         bo->initrd_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1496         if (!bo->file_info.current_path) {
1497                 ret =  EFI_OUT_OF_RESOURCES;
1498                 goto out;
1499         }
1500
1501         bo->description = calloc(1, EFICONFIG_DESCRIPTION_MAX * sizeof(u16));
1502         if (!bo->description) {
1503                 ret =  EFI_OUT_OF_RESOURCES;
1504                 goto out;
1505         }
1506
1507         bo->optional_data = calloc(1, EFICONFIG_OPTIONAL_DATA_MAX * sizeof(u16));
1508         if (!bo->optional_data) {
1509                 ret =  EFI_OUT_OF_RESOURCES;
1510                 goto out;
1511         }
1512
1513         /* copy the preset value */
1514         if (load_option) {
1515                 ret = efi_deserialize_load_option(&lo, load_option, &size);
1516                 if (ret != EFI_SUCCESS)
1517                         goto out;
1518
1519                 if (!lo.label) {
1520                         ret = EFI_INVALID_PARAMETER;
1521                         goto out;
1522                 }
1523                 /* truncate the long label string */
1524                 if (u16_strlen(lo.label) >= EFICONFIG_DESCRIPTION_MAX)
1525                         lo.label[EFICONFIG_DESCRIPTION_MAX - 1] = u'\0';
1526
1527                 u16_strcpy(bo->description, lo.label);
1528
1529                 /* EFI image file path is a first instance */
1530                 if (lo.file_path)
1531                         fill_file_info(lo.file_path, &bo->file_info, device_dp);
1532
1533                 /* Initrd file path(optional) is placed at second instance. */
1534                 initrd_dp = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
1535                 if (initrd_dp) {
1536                         fill_file_info(initrd_dp, &bo->initrd_info, initrd_device_dp);
1537                         efi_free_pool(initrd_dp);
1538                 }
1539
1540                 if (size > 0)
1541                         memcpy(bo->optional_data, lo.optional_data, size);
1542         }
1543
1544         while (1) {
1545                 ret = eficonfig_show_boot_option(bo, header_str);
1546                 if (ret == EFI_SUCCESS && bo->edit_completed)
1547                         break;
1548                 if (ret == EFI_NOT_READY)
1549                         continue;
1550                 if (ret != EFI_SUCCESS)
1551                         goto out;
1552         }
1553
1554         if (bo->initrd_info.dp_volume) {
1555                 dp = eficonfig_create_device_path(bo->initrd_info.dp_volume,
1556                                                  bo->initrd_info.current_path);
1557                 if (!dp) {
1558                         ret = EFI_OUT_OF_RESOURCES;
1559                         goto out;
1560                 }
1561                 initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp, dp);
1562                 efi_free_pool(dp);
1563         }
1564
1565         dp = eficonfig_create_device_path(bo->file_info.dp_volume, bo->file_info.current_path);
1566         if (!dp) {
1567                 ret = EFI_OUT_OF_RESOURCES;
1568                 goto out;
1569         }
1570         final_dp_size = efi_dp_size(dp) + sizeof(END);
1571         if (initrd_dp) {
1572                 final_dp = efi_dp_concat(dp, initrd_dp);
1573                 final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
1574         } else {
1575                 final_dp = efi_dp_dup(dp);
1576         }
1577         efi_free_pool(dp);
1578
1579         if (!final_dp)
1580                 goto out;
1581
1582         if (utf16_utf8_strlen(bo->optional_data)) {
1583                 len = utf16_utf8_strlen(bo->optional_data) + 1;
1584                 tmp = calloc(1, len);
1585                 if (!tmp)
1586                         goto out;
1587                 p = tmp;
1588                 utf16_utf8_strncpy(&p, bo->optional_data, u16_strlen(bo->optional_data));
1589         }
1590
1591         ret = eficonfig_set_boot_option(varname, final_dp, final_dp_size, bo->description, tmp);
1592 out:
1593         free(tmp);
1594         free(bo->optional_data);
1595         free(bo->description);
1596         free(bo->file_info.current_path);
1597         free(bo->initrd_info.current_path);
1598         efi_free_pool(device_dp);
1599         efi_free_pool(initrd_device_dp);
1600         efi_free_pool(initrd_dp);
1601         efi_free_pool(final_dp);
1602
1603         return ret;
1604 }
1605
1606 /**
1607  * eficonfig_process_add_boot_option() - handler to add boot option
1608  *
1609  * @data:       pointer to the data for each entry
1610  * Return:      status code
1611  */
1612 static efi_status_t eficonfig_process_add_boot_option(void *data)
1613 {
1614         u16 varname[9];
1615         efi_status_t ret;
1616         struct eficonfig_boot_option *bo = NULL;
1617
1618         bo = calloc(1, sizeof(struct eficonfig_boot_option));
1619         if (!bo)
1620                 return EFI_OUT_OF_RESOURCES;
1621
1622         ret = eficonfig_get_unused_bootoption(varname, sizeof(varname), &bo->boot_index);
1623         if (ret != EFI_SUCCESS)
1624                 return ret;
1625
1626         ret = eficonfig_edit_boot_option(varname, bo, NULL, 0,  "  ** Add Boot Option ** ");
1627         if (ret != EFI_SUCCESS)
1628                 goto out;
1629
1630         ret = eficonfig_append_bootorder((u16)bo->boot_index);
1631         if (ret != EFI_SUCCESS)
1632                 goto out;
1633
1634 out:
1635         free(bo);
1636
1637         /* to stay the parent menu */
1638         ret = (ret == EFI_ABORTED) ? EFI_SUCCESS : ret;
1639
1640         return ret;
1641 }
1642
1643 /**
1644  * eficonfig_process_boot_selected() - handler to select boot option entry
1645  *
1646  * @data:       pointer to the data for each entry
1647  * Return:      status code
1648  */
1649 static efi_status_t eficonfig_process_boot_selected(void *data)
1650 {
1651         struct eficonfig_boot_selection_data *info = data;
1652
1653         if (info)
1654                 *info->selected = info->boot_index;
1655
1656         return EFI_SUCCESS;
1657 }
1658
1659 /**
1660  * search_bootorder() - search the boot option index in BootOrder
1661  *
1662  * @bootorder:  pointer to the BootOrder variable
1663  * @num:        number of BootOrder entry
1664  * @target:     target boot option index to search
1665  * @index:      pointer to store the index of BootOrder variable
1666  * Return:      true if exists, false otherwise
1667  */
1668 static bool search_bootorder(u16 *bootorder, efi_uintn_t num, u32 target, u32 *index)
1669 {
1670         u32 i;
1671
1672         for (i = 0; i < num; i++) {
1673                 if (target == bootorder[i]) {
1674                         if (index)
1675                                 *index = i;
1676
1677                         return true;
1678                 }
1679         }
1680
1681         return false;
1682 }
1683
1684 /**
1685  * eficonfig_add_boot_selection_entry() - add boot option menu entry
1686  *
1687  * @efi_menu:   pointer to store the efimenu structure
1688  * @boot_index: boot option index to be added
1689  * @selected:   pointer to store the selected boot option index
1690  * Return:      status code
1691  */
1692 static efi_status_t eficonfig_add_boot_selection_entry(struct efimenu *efi_menu,
1693                                                        unsigned int boot_index,
1694                                                        unsigned int *selected)
1695 {
1696         char *buf, *p;
1697         efi_status_t ret;
1698         efi_uintn_t size;
1699         void *load_option;
1700         struct efi_load_option lo;
1701         u16 varname[] = u"Boot####";
1702         struct eficonfig_boot_selection_data *info;
1703
1704         efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
1705         load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1706         if (!load_option)
1707                 return EFI_SUCCESS;
1708
1709         ret = efi_deserialize_load_option(&lo, load_option, &size);
1710         if (ret != EFI_SUCCESS) {
1711                 log_warning("Invalid load option for %ls\n", varname);
1712                 free(load_option);
1713                 return ret;
1714         }
1715
1716         if (size >= sizeof(efi_guid_t) &&
1717             !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
1718                 /*
1719                  * auto generated entry has GUID in optional_data,
1720                  * skip auto generated entry because it will be generated
1721                  * again even if it is edited or deleted.
1722                  */
1723                 free(load_option);
1724                 return EFI_SUCCESS;
1725         }
1726
1727         info = calloc(1, sizeof(struct eficonfig_boot_selection_data));
1728         if (!info) {
1729                 free(load_option);
1730                 return EFI_OUT_OF_RESOURCES;
1731         }
1732
1733         buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
1734         if (!buf) {
1735                 free(load_option);
1736                 free(info);
1737                 return EFI_OUT_OF_RESOURCES;
1738         }
1739         p = buf;
1740         utf16_utf8_strcpy(&p, lo.label);
1741         info->boot_index = boot_index;
1742         info->selected = selected;
1743         ret = eficonfig_append_menu_entry(efi_menu, buf, eficonfig_process_boot_selected, info);
1744         if (ret != EFI_SUCCESS) {
1745                 free(load_option);
1746                 free(info);
1747                 return ret;
1748         }
1749         free(load_option);
1750
1751         return EFI_SUCCESS;
1752 }
1753
1754 /**
1755  * eficonfig_show_boot_selection() - construct boot option menu entry
1756  *
1757  * @selected:   pointer to store the selected boot option index
1758  * Return:      status code
1759  */
1760 static efi_status_t eficonfig_show_boot_selection(unsigned int *selected)
1761 {
1762         u32 i;
1763         u16 *bootorder;
1764         efi_status_t ret;
1765         u16 *var_name16 = NULL;
1766         efi_uintn_t num, size, buf_size;
1767         struct efimenu *efi_menu;
1768         struct list_head *pos, *n;
1769         struct eficonfig_entry *entry;
1770
1771         efi_menu = calloc(1, sizeof(struct efimenu));
1772         if (!efi_menu)
1773                 return EFI_OUT_OF_RESOURCES;
1774
1775         bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1776
1777         INIT_LIST_HEAD(&efi_menu->list);
1778         num = size / sizeof(u16);
1779         /* list the load option in the order of BootOrder variable */
1780         for (i = 0; i < num; i++) {
1781                 ret = eficonfig_add_boot_selection_entry(efi_menu, bootorder[i], selected);
1782                 if (ret != EFI_SUCCESS)
1783                         goto out;
1784
1785                 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1786                         break;
1787         }
1788
1789         /* list the remaining load option not included in the BootOrder */
1790         buf_size = 128;
1791         var_name16 = malloc(buf_size);
1792         if (!var_name16)
1793                 return EFI_OUT_OF_RESOURCES;
1794
1795         var_name16[0] = 0;
1796         for (;;) {
1797                 int index;
1798                 efi_guid_t guid;
1799
1800                 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
1801                 if (ret == EFI_NOT_FOUND)
1802                         break;
1803                 if (ret != EFI_SUCCESS)
1804                         goto out;
1805
1806                 if (efi_varname_is_load_option(var_name16, &index)) {
1807                         /* If the index is included in the BootOrder, skip it */
1808                         if (search_bootorder(bootorder, num, index, NULL))
1809                                 continue;
1810
1811                         ret = eficonfig_add_boot_selection_entry(efi_menu, index, selected);
1812                         if (ret != EFI_SUCCESS)
1813                                 goto out;
1814                 }
1815
1816                 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1817                         break;
1818         }
1819
1820         ret = eficonfig_append_quit_entry(efi_menu);
1821         if (ret != EFI_SUCCESS)
1822                 goto out;
1823
1824         ret = eficonfig_process_common(efi_menu, "  ** Select Boot Option **",
1825                                        eficonfig_menu_desc,
1826                                        eficonfig_display_statusline,
1827                                        eficonfig_print_entry,
1828                                        eficonfig_choice_entry);
1829 out:
1830         list_for_each_safe(pos, n, &efi_menu->list) {
1831                 entry = list_entry(pos, struct eficonfig_entry, list);
1832                 free(entry->data);
1833         }
1834         eficonfig_destroy(efi_menu);
1835
1836         free(var_name16);
1837
1838         return ret;
1839 }
1840
1841 /**
1842  * eficonfig_process_edit_boot_option() - handler to edit boot option
1843  *
1844  * @data:       pointer to the data for each entry
1845  * Return:      status code
1846  */
1847 static efi_status_t eficonfig_process_edit_boot_option(void *data)
1848 {
1849         efi_status_t ret;
1850         efi_uintn_t size;
1851         struct eficonfig_boot_option *bo = NULL;
1852
1853         while (1) {
1854                 unsigned int selected;
1855                 void *load_option;
1856                 u16 varname[] = u"Boot####";
1857
1858                 ret = eficonfig_show_boot_selection(&selected);
1859                 if (ret != EFI_SUCCESS)
1860                         break;
1861
1862                 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1863                 if (!bo) {
1864                         ret = EFI_OUT_OF_RESOURCES;
1865                         goto out;
1866                 }
1867
1868                 bo->boot_index = selected;
1869                 efi_create_indexed_name(varname, sizeof(varname), "Boot", selected);
1870                 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1871                 if (!load_option) {
1872                         free(bo);
1873                         ret = EFI_NOT_FOUND;
1874                         goto out;
1875                 }
1876
1877                 ret = eficonfig_edit_boot_option(varname, bo, load_option, size,
1878                                                  "  ** Edit Boot Option ** ");
1879
1880                 free(load_option);
1881                 free(bo);
1882                 if (ret != EFI_SUCCESS && ret != EFI_ABORTED)
1883                         break;
1884         }
1885 out:
1886         /* to stay the parent menu */
1887         ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1888
1889         return ret;
1890 }
1891
1892 /**
1893  * eficonfig_print_change_boot_order_entry() - print the boot option entry
1894  *
1895  * @data:       pointer to the data associated with each menu entry
1896  */
1897 static void eficonfig_print_change_boot_order_entry(void *data)
1898 {
1899         struct eficonfig_entry *entry = data;
1900         bool reverse = (entry->efi_menu->active == entry->num);
1901
1902         if (entry->efi_menu->start > entry->num || entry->efi_menu->end < entry->num)
1903                 return;
1904
1905         printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
1906                (entry->num - entry->efi_menu->start) + EFICONFIG_MENU_HEADER_ROW_NUM + 1, 7);
1907
1908         if (reverse)
1909                 puts(ANSI_COLOR_REVERSE);
1910
1911         if (entry->num < entry->efi_menu->count - 2) {
1912                 if (((struct eficonfig_boot_order_data *)entry->data)->active)
1913                         printf("[*]  ");
1914                 else
1915                         printf("[ ]  ");
1916         }
1917
1918         printf("%s", entry->title);
1919
1920         if (reverse)
1921                 puts(ANSI_COLOR_RESET);
1922 }
1923
1924 /**
1925  * eficonfig_choice_change_boot_order() - user key input handler
1926  *
1927  * @data:       pointer to the menu entry
1928  * Return:      key string to identify the selected entry
1929  */
1930 char *eficonfig_choice_change_boot_order(void *data)
1931 {
1932         struct cli_ch_state s_cch, *cch = &s_cch;
1933         struct list_head *pos, *n;
1934         struct efimenu *efi_menu = data;
1935         enum bootmenu_key key = BKEY_NONE;
1936         struct eficonfig_entry *entry, *tmp;
1937
1938         cli_ch_init(cch);
1939         while (1) {
1940                 key = bootmenu_loop(NULL, cch);
1941
1942                 switch (key) {
1943                 case BKEY_PLUS:
1944                         if (efi_menu->active > 0 &&
1945                             efi_menu->active < efi_menu->count - 2) {
1946                                 list_for_each_safe(pos, n, &efi_menu->list) {
1947                                         entry = list_entry(pos, struct eficonfig_entry, list);
1948                                         if (entry->num == efi_menu->active)
1949                                                 break;
1950                                 }
1951                                 tmp = list_entry(pos->prev, struct eficonfig_entry, list);
1952                                 entry->num--;
1953                                 tmp->num++;
1954                                 list_del(&tmp->list);
1955                                 list_add(&tmp->list, &entry->list);
1956
1957                                 eficonfig_menu_up(efi_menu);
1958                         }
1959                         return NULL;
1960                 case BKEY_UP:
1961                         if (efi_menu->active > 0)
1962                                 eficonfig_menu_up(efi_menu);
1963
1964                         return NULL;
1965                 case BKEY_MINUS:
1966                         if (efi_menu->active < efi_menu->count - 3) {
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                                                 break;
1971                                 }
1972                                 tmp = list_entry(pos->next, struct eficonfig_entry, list);
1973                                 entry->num++;
1974                                 tmp->num--;
1975                                 list_del(&entry->list);
1976                                 list_add(&entry->list, &tmp->list);
1977
1978                                 eficonfig_menu_down(efi_menu);
1979                         }
1980                         return NULL;
1981                 case BKEY_DOWN:
1982                         if (efi_menu->active < efi_menu->count - 1)
1983                                 eficonfig_menu_down(efi_menu);
1984
1985                         return NULL;
1986                 case BKEY_SAVE:
1987                         /* force to select "Save" entry */
1988                         efi_menu->active = efi_menu->count - 2;
1989                         fallthrough;
1990                 case BKEY_SELECT:
1991                         /* "Save" */
1992                         if (efi_menu->active == efi_menu->count - 2) {
1993                                 list_for_each_prev_safe(pos, n, &efi_menu->list) {
1994                                         entry = list_entry(pos, struct eficonfig_entry, list);
1995                                         if (entry->num == efi_menu->active)
1996                                                 break;
1997                                 }
1998                                 return entry->key;
1999                         }
2000                         /* "Quit" */
2001                         if (efi_menu->active == efi_menu->count - 1) {
2002                                 entry = list_last_entry(&efi_menu->list,
2003                                                         struct eficonfig_entry,
2004                                                         list);
2005                                 return entry->key;
2006                         }
2007                         /* Pressed key is not valid, wait next key press */
2008                         break;
2009                 case BKEY_SPACE:
2010                         if (efi_menu->active < efi_menu->count - 2) {
2011                                 list_for_each_safe(pos, n, &efi_menu->list) {
2012                                         entry = list_entry(pos, struct eficonfig_entry, list);
2013                                         if (entry->num == efi_menu->active) {
2014                                                 struct eficonfig_boot_order_data *data = entry->data;
2015
2016                                                 data->active = !data->active;
2017                                                 return NULL;
2018                                         }
2019                                 }
2020                         }
2021                         /* Pressed key is not valid, wait next key press */
2022                         break;
2023                 case BKEY_QUIT:
2024                         entry = list_last_entry(&efi_menu->list,
2025                                                 struct eficonfig_entry, list);
2026                         return entry->key;
2027                 default:
2028                         /* Pressed key is not valid, wait next key press */
2029                         break;
2030                 }
2031         }
2032 }
2033
2034 /**
2035  * eficonfig_process_save_boot_order() - callback function for "Save" entry
2036  *
2037  * @data:       pointer to the data
2038  * Return:      status code
2039  */
2040 static efi_status_t eficonfig_process_save_boot_order(void *data)
2041 {
2042         u32 count = 0;
2043         efi_status_t ret;
2044         efi_uintn_t size;
2045         struct list_head *pos, *n;
2046         u16 *new_bootorder;
2047         struct efimenu *efi_menu;
2048         struct eficonfig_entry *entry;
2049         struct eficonfig_save_boot_order_data *save_data = data;
2050
2051         efi_menu = save_data->efi_menu;
2052
2053         /*
2054          * The change boot order menu always has "Save" and "Quit" entries.
2055          * !(efi_menu->count - 2) means there is no user defined boot option.
2056          */
2057         if (!(efi_menu->count - 2))
2058                 return EFI_SUCCESS;
2059
2060         new_bootorder = calloc(1, (efi_menu->count - 2) * sizeof(u16));
2061         if (!new_bootorder) {
2062                 ret = EFI_OUT_OF_RESOURCES;
2063                 goto out;
2064         }
2065
2066         /* create new BootOrder */
2067         count = 0;
2068         list_for_each_safe(pos, n, &efi_menu->list) {
2069                 struct eficonfig_boot_order_data *data;
2070
2071                 entry = list_entry(pos, struct eficonfig_entry, list);
2072                 /* exit the loop when iteration reaches "Save" */
2073                 if (!strncmp(entry->title, "Save", strlen("Save")))
2074                         break;
2075
2076                 data = entry->data;
2077                 if (data->active)
2078                         new_bootorder[count++] = data->boot_index;
2079         }
2080
2081         size = count * sizeof(u16);
2082         ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
2083                                    EFI_VARIABLE_NON_VOLATILE |
2084                                    EFI_VARIABLE_BOOTSERVICE_ACCESS |
2085                                    EFI_VARIABLE_RUNTIME_ACCESS,
2086                                    size, new_bootorder, false);
2087
2088         save_data->selected = true;
2089 out:
2090         free(new_bootorder);
2091
2092         return ret;
2093 }
2094
2095 /**
2096  * eficonfig_add_change_boot_order_entry() - add boot order entry
2097  *
2098  * @efi_menu:   pointer to the efimenu structure
2099  * @boot_index: boot option index to be added
2100  * @active:     flag to include the boot option into BootOrder
2101  * Return:      status code
2102  */
2103 static efi_status_t eficonfig_add_change_boot_order_entry(struct efimenu *efi_menu,
2104                                                           u32 boot_index, bool active)
2105 {
2106         char *title, *p;
2107         efi_status_t ret;
2108         efi_uintn_t size;
2109         void *load_option;
2110         struct efi_load_option lo;
2111         u16 varname[] = u"Boot####";
2112         struct eficonfig_boot_order_data *data;
2113
2114         efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
2115         load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
2116         if (!load_option)
2117                 return EFI_SUCCESS;
2118
2119         ret = efi_deserialize_load_option(&lo, load_option, &size);
2120         if (ret != EFI_SUCCESS)
2121                 goto out;
2122
2123         data = calloc(1, sizeof(*data));
2124         if (!data) {
2125                 ret = EFI_OUT_OF_RESOURCES;
2126                 goto out;
2127         }
2128
2129         title = calloc(1, utf16_utf8_strlen(lo.label) + 1);
2130         if (!title) {
2131                 free(data);
2132                 ret = EFI_OUT_OF_RESOURCES;
2133                 goto out;
2134         }
2135         p = title;
2136         utf16_utf8_strcpy(&p, lo.label);
2137
2138         data->boot_index = boot_index;
2139         data->active = active;
2140
2141         ret = eficonfig_append_menu_entry(efi_menu, title, NULL, data);
2142         if (ret != EFI_SUCCESS) {
2143                 free(data);
2144                 free(title);
2145                 goto out;
2146         }
2147
2148 out:
2149         free(load_option);
2150
2151         return ret;
2152 }
2153
2154 /**
2155  * eficonfig_create_change_boot_order_entry() - create boot order entry
2156  *
2157  * @efi_menu:   pointer to the efimenu structure
2158  * @bootorder:  pointer to the BootOrder variable
2159  * @num:        number of BootOrder entry
2160  * Return:      status code
2161  */
2162 static efi_status_t eficonfig_create_change_boot_order_entry(struct efimenu *efi_menu,
2163                                                              u16 *bootorder, efi_uintn_t num)
2164 {
2165         u32 i;
2166         char *title;
2167         efi_status_t ret;
2168         u16 *var_name16 = NULL;
2169         efi_uintn_t size, buf_size;
2170         struct eficonfig_save_boot_order_data *save_data;
2171
2172         /* list the load option in the order of BootOrder variable */
2173         for (i = 0; i < num; i++) {
2174                 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2175                         break;
2176
2177                 ret = eficonfig_add_change_boot_order_entry(efi_menu, bootorder[i], true);
2178                 if (ret != EFI_SUCCESS)
2179                         goto out;
2180         }
2181
2182         /* list the remaining load option not included in the BootOrder */
2183         buf_size = 128;
2184         var_name16 = malloc(buf_size);
2185         if (!var_name16)
2186                 return EFI_OUT_OF_RESOURCES;
2187
2188         var_name16[0] = 0;
2189         for (;;) {
2190                 int index;
2191                 efi_guid_t guid;
2192
2193                 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2194                         break;
2195
2196                 size = buf_size;
2197                 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
2198                 if (ret == EFI_NOT_FOUND)
2199                         break;
2200                 if (ret != EFI_SUCCESS)
2201                         goto out;
2202
2203                 if (efi_varname_is_load_option(var_name16, &index)) {
2204                         /* If the index is included in the BootOrder, skip it */
2205                         if (search_bootorder(bootorder, num, index, NULL))
2206                                 continue;
2207
2208                         ret = eficonfig_add_change_boot_order_entry(efi_menu, index, false);
2209                         if (ret != EFI_SUCCESS)
2210                                 goto out;
2211                 }
2212         }
2213
2214         /* add "Save" and "Quit" entries */
2215         title = strdup("Save");
2216         if (!title) {
2217                 ret = EFI_OUT_OF_RESOURCES;
2218                 goto out;
2219         }
2220
2221         save_data = malloc(sizeof(struct eficonfig_save_boot_order_data));
2222         if (!save_data) {
2223                 ret = EFI_OUT_OF_RESOURCES;
2224                 goto out;
2225         }
2226         save_data->efi_menu = efi_menu;
2227         save_data->selected = false;
2228
2229         ret = eficonfig_append_menu_entry(efi_menu, title,
2230                                           eficonfig_process_save_boot_order,
2231                                           save_data);
2232         if (ret != EFI_SUCCESS)
2233                 goto out;
2234
2235         ret = eficonfig_append_quit_entry(efi_menu);
2236         if (ret != EFI_SUCCESS)
2237                 goto out;
2238
2239         efi_menu->active = 0;
2240 out:
2241         free(var_name16);
2242
2243         return ret;
2244 }
2245
2246 /**
2247  * eficonfig_process_change_boot_order() - handler to change boot order
2248  *
2249  * @data:       pointer to the data for each entry
2250  * Return:      status code
2251  */
2252 static efi_status_t eficonfig_process_change_boot_order(void *data)
2253 {
2254         u16 *bootorder;
2255         efi_status_t ret;
2256         efi_uintn_t num, size;
2257         struct list_head *pos, *n;
2258         struct eficonfig_entry *entry;
2259         struct efimenu *efi_menu;
2260
2261         efi_menu = calloc(1, sizeof(struct efimenu));
2262         if (!efi_menu)
2263                 return EFI_OUT_OF_RESOURCES;
2264
2265         bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2266
2267         INIT_LIST_HEAD(&efi_menu->list);
2268         num = size / sizeof(u16);
2269         ret = eficonfig_create_change_boot_order_entry(efi_menu, bootorder, num);
2270         if (ret != EFI_SUCCESS)
2271                 goto out;
2272
2273         while (1) {
2274                 ret = eficonfig_process_common(efi_menu,
2275                                                "  ** Change Boot Order **",
2276                                                eficonfig_change_boot_order_desc,
2277                                                eficonfig_display_statusline,
2278                                                eficonfig_print_change_boot_order_entry,
2279                                                eficonfig_choice_change_boot_order);
2280                 /* exit from the menu if user selects the "Save" entry. */
2281                 if (ret == EFI_SUCCESS && efi_menu->active == (efi_menu->count - 2)) {
2282                         list_for_each_prev_safe(pos, n, &efi_menu->list) {
2283                                 entry = list_entry(pos, struct eficonfig_entry, list);
2284                                 if (entry->num == efi_menu->active)
2285                                         break;
2286                         }
2287                         if (((struct eficonfig_save_boot_order_data *)entry->data)->selected)
2288                                 break;
2289                 }
2290                 if (ret != EFI_SUCCESS)
2291                         break;
2292         }
2293 out:
2294         free(bootorder);
2295         list_for_each_safe(pos, n, &efi_menu->list) {
2296                 entry = list_entry(pos, struct eficonfig_entry, list);
2297                 free(entry->data);
2298         }
2299         eficonfig_destroy(efi_menu);
2300
2301         /* to stay the parent menu */
2302         ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2303
2304         return ret;
2305 }
2306
2307 /**
2308  * delete_boot_option() - delete selected boot option
2309  *
2310  * @boot_index: boot option index to delete
2311  * Return:      status code
2312  */
2313 static efi_status_t delete_boot_option(u16 boot_index)
2314 {
2315         u16 *bootorder;
2316         u16 varname[9];
2317         efi_status_t ret;
2318         unsigned int index;
2319         efi_uintn_t num, size;
2320
2321         efi_create_indexed_name(varname, sizeof(varname),
2322                                 "Boot", boot_index);
2323         ret = efi_set_variable_int(varname, &efi_global_variable_guid,
2324                                    0, 0, NULL, false);
2325         if (ret != EFI_SUCCESS) {
2326                 log_err("delete boot option(%ls) failed\n", varname);
2327                 return ret;
2328         }
2329
2330         /* update BootOrder if necessary */
2331         bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2332         if (!bootorder)
2333                 return EFI_SUCCESS;
2334
2335         num = size / sizeof(u16);
2336         if (!search_bootorder(bootorder, num, boot_index, &index))
2337                 return EFI_SUCCESS;
2338
2339         memmove(&bootorder[index], &bootorder[index + 1],
2340                 (num - index - 1) * sizeof(u16));
2341         size -= sizeof(u16);
2342         ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
2343                                    EFI_VARIABLE_NON_VOLATILE |
2344                                    EFI_VARIABLE_BOOTSERVICE_ACCESS |
2345                                    EFI_VARIABLE_RUNTIME_ACCESS,
2346                                    size, bootorder, false);
2347
2348         return ret;
2349 }
2350
2351 /**
2352  * eficonfig_process_delete_boot_option() - handler to delete boot option
2353  *
2354  * @data:       pointer to the data for each entry
2355  * Return:      status code
2356  */
2357 static efi_status_t eficonfig_process_delete_boot_option(void *data)
2358 {
2359         efi_status_t ret;
2360         unsigned int selected;
2361
2362         while (1) {
2363                 ret = eficonfig_show_boot_selection(&selected);
2364                 if (ret == EFI_SUCCESS)
2365                         ret = delete_boot_option(selected);
2366
2367                 if (ret != EFI_SUCCESS)
2368                         break;
2369         }
2370
2371         /* to stay the parent menu */
2372         ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2373
2374         return ret;
2375 }
2376
2377 /**
2378  * eficonfig_enumerate_boot_option() - enumerate the possible bootable media
2379  *
2380  * @opt:                pointer to the media boot option structure
2381  * @volume_handles:     pointer to the efi handles
2382  * @count:              number of efi handle
2383  * Return:              status code
2384  */
2385 efi_status_t eficonfig_enumerate_boot_option(struct eficonfig_media_boot_option *opt,
2386                                              efi_handle_t *volume_handles, efi_status_t count)
2387 {
2388         u32 i;
2389         struct efi_handler *handler;
2390         efi_status_t ret = EFI_SUCCESS;
2391
2392         for (i = 0; i < count; i++) {
2393                 u16 *p;
2394                 u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
2395                 char *optional_data;
2396                 struct efi_load_option lo;
2397                 char buf[BOOTMENU_DEVICE_NAME_MAX];
2398                 struct efi_device_path *device_path;
2399
2400                 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
2401                 if (ret != EFI_SUCCESS)
2402                         continue;
2403                 ret = efi_protocol_open(handler, (void **)&device_path,
2404                                         efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2405                 if (ret != EFI_SUCCESS)
2406                         continue;
2407
2408                 ret = efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
2409                 if (ret != EFI_SUCCESS)
2410                         continue;
2411
2412                 p = dev_name;
2413                 utf8_utf16_strncpy(&p, buf, strlen(buf));
2414
2415                 lo.label = dev_name;
2416                 lo.attributes = LOAD_OPTION_ACTIVE;
2417                 lo.file_path = device_path;
2418                 lo.file_path_length = efi_dp_size(device_path) + sizeof(END);
2419                 /*
2420                  * Set the dedicated guid to optional_data, it is used to identify
2421                  * the boot option that automatically generated by the bootmenu.
2422                  * efi_serialize_load_option() expects optional_data is null-terminated
2423                  * utf8 string, so set the "1234567" string to allocate enough space
2424                  * to store guid, instead of realloc the load_option.
2425                  */
2426                 lo.optional_data = "1234567";
2427                 opt[i].size = efi_serialize_load_option(&lo, (u8 **)&opt[i].lo);
2428                 if (!opt[i].size) {
2429                         ret = EFI_OUT_OF_RESOURCES;
2430                         goto out;
2431                 }
2432                 /* set the guid */
2433                 optional_data = (char *)opt[i].lo + (opt[i].size - u16_strsize(u"1234567"));
2434                 memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
2435         }
2436
2437 out:
2438         return ret;
2439 }
2440
2441 /**
2442  * eficonfig_delete_invalid_boot_option() - delete non-existing boot option
2443  *
2444  * @opt:                pointer to the media boot option structure
2445  * @count:              number of media boot option structure
2446  * Return:              status code
2447  */
2448 efi_status_t eficonfig_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
2449                                                   efi_status_t count)
2450 {
2451         efi_uintn_t size;
2452         void *load_option;
2453         u32 i, list_size = 0;
2454         struct efi_load_option lo;
2455         u16 *var_name16 = NULL;
2456         u16 varname[] = u"Boot####";
2457         efi_status_t ret = EFI_SUCCESS;
2458         u16 *delete_index_list = NULL, *p;
2459         efi_uintn_t buf_size;
2460
2461         buf_size = 128;
2462         var_name16 = malloc(buf_size);
2463         if (!var_name16)
2464                 return EFI_OUT_OF_RESOURCES;
2465
2466         var_name16[0] = 0;
2467         for (;;) {
2468                 int index;
2469                 efi_guid_t guid;
2470                 efi_uintn_t tmp;
2471
2472                 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
2473                 if (ret == EFI_NOT_FOUND) {
2474                         /*
2475                          * EFI_NOT_FOUND indicates we retrieved all EFI variables.
2476                          * This should be treated as success.
2477                          */
2478                         ret = EFI_SUCCESS;
2479                         break;
2480                 }
2481                 if (ret != EFI_SUCCESS)
2482                         goto out;
2483
2484                 if (!efi_varname_is_load_option(var_name16, &index))
2485                         continue;
2486
2487                 efi_create_indexed_name(varname, sizeof(varname), "Boot", index);
2488                 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
2489                 if (!load_option)
2490                         continue;
2491
2492                 tmp = size;
2493                 ret = efi_deserialize_load_option(&lo, load_option, &size);
2494                 if (ret != EFI_SUCCESS)
2495                         goto next;
2496
2497                 if (size >= sizeof(efi_guid_bootmenu_auto_generated) &&
2498                     !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
2499                         for (i = 0; i < count; i++) {
2500                                 if (opt[i].size == tmp &&
2501                                     memcmp(opt[i].lo, load_option, tmp) == 0) {
2502                                         opt[i].exist = true;
2503                                         break;
2504                                 }
2505                         }
2506
2507                         /*
2508                          * The entire list of variables must be retrieved by
2509                          * efi_get_next_variable_name_int() before deleting the invalid
2510                          * boot option, just save the index here.
2511                          */
2512                         if (i == count) {
2513                                 p = realloc(delete_index_list, sizeof(u32) *
2514                                             (list_size + 1));
2515                                 if (!p) {
2516                                         ret = EFI_OUT_OF_RESOURCES;
2517                                         goto out;
2518                                 }
2519                                 delete_index_list = p;
2520                                 delete_index_list[list_size++] = index;
2521                         }
2522                 }
2523 next:
2524                 free(load_option);
2525         }
2526
2527         /* delete all invalid boot options */
2528         for (i = 0; i < list_size; i++) {
2529                 ret = delete_boot_option(delete_index_list[i]);
2530                 if (ret != EFI_SUCCESS)
2531                         goto out;
2532         }
2533
2534 out:
2535         free(var_name16);
2536         free(delete_index_list);
2537
2538         return ret;
2539 }
2540
2541 /**
2542  * eficonfig_generate_media_device_boot_option() - generate the media device boot option
2543  *
2544  * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
2545  * and generate the bootmenu entries.
2546  * This function also provide the BOOT#### variable maintenance for
2547  * the media device entries.
2548  *   - Automatically create the BOOT#### variable for the newly detected device,
2549  *     this BOOT#### variable is distinguished by the special GUID
2550  *     stored in the EFI_LOAD_OPTION.optional_data
2551  *   - If the device is not attached to the system, the associated BOOT#### variable
2552  *     is automatically deleted.
2553  *
2554  * Return:      status code
2555  */
2556 efi_status_t eficonfig_generate_media_device_boot_option(void)
2557 {
2558         u32 i;
2559         efi_status_t ret;
2560         efi_uintn_t count;
2561         efi_handle_t *volume_handles = NULL;
2562         struct eficonfig_media_boot_option *opt = NULL;
2563
2564         ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
2565                                            NULL, &count, (efi_handle_t **)&volume_handles);
2566         if (ret != EFI_SUCCESS)
2567                 return ret;
2568
2569         opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
2570         if (!opt)
2571                 goto out;
2572
2573         /* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */
2574         ret = eficonfig_enumerate_boot_option(opt, volume_handles, count);
2575         if (ret != EFI_SUCCESS)
2576                 goto out;
2577
2578         /*
2579          * System hardware configuration may vary depending on the user setup.
2580          * The boot option is automatically added by the bootmenu.
2581          * If the device is not attached to the system, the boot option needs
2582          * to be deleted.
2583          */
2584         ret = eficonfig_delete_invalid_boot_option(opt, count);
2585         if (ret != EFI_SUCCESS)
2586                 goto out;
2587
2588         /* add non-existent boot option */
2589         for (i = 0; i < count; i++) {
2590                 u32 boot_index;
2591                 u16 var_name[9];
2592
2593                 if (!opt[i].exist) {
2594                         ret = eficonfig_get_unused_bootoption(var_name, sizeof(var_name),
2595                                                               &boot_index);
2596                         if (ret != EFI_SUCCESS)
2597                                 goto out;
2598
2599                         ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
2600                                                    EFI_VARIABLE_NON_VOLATILE |
2601                                                    EFI_VARIABLE_BOOTSERVICE_ACCESS |
2602                                                    EFI_VARIABLE_RUNTIME_ACCESS,
2603                                                    opt[i].size, opt[i].lo, false);
2604                         if (ret != EFI_SUCCESS)
2605                                 goto out;
2606
2607                         ret = eficonfig_append_bootorder(boot_index);
2608                         if (ret != EFI_SUCCESS) {
2609                                 efi_set_variable_int(var_name, &efi_global_variable_guid,
2610                                                      0, 0, NULL, false);
2611                                 goto out;
2612                         }
2613                 }
2614         }
2615
2616 out:
2617         if (opt) {
2618                 for (i = 0; i < count; i++)
2619                         free(opt[i].lo);
2620         }
2621         free(opt);
2622         efi_free_pool(volume_handles);
2623
2624         return ret;
2625 }
2626
2627 /**
2628  * eficonfig_init() - do required initialization for eficonfig command
2629  *
2630  * Return:      status code
2631  */
2632 static efi_status_t eficonfig_init(void)
2633 {
2634         efi_status_t ret = EFI_SUCCESS;
2635         static bool init;
2636         struct efi_handler *handler;
2637         unsigned long columns, rows;
2638
2639         if (!init) {
2640                 ret = efi_search_protocol(efi_root, &efi_guid_text_input_protocol, &handler);
2641                 if (ret != EFI_SUCCESS)
2642                         return ret;
2643
2644                 ret = efi_protocol_open(handler, (void **)&cin, efi_root, NULL,
2645                                         EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2646                 if (ret != EFI_SUCCESS)
2647                         return ret;
2648                 ret = efi_search_protocol(efi_root, &efi_guid_text_output_protocol, &handler);
2649                 if (ret != EFI_SUCCESS)
2650                         return ret;
2651
2652                 ret = efi_protocol_open(handler, (void **)&cout, efi_root, NULL,
2653                                         EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2654                 if (ret != EFI_SUCCESS)
2655                         return ret;
2656
2657                 cout->query_mode(cout, cout->mode->mode, &columns, &rows);
2658                 avail_row = rows - (EFICONFIG_MENU_HEADER_ROW_NUM +
2659                                     EFICONFIG_MENU_DESC_ROW_NUM);
2660                 if (avail_row <= 0) {
2661                         eficonfig_print_msg("Console size is too small!");
2662                         return EFI_INVALID_PARAMETER;
2663                 }
2664                 /* TODO: Should we check the minimum column size? */
2665         }
2666
2667         init = true;
2668
2669         return ret;
2670 }
2671
2672 static const struct eficonfig_item maintenance_menu_items[] = {
2673         {"Add Boot Option", eficonfig_process_add_boot_option},
2674         {"Edit Boot Option", eficonfig_process_edit_boot_option},
2675         {"Change Boot Order", eficonfig_process_change_boot_order},
2676         {"Delete Boot Option", eficonfig_process_delete_boot_option},
2677 #if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT) && IS_ENABLED(CONFIG_EFI_MM_COMM_TEE))
2678         {"Secure Boot Configuration", eficonfig_process_secure_boot_config},
2679 #endif
2680         {"Quit", eficonfig_process_quit},
2681 };
2682
2683 /**
2684  * do_eficonfig() - execute `eficonfig` command
2685  *
2686  * @cmdtp:      table entry describing command
2687  * @flag:       bitmap indicating how the command was invoked
2688  * @argc:       number of arguments
2689  * @argv:       command line arguments
2690  * Return:      status code
2691  */
2692 static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
2693 {
2694         efi_status_t ret;
2695         struct efimenu *efi_menu;
2696
2697         if (argc > 1)
2698                 return CMD_RET_USAGE;
2699
2700         ret = efi_init_obj_list();
2701         if (ret != EFI_SUCCESS) {
2702                 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
2703                         ret & ~EFI_ERROR_MASK);
2704
2705                 return CMD_RET_FAILURE;
2706         }
2707
2708         ret = eficonfig_init();
2709         if (ret != EFI_SUCCESS)
2710                 return CMD_RET_FAILURE;
2711
2712         ret = eficonfig_generate_media_device_boot_option();
2713         if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
2714                 return ret;
2715
2716         while (1) {
2717                 efi_menu = eficonfig_create_fixed_menu(maintenance_menu_items,
2718                                                        ARRAY_SIZE(maintenance_menu_items));
2719                 if (!efi_menu)
2720                         return CMD_RET_FAILURE;
2721
2722                 ret = eficonfig_process_common(efi_menu,
2723                                                "  ** UEFI Maintenance Menu **",
2724                                                eficonfig_menu_desc,
2725                                                eficonfig_display_statusline,
2726                                                eficonfig_print_entry,
2727                                                eficonfig_choice_entry);
2728                 eficonfig_destroy(efi_menu);
2729
2730                 if (ret == EFI_ABORTED)
2731                         break;
2732         }
2733
2734         return CMD_RET_SUCCESS;
2735 }
2736
2737 U_BOOT_CMD(
2738         eficonfig, 1, 0, do_eficonfig,
2739         "provide menu-driven UEFI variable maintenance interface",
2740         ""
2741 );