]> Git Repo - J-u-boot.git/blame - cmd/eficonfig.c
eficonfig: menu-driven addition of UEFI boot option
[J-u-boot.git] / cmd / eficonfig.c
CommitLineData
87d79142
MK
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 <common.h>
10#include <charset.h>
11#include <efi_loader.h>
12#include <efi_load_initrd.h>
13#include <efi_config.h>
14#include <efi_variable.h>
15#include <log.h>
16#include <malloc.h>
17#include <menu.h>
18#include <sort.h>
19#include <watchdog.h>
20#include <asm/unaligned.h>
21#include <linux/delay.h>
22
23static struct efi_simple_text_input_protocol *cin;
24
25#define EFICONFIG_DESCRIPTION_MAX 32
26#define EFICONFIG_OPTIONAL_DATA_MAX 64
27
28/**
29 * struct eficonfig_filepath_info - structure to be used to store file path
30 *
31 * @name: file or directory name
32 * @list: list structure
33 */
34struct eficonfig_filepath_info {
35 char *name;
36 struct list_head list;
37};
38
39/**
40 * struct eficonfig_boot_option - structure to be used for updating UEFI boot option
41 *
42 * @file_info: user selected file info
43 * @initrd_info: user selected initrd file info
44 * @boot_index: index of the boot option
45 * @description: pointer to the description string
46 * @optional_data: pointer to the optional_data
47 * @edit_completed: flag indicates edit complete
48 */
49struct eficonfig_boot_option {
50 struct eficonfig_select_file_info file_info;
51 struct eficonfig_select_file_info initrd_info;
52 unsigned int boot_index;
53 u16 *description;
54 u16 *optional_data;
55 bool edit_completed;
56};
57
58/**
59 * struct eficonfig_volume_entry_data - structure to be used to store volume info
60 *
61 * @file_info: pointer to file info structure
62 * @v: pointer to the protocol interface
63 * @dp: pointer to the device path
64 */
65struct eficonfig_volume_entry_data {
66 struct eficonfig_select_file_info *file_info;
67 struct efi_simple_file_system_protocol *v;
68 struct efi_device_path *dp;
69};
70
71/**
72 * struct eficonfig_file_entry_data - structure to be used to store file info
73 *
74 * @file_info: pointer to file info structure
75 * @is_directory: flag to identify the directory or file
76 * @file_name: name of directory or file
77 */
78struct eficonfig_file_entry_data {
79 struct eficonfig_select_file_info *file_info;
80 bool is_directory;
81 char *file_name;
82};
83
84/**
85 * eficonfig_print_msg() - print message
86 *
87 * display the message to the user, user proceeds the screen
88 * with any key press.
89 *
90 * @items: pointer to the structure of each menu entry
91 * @count: the number of menu entry
92 * @menu_header: pointer to the menu header string
93 * Return: status code
94 */
95void eficonfig_print_msg(char *msg)
96{
97 /* Flush input */
98 while (tstc())
99 getchar();
100
101 printf(ANSI_CURSOR_HIDE
102 ANSI_CLEAR_CONSOLE
103 ANSI_CURSOR_POSITION
104 "%s\n\n Press any key to continue", 3, 4, msg);
105
106 getchar();
107}
108
109/**
110 * eficonfig_print_entry() - print each menu entry
111 *
112 * @data: pointer to the data associated with each menu entry
113 */
114static void eficonfig_print_entry(void *data)
115{
116 struct eficonfig_entry *entry = data;
117 int reverse = (entry->efi_menu->active == entry->num);
118
119 /* TODO: support scroll or page for many entries */
120
121 /*
122 * Move cursor to line where the entry will be drawn (entry->num)
123 * First 3 lines(menu header) + 1 empty line
124 */
125 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
126
127 if (reverse)
128 puts(ANSI_COLOR_REVERSE);
129
130 printf("%s", entry->title);
131
132 if (reverse)
133 puts(ANSI_COLOR_RESET);
134}
135
136/**
137 * eficonfig_display_statusline() - print status line
138 *
139 * @m: pointer to the menu structure
140 */
141static void eficonfig_display_statusline(struct menu *m)
142{
143 struct eficonfig_entry *entry;
144
145 if (menu_default_choice(m, (void *)&entry) < 0)
146 return;
147
148 printf(ANSI_CURSOR_POSITION
149 "\n%s\n"
150 ANSI_CURSOR_POSITION ANSI_CLEAR_LINE ANSI_CURSOR_POSITION
151 " Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit"
152 ANSI_CLEAR_LINE_TO_END ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
153 1, 1, entry->efi_menu->menu_header, entry->efi_menu->count + 5, 1,
154 entry->efi_menu->count + 6, 1, entry->efi_menu->count + 7, 1);
155}
156
157/**
158 * eficonfig_choice_entry() - user key input handler
159 *
160 * @data: pointer to the efimenu structure
161 * Return: key string to identify the selected entry
162 */
163static char *eficonfig_choice_entry(void *data)
164{
165 int esc = 0;
166 struct list_head *pos, *n;
167 struct eficonfig_entry *entry;
168 enum bootmenu_key key = KEY_NONE;
169 struct efimenu *efi_menu = data;
170
171 while (1) {
172 bootmenu_loop((struct bootmenu_data *)efi_menu, &key, &esc);
173
174 switch (key) {
175 case KEY_UP:
176 if (efi_menu->active > 0)
177 --efi_menu->active;
178 /* no menu key selected, regenerate menu */
179 return NULL;
180 case KEY_DOWN:
181 if (efi_menu->active < efi_menu->count - 1)
182 ++efi_menu->active;
183 /* no menu key selected, regenerate menu */
184 return NULL;
185 case KEY_SELECT:
186 list_for_each_safe(pos, n, &efi_menu->list) {
187 entry = list_entry(pos, struct eficonfig_entry, list);
188 if (entry->num == efi_menu->active)
189 return entry->key;
190 }
191 break;
192 case KEY_QUIT:
193 /* Quit by choosing the last entry */
194 entry = list_last_entry(&efi_menu->list, struct eficonfig_entry, list);
195 return entry->key;
196 default:
197 /* Pressed key is not valid, no need to regenerate the menu */
198 break;
199 }
200 }
201}
202
203/**
204 * eficonfig_destroy() - destroy efimenu
205 *
206 * @efi_menu: pointer to the efimenu structure
207 */
208void eficonfig_destroy(struct efimenu *efi_menu)
209{
210 struct list_head *pos, *n;
211 struct eficonfig_entry *entry;
212
213 if (!efi_menu)
214 return;
215
216 list_for_each_safe(pos, n, &efi_menu->list) {
217 entry = list_entry(pos, struct eficonfig_entry, list);
218 free(entry->title);
219 list_del(&entry->list);
220 free(entry);
221 }
222 free(efi_menu->menu_header);
223 free(efi_menu);
224}
225
226/**
227 * eficonfig_process_quit() - callback function for "Quit" entry
228 *
229 * @data: pointer to the data
230 * Return: status code
231 */
232efi_status_t eficonfig_process_quit(void *data)
233{
234 return EFI_ABORTED;
235}
236
237/**
238 * append_entry() - append menu item
239 *
240 * @efi_menu: pointer to the efimenu structure
241 * @title: pointer to the entry title
242 * @func: callback of each entry
243 * @data: pointer to the data to be passed to each entry callback
244 * Return: status code
245 */
246static efi_status_t append_entry(struct efimenu *efi_menu,
247 char *title, eficonfig_entry_func func, void *data)
248{
249 struct eficonfig_entry *entry;
250
251 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX)
252 return EFI_OUT_OF_RESOURCES;
253
254 entry = calloc(1, sizeof(struct eficonfig_entry));
255 if (!entry)
256 return EFI_OUT_OF_RESOURCES;
257
258 entry->title = title;
259 sprintf(entry->key, "%d", efi_menu->count);
260 entry->efi_menu = efi_menu;
261 entry->func = func;
262 entry->data = data;
263 entry->num = efi_menu->count++;
264 list_add_tail(&entry->list, &efi_menu->list);
265
266 return EFI_SUCCESS;
267}
268
269/**
270 * append_quit_entry() - append quit entry
271 *
272 * @efi_menu: pointer to the efimenu structure
273 * Return: status code
274 */
275static efi_status_t append_quit_entry(struct efimenu *efi_menu)
276{
277 char *title;
278 efi_status_t ret;
279
280 title = strdup("Quit");
281 if (!title)
282 return EFI_OUT_OF_RESOURCES;
283
284 ret = append_entry(efi_menu, title, eficonfig_process_quit, NULL);
285 if (ret != EFI_SUCCESS)
286 free(title);
287
288 return ret;
289}
290
291/**
292 * eficonfig_create_fixed_menu() - create fixed entry menu structure
293 *
294 * @items: pointer to the menu entry item
295 * @count: the number of menu entry
296 * Return: pointer to the efimenu structure
297 */
298void *eficonfig_create_fixed_menu(const struct eficonfig_item *items, int count)
299{
300 u32 i;
301 char *title;
302 efi_status_t ret;
303 struct efimenu *efi_menu;
304 const struct eficonfig_item *iter = items;
305
306 efi_menu = calloc(1, sizeof(struct efimenu));
307 if (!efi_menu)
308 return NULL;
309
310 INIT_LIST_HEAD(&efi_menu->list);
311 for (i = 0; i < count; i++, iter++) {
312 title = strdup(iter->title);
313 if (!title)
314 goto out;
315
316 ret = append_entry(efi_menu, title, iter->func, iter->data);
317 if (ret != EFI_SUCCESS) {
318 free(title);
319 goto out;
320 }
321 }
322
323 return efi_menu;
324out:
325 eficonfig_destroy(efi_menu);
326
327 return NULL;
328}
329
330/**
331 * eficonfig_process_common() - main handler for UEFI menu
332 *
333 * Construct the structures required to show the menu, then handle
334 * the user input interacting with u-boot menu functions.
335 *
336 * @efi_menu: pointer to the efimenu structure
337 * @menu_header: pointer to the menu header string
338 * Return: status code
339 */
340efi_status_t eficonfig_process_common(struct efimenu *efi_menu, char *menu_header)
341{
342 struct menu *menu;
343 void *choice = NULL;
344 struct list_head *pos, *n;
345 struct eficonfig_entry *entry;
346 efi_status_t ret = EFI_SUCCESS;
347
348 if (efi_menu->count > EFICONFIG_ENTRY_NUM_MAX)
349 return EFI_OUT_OF_RESOURCES;
350
351 efi_menu->delay = -1;
352 efi_menu->active = 0;
353
354 if (menu_header) {
355 efi_menu->menu_header = strdup(menu_header);
356 if (!efi_menu->menu_header)
357 return EFI_OUT_OF_RESOURCES;
358 }
359
360 menu = menu_create(NULL, 0, 1, eficonfig_display_statusline,
361 eficonfig_print_entry, eficonfig_choice_entry,
362 efi_menu);
363 if (!menu)
364 return EFI_INVALID_PARAMETER;
365
366 list_for_each_safe(pos, n, &efi_menu->list) {
367 entry = list_entry(pos, struct eficonfig_entry, list);
368 if (!menu_item_add(menu, entry->key, entry)) {
369 ret = EFI_INVALID_PARAMETER;
370 goto out;
371 }
372 }
373
374 entry = list_first_entry_or_null(&efi_menu->list, struct eficonfig_entry, list);
375 if (entry)
376 menu_default_set(menu, entry->key);
377
378 printf(ANSI_CURSOR_HIDE
379 ANSI_CLEAR_CONSOLE
380 ANSI_CURSOR_POSITION, 1, 1);
381
382 if (menu_get_choice(menu, &choice)) {
383 entry = choice;
384 if (entry->func)
385 ret = entry->func(entry->data);
386 }
387out:
388 menu_destroy(menu);
389
390 printf(ANSI_CLEAR_CONSOLE
391 ANSI_CURSOR_POSITION
392 ANSI_CURSOR_SHOW, 1, 1);
393
394 return ret;
395}
396
397/**
398 * eficonfig_volume_selected() - handler of volume selection
399 *
400 * @data: pointer to the data of selected entry
401 * Return: status code
402 */
403static efi_status_t eficonfig_volume_selected(void *data)
404{
405 struct eficonfig_volume_entry_data *info = data;
406
407 if (info) {
408 info->file_info->current_volume = info->v;
409 info->file_info->dp_volume = info->dp;
410 }
411
412 return EFI_SUCCESS;
413}
414
415/**
416 * create_selected_device_path() - create device path
417 *
418 * @file_info: pointer to the selected file information
419 * Return:
420 * device path or NULL. Caller must free the returned value
421 */
422static
423struct efi_device_path *create_selected_device_path(struct eficonfig_select_file_info *file_info)
424{
425 char *p;
426 void *buf;
427 efi_uintn_t fp_size;
428 struct efi_device_path *dp;
429 struct efi_device_path_file_path *fp;
430
431 fp_size = sizeof(struct efi_device_path) +
432 ((u16_strlen(file_info->current_path) + 1) * sizeof(u16));
433 buf = calloc(1, fp_size + sizeof(END));
434 if (!buf)
435 return NULL;
436
437 fp = buf;
438 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
439 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
440 fp->dp.length = (u16)fp_size;
441 u16_strcpy(fp->str, file_info->current_path);
442
443 p = buf;
444 p += fp_size;
445 *((struct efi_device_path *)p) = END;
446
447 dp = efi_dp_append(file_info->dp_volume, (struct efi_device_path *)buf);
448 free(buf);
449
450 return dp;
451}
452
453/**
454 * eficonfig_file_selected() - handler of file selection
455 *
456 * @data: pointer to the data of selected entry
457 * Return: status code
458 */
459static efi_status_t eficonfig_file_selected(void *data)
460{
461 u16 *tmp;
462 struct eficonfig_file_entry_data *info = data;
463
464 if (!info)
465 return EFI_INVALID_PARAMETER;
466
467 if (!strcmp(info->file_name, "..")) {
468 struct eficonfig_filepath_info *iter;
469 struct list_head *pos, *n;
470 int is_last;
471 char *filepath;
472 tmp = info->file_info->current_path;
473
474 memset(info->file_info->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
475 filepath = calloc(1, EFICONFIG_FILE_PATH_MAX);
476 if (!filepath)
477 return EFI_OUT_OF_RESOURCES;
478
479 list_for_each_safe(pos, n, &info->file_info->filepath_list) {
480 iter = list_entry(pos, struct eficonfig_filepath_info, list);
481
482 is_last = list_is_last(&iter->list, &info->file_info->filepath_list);
483 if (is_last) {
484 list_del(&iter->list);
485 free(iter->name);
486 free(iter);
487 break;
488 }
489 strlcat(filepath, iter->name, EFICONFIG_FILE_PATH_MAX);
490 }
491 utf8_utf16_strcpy(&tmp, filepath);
492 } else {
493 size_t new_len;
494 struct eficonfig_filepath_info *filepath_info;
495
496 new_len = u16_strlen(info->file_info->current_path) +
497 strlen(info->file_name);
498 if (new_len >= EFICONFIG_FILE_PATH_MAX) {
499 eficonfig_print_msg("File path is too long!");
500 return EFI_INVALID_PARAMETER;
501 }
502 tmp = &info->file_info->current_path[u16_strlen(info->file_info->current_path)];
503 utf8_utf16_strcpy(&tmp, info->file_name);
504
505 filepath_info = calloc(1, sizeof(struct eficonfig_filepath_info));
506 if (!filepath_info)
507 return EFI_OUT_OF_RESOURCES;
508
509 filepath_info->name = strdup(info->file_name);
510 if (!filepath_info->name) {
511 free(filepath_info);
512 return EFI_OUT_OF_RESOURCES;
513 }
514 list_add_tail(&filepath_info->list, &info->file_info->filepath_list);
515
516 if (!info->is_directory)
517 info->file_info->file_selected = true;
518 }
519
520 return EFI_SUCCESS;
521}
522
523/**
524 * eficonfig_select_volume() - construct the volume selection menu
525 *
526 * @file_info: pointer to the file selection structure
527 * Return: status code
528 */
529static efi_status_t eficonfig_select_volume(struct eficonfig_select_file_info *file_info)
530{
531 u32 i;
532 efi_status_t ret;
533 efi_uintn_t count;
534 struct efimenu *efi_menu;
535 struct list_head *pos, *n;
536 struct efi_handler *handler;
537 struct eficonfig_entry *entry;
538 struct efi_device_path *device_path;
539 efi_handle_t *volume_handles = NULL;
540 struct efi_simple_file_system_protocol *v;
541
542 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
543 NULL, &count, (efi_handle_t **)&volume_handles);
544 if (ret != EFI_SUCCESS) {
545 eficonfig_print_msg("No block device found!");
546 return ret;
547 }
548
549 efi_menu = calloc(1, sizeof(struct efimenu));
550 if (!efi_menu)
551 return EFI_OUT_OF_RESOURCES;
552
553 INIT_LIST_HEAD(&efi_menu->list);
554 for (i = 0; i < count; i++) {
555 char *devname;
556 struct efi_block_io *block_io;
557 struct eficonfig_volume_entry_data *info;
558
559 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
560 break;
561
562 ret = efi_search_protocol(volume_handles[i],
563 &efi_simple_file_system_protocol_guid, &handler);
564 if (ret != EFI_SUCCESS)
565 continue;
566 ret = efi_protocol_open(handler, (void **)&v, efi_root, NULL,
567 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
568 if (ret != EFI_SUCCESS)
569 continue;
570
571 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
572 if (ret != EFI_SUCCESS)
573 continue;
574 ret = efi_protocol_open(handler, (void **)&device_path,
575 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
576 if (ret != EFI_SUCCESS)
577 continue;
578
579 ret = efi_search_protocol(volume_handles[i], &efi_block_io_guid, &handler);
580 if (ret != EFI_SUCCESS)
581 continue;
582 ret = efi_protocol_open(handler, (void **)&block_io,
583 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
584 if (ret != EFI_SUCCESS)
585 continue;
586
587 info = calloc(1, sizeof(struct eficonfig_volume_entry_data));
588 if (!info) {
589 ret = EFI_OUT_OF_RESOURCES;
590 goto out;
591 }
592
593 devname = calloc(1, BOOTMENU_DEVICE_NAME_MAX);
594 if (!devname) {
595 free(info);
596 ret = EFI_OUT_OF_RESOURCES;
597 goto out;
598 }
599 ret = efi_disk_get_device_name(volume_handles[i], devname,
600 BOOTMENU_DEVICE_NAME_MAX);
601 if (ret != EFI_SUCCESS) {
602 free(info);
603 goto out;
604 }
605
606 info->v = v;
607 info->dp = device_path;
608 info->file_info = file_info;
609 ret = append_entry(efi_menu, devname, eficonfig_volume_selected, info);
610 if (ret != EFI_SUCCESS) {
611 free(info);
612 goto out;
613 }
614 }
615
616 ret = append_quit_entry(efi_menu);
617 if (ret != EFI_SUCCESS)
618 goto out;
619
620 ret = eficonfig_process_common(efi_menu, " ** Select Volume **");
621out:
622 efi_free_pool(volume_handles);
623 list_for_each_safe(pos, n, &efi_menu->list) {
624 entry = list_entry(pos, struct eficonfig_entry, list);
625 free(entry->data);
626 }
627 eficonfig_destroy(efi_menu);
628
629 return ret;
630}
631
632/**
633 * sort_file() - sort the file name in ascii order
634 *
635 * @data1: pointer to the file entry data
636 * @data2: pointer to the file entry data
637 * Return: -1 if the data1 file name is less than data2 file name,
638 * 0 if both file name match,
639 * 1 if the data1 file name is greater thant data2 file name.
640 */
641static int sort_file(const void *arg1, const void *arg2)
642{
643 const struct eficonfig_file_entry_data *data1, *data2;
644
645 data1 = *((const struct eficonfig_file_entry_data **)arg1);
646 data2 = *((const struct eficonfig_file_entry_data **)arg2);
647
648 return strcasecmp(data1->file_name, data2->file_name);
649}
650
651/**
652 * eficonfig_create_file_entry() - construct the file menu entry
653 *
654 * @efi_menu: pointer to the efimenu structure
655 * @count: number of the directory and file
656 * @tmp_infos: pointer to the entry data array
657 * @f: pointer to the file handle
658 * @buf: pointer to the buffer to store the directory information
659 * @file_info: pointer to the file selection structure
660 * Return: status code
661 */
662static efi_status_t
663eficonfig_create_file_entry(struct efimenu *efi_menu, u32 count,
664 struct eficonfig_file_entry_data **tmp_infos,
665 struct efi_file_handle *f, struct efi_file_info *buf,
666 struct eficonfig_select_file_info *file_info)
667{
668 char *name, *p;
669 efi_uintn_t len;
670 efi_status_t ret;
671 u32 i, entry_num = 0;
672 struct eficonfig_file_entry_data *info;
673
674 efi_file_setpos_int(f, 0);
675 /* Read directory and construct menu structure */
676 for (i = 0; i < count; i++) {
677 if (entry_num >= EFICONFIG_ENTRY_NUM_MAX - 1)
678 break;
679
680 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
681 ret = efi_file_read_int(f, &len, buf);
682 if (ret != EFI_SUCCESS || len == 0)
683 break;
684
685 info = calloc(1, sizeof(struct eficonfig_file_entry_data));
686 if (!info) {
687 ret = EFI_OUT_OF_RESOURCES;
688 goto out;
689 }
690
691 /* append '\\' at the end of directory name */
692 name = calloc(1, utf16_utf8_strlen(buf->file_name) + 2);
693 if (!name) {
694 ret = EFI_OUT_OF_RESOURCES;
695 free(info);
696 goto out;
697 }
698 p = name;
699 utf16_utf8_strcpy(&p, buf->file_name);
700 if (buf->attribute & EFI_FILE_DIRECTORY) {
701 /* filter out u'.' */
702 if (!u16_strcmp(buf->file_name, u".")) {
703 free(info);
704 free(name);
705 continue;
706 }
707 name[u16_strlen(buf->file_name)] = '\\';
708 info->is_directory = true;
709 }
710
711 info->file_name = name;
712 info->file_info = file_info;
713 tmp_infos[entry_num++] = info;
714 }
715
716 qsort(tmp_infos, entry_num, sizeof(*tmp_infos),
717 (int (*)(const void *, const void *))sort_file);
718
719 for (i = 0; i < entry_num; i++) {
720 ret = append_entry(efi_menu, tmp_infos[i]->file_name,
721 eficonfig_file_selected, tmp_infos[i]);
722 if (ret != EFI_SUCCESS)
723 goto out;
724 }
725
726out:
727 return ret;
728}
729
730/**
731 * eficonfig_select_file() - construct the file selection menu
732 *
733 * @file_info: pointer to the file selection structure
734 * @root: pointer to the file handle
735 * Return: status code
736 */
737static efi_status_t eficonfig_select_file(struct eficonfig_select_file_info *file_info,
738 struct efi_file_handle *root)
739{
740 u32 count = 0, i;
741 efi_uintn_t len;
742 efi_status_t ret;
743 struct efimenu *efi_menu;
744 struct efi_file_handle *f;
745 struct efi_file_info *buf;
746 struct eficonfig_file_entry_data **tmp_infos;
747
748 buf = calloc(1, sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE);
749 if (!buf)
750 return EFI_OUT_OF_RESOURCES;
751
752 while (!file_info->file_selected) {
753 efi_menu = calloc(1, sizeof(struct efimenu));
754 if (!efi_menu) {
755 ret = EFI_OUT_OF_RESOURCES;
756 goto out;
757 }
758 INIT_LIST_HEAD(&efi_menu->list);
759
760 ret = efi_file_open_int(root, &f, file_info->current_path, EFI_FILE_MODE_READ, 0);
761 if (ret != EFI_SUCCESS) {
762 eficonfig_print_msg("Reading volume failed!");
763 free(efi_menu);
764 ret = EFI_ABORTED;
765 goto out;
766 }
767
768 /* Count the number of directory entries */
769 for (;;) {
770 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
771 ret = efi_file_read_int(f, &len, buf);
772 if (ret != EFI_SUCCESS || len == 0)
773 break;
774
775 count++;
776 }
777
778 /* allocate array to sort the entry */
779 tmp_infos = calloc(count, sizeof(*tmp_infos));
780 if (!tmp_infos) {
781 ret = EFI_OUT_OF_RESOURCES;
782 goto err;
783 }
784
785 ret = eficonfig_create_file_entry(efi_menu, count, tmp_infos,
786 f, buf, file_info);
787 if (ret != EFI_SUCCESS)
788 goto err;
789
790 ret = append_quit_entry(efi_menu);
791 if (ret != EFI_SUCCESS)
792 goto err;
793
794 ret = eficonfig_process_common(efi_menu, " ** Select File **");
795err:
796 efi_file_close_int(f);
797 eficonfig_destroy(efi_menu);
798
799 if (tmp_infos) {
800 for (i = 0; i < count; i++)
801 free(tmp_infos[i]);
802 }
803
804 free(tmp_infos);
805
806 if (ret != EFI_SUCCESS)
807 break;
808 }
809
810out:
811 free(buf);
812
813 return ret;
814}
815
816/**
817 * handle_user_input() - handle user input
818 *
819 * @buf: pointer to the buffer
820 * @buf_size: size of the buffer
821 * @cursor_col: cursor column for user input
822 * @msg: pointer to the string to display
823 * Return: status code
824 */
825static efi_status_t handle_user_input(u16 *buf, int buf_size,
826 int cursor_col, char *msg)
827{
828 u16 *tmp;
829 efi_status_t ret;
830
831 printf(ANSI_CLEAR_CONSOLE
832 ANSI_CURSOR_POSITION
833 "%s"
834 ANSI_CURSOR_POSITION
835 " Press ENTER to complete, ESC/CTRL+C to quit",
836 0, 1, msg, 8, 1);
837
838 /* tmp is used to accept user cancel */
839 tmp = calloc(1, buf_size * sizeof(u16));
840 if (!tmp)
841 return EFI_OUT_OF_RESOURCES;
842
843 ret = efi_console_get_u16_string(cin, tmp, buf_size, NULL, 4, cursor_col);
844 if (ret == EFI_SUCCESS)
845 u16_strcpy(buf, tmp);
846
847 free(tmp);
848
849 /* to stay the parent menu */
850 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
851
852 return ret;
853}
854
855/**
856 * eficonfig_boot_add_enter_description() - handle user input for description
857 *
858 * @data: pointer to the internal boot option structure
859 * Return: status code
860 */
861static efi_status_t eficonfig_boot_add_enter_description(void *data)
862{
863 struct eficonfig_boot_option *bo = data;
864
865 return handle_user_input(bo->description, EFICONFIG_DESCRIPTION_MAX, 22,
866 "\n ** Edit Description **\n"
867 "\n"
868 " enter description: ");
869}
870
871/**
872 * eficonfig_boot_add_optional_data() - handle user input for optional data
873 *
874 * @data: pointer to the internal boot option structure
875 * Return: status code
876 */
877static efi_status_t eficonfig_boot_add_optional_data(void *data)
878{
879 struct eficonfig_boot_option *bo = data;
880
881 return handle_user_input(bo->optional_data, EFICONFIG_OPTIONAL_DATA_MAX, 24,
882 "\n ** Edit Optional Data **\n"
883 "\n"
884 " enter optional data:");
885}
886
887/**
888 * eficonfig_boot_edit_save() - handler to save the boot option
889 *
890 * @data: pointer to the internal boot option structure
891 * Return: status code
892 */
893static efi_status_t eficonfig_boot_edit_save(void *data)
894{
895 struct eficonfig_boot_option *bo = data;
896
897 if (u16_strlen(bo->description) == 0) {
898 eficonfig_print_msg("Boot Description is empty!");
899 bo->edit_completed = false;
900 return EFI_NOT_READY;
901 }
902 if (u16_strlen(bo->file_info.current_path) == 0) {
903 eficonfig_print_msg("File is not selected!");
904 bo->edit_completed = false;
905 return EFI_NOT_READY;
906 }
907
908 bo->edit_completed = true;
909
910 return EFI_SUCCESS;
911}
912
913/**
914 * eficonfig_process_select_file() - callback function for "Select File" entry
915 *
916 * @data: pointer to the data
917 * Return: status code
918 */
919efi_status_t eficonfig_process_select_file(void *data)
920{
921 return EFI_SUCCESS;
922}
923
924/**
925 * eficonfig_process_clear_file_selection() - callback function for "Clear" entry
926 *
927 * @data: pointer to the data
928 * Return: status code
929 */
930efi_status_t eficonfig_process_clear_file_selection(void *data)
931{
932 struct eficonfig_select_file_info *file_info = data;
933
934 /* clear the existing file information */
935 file_info->current_volume = NULL;
936 file_info->current_path[0] = u'\0';
937 file_info->dp_volume = NULL;
938
939 return EFI_ABORTED;
940}
941
942static struct eficonfig_item select_file_menu_items[] = {
943 {"Select File", eficonfig_process_select_file},
944 {"Clear", eficonfig_process_clear_file_selection},
945 {"Quit", eficonfig_process_quit},
946};
947
948
949/**
950 * eficonfig_display_select_file_option() - display select file option
951 *
952 * @file_info: pointer to the file information structure
953 * Return: status code
954 */
955efi_status_t eficonfig_display_select_file_option(struct eficonfig_select_file_info *file_info)
956{
957 efi_status_t ret;
958 struct efimenu *efi_menu;
959
960 select_file_menu_items[1].data = file_info;
961 efi_menu = eficonfig_create_fixed_menu(select_file_menu_items,
962 ARRAY_SIZE(select_file_menu_items));
963 if (!efi_menu)
964 return EFI_OUT_OF_RESOURCES;
965
966 ret = eficonfig_process_common(efi_menu, " ** Update File **");
967 if (ret != EFI_SUCCESS) /* User selects "Clear" or "Quit" */
968 ret = EFI_NOT_READY;
969
970 eficonfig_destroy(efi_menu);
971
972 return ret;
973}
974
975/**
976 * eficonfig_select_file_handler() - handle user file selection
977 *
978 * @data: pointer to the data
979 * Return: status code
980 */
981efi_status_t eficonfig_select_file_handler(void *data)
982{
983 size_t len;
984 efi_status_t ret;
985 struct list_head *pos, *n;
986 struct efi_file_handle *root;
987 struct eficonfig_filepath_info *item;
988 struct eficonfig_select_file_info *tmp = NULL;
989 struct eficonfig_select_file_info *file_info = data;
990
991 ret = eficonfig_display_select_file_option(file_info);
992 if (ret != EFI_SUCCESS)
993 return ret;
994
995 tmp = calloc(1, sizeof(struct eficonfig_select_file_info));
996 if (!tmp)
997 return EFI_OUT_OF_RESOURCES;
998
999 tmp->current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1000 if (!tmp->current_path) {
1001 free(tmp);
1002 return EFI_OUT_OF_RESOURCES;
1003 }
1004 INIT_LIST_HEAD(&tmp->filepath_list);
1005
1006 while (!tmp->file_selected) {
1007 tmp->current_volume = NULL;
1008 memset(tmp->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
1009
1010 ret = eficonfig_select_volume(tmp);
1011 if (ret != EFI_SUCCESS)
1012 goto out;
1013
1014 if (!tmp->current_volume)
1015 return EFI_INVALID_PARAMETER;
1016
1017 ret = efi_open_volume_int(tmp->current_volume, &root);
1018 if (ret != EFI_SUCCESS)
1019 goto out;
1020
1021 ret = eficonfig_select_file(tmp, root);
1022 if (ret == EFI_ABORTED)
1023 continue;
1024 if (ret != EFI_SUCCESS)
1025 goto out;
1026 }
1027
1028out:
1029 if (ret == EFI_SUCCESS) {
1030 len = u16_strlen(tmp->current_path);
1031 len = (len >= EFICONFIG_FILE_PATH_MAX) ? (EFICONFIG_FILE_PATH_MAX - 1) : len;
1032 memcpy(file_info->current_path, tmp->current_path, len * sizeof(u16));
1033 file_info->current_path[len] = u'\0';
1034 file_info->current_volume = tmp->current_volume;
1035 file_info->dp_volume = tmp->dp_volume;
1036 }
1037
1038 list_for_each_safe(pos, n, &tmp->filepath_list) {
1039 item = list_entry(pos, struct eficonfig_filepath_info, list);
1040 list_del(&item->list);
1041 free(item->name);
1042 free(item);
1043 }
1044 free(tmp->current_path);
1045 free(tmp);
1046
1047 /* to stay the parent menu */
1048 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1049
1050 return ret;
1051}
1052
1053/**
1054 * eficonfig_get_unused_bootoption() - get unused "Boot####" index
1055 *
1056 * @buf: pointer to the buffer to store boot option variable name
1057 * @buf_size: buffer size
1058 * @index: pointer to store the index in the BootOrder variable
1059 * Return: status code
1060 */
1061efi_status_t eficonfig_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
1062 unsigned int *index)
1063{
1064 u32 i;
1065 efi_status_t ret;
1066 efi_uintn_t size;
1067
1068 if (buf_size < u16_strsize(u"Boot####"))
1069 return EFI_BUFFER_TOO_SMALL;
1070
1071 for (i = 0; i <= 0xFFFF; i++) {
1072 size = 0;
1073 efi_create_indexed_name(buf, buf_size, "Boot", i);
1074 ret = efi_get_variable_int(buf, &efi_global_variable_guid,
1075 NULL, &size, NULL, NULL);
1076 if (ret == EFI_BUFFER_TOO_SMALL)
1077 continue;
1078 else
1079 break;
1080 }
1081
1082 if (i > 0xFFFF)
1083 return EFI_OUT_OF_RESOURCES;
1084
1085 *index = i;
1086
1087 return EFI_SUCCESS;
1088}
1089
1090/**
1091 * eficonfig_set_boot_option() - set boot option
1092 *
1093 * @varname: pointer to variable name
1094 * @dp: pointer to device path
1095 * @label: pointer to label string
1096 * @optional_data: pointer to optional data
1097 * Return: status code
1098 */
1099static efi_status_t eficonfig_set_boot_option(u16 *varname, struct efi_device_path *dp,
1100 efi_uintn_t dp_size, u16 *label, char *optional_data)
1101{
1102 void *p = NULL;
1103 efi_status_t ret;
1104 efi_uintn_t size;
1105 struct efi_load_option lo;
1106
1107 lo.file_path = dp;
1108 lo.file_path_length = dp_size;
1109 lo.attributes = LOAD_OPTION_ACTIVE;
1110 lo.optional_data = optional_data;
1111 lo.label = label;
1112
1113 size = efi_serialize_load_option(&lo, (u8 **)&p);
1114 if (!size)
1115 return EFI_INVALID_PARAMETER;
1116
1117 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1118 EFI_VARIABLE_NON_VOLATILE |
1119 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1120 EFI_VARIABLE_RUNTIME_ACCESS,
1121 size, p, false);
1122 free(p);
1123
1124 return ret;
1125}
1126
1127/**
1128 * eficonfig_append_bootorder() - append new boot option in BootOrder variable
1129 *
1130 * @index: "Boot####" index to append to BootOrder variable
1131 * Return: status code
1132 */
1133efi_status_t eficonfig_append_bootorder(u16 index)
1134{
1135 u16 *bootorder;
1136 efi_status_t ret;
1137 u16 *new_bootorder = NULL;
1138 efi_uintn_t last, size, new_size;
1139
1140 /* append new boot option */
1141 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1142 last = size / sizeof(u16);
1143 new_size = size + sizeof(u16);
1144 new_bootorder = calloc(1, new_size);
1145 if (!new_bootorder) {
1146 ret = EFI_OUT_OF_RESOURCES;
1147 goto out;
1148 }
1149 memcpy(new_bootorder, bootorder, size);
1150 new_bootorder[last] = index;
1151
1152 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1153 EFI_VARIABLE_NON_VOLATILE |
1154 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1155 EFI_VARIABLE_RUNTIME_ACCESS,
1156 new_size, new_bootorder, false);
1157 if (ret != EFI_SUCCESS)
1158 goto out;
1159
1160out:
1161 free(bootorder);
1162 free(new_bootorder);
1163
1164 return ret;
1165}
1166
1167/**
1168 * create_boot_option_entry() - create boot option entry
1169 *
1170 * @efi_menu: pointer to the efimenu structure
1171 * @title: pointer to the entry title
1172 * @val: pointer to boot option label
1173 * @func: callback of each entry
1174 * @data: pointer to the data to be passed to each entry callback
1175 * Return: status code
1176 */
1177static efi_status_t create_boot_option_entry(struct efimenu *efi_menu, char *title, u16 *val,
1178 eficonfig_entry_func func, void *data)
1179{
1180 u32 len;
1181 char *p, *buf;
1182
1183 len = strlen(title) + 1;
1184 if (val)
1185 len += utf16_utf8_strlen(val);
1186 buf = calloc(1, len);
1187 if (!buf)
1188 return EFI_OUT_OF_RESOURCES;
1189
1190 strcpy(buf, title);
1191 if (val) {
1192 p = buf + strlen(title);
1193 utf16_utf8_strcpy(&p, val);
1194 }
1195
1196 return append_entry(efi_menu, buf, func, data);
1197}
1198
1199/**
1200 * prepare_file_selection_entry() - prepare file selection entry
1201 *
1202 * @efi_menu: pointer to the efimenu structure
1203 * @title: pointer to the title string
1204 * @file_info: pointer to the file info
1205 * Return: status code
1206 */
1207static efi_status_t prepare_file_selection_entry(struct efimenu *efi_menu, char *title,
1208 struct eficonfig_select_file_info *file_info)
1209{
1210 u32 len;
1211 efi_status_t ret;
1212 u16 *file_name, *p;
1213 efi_handle_t handle;
1214 char devname[BOOTMENU_DEVICE_NAME_MAX] = {0};
1215
1216 /* get the device name only when the user already selected the file path */
1217 handle = efi_dp_find_obj(file_info->dp_volume, NULL, NULL);
1218 if (handle) {
1219 ret = efi_disk_get_device_name(handle, devname, BOOTMENU_DEVICE_NAME_MAX);
1220 if (ret != EFI_SUCCESS)
1221 return ret;
1222 }
1223
1224 /* append u'/' to devname, it is just for display purpose. */
1225 if (file_info->current_path[0] != u'\0' && file_info->current_path[0] != u'/')
1226 strlcat(devname, "/", BOOTMENU_DEVICE_NAME_MAX);
1227
1228 len = strlen(devname);
1229 len += utf16_utf8_strlen(file_info->current_path) + 1;
1230 file_name = calloc(1, len * sizeof(u16));
1231 if (!file_name)
1232 return ret;
1233
1234 p = file_name;
1235 utf8_utf16_strcpy(&p, devname);
1236 u16_strlcat(file_name, file_info->current_path, len);
1237 ret = create_boot_option_entry(efi_menu, title, file_name,
1238 eficonfig_select_file_handler, file_info);
1239 free(file_name);
1240 return ret;
1241}
1242
1243/**
1244 * eficonfig_show_boot_option() - prepare menu entry for editing boot option
1245 *
1246 * Construct the structures to create edit boot option menu
1247 *
1248 * @bo: pointer to the boot option
1249 * @header_str: pointer to the header string
1250 * Return: status code
1251 */
1252static efi_status_t eficonfig_show_boot_option(struct eficonfig_boot_option *bo,
1253 char *header_str)
1254{
1255 efi_status_t ret;
1256 struct efimenu *efi_menu;
1257
1258 efi_menu = calloc(1, sizeof(struct efimenu));
1259 if (!efi_menu)
1260 return EFI_OUT_OF_RESOURCES;
1261
1262 INIT_LIST_HEAD(&efi_menu->list);
1263
1264 ret = create_boot_option_entry(efi_menu, "Description: ", bo->description,
1265 eficonfig_boot_add_enter_description, bo);
1266 if (ret != EFI_SUCCESS)
1267 goto out;
1268
1269 ret = prepare_file_selection_entry(efi_menu, "File: ", &bo->file_info);
1270 if (ret != EFI_SUCCESS)
1271 goto out;
1272
1273 ret = prepare_file_selection_entry(efi_menu, "Initrd File: ", &bo->initrd_info);
1274 if (ret != EFI_SUCCESS)
1275 goto out;
1276
1277 ret = create_boot_option_entry(efi_menu, "Optional Data: ", bo->optional_data,
1278 eficonfig_boot_add_optional_data, bo);
1279 if (ret != EFI_SUCCESS)
1280 goto out;
1281
1282 ret = create_boot_option_entry(efi_menu, "Save", NULL,
1283 eficonfig_boot_edit_save, bo);
1284 if (ret != EFI_SUCCESS)
1285 goto out;
1286
1287 ret = create_boot_option_entry(efi_menu, "Quit", NULL,
1288 eficonfig_process_quit, NULL);
1289 if (ret != EFI_SUCCESS)
1290 goto out;
1291
1292 ret = eficonfig_process_common(efi_menu, header_str);
1293out:
1294 eficonfig_destroy(efi_menu);
1295
1296 return ret;
1297}
1298
1299/**
1300 * fill_file_info() - fill the file info from efi_device_path structure
1301 *
1302 * @dp: pointer to the device path
1303 * @file_info: pointer to the file info structure
1304 * @device_dp: pointer to the volume device path
1305 */
1306static void fill_file_info(struct efi_device_path *dp,
1307 struct eficonfig_select_file_info *file_info,
1308 struct efi_device_path *device_dp)
1309{
1310 u16 *file_str, *p;
1311 struct efi_device_path *file_dp = NULL;
1312
1313 efi_dp_split_file_path(dp, &device_dp, &file_dp);
1314 file_info->dp_volume = device_dp;
1315
1316 if (file_dp) {
1317 file_str = efi_dp_str(file_dp);
1318 /*
1319 * efi_convert_device_path_to_text() automatically adds u'/' at the
1320 * beginning of file name, remove u'/' before copying to current_path
1321 */
1322 p = file_str;
1323 if (p[0] == u'/')
1324 p++;
1325
1326 u16_strcpy(file_info->current_path, p);
1327 efi_free_pool(file_dp);
1328 efi_free_pool(file_str);
1329 }
1330}
1331
1332/**
1333 * eficonfig_edit_boot_option() - prepare boot option structure for editing
1334 *
1335 * Construct the boot option structure and copy the existing value
1336 *
1337 * @varname: pointer to the UEFI variable name
1338 * @bo: pointer to the boot option
1339 * @load_option: pointer to the load option
1340 * @load_option_size: size of the load option
1341 * @header_str: pointer to the header string
1342 * Return : status code
1343 */
1344static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_boot_option *bo,
1345 void *load_option, efi_uintn_t load_option_size,
1346 char *header_str)
1347{
1348 size_t len;
1349 efi_status_t ret;
1350 char *tmp = NULL, *p;
1351 struct efi_load_option lo = {0};
1352 efi_uintn_t final_dp_size;
1353 struct efi_device_path *dp = NULL;
1354 efi_uintn_t size = load_option_size;
1355 struct efi_device_path *final_dp = NULL;
1356 struct efi_device_path *device_dp = NULL;
1357 struct efi_device_path *initrd_dp = NULL;
1358 struct efi_device_path *initrd_device_dp = NULL;
1359
1360 const struct efi_initrd_dp id_dp = {
1361 .vendor = {
1362 {
1363 DEVICE_PATH_TYPE_MEDIA_DEVICE,
1364 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
1365 sizeof(id_dp.vendor),
1366 },
1367 EFI_INITRD_MEDIA_GUID,
1368 },
1369 .end = {
1370 DEVICE_PATH_TYPE_END,
1371 DEVICE_PATH_SUB_TYPE_END,
1372 sizeof(id_dp.end),
1373 }
1374 };
1375
1376 bo->file_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1377 if (!bo->file_info.current_path) {
1378 ret = EFI_OUT_OF_RESOURCES;
1379 goto out;
1380 }
1381
1382 bo->initrd_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1383 if (!bo->file_info.current_path) {
1384 ret = EFI_OUT_OF_RESOURCES;
1385 goto out;
1386 }
1387
1388 bo->description = calloc(1, EFICONFIG_DESCRIPTION_MAX * sizeof(u16));
1389 if (!bo->description) {
1390 ret = EFI_OUT_OF_RESOURCES;
1391 goto out;
1392 }
1393
1394 bo->optional_data = calloc(1, EFICONFIG_OPTIONAL_DATA_MAX * sizeof(u16));
1395 if (!bo->optional_data) {
1396 ret = EFI_OUT_OF_RESOURCES;
1397 goto out;
1398 }
1399
1400 /* copy the preset value */
1401 if (load_option) {
1402 ret = efi_deserialize_load_option(&lo, load_option, &size);
1403 if (ret != EFI_SUCCESS)
1404 goto out;
1405
1406 if (!lo.label || (lo.label && u16_strlen(lo.label) >= EFICONFIG_DESCRIPTION_MAX)) {
1407 ret = EFI_INVALID_PARAMETER;
1408 goto out;
1409 }
1410 u16_strcpy(bo->description, lo.label);
1411
1412 /* EFI image file path is a first instance */
1413 if (lo.file_path)
1414 fill_file_info(lo.file_path, &bo->file_info, device_dp);
1415
1416 /* Initrd file path(optional) is placed at second instance. */
1417 initrd_dp = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
1418 if (initrd_dp) {
1419 fill_file_info(initrd_dp, &bo->initrd_info, initrd_device_dp);
1420 efi_free_pool(initrd_dp);
1421 }
1422
1423 if (size > 0)
1424 memcpy(bo->optional_data, lo.optional_data, size);
1425 }
1426
1427 while (1) {
1428 ret = eficonfig_show_boot_option(bo, header_str);
1429 if (ret == EFI_SUCCESS && bo->edit_completed)
1430 break;
1431 if (ret == EFI_NOT_READY)
1432 continue;
1433 if (ret != EFI_SUCCESS)
1434 goto out;
1435 }
1436
1437 if (bo->initrd_info.dp_volume) {
1438 dp = create_selected_device_path(&bo->initrd_info);
1439 if (!dp) {
1440 ret = EFI_OUT_OF_RESOURCES;
1441 goto out;
1442 }
1443 initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp, dp);
1444 efi_free_pool(dp);
1445 }
1446
1447 dp = create_selected_device_path(&bo->file_info);
1448 if (!dp) {
1449 ret = EFI_OUT_OF_RESOURCES;
1450 goto out;
1451 }
1452 final_dp_size = efi_dp_size(dp) + sizeof(END);
1453 if (initrd_dp) {
1454 final_dp = efi_dp_concat(dp, initrd_dp);
1455 final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
1456 } else {
1457 final_dp = efi_dp_dup(dp);
1458 }
1459 efi_free_pool(dp);
1460
1461 if (!final_dp)
1462 goto out;
1463
1464 if (utf16_utf8_strlen(bo->optional_data)) {
1465 len = utf16_utf8_strlen(bo->optional_data) + 1;
1466 tmp = calloc(1, len);
1467 if (!tmp)
1468 goto out;
1469 p = tmp;
1470 utf16_utf8_strncpy(&p, bo->optional_data, u16_strlen(bo->optional_data));
1471 }
1472
1473 ret = eficonfig_set_boot_option(varname, final_dp, final_dp_size, bo->description, tmp);
1474 if (ret != EFI_SUCCESS)
1475 goto out;
1476out:
1477 free(tmp);
1478 free(bo->optional_data);
1479 free(bo->description);
1480 free(bo->file_info.current_path);
1481 free(bo->initrd_info.current_path);
1482 efi_free_pool(device_dp);
1483 efi_free_pool(initrd_device_dp);
1484 efi_free_pool(initrd_dp);
1485 efi_free_pool(final_dp);
1486
1487 return ret;
1488}
1489
1490/**
1491 * eficonfig_process_add_boot_option() - handler to add boot option
1492 *
1493 * @data: pointer to the data for each entry
1494 * Return: status code
1495 */
1496static efi_status_t eficonfig_process_add_boot_option(void *data)
1497{
1498 u16 varname[9];
1499 efi_status_t ret;
1500 struct eficonfig_boot_option *bo = NULL;
1501
1502 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1503 if (!bo)
1504 return EFI_OUT_OF_RESOURCES;
1505
1506 ret = eficonfig_get_unused_bootoption(varname, sizeof(varname), &bo->boot_index);
1507 if (ret != EFI_SUCCESS)
1508 return ret;
1509
1510 ret = eficonfig_edit_boot_option(varname, bo, NULL, 0, " ** Add Boot Option ** ");
1511 if (ret != EFI_SUCCESS)
1512 goto out;
1513
1514 ret = eficonfig_append_bootorder((u16)bo->boot_index);
1515 if (ret != EFI_SUCCESS)
1516 goto out;
1517
1518out:
1519 free(bo);
1520
1521 /* to stay the parent menu */
1522 ret = (ret == EFI_ABORTED) ? EFI_SUCCESS : ret;
1523
1524 return ret;
1525}
1526
1527/**
1528 * eficonfig_init() - do required initialization for eficonfig command
1529 *
1530 * Return: status code
1531 */
1532static efi_status_t eficonfig_init(void)
1533{
1534 efi_status_t ret = EFI_SUCCESS;
1535 static bool init;
1536 struct efi_handler *handler;
1537
1538 if (!init) {
1539 ret = efi_search_protocol(efi_root, &efi_guid_text_input_protocol, &handler);
1540 if (ret != EFI_SUCCESS)
1541 return ret;
1542
1543 ret = efi_protocol_open(handler, (void **)&cin, efi_root, NULL,
1544 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1545 if (ret != EFI_SUCCESS)
1546 return ret;
1547 }
1548
1549 init = true;
1550
1551 return ret;
1552}
1553
1554static const struct eficonfig_item maintenance_menu_items[] = {
1555 {"Add Boot Option", eficonfig_process_add_boot_option},
1556 {"Quit", eficonfig_process_quit},
1557};
1558
1559/**
1560 * do_eficonfig() - execute `eficonfig` command
1561 *
1562 * @cmdtp: table entry describing command
1563 * @flag: bitmap indicating how the command was invoked
1564 * @argc: number of arguments
1565 * @argv: command line arguments
1566 * Return: status code
1567 */
1568static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1569{
1570 efi_status_t ret;
1571 struct efimenu *efi_menu;
1572
1573 if (argc > 1)
1574 return CMD_RET_USAGE;
1575
1576 ret = efi_init_obj_list();
1577 if (ret != EFI_SUCCESS) {
1578 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1579 ret & ~EFI_ERROR_MASK);
1580
1581 return CMD_RET_FAILURE;
1582 }
1583
1584 ret = eficonfig_init();
1585 if (ret != EFI_SUCCESS)
1586 return CMD_RET_FAILURE;
1587
1588 while (1) {
1589 efi_menu = eficonfig_create_fixed_menu(maintenance_menu_items,
1590 ARRAY_SIZE(maintenance_menu_items));
1591 if (!efi_menu)
1592 return CMD_RET_FAILURE;
1593
1594 ret = eficonfig_process_common(efi_menu, " ** UEFI Maintenance Menu **");
1595 eficonfig_destroy(efi_menu);
1596
1597 if (ret == EFI_ABORTED)
1598 break;
1599 }
1600
1601 return CMD_RET_SUCCESS;
1602}
1603
1604U_BOOT_CMD(
1605 eficonfig, 1, 0, do_eficonfig,
1606 "provide menu-driven UEFI variable maintenance interface",
1607 ""
1608);
This page took 0.190282 seconds and 4 git commands to generate.