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