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